From 58c70437cb7329faf9a898af23710941ef450189 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Tue, 14 Feb 2023 12:00:39 -0500 Subject: [PATCH 1/7] Declarations8: add rule DCL30-C --- .vscode/tasks.json | 1 + ...eObjectsWithAppropriateStorageDurations.md | 18 ++++++ ...eObjectsWithAppropriateStorageDurations.ql | 58 +++++++++++++++++++ ...tsWithAppropriateStorageDurations.expected | 4 ++ ...jectsWithAppropriateStorageDurations.qlref | 1 + c/cert/test/rules/DCL30-C/test.c | 34 +++++++++++ .../cpp/exclusions/c/Declarations8.qll | 26 +++++++++ .../cpp/exclusions/c/RuleMetadata.qll | 3 + rule_packages/c/Declarations8.json | 26 +++++++++ rules.csv | 2 +- 10 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.md create mode 100644 c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.ql create mode 100644 c/cert/test/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.expected create mode 100644 c/cert/test/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.qlref create mode 100644 c/cert/test/rules/DCL30-C/test.c create mode 100644 cpp/common/src/codingstandards/cpp/exclusions/c/Declarations8.qll create mode 100644 rule_packages/c/Declarations8.json diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 44b4c4b31e..11cca2a027 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -211,6 +211,7 @@ "Declarations5", "Declarations6", "Declarations7", + "Declarations8", "Exceptions1", "Exceptions2", "Expressions", diff --git a/c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.md b/c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.md new file mode 100644 index 0000000000..2f43befe14 --- /dev/null +++ b/c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.md @@ -0,0 +1,18 @@ +# DCL30-C: Declare objects with appropriate storage durations + +This query implements the CERT-C rule DCL30-C: + +> Declare objects with appropriate storage durations + + +## CERT + +** REPLACE THIS BY RUNNING THE SCRIPT `scripts/help/cert-help-extraction.py` ** + +## Implementation notes + +The rule checks specifically for pointers to objects with automatic storage duration with respect to the following cases: returned by functions, assigned to function output parameters and assigned to static storage duration variables. + +## References + +* CERT-C: [DCL30-C: Declare objects with appropriate storage durations](https://wiki.sei.cmu.edu/confluence/display/c) diff --git a/c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.ql b/c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.ql new file mode 100644 index 0000000000..c278a3a340 --- /dev/null +++ b/c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.ql @@ -0,0 +1,58 @@ +/** + * @id c/cert/declare-objects-with-appropriate-storage-durations + * @name DCL30-C: Declare objects with appropriate storage durations + * @description When storage durations are not compatible between assigned pointers it can lead to + * referring to objects outside of their lifetime, which is undefined behaviour. + * @kind problem + * @precision high + * @problem.severity error + * @tags external/cert/id/dcl30-c + * correctness + * external/cert/obligation/rule + */ + +import cpp +import codingstandards.c.cert +import codingstandards.cpp.FunctionParameter +import semmle.code.cpp.dataflow.DataFlow + +class Source extends StackVariable { + Source() { not this instanceof FunctionParameter } +} + +abstract class Sink extends DataFlow::Node { } + +class FunctionSink extends Sink { + FunctionSink() { + //output parameter + exists(FunctionParameter f | + f.getAnAccess() = this.(DataFlow::PostUpdateNode).getPreUpdateNode().asExpr() and + f.getUnderlyingType() instanceof PointerType + ) + or + //function returns pointer + exists(Function f, ReturnStmt r | + f.getType() instanceof PointerType and + r.getEnclosingFunction() = f and + r.getExpr() = this.asExpr() + ) + } +} + +class StaticSink extends Sink { + StaticSink() { + exists(StaticStorageDurationVariable s | + this.(DataFlow::PostUpdateNode).getPreUpdateNode().asExpr() = s.getAnAccess() and + s.getUnderlyingType() instanceof PointerType + ) + } +} + +from DataFlow::Node src, DataFlow::Node sink +where + not isExcluded(sink.asExpr(), + Declarations8Package::declareObjectsWithAppropriateStorageDurationsQuery()) and + exists(Source s | src.asExpr() = s.getAnAccess()) and + sink instanceof Sink and + DataFlow::localFlow(src, sink) +select sink, "$@ with automatic storage may be accessible outside of its lifetime.", src, src.toString() diff --git a/c/cert/test/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.expected b/c/cert/test/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.expected new file mode 100644 index 0000000000..92a57418e2 --- /dev/null +++ b/c/cert/test/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.expected @@ -0,0 +1,4 @@ +| test.c:3:10:3:10 | a | $@ with automatic storage may be accessible outside of its lifetime. | test.c:3:10:3:10 | a | a | +| test.c:15:4:15:8 | param [inner post update] | $@ with automatic storage may be accessible outside of its lifetime. | test.c:15:12:15:13 | a2 | a2 | +| test.c:21:3:21:3 | g [post update] | $@ with automatic storage may be accessible outside of its lifetime. | test.c:21:7:21:8 | a3 | a3 | +| test.c:32:3:32:3 | g [post update] | $@ with automatic storage may be accessible outside of its lifetime. | test.c:32:7:32:8 | a5 | a5 | diff --git a/c/cert/test/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.qlref b/c/cert/test/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.qlref new file mode 100644 index 0000000000..0c2def1693 --- /dev/null +++ b/c/cert/test/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.qlref @@ -0,0 +1 @@ +rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.ql \ No newline at end of file diff --git a/c/cert/test/rules/DCL30-C/test.c b/c/cert/test/rules/DCL30-C/test.c new file mode 100644 index 0000000000..f703f158c0 --- /dev/null +++ b/c/cert/test/rules/DCL30-C/test.c @@ -0,0 +1,34 @@ +char *f(void) { + char a[1]; + return a; // NON_COMPLIANT +} + +char f1(void) { + char a1[1]; + a1[0] = 'a'; + return a1[0]; // COMPLIANT +} + +void f2(char **param) { + char a2[1]; + a2[0] = 'a'; + *param = a2; // NON_COMPLIANT +} + +const char *g; +void f3(void) { + const char a3[] = "test"; + g = a3; // NON_COMPLIANT +} + +void f4(void) { + const char a4[] = "test"; + const char *p = a4; // COMPLIANT +} + +#include +void f5(void) { + const char a5[] = "test"; + g = a5; // COMPLIANT[FALSE_POSITIVE] + g = NULL; +} \ No newline at end of file diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations8.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations8.qll new file mode 100644 index 0000000000..33cacba73b --- /dev/null +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations8.qll @@ -0,0 +1,26 @@ +//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ +import cpp +import RuleMetadata +import codingstandards.cpp.exclusions.RuleMetadata + +newtype Declarations8Query = TDeclareObjectsWithAppropriateStorageDurationsQuery() + +predicate isDeclarations8QueryMetadata(Query query, string queryId, string ruleId, string category) { + query = + // `Query` instance for the `declareObjectsWithAppropriateStorageDurations` query + Declarations8Package::declareObjectsWithAppropriateStorageDurationsQuery() and + queryId = + // `@id` for the `declareObjectsWithAppropriateStorageDurations` query + "c/cert/declare-objects-with-appropriate-storage-durations" and + ruleId = "DCL30-C" and + category = "rule" +} + +module Declarations8Package { + Query declareObjectsWithAppropriateStorageDurationsQuery() { + //autogenerate `Query` type + result = + // `Query` type for `declareObjectsWithAppropriateStorageDurations` query + TQueryC(TDeclarations8PackageQuery(TDeclareObjectsWithAppropriateStorageDurationsQuery())) + } +} diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll index 3fa8156798..74bd3427b8 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll @@ -22,6 +22,7 @@ import Declarations4 import Declarations5 import Declarations6 import Declarations7 +import Declarations8 import Expressions import IO1 import IO2 @@ -67,6 +68,7 @@ newtype TCQuery = TDeclarations5PackageQuery(Declarations5Query q) or TDeclarations6PackageQuery(Declarations6Query q) or TDeclarations7PackageQuery(Declarations7Query q) or + TDeclarations8PackageQuery(Declarations8Query q) or TExpressionsPackageQuery(ExpressionsQuery q) or TIO1PackageQuery(IO1Query q) or TIO2PackageQuery(IO2Query q) or @@ -112,6 +114,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat isDeclarations5QueryMetadata(query, queryId, ruleId, category) or isDeclarations6QueryMetadata(query, queryId, ruleId, category) or isDeclarations7QueryMetadata(query, queryId, ruleId, category) or + isDeclarations8QueryMetadata(query, queryId, ruleId, category) or isExpressionsQueryMetadata(query, queryId, ruleId, category) or isIO1QueryMetadata(query, queryId, ruleId, category) or isIO2QueryMetadata(query, queryId, ruleId, category) or diff --git a/rule_packages/c/Declarations8.json b/rule_packages/c/Declarations8.json new file mode 100644 index 0000000000..e92056f2ee --- /dev/null +++ b/rule_packages/c/Declarations8.json @@ -0,0 +1,26 @@ +{ + "CERT-C": { + "DCL30-C": { + "properties": { + "obligation": "rule" + }, + "queries": [ + { + "description": "When storage durations are not compatible between assigned pointers it can lead to referring to objects outside of their lifetime, which is undefined behaviour.", + "kind": "problem", + "name": "Declare objects with appropriate storage durations", + "precision": "high", + "severity": "error", + "short_name": "DeclareObjectsWithAppropriateStorageDurations", + "tags": [ + "correctness" + ], + "implementation_scope": { + "description": "The rule checks specifically for pointers to objects with automatic storage duration with respect to the following cases: returned by functions, assigned to function output parameters and assigned to static storage duration variables." + } + } + ], + "title": "Declare objects with appropriate storage durations" + } + } +} \ No newline at end of file diff --git a/rules.csv b/rules.csv index c455e2ca15..6902671f7a 100644 --- a/rules.csv +++ b/rules.csv @@ -498,7 +498,7 @@ c,CERT-C,CON39-C,Yes,Rule,,,Do not join or detach a thread that was previously j c,CERT-C,CON40-C,Yes,Rule,,,Do not refer to an atomic variable twice in an expression,,Concurrency5,Medium, c,CERT-C,CON41-C,Yes,Rule,,,Wrap functions that can fail spuriously in a loop,CON53-CPP,Concurrency3,Medium, c,CERT-C,CON43-C,OutOfScope,Rule,,,Do not allow data races in multithreaded code,,,, -c,CERT-C,DCL30-C,Yes,Rule,,,Declare objects with appropriate storage durations,,Declarations,Hard, +c,CERT-C,DCL30-C,Yes,Rule,,,Declare objects with appropriate storage durations,,Declarations8,Hard, c,CERT-C,DCL31-C,Yes,Rule,,,Declare identifiers before using them,,Declarations1,Medium, c,CERT-C,DCL36-C,No,Rule,,,Do not declare an identifier with conflicting linkage classifications,,,, c,CERT-C,DCL37-C,Yes,Rule,,,Do not declare or define a reserved identifier,,Declarations1,Easy, From 5d78f26c65932474e6f747887a407ae0840c5ee7 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Tue, 14 Feb 2023 12:19:14 -0500 Subject: [PATCH 2/7] Declarations8: add helpfile DCL30-C --- ...eObjectsWithAppropriateStorageDurations.md | 177 +++++++++++++++++- 1 file changed, 175 insertions(+), 2 deletions(-) diff --git a/c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.md b/c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.md index 2f43befe14..f046dbc056 100644 --- a/c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.md +++ b/c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.md @@ -5,9 +5,182 @@ This query implements the CERT-C rule DCL30-C: > Declare objects with appropriate storage durations -## CERT -** REPLACE THIS BY RUNNING THE SCRIPT `scripts/help/cert-help-extraction.py` ** +## Description + +Every object has a storage duration that determines its lifetime: *static*, *thread*, *automatic*, or *allocated*. + +According to the C Standard, 6.2.4, paragraph 2 \[[ISO/IEC 9899:2011](https://wiki.sei.cmu.edu/confluence/display/c/AA.+Bibliography#AA.Bibliography-ISO-IEC9899-2011)\], + +> The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. An object exists, has a constant address, and retains its last-stored value throughout its lifetime. If an object is referred to outside of its lifetime, the behavior is undefined. The value of a pointer becomes indeterminate when the object it points to reaches the end of its lifetime. + + +Do not attempt to access an object outside of its lifetime. Attempting to do so is [undefined behavior](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-undefinedbehavior) and can lead to an exploitable [vulnerability](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-vulnerability). (See also [undefined behavior 9](https://wiki.sei.cmu.edu/confluence/display/c/CC.+Undefined+Behavior#CC.UndefinedBehavior-ub_9) in the C Standard, Annex J.) + +## Noncompliant Code Example (Differing Storage Durations) + +In this noncompliant code example, the address of the variable `c_str` with automatic storage duration is assigned to the variable `p`, which has static storage duration. The assignment itself is valid, but it is invalid for `c_str` to go out of scope while `p` holds its address, as happens at the end of `dont_do_this``()`. + +```cpp +#include + +const char *p; +void dont_do_this(void) { + const char c_str[] = "This will change"; + p = c_str; /* Dangerous */ +} + +void innocuous(void) { + printf("%s\n", p); +} + +int main(void) { + dont_do_this(); + innocuous(); + return 0; +} +``` + +## Compliant Solution (Same Storage Durations) + +In this compliant solution, `p` is declared with the same storage duration as `c_str`, preventing `p` from taking on an [indeterminate value](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-indeterminatevalue) outside of `this_is_OK()`: + +```cpp +void this_is_OK(void) { + const char c_str[] = "Everything OK"; + const char *p = c_str; + /* ... */ +} +/* p is inaccessible outside the scope of string c_str */ + +``` +Alternatively, both `p` and `c_str` could be declared with static storage duration. + +## Compliant Solution (Differing Storage Durations) + +If it is necessary for `p` to be defined with static storage duration but `c_str` with a more limited duration, then `p` can be set to `NULL` before `c_str` is destroyed. This practice prevents `p` from taking on an [indeterminate value](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-indeterminatevalue), although any references to `p` must check for `NULL`. + +```cpp +const char *p; +void is_this_OK(void) { + const char c_str[] = "Everything OK?"; + p = c_str; + /* ... */ + p = NULL; +} + +``` + +## Noncompliant Code Example (Return Values) + +In this noncompliant code sample, the function `init_array``()` returns a pointer to a character array with automatic storage duration, which is accessible to the caller: + +```cpp +char *init_array(void) { + char array[10]; + /* Initialize array */ + return array; +} + +``` +Some compilers generate a diagnostic message when a pointer to an object with automatic storage duration is returned from a function, as in this example. Programmers should compile code at high warning levels and resolve any diagnostic messages. (See [MSC00-C. Compile cleanly at high warning levels](https://wiki.sei.cmu.edu/confluence/display/c/MSC00-C.+Compile+cleanly+at+high+warning+levels).) + +## Compliant Solution (Return Values) + +The solution, in this case, depends on the intent of the programmer. If the intent is to modify the value of `array` and have that modification persist outside the scope of `init_array()`, the desired behavior can be achieved by declaring `array` elsewhere and passing it as an argument to `init_array()`: + +```cpp +#include +void init_array(char *array, size_t len) { + /* Initialize array */ + return; +} + +int main(void) { + char array[10]; + init_array(array, sizeof(array) / sizeof(array[0])); + /* ... */ + return 0; +} + +``` + +## Noncompliant Code Example (Output Parameter) + +In this noncompliant code example, the function `squirrel_away()` stores a pointer to local variable `local` into a location pointed to by function parameter `ptr_param`. Upon the return of `squirrel_away()`, the pointer `ptr_param` points to a variable that has an expired lifetime. + +```cpp +void squirrel_away(char **ptr_param) { + char local[10]; + /* Initialize array */ + *ptr_param = local; +} + +void rodent(void) { + char *ptr; + squirrel_away(&ptr); + /* ptr is live but invalid here */ +} + +``` + +## Compliant Solution (Output Parameter) + +In this compliant solution, the variable `local` has static storage duration; consequently, `ptr` can be used to reference the `local` array within the `rodent()` function: + +```cpp +char local[10]; + +void squirrel_away(char **ptr_param) { + /* Initialize array */ + *ptr_param = local; +} + +void rodent(void) { + char *ptr; + squirrel_away(&ptr); + /* ptr is valid in this scope */ +} + +``` + +## Risk Assessment + +Referencing an object outside of its lifetime can result in an attacker being able to execute arbitrary code. + +
Rule Severity Likelihood Remediation Cost Priority Level
DCL30-C High Probable High P6 L2
+ + +## Automated Detection + +
Tool Version Checker Description
Astrée 22.04 pointered-deallocation return-reference-local Fully checked
Axivion Bauhaus Suite 7.2.0 CertC-DCL30 Fully implemented
CodeSonar 7.2p0 LANG.STRUCT.RPL Returns pointer to local
Compass/ROSE Can detect violations of this rule. It automatically detects returning pointers to local variables. Detecting more general cases, such as examples where static pointers are set to local variables which then go out of scope, would be difficult
Coverity 2017.07 RETURN_LOCAL Finds many instances where a function will return a pointer to a local stack variable. Coverity Prevent cannot discover all violations of this rule, so further verification is necessary
Helix QAC 2022.4 C3217, C3225, C3230, C4140 C++2515, C++2516, C++2527, C++2528, C++4026, C++4624, C++4629
Klocwork 2022.4 LOCRET.ARGLOCRET.GLOB LOCRET.RET
LDRA tool suite 9.7.1 42 D, 77 D, 71 S, 565 S Enhanced Enforcement
Parasoft C/C++test 2022.2 CERT_C-DCL30-a CERT_C-DCL30-b The address of an object with automatic storage shall not be returned from a function The address of an object with automatic storage shall not be assigned to another object that may persist after the first object has ceased to exist
PC-lint Plus 1.4 604, 674, 733, 789 Partially supported
Polyspace Bug Finder R2022b CERT C: Rule DCL30-C Checks for pointer or reference to stack variable leaving scope (rule fully covered)
PRQA QA-C 9.7 3217, 3225, 3230, 4140 Partially implemented
PRQA QA-C++ 4.4 2515, 2516, 2527, 2528, 4026, 4624, 4629
PVS-Studio 7.23 V506 , V507 , V558 , V623 , V723 , V738
RuleChecker 22.04 return-reference-local Partially checked
Splint 3.1.1
TrustInSoft Analyzer 1.38 dangling_pointer Exhaustively detects undefined behavior (see one compliant and one non-compliant example ).
+ + +## Related Vulnerabilities + +Search for [vulnerabilities](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-vulnerability) resulting from the violation of this rule on the [CERT website](https://www.kb.cert.org/vulnotes/bymetric?searchview&query=FIELD+KEYWORDS+contains+DCL30-C). + +## Related Guidelines + +[Key here](https://wiki.sei.cmu.edu/confluence/display/c/How+this+Coding+Standard+is+Organized#HowthisCodingStandardisOrganized-RelatedGuidelines) (explains table format and definitions) + +
Taxonomy Taxonomy item Relationship
CERT C Secure Coding Standard MSC00-C. Compile cleanly at high warning levels Prior to 2018-01-12: CERT: Unspecified Relationship
CERT C EXP54-CPP. Do not access an object outside of its lifetime Prior to 2018-01-12: CERT: Unspecified Relationship
ISO/IEC TR 24772:2013 Dangling References to Stack Frames \[DCM\] Prior to 2018-01-12: CERT: Unspecified Relationship
ISO/IEC TS 17961 Escaping of the address of an automatic object \[addrescape\] Prior to 2018-01-12: CERT: Unspecified Relationship
MISRA C:2012 Rule 18.6 (required) Prior to 2018-01-12: CERT: Unspecified Relationship
+ + +## CERT-CWE Mapping Notes + +[Key here](https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=87152408#HowthisCodingStandardisOrganized-CERT-CWEMappingNotes) for mapping notes + +**CWE-562 and DCL30-C** + +DCL30-C = Union( CWE-562, list) where list = + +* Assigning a stack pointer to an argument (thereby letting it outlive the current function + +## Bibliography + +
\[ Coverity 2007 \]
\[ ISO/IEC 9899:2011 \] 6.2.4, "Storage Durations of Objects"
+ ## Implementation notes From 44d3abd8bc1a1c53337f18e315b420cc9b2fb6a6 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Tue, 14 Feb 2023 12:23:04 -0500 Subject: [PATCH 3/7] Declarations8: format fix helpfile DCL30-C --- .../DCL30-C/DeclareObjectsWithAppropriateStorageDurations.md | 1 - 1 file changed, 1 deletion(-) diff --git a/c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.md b/c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.md index f046dbc056..d088b49c41 100644 --- a/c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.md +++ b/c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.md @@ -5,7 +5,6 @@ This query implements the CERT-C rule DCL30-C: > Declare objects with appropriate storage durations - ## Description Every object has a storage duration that determines its lifetime: *static*, *thread*, *automatic*, or *allocated*. From a5a8c6eb0459d902aabf5b3edd0124cdc1c341dc Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Wed, 15 Feb 2023 17:12:22 -0500 Subject: [PATCH 4/7] Declarations8: rework DCL30-C --- ...ropriateStorageDurationsFunctionReturn.md} | 2 +- ...ropriateStorageDurationsFunctionReturn.ql} | 14 +- ...priateStorageDurationsStackAdressEscape.md | 190 ++++++++++++++++++ ...priateStorageDurationsStackAdressEscape.ql | 22 ++ ...ateStorageDurationsFunctionReturn.expected | 2 + ...priateStorageDurationsFunctionReturn.qlref | 1 + ...eStorageDurationsStackAdressEscape.testref | 1 + ...tsWithAppropriateStorageDurations.expected | 4 - ...jectsWithAppropriateStorageDurations.qlref | 1 - .../cpp/exclusions/c/Declarations8.qll | 34 +++- rule_packages/c/Declarations8.json | 19 +- 11 files changed, 263 insertions(+), 27 deletions(-) rename c/cert/src/rules/DCL30-C/{DeclareObjectsWithAppropriateStorageDurations.md => AppropriateStorageDurationsFunctionReturn.md} (98%) rename c/cert/src/rules/DCL30-C/{DeclareObjectsWithAppropriateStorageDurations.ql => AppropriateStorageDurationsFunctionReturn.ql} (80%) create mode 100644 c/cert/src/rules/DCL30-C/AppropriateStorageDurationsStackAdressEscape.md create mode 100644 c/cert/src/rules/DCL30-C/AppropriateStorageDurationsStackAdressEscape.ql create mode 100644 c/cert/test/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.expected create mode 100644 c/cert/test/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.qlref create mode 100644 c/cert/test/rules/DCL30-C/AppropriateStorageDurationsStackAdressEscape.testref delete mode 100644 c/cert/test/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.expected delete mode 100644 c/cert/test/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.qlref diff --git a/c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.md b/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.md similarity index 98% rename from c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.md rename to c/cert/src/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.md index d088b49c41..8124cd49cd 100644 --- a/c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.md +++ b/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.md @@ -183,7 +183,7 @@ DCL30-C = Union( CWE-562, list) where list = ## Implementation notes -The rule checks specifically for pointers to objects with automatic storage duration with respect to the following cases: returned by functions, assigned to function output parameters and assigned to static storage duration variables. +The rule checks specifically for pointers to objects with automatic storage duration that are returned by functions or assigned to function output parameters. ## References diff --git a/c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.ql b/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.ql similarity index 80% rename from c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.ql rename to c/cert/src/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.ql index c278a3a340..ab9b70912d 100644 --- a/c/cert/src/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.ql +++ b/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.ql @@ -39,20 +39,12 @@ class FunctionSink extends Sink { } } -class StaticSink extends Sink { - StaticSink() { - exists(StaticStorageDurationVariable s | - this.(DataFlow::PostUpdateNode).getPreUpdateNode().asExpr() = s.getAnAccess() and - s.getUnderlyingType() instanceof PointerType - ) - } -} - from DataFlow::Node src, DataFlow::Node sink where not isExcluded(sink.asExpr(), - Declarations8Package::declareObjectsWithAppropriateStorageDurationsQuery()) and + Declarations8Package::appropriateStorageDurationsFunctionReturnQuery()) and exists(Source s | src.asExpr() = s.getAnAccess()) and sink instanceof Sink and DataFlow::localFlow(src, sink) -select sink, "$@ with automatic storage may be accessible outside of its lifetime.", src, src.toString() +select sink, "$@ with automatic storage may be accessible outside of its lifetime.", src, + src.toString() diff --git a/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsStackAdressEscape.md b/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsStackAdressEscape.md new file mode 100644 index 0000000000..1926ffd7aa --- /dev/null +++ b/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsStackAdressEscape.md @@ -0,0 +1,190 @@ +# DCL30-C: Declare objects with appropriate storage durations + +This query implements the CERT-C rule DCL30-C: + +> Declare objects with appropriate storage durations + + +## Description + +Every object has a storage duration that determines its lifetime: *static*, *thread*, *automatic*, or *allocated*. + +According to the C Standard, 6.2.4, paragraph 2 \[[ISO/IEC 9899:2011](https://wiki.sei.cmu.edu/confluence/display/c/AA.+Bibliography#AA.Bibliography-ISO-IEC9899-2011)\], + +> The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. An object exists, has a constant address, and retains its last-stored value throughout its lifetime. If an object is referred to outside of its lifetime, the behavior is undefined. The value of a pointer becomes indeterminate when the object it points to reaches the end of its lifetime. + + +Do not attempt to access an object outside of its lifetime. Attempting to do so is [undefined behavior](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-undefinedbehavior) and can lead to an exploitable [vulnerability](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-vulnerability). (See also [undefined behavior 9](https://wiki.sei.cmu.edu/confluence/display/c/CC.+Undefined+Behavior#CC.UndefinedBehavior-ub_9) in the C Standard, Annex J.) + +## Noncompliant Code Example (Differing Storage Durations) + +In this noncompliant code example, the address of the variable `c_str` with automatic storage duration is assigned to the variable `p`, which has static storage duration. The assignment itself is valid, but it is invalid for `c_str` to go out of scope while `p` holds its address, as happens at the end of `dont_do_this``()`. + +```cpp +#include + +const char *p; +void dont_do_this(void) { + const char c_str[] = "This will change"; + p = c_str; /* Dangerous */ +} + +void innocuous(void) { + printf("%s\n", p); +} + +int main(void) { + dont_do_this(); + innocuous(); + return 0; +} +``` + +## Compliant Solution (Same Storage Durations) + +In this compliant solution, `p` is declared with the same storage duration as `c_str`, preventing `p` from taking on an [indeterminate value](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-indeterminatevalue) outside of `this_is_OK()`: + +```cpp +void this_is_OK(void) { + const char c_str[] = "Everything OK"; + const char *p = c_str; + /* ... */ +} +/* p is inaccessible outside the scope of string c_str */ + +``` +Alternatively, both `p` and `c_str` could be declared with static storage duration. + +## Compliant Solution (Differing Storage Durations) + +If it is necessary for `p` to be defined with static storage duration but `c_str` with a more limited duration, then `p` can be set to `NULL` before `c_str` is destroyed. This practice prevents `p` from taking on an [indeterminate value](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-indeterminatevalue), although any references to `p` must check for `NULL`. + +```cpp +const char *p; +void is_this_OK(void) { + const char c_str[] = "Everything OK?"; + p = c_str; + /* ... */ + p = NULL; +} + +``` + +## Noncompliant Code Example (Return Values) + +In this noncompliant code sample, the function `init_array``()` returns a pointer to a character array with automatic storage duration, which is accessible to the caller: + +```cpp +char *init_array(void) { + char array[10]; + /* Initialize array */ + return array; +} + +``` +Some compilers generate a diagnostic message when a pointer to an object with automatic storage duration is returned from a function, as in this example. Programmers should compile code at high warning levels and resolve any diagnostic messages. (See [MSC00-C. Compile cleanly at high warning levels](https://wiki.sei.cmu.edu/confluence/display/c/MSC00-C.+Compile+cleanly+at+high+warning+levels).) + +## Compliant Solution (Return Values) + +The solution, in this case, depends on the intent of the programmer. If the intent is to modify the value of `array` and have that modification persist outside the scope of `init_array()`, the desired behavior can be achieved by declaring `array` elsewhere and passing it as an argument to `init_array()`: + +```cpp +#include +void init_array(char *array, size_t len) { + /* Initialize array */ + return; +} + +int main(void) { + char array[10]; + init_array(array, sizeof(array) / sizeof(array[0])); + /* ... */ + return 0; +} + +``` + +## Noncompliant Code Example (Output Parameter) + +In this noncompliant code example, the function `squirrel_away()` stores a pointer to local variable `local` into a location pointed to by function parameter `ptr_param`. Upon the return of `squirrel_away()`, the pointer `ptr_param` points to a variable that has an expired lifetime. + +```cpp +void squirrel_away(char **ptr_param) { + char local[10]; + /* Initialize array */ + *ptr_param = local; +} + +void rodent(void) { + char *ptr; + squirrel_away(&ptr); + /* ptr is live but invalid here */ +} + +``` + +## Compliant Solution (Output Parameter) + +In this compliant solution, the variable `local` has static storage duration; consequently, `ptr` can be used to reference the `local` array within the `rodent()` function: + +```cpp +char local[10]; + +void squirrel_away(char **ptr_param) { + /* Initialize array */ + *ptr_param = local; +} + +void rodent(void) { + char *ptr; + squirrel_away(&ptr); + /* ptr is valid in this scope */ +} + +``` + +## Risk Assessment + +Referencing an object outside of its lifetime can result in an attacker being able to execute arbitrary code. + +
Rule Severity Likelihood Remediation Cost Priority Level
DCL30-C High Probable High P6 L2
+ + +## Automated Detection + +
Tool Version Checker Description
Astrée 22.04 pointered-deallocation return-reference-local Fully checked
Axivion Bauhaus Suite 7.2.0 CertC-DCL30 Fully implemented
CodeSonar 7.2p0 LANG.STRUCT.RPL Returns pointer to local
Compass/ROSE Can detect violations of this rule. It automatically detects returning pointers to local variables. Detecting more general cases, such as examples where static pointers are set to local variables which then go out of scope, would be difficult
Coverity 2017.07 RETURN_LOCAL Finds many instances where a function will return a pointer to a local stack variable. Coverity Prevent cannot discover all violations of this rule, so further verification is necessary
Helix QAC 2022.4 C3217, C3225, C3230, C4140 C++2515, C++2516, C++2527, C++2528, C++4026, C++4624, C++4629
Klocwork 2022.4 LOCRET.ARGLOCRET.GLOB LOCRET.RET
LDRA tool suite 9.7.1 42 D, 77 D, 71 S, 565 S Enhanced Enforcement
Parasoft C/C++test 2022.2 CERT_C-DCL30-a CERT_C-DCL30-b The address of an object with automatic storage shall not be returned from a function The address of an object with automatic storage shall not be assigned to another object that may persist after the first object has ceased to exist
PC-lint Plus 1.4 604, 674, 733, 789 Partially supported
Polyspace Bug Finder R2022b CERT C: Rule DCL30-C Checks for pointer or reference to stack variable leaving scope (rule fully covered)
PRQA QA-C 9.7 3217, 3225, 3230, 4140 Partially implemented
PRQA QA-C++ 4.4 2515, 2516, 2527, 2528, 4026, 4624, 4629
PVS-Studio 7.23 V506 , V507 , V558 , V623 , V723 , V738
RuleChecker 22.04 return-reference-local Partially checked
Splint 3.1.1
TrustInSoft Analyzer 1.38 dangling_pointer Exhaustively detects undefined behavior (see one compliant and one non-compliant example ).
+ + +## Related Vulnerabilities + +Search for [vulnerabilities](https://wiki.sei.cmu.edu/confluence/display/c/BB.+Definitions#BB.Definitions-vulnerability) resulting from the violation of this rule on the [CERT website](https://www.kb.cert.org/vulnotes/bymetric?searchview&query=FIELD+KEYWORDS+contains+DCL30-C). + +## Related Guidelines + +[Key here](https://wiki.sei.cmu.edu/confluence/display/c/How+this+Coding+Standard+is+Organized#HowthisCodingStandardisOrganized-RelatedGuidelines) (explains table format and definitions) + +
Taxonomy Taxonomy item Relationship
CERT C Secure Coding Standard MSC00-C. Compile cleanly at high warning levels Prior to 2018-01-12: CERT: Unspecified Relationship
CERT C EXP54-CPP. Do not access an object outside of its lifetime Prior to 2018-01-12: CERT: Unspecified Relationship
ISO/IEC TR 24772:2013 Dangling References to Stack Frames \[DCM\] Prior to 2018-01-12: CERT: Unspecified Relationship
ISO/IEC TS 17961 Escaping of the address of an automatic object \[addrescape\] Prior to 2018-01-12: CERT: Unspecified Relationship
MISRA C:2012 Rule 18.6 (required) Prior to 2018-01-12: CERT: Unspecified Relationship
+ + +## CERT-CWE Mapping Notes + +[Key here](https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=87152408#HowthisCodingStandardisOrganized-CERT-CWEMappingNotes) for mapping notes + +**CWE-562 and DCL30-C** + +DCL30-C = Union( CWE-562, list) where list = + +* Assigning a stack pointer to an argument (thereby letting it outlive the current function + +## Bibliography + +
\[ Coverity 2007 \]
\[ ISO/IEC 9899:2011 \] 6.2.4, "Storage Durations of Objects"
+ + +## Implementation notes + +The rule checks specifically for pointers to objects with automatic storage duration that are assigned to static storage duration variables. + +## References + +* CERT-C: [DCL30-C: Declare objects with appropriate storage durations](https://wiki.sei.cmu.edu/confluence/display/c) diff --git a/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsStackAdressEscape.ql b/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsStackAdressEscape.ql new file mode 100644 index 0000000000..afbc1e62e8 --- /dev/null +++ b/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsStackAdressEscape.ql @@ -0,0 +1,22 @@ +/** + * @id c/cert/appropriate-storage-durations-stack-adress-escape + * @name DCL30-C: Declare objects with appropriate storage durations + * @description When storage durations are not compatible between assigned pointers it can lead to + * referring to objects outside of their lifetime, which is undefined behaviour. + * @kind problem + * @precision high + * @problem.severity error + * @tags external/cert/id/dcl30-c + * correctness + * external/cert/obligation/rule + */ + +import cpp +import codingstandards.c.cert +import codingstandards.cpp.rules.donotcopyaddressofautostorageobjecttootherobject.DoNotCopyAddressOfAutoStorageObjectToOtherObject + +class AppropriateStorageDurationsStackAdressEscapeQuery extends DoNotCopyAddressOfAutoStorageObjectToOtherObjectSharedQuery { + AppropriateStorageDurationsStackAdressEscapeQuery() { + this = Declarations8Package::appropriateStorageDurationsStackAdressEscapeQuery() + } +} diff --git a/c/cert/test/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.expected b/c/cert/test/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.expected new file mode 100644 index 0000000000..ff842ddcad --- /dev/null +++ b/c/cert/test/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.expected @@ -0,0 +1,2 @@ +| test.c:3:10:3:10 | a | $@ with automatic storage may be accessible outside of its lifetime. | test.c:3:10:3:10 | a | a | +| test.c:15:4:15:8 | param [inner post update] | $@ with automatic storage may be accessible outside of its lifetime. | test.c:15:12:15:13 | a2 | a2 | diff --git a/c/cert/test/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.qlref b/c/cert/test/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.qlref new file mode 100644 index 0000000000..6541115217 --- /dev/null +++ b/c/cert/test/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.qlref @@ -0,0 +1 @@ +rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.ql \ No newline at end of file diff --git a/c/cert/test/rules/DCL30-C/AppropriateStorageDurationsStackAdressEscape.testref b/c/cert/test/rules/DCL30-C/AppropriateStorageDurationsStackAdressEscape.testref new file mode 100644 index 0000000000..e1ff9b5ae0 --- /dev/null +++ b/c/cert/test/rules/DCL30-C/AppropriateStorageDurationsStackAdressEscape.testref @@ -0,0 +1 @@ +c/common/test/rules/donotcopyaddressofautostorageobjecttootherobject/DoNotCopyAddressOfAutoStorageObjectToOtherObject.ql \ No newline at end of file diff --git a/c/cert/test/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.expected b/c/cert/test/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.expected deleted file mode 100644 index 92a57418e2..0000000000 --- a/c/cert/test/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.expected +++ /dev/null @@ -1,4 +0,0 @@ -| test.c:3:10:3:10 | a | $@ with automatic storage may be accessible outside of its lifetime. | test.c:3:10:3:10 | a | a | -| test.c:15:4:15:8 | param [inner post update] | $@ with automatic storage may be accessible outside of its lifetime. | test.c:15:12:15:13 | a2 | a2 | -| test.c:21:3:21:3 | g [post update] | $@ with automatic storage may be accessible outside of its lifetime. | test.c:21:7:21:8 | a3 | a3 | -| test.c:32:3:32:3 | g [post update] | $@ with automatic storage may be accessible outside of its lifetime. | test.c:32:7:32:8 | a5 | a5 | diff --git a/c/cert/test/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.qlref b/c/cert/test/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.qlref deleted file mode 100644 index 0c2def1693..0000000000 --- a/c/cert/test/rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.qlref +++ /dev/null @@ -1 +0,0 @@ -rules/DCL30-C/DeclareObjectsWithAppropriateStorageDurations.ql \ No newline at end of file diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations8.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations8.qll index 33cacba73b..767373b1c2 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations8.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/Declarations8.qll @@ -3,24 +3,42 @@ import cpp import RuleMetadata import codingstandards.cpp.exclusions.RuleMetadata -newtype Declarations8Query = TDeclareObjectsWithAppropriateStorageDurationsQuery() +newtype Declarations8Query = + TAppropriateStorageDurationsStackAdressEscapeQuery() or + TAppropriateStorageDurationsFunctionReturnQuery() predicate isDeclarations8QueryMetadata(Query query, string queryId, string ruleId, string category) { query = - // `Query` instance for the `declareObjectsWithAppropriateStorageDurations` query - Declarations8Package::declareObjectsWithAppropriateStorageDurationsQuery() and + // `Query` instance for the `appropriateStorageDurationsStackAdressEscape` query + Declarations8Package::appropriateStorageDurationsStackAdressEscapeQuery() and queryId = - // `@id` for the `declareObjectsWithAppropriateStorageDurations` query - "c/cert/declare-objects-with-appropriate-storage-durations" and + // `@id` for the `appropriateStorageDurationsStackAdressEscape` query + "c/cert/appropriate-storage-durations-stack-adress-escape" and + ruleId = "DCL30-C" and + category = "rule" + or + query = + // `Query` instance for the `appropriateStorageDurationsFunctionReturn` query + Declarations8Package::appropriateStorageDurationsFunctionReturnQuery() and + queryId = + // `@id` for the `appropriateStorageDurationsFunctionReturn` query + "c/cert/appropriate-storage-durations-function-return" and ruleId = "DCL30-C" and category = "rule" } module Declarations8Package { - Query declareObjectsWithAppropriateStorageDurationsQuery() { + Query appropriateStorageDurationsStackAdressEscapeQuery() { + //autogenerate `Query` type + result = + // `Query` type for `appropriateStorageDurationsStackAdressEscape` query + TQueryC(TDeclarations8PackageQuery(TAppropriateStorageDurationsStackAdressEscapeQuery())) + } + + Query appropriateStorageDurationsFunctionReturnQuery() { //autogenerate `Query` type result = - // `Query` type for `declareObjectsWithAppropriateStorageDurations` query - TQueryC(TDeclarations8PackageQuery(TDeclareObjectsWithAppropriateStorageDurationsQuery())) + // `Query` type for `appropriateStorageDurationsFunctionReturn` query + TQueryC(TDeclarations8PackageQuery(TAppropriateStorageDurationsFunctionReturnQuery())) } } diff --git a/rule_packages/c/Declarations8.json b/rule_packages/c/Declarations8.json index e92056f2ee..a70523b72f 100644 --- a/rule_packages/c/Declarations8.json +++ b/rule_packages/c/Declarations8.json @@ -9,14 +9,29 @@ "description": "When storage durations are not compatible between assigned pointers it can lead to referring to objects outside of their lifetime, which is undefined behaviour.", "kind": "problem", "name": "Declare objects with appropriate storage durations", + "precision": "very-high", + "severity": "error", + "short_name": "AppropriateStorageDurationsStackAdressEscape", + "shared_implementation_short_name": "DoNotCopyAddressOfAutoStorageObjectToOtherObject", + "tags": [ + "correctness" + ], + "implementation_scope": { + "description": "The rule checks specifically for pointers to objects with automatic storage duration that are assigned to static storage duration variables." + } + }, + { + "description": "When pointers to local variables are returned by a function it can lead to referring to objects outside of their lifetime, which is undefined behaviour.", + "kind": "problem", + "name": "Declare objects with appropriate storage durations", "precision": "high", "severity": "error", - "short_name": "DeclareObjectsWithAppropriateStorageDurations", + "short_name": "AppropriateStorageDurationsFunctionReturn", "tags": [ "correctness" ], "implementation_scope": { - "description": "The rule checks specifically for pointers to objects with automatic storage duration with respect to the following cases: returned by functions, assigned to function output parameters and assigned to static storage duration variables." + "description": "The rule checks specifically for pointers to objects with automatic storage duration that are returned by functions or assigned to function output parameters." } } ], From 28fbabe443f9b4479085c17650e936cf0e7b8570 Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Wed, 15 Feb 2023 17:19:40 -0500 Subject: [PATCH 5/7] Declarations8: fix metadata DCL30-C --- .../DCL30-C/AppropriateStorageDurationsStackAdressEscape.ql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsStackAdressEscape.ql b/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsStackAdressEscape.ql index afbc1e62e8..95e84a6622 100644 --- a/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsStackAdressEscape.ql +++ b/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsStackAdressEscape.ql @@ -4,7 +4,7 @@ * @description When storage durations are not compatible between assigned pointers it can lead to * referring to objects outside of their lifetime, which is undefined behaviour. * @kind problem - * @precision high + * @precision very-high * @problem.severity error * @tags external/cert/id/dcl30-c * correctness From a9a9fe158d60282726cc04b224e5a52414ea2deb Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Wed, 15 Feb 2023 17:23:50 -0500 Subject: [PATCH 6/7] Declarations8: fix metadata DCL30-C --- .../DCL30-C/AppropriateStorageDurationsFunctionReturn.ql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.ql b/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.ql index ab9b70912d..595bd2e1d4 100644 --- a/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.ql +++ b/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.ql @@ -1,8 +1,8 @@ /** - * @id c/cert/declare-objects-with-appropriate-storage-durations + * @id c/cert/appropriate-storage-durations-function-return * @name DCL30-C: Declare objects with appropriate storage durations - * @description When storage durations are not compatible between assigned pointers it can lead to - * referring to objects outside of their lifetime, which is undefined behaviour. + * @description When pointers to local variables are returned by a function it can lead to referring + * to objects outside of their lifetime, which is undefined behaviour. * @kind problem * @precision high * @problem.severity error From bb0e6a3c08e112ef7f4e71b653a7a26434183abe Mon Sep 17 00:00:00 2001 From: Kristen Newbury Date: Wed, 22 Feb 2023 10:06:30 -0500 Subject: [PATCH 7/7] Declarations8: address review comments --- .../AppropriateStorageDurationsFunctionReturn.ql | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.ql b/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.ql index 595bd2e1d4..b5d7e5e378 100644 --- a/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.ql +++ b/c/cert/src/rules/DCL30-C/AppropriateStorageDurationsFunctionReturn.ql @@ -13,19 +13,16 @@ import cpp import codingstandards.c.cert -import codingstandards.cpp.FunctionParameter import semmle.code.cpp.dataflow.DataFlow class Source extends StackVariable { - Source() { not this instanceof FunctionParameter } + Source() { not this instanceof Parameter } } -abstract class Sink extends DataFlow::Node { } - -class FunctionSink extends Sink { - FunctionSink() { +class Sink extends DataFlow::Node { + Sink() { //output parameter - exists(FunctionParameter f | + exists(Parameter f | f.getAnAccess() = this.(DataFlow::PostUpdateNode).getPreUpdateNode().asExpr() and f.getUnderlyingType() instanceof PointerType )