File tree Expand file tree Collapse file tree 3 files changed +185
-0
lines changed Expand file tree Collapse file tree 3 files changed +185
-0
lines changed Original file line number Diff line number Diff line change 1+ id : file-access-before-action-c
2+ language : c
3+ severity : warning
4+ message : >-
5+ A check is done with `access` and then the file is later used. There is
6+ no guarantee that the status of the file has not changed since the call to
7+ `access` which may allow attackers to bypass permission checks.
8+ note : >-
9+ [CWE-367]: Time-of-check Time-of-use (TOCTOU) Race Condition
10+ [REFERENCES]
11+ - https://wiki.sei.cmu.edu/confluence/display/c/FIO45-C.+Avoid+TOCTOU+race+conditions+while+accessing+files
12+ utils :
13+ match_unlink_identifier :
14+ kind : identifier
15+ regex : unlink|fopen|freopen|remove|rename|access|open|stat|lstat|unlink|mkdir|rmdir|chdir|folly::readFile|folly::writeFile|folly::writeFileAtomic|folly::writeFileAtomicNoThrow|folly::File
16+ inside :
17+ kind : call_expression
18+ inside :
19+ kind : expression_statement
20+ inside :
21+ kind : compound_statement
22+ inside :
23+ stopBy : end
24+ kind : if_statement
25+ has :
26+ stopBy : end
27+ kind : call_expression
28+ all :
29+ - has :
30+ kind : identifier
31+ pattern : $R
32+ - has :
33+ kind : argument_list
34+ all :
35+ - has :
36+ kind : identifier
37+ regex : ^original_key
38+ - has :
39+ kind : identifier
40+ regex : F_OK|R_OK|W_OK|X_OK
41+
42+ match_fopen_identifier :
43+ kind : identifier
44+ regex : unlink|fopen|freopen|remove|rename|access|open|stat|lstat|unlink|mkdir|rmdir|chdir|folly::readFile|folly::writeFile|folly::writeFileAtomic|folly::writeFileAtomicNoThrow|folly::File
45+ inside :
46+ kind : call_expression
47+ inside :
48+ stopBy : end
49+ kind : compound_statement
50+ inside :
51+ stopBy : end
52+ kind : if_statement
53+ has :
54+ stopBy : end
55+ kind : call_expression
56+ all :
57+ - has :
58+ kind : identifier
59+ pattern : $L
60+ - has :
61+ kind : argument_list
62+ all :
63+ - has :
64+ kind : identifier
65+ regex : ^original_key
66+ - has :
67+ kind : identifier
68+ regex : F_OK|R_OK|W_OK|X_OK
69+
70+ rule :
71+ any :
72+ - matches : match_unlink_identifier
73+ - matches : match_fopen_identifier
74+ constraints :
75+ R :
76+ regex : ^(access|faccessat|faccessat2|)$
77+ L :
78+ regex : ^(access|faccessat|faccessat2|)$
Original file line number Diff line number Diff line change 1+ id : file-access-before-action-c
2+ snapshots :
3+ ? |
4+ {
5+ const char *original_key = "path/to/file/filename";
6+ const char *mirror_key = "path/to/another/file/filename";
7+
8+ if ((access(original_key, F_OK) == 0) && (access(mirror_key, F_OK) == 0))
9+ {
10+ copy_file("/bin/cp %s %s", original_key, mirror_key);
11+
12+ // ruleid : file-access-before-action
13+ unlink(original_key);
14+ }
15+ }
16+ void test_002()
17+ {
18+ const char *original_key = "path/to/file/filename";
19+
20+ if (access(original_key, W_OK) == 0)
21+ {
22+ // ruleid : file-access-before-action
23+ FILe *fp = fopen(original_key, "wb");
24+ }
25+ }
26+ : labels :
27+ - source : unlink
28+ style : primary
29+ start : 293
30+ end : 299
31+ - source : access
32+ style : secondary
33+ start : 118
34+ end : 124
35+ - source : original_key
36+ style : secondary
37+ start : 125
38+ end : 137
39+ - source : F_OK
40+ style : secondary
41+ start : 139
42+ end : 143
43+ - source : (original_key, F_OK)
44+ style : secondary
45+ start : 124
46+ end : 144
47+ - source : access(original_key, F_OK)
48+ style : secondary
49+ start : 118
50+ end : 144
51+ - source : |-
52+ if ((access(original_key, F_OK) == 0) && (access(mirror_key, F_OK) == 0))
53+ {
54+ copy_file("/bin/cp %s %s", original_key, mirror_key);
55+
56+ // ruleid: file-access-before-action
57+ unlink(original_key);
58+ }
59+ style: secondary
60+ start: 113
61+ end: 316
62+ - source : |-
63+ {
64+ copy_file("/bin/cp %s %s", original_key, mirror_key);
65+
66+ // ruleid: file-access-before-action
67+ unlink(original_key);
68+ }
69+ style: secondary
70+ start: 187
71+ end: 316
72+ - source : unlink(original_key);
73+ style : secondary
74+ start : 293
75+ end : 314
76+ - source : unlink(original_key)
77+ style : secondary
78+ start : 293
79+ end : 313
Original file line number Diff line number Diff line change 1+ id : file-access-before-action-c
2+ valid :
3+ - |
4+
5+ invalid :
6+ - |
7+ {
8+ const char *original_key = "path/to/file/filename";
9+ const char *mirror_key = "path/to/another/file/filename";
10+
11+ if ((access(original_key, F_OK) == 0) && (access(mirror_key, F_OK) == 0))
12+ {
13+ copy_file("/bin/cp %s %s", original_key, mirror_key);
14+
15+ // ruleid: file-access-before-action
16+ unlink(original_key);
17+ }
18+ }
19+ void test_002()
20+ {
21+ const char *original_key = "path/to/file/filename";
22+
23+ if (access(original_key, W_OK) == 0)
24+ {
25+ // ruleid: file-access-before-action
26+ FILe *fp = fopen(original_key, "wb");
27+ }
28+ }
You can’t perform that action at this time.
0 commit comments