-
Notifications
You must be signed in to change notification settings - Fork 9
More Rules #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More Rules #8
Changes from all commits
12e05e0
bf998be
640eba4
57c1c83
3c917d0
7ae95c5
5536878
1c372cd
6e2626e
50919dc
d3b8e8f
1b510d2
88eb5a4
c68bd1e
df25bc7
3ed4514
e4992b2
a8f9725
6e1f903
c3aaeba
88bd8dc
fbed6dc
66f9735
d298eec
6235236
e621b4e
a03697c
7587e49
a1d5bc5
d5609b3
4db2725
a1c20ff
4666d0c
f83aec6
6e710c5
52b258f
e9a1690
b2c8b05
78143e9
381a23e
e00b5fa
93e4125
aeccebc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
id: small-key-size-c | ||
language: c | ||
severity: warning | ||
message: >- | ||
$KEY_FUNCTION` is using a key size of only $KEY_BITS bits. This is | ||
less than the recommended key size of 2048 bits. | ||
note: >- | ||
[CWE-326]: Inadequate Encryption Strength | ||
[OWASP A02:2021]: Cryptographic Failures | ||
[OWASP A03:2017]: Sensitive Data Exposure | ||
[REFERENCES] | ||
https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf | ||
https://owasp.org/Top10/A02_2021-Cryptographic_Failures | ||
utils: | ||
Match_pattern_with_prefix_statement: | ||
kind: expression_statement | ||
all: | ||
- has: | ||
stopBy: end | ||
kind: call_expression | ||
all: | ||
- has: | ||
stopBy: end | ||
kind: identifier | ||
pattern: $AST | ||
- has: | ||
stopBy: end | ||
kind: argument_list | ||
has: | ||
stopby: end | ||
kind: identifier | ||
pattern: $Q | ||
- follows: | ||
stopBy: end | ||
kind: declaration | ||
has: | ||
stopBy: end | ||
kind: init_declarator | ||
all: | ||
- has: | ||
stopBy: end | ||
kind: identifier | ||
pattern: $Q | ||
- has: | ||
stopBy: end | ||
kind: number_literal | ||
pattern: $AASS | ||
|
||
rule: | ||
kind: expression_statement | ||
matches: Match_pattern_with_prefix_statement | ||
constraints: | ||
AST: | ||
regex: (DH_generate_parameters_ex|DSA_generate_parameters_ex|EVP_PKEY_CTX_set_dh_paramgen_prime_len|EVP_PKEY_CTX_set_dsa_paramgen_bits|EVP_PKEY_CTX_set_rsa_keygen_bits|RSA_generate_key_ex|RSA_generate_key_fips) | ||
AASS: | ||
regex: '^(-?(0|[1-9][0-9]?|[1-9][0-9]{2}|1[0-9]{3}|20[0-3][0-9]|204[0-7])(\.[0-9]+)?|0|-[1-9][0-9]*|-[1-9][0-9]{2,}|-1[0-9]{3}|-20[0-3][0-9]|-204[0-7])$' | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
id: std-return-data-c | ||
language: c | ||
severity: warning | ||
message: >- | ||
$FUNC` returns a pointer to the memory owned by `$VAR`. This pointer | ||
is invalid after `$VAR` goes out of scope, which can trigger a use after | ||
free. | ||
note: >- | ||
[CWE-416: Use After Free. | ||
[REFERENCES] | ||
- https://wiki.sei.cmu.edu/confluence/display/c/DCL30-C.+Declare+objects+with+appropriate+storage+durations | ||
utils: | ||
MATCH_RETURN_STATEMENT_WITH_STD: | ||
kind: return_statement | ||
all: | ||
- has: | ||
stopBy: end | ||
kind: call_expression | ||
has: | ||
stopBy: end | ||
kind: field_expression | ||
has: | ||
stopBy: end | ||
kind: identifier | ||
pattern: $R | ||
- follows: | ||
stopBy: end | ||
kind: labeled_statement | ||
all: | ||
- has: | ||
stopBy: end | ||
kind: statement_identifier | ||
regex: ^std | ||
- has: | ||
stopBy: end | ||
kind: expression_statement | ||
has: | ||
stopBy: end | ||
kind: binary_expression | ||
all: | ||
- has: | ||
stopBy: end | ||
kind: binary_expression | ||
all: | ||
- has: | ||
stopBy: end | ||
kind: identifier | ||
regex: (vector|array|deque|forward_list|list|map|multimap|multiset|set|unordered_map|unordered_multimap|unordered_multiset|unordered_set) | ||
- has: | ||
stopBy: end | ||
kind: identifier | ||
- has: | ||
stopBy: end | ||
kind: identifier | ||
pattern: $R | ||
inside: | ||
stopBy: end | ||
kind: function_definition | ||
has: | ||
stopBy: end | ||
kind: primitive_type | ||
|
||
MATCH_RETURN_STATEMENT_WITHOUT_STD: | ||
kind: return_statement | ||
all: | ||
- has: | ||
stopBy: end | ||
kind: call_expression | ||
has: | ||
stopBy: end | ||
kind: field_expression | ||
has: | ||
stopBy: end | ||
kind: identifier | ||
pattern: $R | ||
- follows: | ||
stopBy: end | ||
kind: expression_statement | ||
has: | ||
stopBy: end | ||
kind: binary_expression | ||
all: | ||
- has: | ||
stopBy: end | ||
kind: binary_expression | ||
all: | ||
- has: | ||
stopBy: end | ||
kind: identifier | ||
regex: (vector|array|deque|forward_list|list|map|multimap|multiset|set|unordered_map|unordered_multimap|unordered_multiset|unordered_set) | ||
- has: | ||
stopBy: end | ||
kind: identifier | ||
- has: | ||
stopBy: end | ||
kind: identifier | ||
pattern: $R | ||
inside: | ||
stopBy: end | ||
kind: function_definition | ||
has: | ||
stopBy: end | ||
kind: primitive_type | ||
|
||
rule: | ||
kind: return_statement | ||
any: | ||
- matches: MATCH_RETURN_STATEMENT_WITH_STD | ||
- matches: MATCH_RETURN_STATEMENT_WITHOUT_STD |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,109 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
id: return-c-str-cpp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
language: cpp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
severity: warning | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: >- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"`$FUNC` returns a pointer to the memory owned by `$STR`. This pointer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
is invalid after `$STR` goes out of scope, which can trigger a use after | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
free." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
note: >- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[CWE-416] Use After Free | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[REFERENCES] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- https://wiki.sei.cmu.edu/confluence/display/c/DCL30-C.+Declare+objects+with+appropriate+storage+durations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- https://wiki.sei.cmu.edu/confluence/display/cplusplus/EXP54-CPP.+Do+not+access+an+object+outside+of+its+lifetime | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
utils: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
util_for_declaration_inside_function: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
kind: return_statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pattern: return $STR.$METHOD(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
follows: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
kind: declaration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
stopBy: end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
any: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: string $STR; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: wstring $STR; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: basic_string $STR; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: std::string $STR; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: std::wstring $STR; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: std::basic_string<$TYPE> $STR; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
util_for_assignment_inside_function: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
kind: return_statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pattern: return $STR.$METHOD(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
follows: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
kind: declaration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
stopBy: end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
any: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: string $STR = string($STRING); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: wstring $STR = wstring($STRING); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: basic_string<$TYPE> $STR = basic_string<$TYPE>($STRING); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: std::string $STR = std::string($STRING); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: std::wstring $STR = std::wstring($STRING); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: std::basic_string<$TYPE> $STR = std::basic_string<$TYPE>($STRING); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
util_for_func_params: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
kind: return_statement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pattern: return $STR.$METHOD(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inside: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
stopBy: end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
kind: function_definition | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
has: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
stopBy: end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
kind: parameter_list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
has: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
stopBy: end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
kind: parameter_declaration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
has: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
stopBy: end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
kind: identifier | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
field: declarator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pattern: $STR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
any: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- has: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
any: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- kind: type_identifier | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pattern: $IDENTIFIFER | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- kind: qualified_identifier | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
any: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- all: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- has: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
kind: namespace_identifier | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pattern: $NAMESPACE_IDEN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- has: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
kind: template_type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
all: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- has: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
kind: type_identifier | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
field: name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pattern: $BASIC_STR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
precedes: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
kind: template_argument_list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: $IDENTIFIFER | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- kind: template_type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
has: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
kind: type_identifier | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
field: name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pattern: $BASIC_STR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
precedes: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
kind: template_argument_list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
any: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- matches: util_for_declaration_inside_function | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- matches: util_for_assignment_inside_function | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- matches: util_for_func_params | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: return basic_string<$TYPE>($$$).$METHOD(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: return std::basic_string<$TYPE>($$$).$METHOD(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: return string($$$).$METHOD(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: return std::string($$$).$METHOD(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: return wstring($$$).$METHOD(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- pattern: return std::wstring($$$).$METHOD(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
constraints: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
METHOD: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
regex: ^(c_str|data)$ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
IDENTIFIFER: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
regex: ^(string|wstring|std::string|std::wstring)$ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
BASIC_STR: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
regex: ^(basic_string)$ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
NAMESPACE_IDEN: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
regex: ^(std)$ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+101
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix typo and consider regex optimization The constraints effectively limit the rule to specific methods and types. However, there are two issues to address:
Please apply the following changes: - IDENTIFIFER:
+ IDENTIFIER:
- regex: ^(string|wstring|std::string|std::wstring)$
+ regex: ^(((std::)?(string|wstring))|(basic_string))$
- BASIC_STR:
- regex: ^(basic_string)$ This change corrects the typo and combines the IDENTIFIER and BASIC_STR patterns into a single, more efficient regex. 📝 Committable suggestion
Suggested change
Comment on lines
+101
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM with a minor typo: Effective constraints definition The constraints section effectively limits the rule to relevant methods (c_str and data) and string types, ensuring that the rule targets the intended scenarios. There's a typo in the "IDENTIFIFER" constraint name. It should be "IDENTIFIER". Please apply the following fix: - IDENTIFIFER:
+ IDENTIFIER:
regex: ^(string|wstring|std::string|std::wstring)$ Also, update any references to this constraint in the utils section accordingly. 📝 Committable suggestion
Suggested change
Comment on lines
+101
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM with minor correction: Effective constraints with a typo The constraints effectively limit the rule to specific methods and types, which should help prevent false positives. The regular expressions used are precise and well-defined. There's a minor typo in the constraint name "IDENTIFIFER". Please correct it to "IDENTIFIER": - IDENTIFIFER:
+ IDENTIFIER:
regex: ^(string|wstring|std::string|std::wstring)$ Also, ensure that all references to this constraint throughout the file are updated accordingly. 📝 Committable suggestion
Suggested change
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,13 @@ | ||||||||||||||||||||||||||||||||||||||
id: sizeof-this-cpp | ||||||||||||||||||||||||||||||||||||||
language: cpp | ||||||||||||||||||||||||||||||||||||||
severity: warning | ||||||||||||||||||||||||||||||||||||||
message: >- | ||||||||||||||||||||||||||||||||||||||
Do not use `sizeof(this)` to get the number of bytes of the object in | ||||||||||||||||||||||||||||||||||||||
memory. It returns the size of the pointer, not the size of the object. | ||||||||||||||||||||||||||||||||||||||
note: >- | ||||||||||||||||||||||||||||||||||||||
[CWE-467]: Use of sizeof() on a Pointer Type | ||||||||||||||||||||||||||||||||||||||
[REFERENCES] | ||||||||||||||||||||||||||||||||||||||
- https://wiki.sei.cmu.edu/confluence/display/c/ARR01-C.+Do+not+apply+the+sizeof+operator+to+a+pointer+when+taking+the+size+of+an+array | ||||||||||||||||||||||||||||||||||||||
rule: | ||||||||||||||||||||||||||||||||||||||
any: | ||||||||||||||||||||||||||||||||||||||
- pattern: "sizeof(this)" | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance pattern matching for better coverage and accuracy. While the current pattern will catch the basic case, it may miss variations and could potentially flag false positives. Consider enhancing the pattern for better coverage and accuracy. Here's a suggested improvement: rule:
any:
- - pattern: "sizeof(this)"
+ - pattern: 'sizeof\s*\(\s*this\s*\)'
+ inside:
+ - function
+ - method
+ not-inside:
+ - comment
+ - string This pattern:
📝 Committable suggestion
Suggested change
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider making the pattern more robust. The current pattern will correctly identify the problematic Consider updating the pattern to account for potential spaces: rule:
any:
- - pattern: "sizeof(this)"
+ - pattern: 'sizeof\s*\(\s*this\s*\)' This pattern will match 📝 Committable suggestion
Suggested change
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider expanding the pattern matching. While the current pattern "sizeof(this)" will catch the basic problematic usage, it might be too simplistic. Consider the following suggestions:
Here's a suggested improvement to make the rule more robust: rule:
pattern-either:
- pattern: "sizeof(this)"
- pattern: "sizeof (this)"
inside:
pattern-not:
- pattern-inside: "// ..."
- pattern-inside: "/* ... */"
- pattern-inside: "\"...\"" This modification will catch variations with spaces and exclude matches within comments and string literals. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
id: small-key-size-cpp | ||
language: cpp | ||
severity: warning | ||
message: >- | ||
$KEY_FUNCTION` is using a key size of only $KEY_BITS bits. This is | ||
less than the recommended key size of 2048 bits. | ||
note: >- | ||
[CWE-326]: Inadequate Encryption Strength | ||
[OWASP A02:2021]: Cryptographic Failures | ||
[OWASP A03:2017]: Sensitive Data Exposure | ||
[REFERENCES] | ||
https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf | ||
https://owasp.org/Top10/A02_2021-Cryptographic_Failures | ||
utils: | ||
Match_pattern_with_prefix_statement: | ||
kind: expression_statement | ||
all: | ||
- has: | ||
stopBy: end | ||
kind: call_expression | ||
all: | ||
- has: | ||
stopBy: end | ||
kind: identifier | ||
pattern: $AST | ||
- has: | ||
stopBy: end | ||
kind: argument_list | ||
has: | ||
stopby: end | ||
kind: identifier | ||
pattern: $Q | ||
- follows: | ||
stopBy: end | ||
kind: declaration | ||
has: | ||
stopBy: end | ||
kind: init_declarator | ||
all: | ||
- has: | ||
stopBy: end | ||
kind: identifier | ||
pattern: $Q | ||
- has: | ||
stopBy: end | ||
kind: number_literal | ||
pattern: $AASS | ||
|
||
rule: | ||
kind: expression_statement | ||
matches: Match_pattern_with_prefix_statement | ||
constraints: | ||
AST: | ||
regex: (DH_generate_parameters_ex|DSA_generate_parameters_ex|EVP_PKEY_CTX_set_dh_paramgen_prime_len|EVP_PKEY_CTX_set_dsa_paramgen_bits|EVP_PKEY_CTX_set_rsa_keygen_bits|RSA_generate_key_ex|RSA_generate_key_fips) | ||
AASS: | ||
regex: '^(-?(0|[1-9][0-9]?|[1-9][0-9]{2}|1[0-9]{3}|20[0-3][0-9]|204[0-7])(\.[0-9]+)?|0|-[1-9][0-9]*|-[1-9][0-9]{2,}|-1[0-9]{3}|-20[0-3][0-9]|-204[0-7])$' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AST constraint looks good, but AASS constraint needs improvement.
The AST constraint effectively covers various key generation functions, which is excellent for comprehensive checking. However, the AASS constraint, which is meant to match the key size, has some issues:
Consider simplifying and improving the AASS constraint:
This improved version:
transform: parseInt
to convert the matched string to an integer.range
check to ensure the value is between 1 and 2047.This approach is more readable, maintainable, and flexible if the requirements change in the future.