Skip to content

Commit 3ab73c8

Browse files
committed
C++: Improve the cpp/path-injection qhelp
1 parent e10333b commit 3ab73c8

File tree

5 files changed

+122
-28
lines changed

5 files changed

+122
-28
lines changed

cpp/ql/src/Security/CWE/CWE-022/TaintedPath.qhelp

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,57 @@
33
"qhelp.dtd">
44
<qhelp>
55
<overview>
6-
<p>Accessing paths controlled by users can allow an attacker to access unexpected resources. This
6+
<p>Accessing paths controlled by users can allow an attacker to access unexpected resources. This
77
can result in sensitive information being revealed or deleted, or an attacker being able to influence
88
behavior by modifying unexpected files.</p>
99

10-
<p>Paths that are naively constructed from data controlled by a user may contain unexpected special characters,
11-
such as "..". Such a path may potentially point to any directory on the filesystem.</p>
10+
<p>Paths that are naively constructed from data controlled by a user may be absolute paths, or may contain
11+
unexpected special characters such as "..". Such a path could point anywhere on the file system.</p>
1212

1313
</overview>
1414
<recommendation>
1515

16-
<p>Validate user input before using it to construct a filepath. Ideally, follow these rules:</p>
16+
<p>Validate user input before using it to construct a file path.</p>
1717

18-
<ul>
19-
<li>Do not allow more than a single "." character.</li>
20-
<li>Do not allow directory separators such as "/" or "\" (depending on the filesystem).</li>
21-
<li>Do not rely on simply replacing problematic sequences such as "../". For example, after applying this filter to
22-
".../...//" the resulting string would still be "../".</li>
23-
<li>Ideally use a whitelist of known good patterns.</li>
24-
</ul>
18+
<p>Common validation methods include checking that the normalized path is relative and does not contain
19+
any ".." components, or checking that the path is contained within a safe folder. The method you should use depends
20+
on how the path is used in the application, and whether the path should be a single path component.
21+
</p>
22+
23+
<p>If the path should be a single path component (such as a file name), you can check for the existence
24+
of any path separators ("/" or "\"), or ".." sequences in the input, and reject the input if any are found.
25+
</p>
26+
27+
<p>
28+
Note that removing "../" sequences is <i>not</i> sufficient, since the input could still contain a path separator
29+
followed by "..". For example, the input ".../...//" would still result in the string "../" if only "../" sequences
30+
are removed.
31+
</p>
32+
33+
<p>Finally, the simplest (but most restrictive) option is to use an allow list of safe patterns and make sure that
34+
the user input matches one of these patterns.</p>
2535

2636
</recommendation>
2737
<example>
2838

29-
<p>In this example, a username and file are read from the arguments to main and then used to access a file in the
30-
user's home directory. However, a malicious user could enter a filename which contains special
31-
characters. For example, the string "../../etc/passwd" will result in the code reading the file located at
32-
"/home/[user]/../../etc/passwd", which is the system's password file. This could potentially allow them to
33-
access all the system's passwords.</p>
39+
<p>In this example, a file name is read from a user and then used to access a file.
40+
However, a malicious user could enter a file name anywhere on the file system,
41+
such as "/etc/passwd" or "../../../etc/passwd".</p>
42+
43+
<sample src="examples/TaintedPath.c" />
44+
45+
<p>
46+
If the input should only be a file name, you can check that it doesn't contain any path separators or ".." sequences.
47+
</p>
48+
49+
<sample src="examples/TaintedPathNormalize.c" />
50+
51+
<p>
52+
If the input should be within a specific directory, you can check that the resolved path
53+
is still contained within that directory.
54+
</p>
3455

35-
<sample src="TaintedPath.c" />
56+
<sample src="examples/TaintedPathFolder.c" />
3657

3758
</example>
3859
<references>
@@ -41,6 +62,7 @@ access all the system's passwords.</p>
4162
OWASP:
4263
<a href="https://owasp.org/www-community/attacks/Path_Traversal">Path Traversal</a>.
4364
</li>
65+
<li>Rails: <a href="https://api.rubyonrails.org/classes/ActiveStorage/Filename.html#method-i-sanitized">ActiveStorage::Filename#sanitized</a>.</li>
4466

4567
</references>
4668
</qhelp>

cpp/ql/src/Security/CWE/CWE-022/TaintedPath.c renamed to cpp/ql/src/Security/CWE/CWE-022/examples/TaintedPath.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,4 @@ int main(int argc, char** argv) {
99
// BAD: a string from the user is used in a filename
1010
fopen(fileName, "wb+");
1111
}
12-
13-
{
14-
char fileBuffer[FILENAME_MAX] = "/home/";
15-
char *fileName = fileBuffer;
16-
size_t len = strlen(fileName);
17-
// GOOD: use a fixed file
18-
char* fixed = "jim/file.txt";
19-
strncat(fileName+len, fixed, FILENAME_MAX-len-1);
20-
fopen(fileName, "wb+");
21-
}
2212
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
int main(int argc, char** argv) {
5+
char *userAndFile = argv[2];
6+
char baseDir[PATH_MAX] = "/home/user/public/";
7+
char fullPath[PATH_MAX];
8+
char resolvedPath[PATH_MAX];
9+
10+
// Attempt to concatenate the base directory and the user-supplied path
11+
snprintf(fullPath, sizeof(fullPath), "%s%s", baseDir, userAndFile);
12+
13+
// Resolve the absolute path, normalizing any ".." or "."
14+
if (realpath(fullPath, resolvedPath) == NULL) {
15+
perror("Error resolving path");
16+
return 1;
17+
}
18+
19+
// Check if the resolved path starts with the base directory
20+
if (strncmp(baseDir, resolvedPath, strlen(baseDir)) != 0) {
21+
return 1;
22+
}
23+
24+
// GOOD: Path is within the intended directory
25+
FILE *file = fopen(resolvedPath, "wb+");
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
int main(int argc, char** argv) {
5+
6+
char *userAndFile = argv[2];
7+
// Check for invalid sequences in the user input
8+
if (strstr(userAndFile, "..") || strchr(userAndFile, '/') || strchr(userAndFile, '\\')) {
9+
printf("Invalid filename.\n");
10+
return 1;
11+
}
12+
13+
char fileBuffer[FILENAME_MAX] = "/home/user/files/";
14+
// Ensure buffer overflow is prevented
15+
strncat(fileBuffer, userAndFile, FILENAME_MAX - strlen(fileBuffer) - 1);
16+
// GOOD: We know that the filename is safe and stays within the public folder
17+
FILE *file = fopen(fileBuffer, "wb+");
18+
}

cpp/ql/test/query-tests/Security/CWE/CWE-022/semmle/tests/test.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Associated with CWE-022: Improper Limitation of a Pathname to a Restricted Directory. http://cwe.mitre.org/data/definitions/22.html
33

44
#include "stdlib.h"
5-
5+
#define PATH_MAX 4096
66
///// Test code /////
77

88
int main(int argc, char** argv) {
@@ -56,6 +56,44 @@ int main(int argc, char** argv) {
5656
void read(const char *fileName);
5757
read(argv[1]); // BAD
5858
}
59+
60+
{
61+
char *userAndFile = argv[2];
62+
// Check for invalid sequences in the user input
63+
if (strstr(userAndFile, "..") || strchr(userAndFile, '/') || strchr(userAndFile, '\\')) {
64+
// printf("Invalid filename.\n");
65+
return 1;
66+
}
67+
68+
char fileBuffer[FILENAME_MAX] = "/home/user/files/";
69+
// Ensure buffer overflow is prevented
70+
strncat(fileBuffer, userAndFile, FILENAME_MAX - strlen(fileBuffer) - 1);
71+
// GOOD: We know that the filename is safe and stays within the public folder. But we currently get an FP here.
72+
FILE *file = fopen(fileBuffer, "wb+");
73+
}
74+
75+
{
76+
char *userAndFile = argv[2];
77+
char baseDir[PATH_MAX] = "/home/user/public/";
78+
char fullPath[PATH_MAX];
79+
char resolvedPath[PATH_MAX];
80+
81+
// Attempt to concatenate the base directory and the user-supplied path
82+
snprintf(fullPath, sizeof(fullPath), "%s%s", baseDir, userAndFile);
83+
84+
// Resolve the absolute path, normalizing any ".." or "."
85+
if (realpath(fullPath, resolvedPath) == 0) {
86+
return 1;
87+
}
88+
89+
// Check if the resolved path starts with the base directory
90+
if (strncmp(baseDir, resolvedPath, strlen(baseDir)) != 0) {
91+
return 1;
92+
}
93+
94+
// GOOD: Path is within the intended directory
95+
FILE *file = fopen(resolvedPath, "wb+");
96+
}
5997
}
6098

6199
void read(char *fileName) {

0 commit comments

Comments
 (0)