Skip to content

Add AST rules for detecting world-writable file creation in C/C++ #191

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 45 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
bb7b8e1
removed missing-secure-java
Dec 16, 2024
308e947
Merge branch 'coderabbitai:main' into main
ESS-ENN Jan 6, 2025
46efd55
Merge branch 'coderabbitai:main' into main
ESS-ENN Jan 8, 2025
3ac0204
Merge branch 'coderabbitai:main' into main
ESS-ENN Jan 8, 2025
7caba92
Merge branch 'coderabbitai:main' into main
ESS-ENN Jan 9, 2025
d3a2776
Merge branch 'coderabbitai:main' into main
ESS-ENN Jan 10, 2025
2e86380
Merge branch 'coderabbitai:main' into main
ESS-ENN Jan 14, 2025
b072db7
Merge branch 'coderabbitai:main' into main
ESS-ENN Jan 20, 2025
65dce29
Merge branch 'coderabbitai:main' into main
ESS-ENN Jan 22, 2025
12bb3aa
httponly-false-csharp
ESS-ENN Jan 22, 2025
2c5ea88
use-of-md5-digest-utils-java
ESS-ENN Jan 22, 2025
d3067f1
removing use-of-md5-digest-utils and httponly-false-csharp
ESS-ENN Jan 22, 2025
56d4011
Merge branch 'coderabbitai:main' into main
ESS-ENN Jan 28, 2025
f7f1d72
Merge branch 'coderabbitai:main' into main
ESS-ENN Jan 29, 2025
0d53f8e
Merge branch 'coderabbitai:main' into main
ESS-ENN Feb 4, 2025
b622bca
Merge branch 'coderabbitai:main' into main
ESS-ENN Feb 6, 2025
67459bf
Merge branch 'coderabbitai:main' into main
ESS-ENN Feb 7, 2025
b8b2adb
Merge branch 'coderabbitai:main' into main
ESS-ENN Feb 18, 2025
9b29d0a
Merge branch 'coderabbitai:main' into main
ESS-ENN Mar 3, 2025
24510d4
Merge branch 'coderabbitai:main' into main
ESS-ENN Mar 4, 2025
6c6d229
Merge remote-tracking branch 'upstream/main' into main
ESS-ENN Mar 4, 2025
c5f0d85
Merge branch 'main' of https://github.com/ESS-ENN/ast-grep-essentials…
ESS-ENN Mar 4, 2025
4104e61
Merge branch 'coderabbitai:main' into main
ESS-ENN Mar 5, 2025
36b1114
Merge remote-tracking branch 'upstream/main' into main
ESS-ENN Mar 5, 2025
befc6b0
Merge branch 'main' of https://github.com/ESS-ENN/ast-grep-essentials…
ESS-ENN Mar 5, 2025
7bc886d
Merge branch 'coderabbitai:main' into main
ESS-ENN Mar 10, 2025
fc13546
Merge remote-tracking branch 'upstream/main' into main
ESS-ENN Mar 10, 2025
e4a1853
Merge branch 'main' of https://github.com/ESS-ENN/ast-grep-essentials…
ESS-ENN Mar 10, 2025
b6203b0
Merge branch 'coderabbitai:main' into main
ESS-ENN Mar 18, 2025
5327545
Merge remote-tracking branch 'upstream/main' into main
ESS-ENN Mar 20, 2025
f59e083
Merge remote-tracking branch 'upstream/main' into main
ESS-ENN Mar 25, 2025
e0d7253
Merge branch 'coderabbitai:main' into main
ESS-ENN Mar 27, 2025
a545c52
Merge remote-tracking branch 'upstream/main' into main
ESS-ENN Mar 27, 2025
a6d049d
Merge branch 'main' of https://github.com/ESS-ENN/ast-grep-essentials…
ESS-ENN Mar 27, 2025
33141b9
Merge branch 'coderabbitai:main' into main
ESS-ENN Mar 27, 2025
9996ae6
Merge remote-tracking branch 'upstream/main' into main
ESS-ENN Mar 27, 2025
8c3da56
Merge branch 'main' of https://github.com/ESS-ENN/ast-grep-essentials…
ESS-ENN Mar 27, 2025
b81739b
Merge branch 'coderabbitai:main' into main
ESS-ENN Mar 28, 2025
3f2baf5
Merge remote-tracking branch 'upstream/main' into main
ESS-ENN Mar 31, 2025
de75fac
Merge branch 'main' of https://github.com/ESS-ENN/ast-grep-essentials…
ESS-ENN Mar 31, 2025
f07e00a
Merge branch 'coderabbitai:main' into main
ESS-ENN Mar 31, 2025
d02b26d
Merge remote-tracking branch 'upstream/main' into main
ESS-ENN Mar 31, 2025
619fd4b
Merge branch 'main' of https://github.com/ESS-ENN/ast-grep-essentials…
ESS-ENN Mar 31, 2025
62ff00a
world-writable-file-c
ESS-ENN Mar 31, 2025
ce381a6
world-writable-file-cpp
ESS-ENN Mar 31, 2025
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
328 changes: 328 additions & 0 deletions rules/c/security/world-writable-file-c.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,328 @@
id: world-writable-file-c
language: c
severity: warning
message: >-
This call makes a world-writable file which allows any user on a machine to write to the file. This may allow attackers to influence the behaviour of this process by writing to the file.
note: >-
[CWE-732]: Incorrect Permission Assignment for Critical Resource
[REFERENCES]
- https://wiki.sei.cmu.edu/confluence/display/c/FIO06-C.+Create+files+with+appropriate+access+permissions

ast-grep-essentials: true

utils:
follows_umask:
follows:
stopBy: end
kind: expression_statement
has:
kind: call_expression
nthChild: 1
all:
- has:
nthChild: 1
kind: identifier
field: function
regex: ^umask$
- has:
nthChild: 2
kind: argument_list
field: arguments

AND_2_EQUALS_2_&_S_IXXXX:
any:
- kind: number_literal
regex: ^-?([2367]|[0-9]*(0[2367]|1[014589]|2[2367]|3[014589]|4[2367]|5[014589]|6[2367]|7[014589]|8[2367]|9[014589]))$
- all:
- any:
- kind: binary_expression
- kind: identifier
- regex: (\s*S_I[A-Z]{4}\s*\|)*S_I[A-Z]{4}
- regex: .*\bS_IWOTH\b.*

rule:
any:
# chmod/fchmod/creat
- any:
- matches: AND_2_EQUALS_2_&_S_IXXXX
- kind: identifier
pattern: $MODE
inside:
stopBy: end
follows:
stopBy: end
any:
- kind: declaration
all:
- has:
kind: init_declarator
all:
- has:
kind: identifier
field: declarator
pattern: $MODE
- has:
nthChild: 2
matches: AND_2_EQUALS_2_&_S_IXXXX
- kind: expression_statement
any:
- has:
kind: assignment_expression
all:
- has:
nthChild: 1
kind: identifier
pattern: $MODE
- has:
nthChild: 2
matches: AND_2_EQUALS_2_&_S_IXXXX
- has:
kind: comma_expression
has:
kind: assignment_expression
all:
- has:
nthChild: 1
kind: identifier
pattern: $MODE
- has:
nthChild: 2
matches: AND_2_EQUALS_2_&_S_IXXXX
nthChild:
position: 2
ofRule:
not:
kind: comment
inside:
kind: argument_list
nthChild: 2
not:
has:
nthChild:
position: 3
ofRule:
not:
kind: comment
follows:
kind: identifier
regex: ^(chmod|fchmod|creat)$
inside:
kind: call_expression
not:
any:
- matches: follows_umask
- inside:
stopBy: end
matches: follows_umask

# fchmodat
- any:
- matches: AND_2_EQUALS_2_&_S_IXXXX
- kind: identifier
pattern: $MODE
inside:
stopBy: end
follows:
stopBy: end
any:
- kind: declaration
all:
- has:
kind: init_declarator
all:
- has:
kind: identifier
field: declarator
pattern: $MODE
- has:
nthChild: 2
matches: AND_2_EQUALS_2_&_S_IXXXX
- kind: expression_statement
any:
- has:
kind: assignment_expression
all:
- has:
nthChild: 1
kind: identifier
pattern: $MODE
- has:
nthChild: 2
matches: AND_2_EQUALS_2_&_S_IXXXX
- has:
kind: comma_expression
has:
kind: assignment_expression
all:
- has:
nthChild: 1
kind: identifier
pattern: $MODE
- has:
nthChild: 2
matches: AND_2_EQUALS_2_&_S_IXXXX
nthChild:
position: 3
ofRule:
not:
kind: comment
inside:
kind: argument_list
nthChild: 2
follows:
kind: identifier
regex: ^(fchmodat)$
inside:
kind: call_expression
not:
any:
- matches: follows_umask
- inside:
stopBy: end
matches: follows_umask

# open
- any:
- matches: AND_2_EQUALS_2_&_S_IXXXX
- kind: identifier
pattern: $MODE
inside:
stopBy: end
follows:
stopBy: end
any:
- kind: declaration
all:
- has:
kind: init_declarator
all:
- has:
kind: identifier
field: declarator
pattern: $MODE
- has:
nthChild: 2
matches: AND_2_EQUALS_2_&_S_IXXXX
- kind: expression_statement
any:
- has:
kind: assignment_expression
all:
- has:
nthChild: 1
kind: identifier
pattern: $MODE
- has:
nthChild: 2
matches: AND_2_EQUALS_2_&_S_IXXXX
- has:
kind: comma_expression
has:
kind: assignment_expression
all:
- has:
nthChild: 1
kind: identifier
pattern: $MODE
- has:
nthChild: 2
matches: AND_2_EQUALS_2_&_S_IXXXX
nthChild:
position: 3
ofRule:
not:
kind: comment
inside:
kind: argument_list
nthChild: 2
not:
has:
nthChild:
position: 4
ofRule:
not:
kind: comment
follows:
kind: identifier
regex: ^(open)$
inside:
kind: call_expression
not:
any:
- matches: follows_umask
- inside:
stopBy: end
matches: follows_umask

# openat
- any:
- matches: AND_2_EQUALS_2_&_S_IXXXX
- kind: identifier
pattern: $MODE
inside:
stopBy: end
follows:
stopBy: end
any:
- kind: declaration
all:
- has:
kind: init_declarator
all:
- has:
kind: identifier
field: declarator
pattern: $MODE
- has:
nthChild: 2
matches: AND_2_EQUALS_2_&_S_IXXXX
- kind: expression_statement
any:
- has:
kind: assignment_expression
all:
- has:
nthChild: 1
kind: identifier
pattern: $MODE
- has:
nthChild: 2
matches: AND_2_EQUALS_2_&_S_IXXXX
- has:
kind: comma_expression
has:
kind: assignment_expression
all:
- has:
nthChild: 1
kind: identifier
pattern: $MODE
- has:
nthChild: 2
matches: AND_2_EQUALS_2_&_S_IXXXX
nthChild:
position: 4
ofRule:
not:
kind: comment
inside:
kind: argument_list
nthChild: 2
not:
has:
nthChild:
position: 5
ofRule:
not:
kind: comment
follows:
kind: identifier
regex: ^(openat)$
inside:
kind: call_expression
not:
any:
- matches: follows_umask
- inside:
stopBy: end
matches: follows_umask
Loading