I have a semgrep rule with the following pattern: " pattern-either: - patterns: -…
- User
I have a semgrep rule with the following pattern: " pattern-either: - patterns: - pattern-either: - pattern-regex: a1_fmt(...) - pattern: printf(...) - pattern-not: $FUNC("...",...) "
I want it to only match the first call, and not the second in this test case: void build_string(char *string) { // ruleid: raptor-format-string-bugs flof_a1_fmt(string, "foo");
code// ok: raptor-format-string-bugs flof_a1_fmt("%s\n", string);}
In the test case, the function is called flof_a1_fmt, but it can be any name that ends with "a1_fmt". Please help me modify the rule to only match the vulnerable format string
- Assistant
To modify your Semgrep rule to only match calls to functions ending with
a1_fmtwhere the first argument is not a string literal, you can use themetavariable-regexfeature to match function names ending witha1_fmt, and adjust thepattern-notto exclude cases where the first argument is a string literal.Here's the updated rule:
yaml