Skip to content

Commit d991fa8

Browse files
authored
Merge pull request #552 from geoffw0/move-security-tests-add
CPP: Add the Semmle security tests.
2 parents d64067a + f034abc commit d991fa8

File tree

176 files changed

+6237
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

176 files changed

+6237
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:17:11:17:18 | fileName | This argument to a file access function is derived from $@ and then passed to fopen(filename) | test.c:9:23:9:26 | argv | user input (argv) |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security/CWE/CWE-022/TaintedPath.ql
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Semmle test case for rule TaintedPath.ql (User-controlled data in path expression)
2+
// Associated with CWE-022: Improper Limitation of a Pathname to a Restricted Directory. http://cwe.mitre.org/data/definitions/22.html
3+
4+
///// Library routines /////
5+
6+
typedef struct {} FILE;
7+
#define FILENAME_MAX 1000
8+
typedef unsigned long size_t;
9+
10+
FILE *fopen(const char *filename, const char *mode);
11+
int sprintf(char *s, const char *format, ...);
12+
size_t strlen(const char *s);
13+
char *strncat(char *s1, const char *s2, size_t n);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Semmle test case for rule TaintedPath.ql (User-controlled data in path expression)
2+
// Associated with CWE-022: Improper Limitation of a Pathname to a Restricted Directory. http://cwe.mitre.org/data/definitions/22.html
3+
4+
#include "stdlib.h"
5+
6+
///// Test code /////
7+
8+
int main(int argc, char** argv) {
9+
char *userAndFile = argv[2];
10+
11+
{
12+
char fileBuffer[FILENAME_MAX] = "/home/";
13+
char *fileName = fileBuffer;
14+
size_t len = strlen(fileName);
15+
strncat(fileName+len, userAndFile, FILENAME_MAX-len-1);
16+
// BAD: a string from the user is used in a filename
17+
fopen(fileName, "wb+");
18+
}
19+
20+
{
21+
char fileBuffer[FILENAME_MAX] = "/home/";
22+
char *fileName = fileBuffer;
23+
size_t len = strlen(fileName);
24+
// GOOD: use a fixed file
25+
char* fixed = "file.txt";
26+
strncat(fileName+len, fixed, FILENAME_MAX-len-1);
27+
fopen(fileName, "wb+");
28+
}
29+
}
30+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:21:12:21:19 | command1 | This argument to an OS command is derived from $@ and then passed to system(string) | test.c:14:20:14:23 | argv | user input (argv) |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security/CWE/CWE-078/ExecTainted.ql
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Semmle test case for rule ExecTainted.ql (Uncontrolled data used in OS command)
2+
// Associated with CWE-078: OS Command Injection. http://cwe.mitre.org/data/definitions/78.html
3+
4+
///// Library routines /////
5+
6+
int sprintf(char *s, const char *format, ...);
7+
int system(const char *string);
8+
9+
extern void encodeShellString(char *shellStr, int maxChars, const char* cStr);
10+
11+
///// Test code /////
12+
13+
int main(int argc, char** argv) {
14+
char *userName = argv[2];
15+
16+
{
17+
// BAD: a string from the user is injected directly into
18+
// a command.
19+
char command1[1000] = {0};
20+
sprintf(command1, "userinfo -v \"%s\"", userName);
21+
system(command1);
22+
}
23+
24+
{
25+
// GOOD: the user string is encoded by a library routine.
26+
char userNameQuoted[1000] = {0};
27+
encodeShellString(userNameQuoted, 1000, userName);
28+
char command2[1000] = {0};
29+
sprintf(command2, "userinfo -v %s", userNameQuoted);
30+
system(command2);
31+
}
32+
}
33+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| search.c:17:8:17:12 | query | Cross-site scripting vulnerability due to $@. | search.c:41:21:41:26 | call to getenv | this query data |
2+
| search.c:23:39:23:43 | query | Cross-site scripting vulnerability due to $@. | search.c:41:21:41:26 | call to getenv | this query data |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security/CWE/CWE-079/CgiXss.ql
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Semmle test case for rule CgiXss.ql (CGI script vulnerable to cross-site scripting)
2+
// Associated with CWE-079: Cross-site Scripting. http://cwe.mitre.org/data/definitions/79.html
3+
4+
// Standard library routines
5+
void free(void *ptr);
6+
char *getenv(const char *name);
7+
int printf(const char *format, ...);
8+
int puts(const char *s);
9+
10+
// External routines
11+
char* do_search(char* query);
12+
char* escape_html(char* raw);
13+
14+
void bad_server1(char* query) {
15+
puts("<p>Query results for ");
16+
// BAD: Printing out an HTTP parameter with no escaping
17+
puts(query);
18+
puts("\n<p>\n");
19+
puts(do_search(query));
20+
}
21+
22+
void bad_server2(char* query) {
23+
printf("<p>Query results for %s\n", query);
24+
// BAD: Printing out an HTTP parameter with no escaping
25+
puts("\n<p>");
26+
puts(do_search(query));
27+
}
28+
29+
void good_server(char* query) {
30+
puts("<p>Query results for ");
31+
// GOOD: Escape HTML characters before adding to a page
32+
char* query_escaped = escape_html(query);
33+
puts(query_escaped);
34+
free(query_escaped);
35+
36+
puts("\n<p>\n");
37+
puts(do_search(query));
38+
}
39+
40+
int main(int argc, char** argv) {
41+
char* raw_query = getenv("QUERY_STRING");
42+
if (strcmp("good", argv[0]) == 0) {
43+
good_server(raw_query);
44+
} else if (strcmp("bad1", argv[0]) == 0) {
45+
bad_server1(raw_query);
46+
} else {
47+
bad_server2(raw_query);
48+
}
49+
}
50+

0 commit comments

Comments
 (0)