Skip to content

Rules - dont-call-system c/cpp #17

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

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions rules/c/security/dont-call-system-c.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
id: dont-call-system-c
language: c
severity: warning
message: >-
Don't call `system`. It's a high-level wrapper that allows for stacking
multiple commands. Always prefer a more restrictive API such as calling
`execve` from the `exec` family.
note: >-
[CWE-78] Improper Neutralization of Special Elements used in an OS
Command ('OS Command Injection').
[REFERENCES]
- https://owasp.org/Top10/A03_2021-Injection
utils:
PATTERN_SYSTEM:
kind: call_expression
all:
- has:
stopBy: neighbor
kind: identifier
regex: "^system$"
- has:
stopBy: neighbor
kind: argument_list
rule:
kind: call_expression
matches: PATTERN_SYSTEM
26 changes: 26 additions & 0 deletions rules/cpp/security/dont-call-system-cpp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
id: dont-call-system-cpp
language: cpp
severity: warning
message: >-
Don't call `system`. It's a high-level wrapper that allows for stacking
multiple commands. Always prefer a more restrictive API such as calling
`execve` from the `exec` family.
note: >-
[CWE-78] Improper Neutralization of Special Elements used in an OS
Command ('OS Command Injection').
[REFERENCES]
- https://owasp.org/Top10/A03_2021-Injection
utils:
PATTERN_SYSTEM:
kind: call_expression
all:
- has:
stopBy: neighbor
kind: identifier
regex: "^system$"
- has:
stopBy: neighbor
kind: argument_list
rule:
kind: call_expression
matches: PATTERN_SYSTEM
41 changes: 41 additions & 0 deletions tests/__snapshots__/dont-call-system-c-snapshot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
id: dont-call-system-c
snapshots:
? |
void test_002(const char *input)
{
char cmdbuf[BUFFERSIZE];
int len_wanted = snprintf(cmdbuf, BUFFERSIZE,
"any_cmd '%s'", input);
system(cmdbuf);
}
void test_001(const char *input)
{
char cmdbuf[BUFFERSIZE];
int len_wanted = snprintf(cmdbuf, BUFFERSIZE,
"any_cmd '%s'", input);
if (len_wanted >= BUFFERSIZE)
{
/* Handle error */
}
else if (len_wanted < 0)
{
/* Handle error */
}
else if (system(cmdbuf) == -1)
{
/* Handle error */
}
}
: labels:
- source: system(cmdbuf)
style: primary
start: 156
end: 170
- source: system
style: secondary
start: 156
end: 162
- source: (cmdbuf)
style: secondary
start: 162
end: 170
41 changes: 41 additions & 0 deletions tests/__snapshots__/dont-call-system-cpp-snapshot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
id: dont-call-system-cpp
snapshots:
? |
void test_002(const char *input)
{
char cmdbuf[BUFFERSIZE];
int len_wanted = snprintf(cmdbuf, BUFFERSIZE,
"any_cmd '%s'", input);
system(cmdbuf);
}
void test_001(const char *input)
{
char cmdbuf[BUFFERSIZE];
int len_wanted = snprintf(cmdbuf, BUFFERSIZE,
"any_cmd '%s'", input);
if (len_wanted >= BUFFERSIZE)
{
/* Handle error */
}
else if (len_wanted < 0)
{
/* Handle error */
}
else if (system(cmdbuf) == -1)
{
/* Handle error */
}
}
: labels:
- source: system(cmdbuf)
style: primary
start: 156
end: 170
- source: system
style: secondary
start: 156
end: 162
- source: (cmdbuf)
style: secondary
start: 162
end: 170
34 changes: 34 additions & 0 deletions tests/c/dont-call-system-c-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
id: dont-call-system-c
valid:
- |
void test_003(const char *input)
{
storer->store_binary(Clocks->system());
}
invalid:
- |
void test_002(const char *input)
{
char cmdbuf[BUFFERSIZE];
int len_wanted = snprintf(cmdbuf, BUFFERSIZE,
"any_cmd '%s'", input);
system(cmdbuf);
}
void test_001(const char *input)
{
char cmdbuf[BUFFERSIZE];
int len_wanted = snprintf(cmdbuf, BUFFERSIZE,
"any_cmd '%s'", input);
if (len_wanted >= BUFFERSIZE)
{
/* Handle error */
}
else if (len_wanted < 0)
{
/* Handle error */
}
else if (system(cmdbuf) == -1)
{
/* Handle error */
}
}
34 changes: 34 additions & 0 deletions tests/cpp/dont-call-system-cpp-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
id: dont-call-system-cpp
valid:
- |
void test_003(const char *input)
{
storer->store_binary(Clocks->system());
}
invalid:
- |
void test_002(const char *input)
{
char cmdbuf[BUFFERSIZE];
int len_wanted = snprintf(cmdbuf, BUFFERSIZE,
"any_cmd '%s'", input);
system(cmdbuf);
}
void test_001(const char *input)
{
char cmdbuf[BUFFERSIZE];
int len_wanted = snprintf(cmdbuf, BUFFERSIZE,
"any_cmd '%s'", input);
if (len_wanted >= BUFFERSIZE)
{
/* Handle error */
}
else if (len_wanted < 0)
{
/* Handle error */
}
else if (system(cmdbuf) == -1)
{
/* Handle error */
}
}