I have the following semgrep rule for a specific format vulnerability search: rules: -…
- User
I have the following semgrep rule for a specific format vulnerability search: rules:
-
id: format-string-bugs-specific metadata: confidence: MEDIUM
message: Specific format vulnerability severity: ERROR languages:
- c
- cpp pattern-either:
format string in 1st arg
- patterns:
- pattern-either:
- pattern: specificlog(...)
- pattern-not: $FUN("...", ...)
- pattern-either:
This rule correctly matches the first two test-cases, but not the unsafe behaviour in test_funcptr_specificlog: Test file: #include <stdio.h> #include <syslog.h>
#define BUFSIZE 256
void test_specificlog(char *string) { char buf[BUFSIZE]; // error specificlog(string);
// ok specificlog("%s", string);}
void test_funcptr_specificlog(char *string) { // OK void (*v7)(const char , ...); v7 = (void ()(const char *, ...))specificlog; v7(string);
// error void (*v7)(const char *, ...); v7 = (void (*)(const char *, ...))specificlog; v7("%s", string);}
-
- Assistant
To modify your Semgrep rule to also catch unsafe behavior in
test_funcptr_specificlog, you need to account for cases wherespecificlogis called indirectly through a function pointer. Here's how you can adjust your rule:rules: - id: format-string-bugs-specific message: Specific format vulnerability severity: ERROR languages: - c - cpp pattern-either: # Direct calls to specificlog with an unsafe format string - patterns: - pattern: specificlog($FMT, ...) - pattern-not: specificlog("...", ...) # Calls via function pointers assigned from specificlog - patterns: - pattern: | $FP = (void (*...)) specificlog; ... $FP($FMT, ...); - pattern-not: $FP("...", ...)