Skip to content

Commit 195bdd3

Browse files
committed
Modified file-access-before-action-c and cpp
1 parent c34b704 commit 195bdd3

5 files changed

+213
-20
lines changed

rules/cpp/security/file-access-before-action-cpp.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ note: >-
88
[REFERENCES]
99
- https://wiki.sei.cmu.edu/confluence/display/c/FIO45-C.+Avoid+TOCTOU+race+conditions+while+accessing+files
1010
11+
ast-grep-essentials: true
12+
1113
utils:
1214
PATTERN_1(identifier):
1315
kind: identifier

tests/__snapshots__/file-access-before-action-c-snapshot.yml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,100 @@
11
id: file-access-before-action-c
22
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+
copy_file("/bin/cp %s %s", original_key, mirror_key);
10+
unlink(original_key);
11+
}
12+
13+
void test_002(){
14+
const char *original_key = "path/to/file/filename";
15+
16+
if (access(original_key, W_OK) == 0){
17+
File *fp = fopen(original_key, "wb");
18+
}
19+
}
20+
}
21+
: labels:
22+
- source: unlink
23+
style: primary
24+
start: 260
25+
end: 266
26+
- source: original_key
27+
style: secondary
28+
start: 267
29+
end: 279
30+
- source: (original_key)
31+
style: secondary
32+
start: 266
33+
end: 280
34+
- source: original_key
35+
style: secondary
36+
start: 131
37+
end: 143
38+
- source: F_OK
39+
style: secondary
40+
start: 145
41+
end: 149
42+
- source: (original_key, F_OK)
43+
style: secondary
44+
start: 130
45+
end: 150
46+
- source: access
47+
style: secondary
48+
start: 124
49+
end: 130
50+
- source: access(original_key, F_OK)
51+
style: secondary
52+
start: 124
53+
end: 150
54+
- source: ==
55+
style: secondary
56+
start: 151
57+
end: 153
58+
- source: '0'
59+
style: secondary
60+
start: 154
61+
end: 155
62+
- source: access(original_key, F_OK) == 0
63+
style: secondary
64+
start: 124
65+
end: 155
66+
- source: (access(original_key, F_OK) == 0)
67+
style: secondary
68+
start: 123
69+
end: 156
70+
- source: (access(original_key, F_OK) == 0) && (access(mirror_key, F_OK) == 0)
71+
style: secondary
72+
start: 123
73+
end: 191
74+
- source: ((access(original_key, F_OK) == 0) && (access(mirror_key, F_OK) == 0))
75+
style: secondary
76+
start: 122
77+
end: 192
78+
- source: |-
79+
if ((access(original_key, F_OK) == 0) && (access(mirror_key, F_OK) == 0)){
80+
copy_file("/bin/cp %s %s", original_key, mirror_key);
81+
unlink(original_key);
82+
}
83+
style: secondary
84+
start: 119
85+
end: 285
86+
- source: |-
87+
{
88+
copy_file("/bin/cp %s %s", original_key, mirror_key);
89+
unlink(original_key);
90+
}
91+
style: secondary
92+
start: 192
93+
end: 285
94+
- source: unlink(original_key)
95+
style: secondary
96+
start: 260
97+
end: 280
398
? |
499
{
5100
const char *original_key = "path/to/file/filename";

tests/__snapshots__/file-access-before-action-cpp-snapshot.yml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,99 @@
11
id: file-access-before-action-cpp
22
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+
copy_file("/bin/cp %s %s", original_key, mirror_key);
10+
unlink(original_key);
11+
}
12+
13+
void test_002(){
14+
const char *original_key = "path/to/file/filename";
15+
if (access(original_key, W_OK) == 0){
16+
FILe *fp = fopen(original_key, "wb");
17+
}
18+
}
19+
}
20+
: labels:
21+
- source: unlink
22+
style: primary
23+
start: 260
24+
end: 266
25+
- source: original_key
26+
style: secondary
27+
start: 267
28+
end: 279
29+
- source: (original_key)
30+
style: secondary
31+
start: 266
32+
end: 280
33+
- source: original_key
34+
style: secondary
35+
start: 131
36+
end: 143
37+
- source: F_OK
38+
style: secondary
39+
start: 145
40+
end: 149
41+
- source: (original_key, F_OK)
42+
style: secondary
43+
start: 130
44+
end: 150
45+
- source: access
46+
style: secondary
47+
start: 124
48+
end: 130
49+
- source: access(original_key, F_OK)
50+
style: secondary
51+
start: 124
52+
end: 150
53+
- source: ==
54+
style: secondary
55+
start: 151
56+
end: 153
57+
- source: '0'
58+
style: secondary
59+
start: 154
60+
end: 155
61+
- source: access(original_key, F_OK) == 0
62+
style: secondary
63+
start: 124
64+
end: 155
65+
- source: (access(original_key, F_OK) == 0)
66+
style: secondary
67+
start: 123
68+
end: 156
69+
- source: (access(original_key, F_OK) == 0) && (access(mirror_key, F_OK) == 0)
70+
style: secondary
71+
start: 123
72+
end: 191
73+
- source: ((access(original_key, F_OK) == 0) && (access(mirror_key, F_OK) == 0))
74+
style: secondary
75+
start: 122
76+
end: 192
77+
- source: |-
78+
if ((access(original_key, F_OK) == 0) && (access(mirror_key, F_OK) == 0)){
79+
copy_file("/bin/cp %s %s", original_key, mirror_key);
80+
unlink(original_key);
81+
}
82+
style: secondary
83+
start: 119
84+
end: 285
85+
- source: |-
86+
{
87+
copy_file("/bin/cp %s %s", original_key, mirror_key);
88+
unlink(original_key);
89+
}
90+
style: secondary
91+
start: 192
92+
end: 285
93+
- source: unlink(original_key)
94+
style: secondary
95+
start: 260
96+
end: 280
397
? |
498
{
599
const char *original_key = "path/to/file/filename";

tests/c/file-access-before-action-c-test.yml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ valid:
55
invalid:
66
- |
77
{
8-
const char *original_key = "path/to/file/filename";
9-
const char *mirror_key = "path/to/another/file/filename";
8+
const char *original_key = "path/to/file/filename";
9+
const char *mirror_key = "path/to/another/file/filename";
1010
11-
if ((access(original_key, F_OK) == 0) && (access(mirror_key, F_OK) == 0)){
12-
copy_file("/bin/cp %s %s", original_key, mirror_key);
13-
unlink(original_key);
14-
}
11+
if ((access(original_key, F_OK) == 0) && (access(mirror_key, F_OK) == 0)){
12+
copy_file("/bin/cp %s %s", original_key, mirror_key);
13+
unlink(original_key);
14+
}
1515
16-
void test_002(){
17-
const char *original_key = "path/to/file/filename";
16+
void test_002(){
17+
const char *original_key = "path/to/file/filename";
1818
19-
if (access(original_key, W_OK) == 0){
20-
File *fp = fopen(original_key, "wb");
19+
if (access(original_key, W_OK) == 0){
20+
File *fp = fopen(original_key, "wb");
21+
}
2122
}
2223
}

tests/cpp/file-access-before-action-cpp-test.yml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ valid:
55
invalid:
66
- |
77
{
8-
const char *original_key = "path/to/file/filename";
9-
const char *mirror_key = "path/to/another/file/filename";
8+
const char *original_key = "path/to/file/filename";
9+
const char *mirror_key = "path/to/another/file/filename";
1010
11-
if ((access(original_key, F_OK) == 0) && (access(mirror_key, F_OK) == 0)){
12-
copy_file("/bin/cp %s %s", original_key, mirror_key);
13-
unlink(original_key);
14-
}
11+
if ((access(original_key, F_OK) == 0) && (access(mirror_key, F_OK) == 0)){
12+
copy_file("/bin/cp %s %s", original_key, mirror_key);
13+
unlink(original_key);
14+
}
1515
16-
void test_002(){
17-
const char *original_key = "path/to/file/filename";
18-
if (access(original_key, W_OK) == 0){
19-
FILe *fp = fopen(original_key, "wb");
16+
void test_002(){
17+
const char *original_key = "path/to/file/filename";
18+
if (access(original_key, W_OK) == 0){
19+
FILe *fp = fopen(original_key, "wb");
20+
}
2021
}
2122
}

0 commit comments

Comments
 (0)