From bb7b8e1ad0e345862980a163b688f4554ebc1df8 Mon Sep 17 00:00:00 2001 From: Sakshis Date: Mon, 16 Dec 2024 13:09:06 +0000 Subject: [PATCH 1/6] removed missing-secure-java --- rules/java/security/missing-secure-java.yml | 70 ------------------- .../missing-secure-java-snapshot.yml | 32 --------- tests/java/missing-secure-java-test.yml | 15 ---- 3 files changed, 117 deletions(-) delete mode 100644 rules/java/security/missing-secure-java.yml delete mode 100644 tests/__snapshots__/missing-secure-java-snapshot.yml delete mode 100644 tests/java/missing-secure-java-test.yml diff --git a/rules/java/security/missing-secure-java.yml b/rules/java/security/missing-secure-java.yml deleted file mode 100644 index 755e6660..00000000 --- a/rules/java/security/missing-secure-java.yml +++ /dev/null @@ -1,70 +0,0 @@ -id: missing-secure-java -language: java -severity: warning -message: >- - Detected a cookie where the `Secure` flag is either missing or - disabled. The `Secure` cookie flag instructs the browser to forbid sending - the cookie over an insecure HTTP request. Set the `Secure` flag to `true` - so the cookie will only be sent over HTTPS. -note: >- - [CWE-614]: Sensitive Cookie in HTTPS Session Without 'Secure' Attribute - [OWASP A05:2021]: Security Misconfiguration - [REFERENCES] - - https://owasp.org/Top10/A05_2021-Security_Misconfiguration -utils: - match_without_httponly: - kind: argument_list - has: - kind: object_creation_expression - inside: - stopBy: end - kind: method_invocation - - match_cookie_last: - kind: argument_list - has: - kind: method_invocation - has: - kind: argument_list - has: - kind: string_literal - - match_instance: - kind: local_variable_declaration - has: - stopBy: end - kind: identifier - follows: - stopBy: end - kind: variable_declarator - - match_identifier_with_simplecookie: - kind: identifier - inside: - stopBy: end - kind: local_variable_declaration - all: - - has: - stopBy: end - kind: type_identifier - regex: '^SimpleCookie$|^Cookie$' - - has: - stopBy: neighbor - kind: variable_declarator - all: - - has: - stopBy: neighbor - kind: identifier - - has: - stopBy: neighbor - kind: object_creation_expression - - not: - precedes: - stopBy: neighbor - kind: expression_statement -rule: - any: - - matches: match_instance - - matches: match_without_httponly - - matches: match_cookie_last - - matches: match_identifier_with_simplecookie diff --git a/tests/__snapshots__/missing-secure-java-snapshot.yml b/tests/__snapshots__/missing-secure-java-snapshot.yml deleted file mode 100644 index 3931463b..00000000 --- a/tests/__snapshots__/missing-secure-java-snapshot.yml +++ /dev/null @@ -1,32 +0,0 @@ -id: missing-secure-java -snapshots: - ? | - SimpleCookie s = new SimpleCookie("foo", "bar"); - .orElse( new NettyCookie( "foo", "bar" ) ); - Cookie z = new NettyCookie("foo", "bar"); - return HttpResponse.ok().cookie(Cookie.of("zzz", "ddd")); - : labels: - - source: s - style: primary - start: 13 - end: 14 - - source: SimpleCookie - style: secondary - start: 0 - end: 12 - - source: s - style: secondary - start: 13 - end: 14 - - source: new SimpleCookie("foo", "bar") - style: secondary - start: 17 - end: 47 - - source: s = new SimpleCookie("foo", "bar") - style: secondary - start: 13 - end: 47 - - source: SimpleCookie s = new SimpleCookie("foo", "bar"); - style: secondary - start: 0 - end: 48 diff --git a/tests/java/missing-secure-java-test.yml b/tests/java/missing-secure-java-test.yml deleted file mode 100644 index 507f951f..00000000 --- a/tests/java/missing-secure-java-test.yml +++ /dev/null @@ -1,15 +0,0 @@ -id: missing-secure-java -valid: - - | - Cookie c1 = getCookieSomewhere(); - return HttpResponse.ok().cookie(Cookie.of("foo", "bar").secure(true)); - Cookie cookie = request.getCookies().findCookie( "foobar" ) - Cookie c = new NettyCookie("foo", "bar"); - c.secure(true); - NettyCookie r = new NettyCookie("foo", "bar").secure(true); -invalid: - - | - SimpleCookie s = new SimpleCookie("foo", "bar"); - .orElse( new NettyCookie( "foo", "bar" ) ); - Cookie z = new NettyCookie("foo", "bar"); - return HttpResponse.ok().cookie(Cookie.of("zzz", "ddd")); From 12bb3aab8d57915cd459d2e2ac04c42dfb2dca48 Mon Sep 17 00:00:00 2001 From: ESS ENN Date: Wed, 22 Jan 2025 18:58:13 +0530 Subject: [PATCH 2/6] httponly-false-csharp --- rules/csharp/security/httponly-false-csharp | 48 +++++++++++++++++++++ tests/csharp/httponly-false-csharp-test.yml | 9 ++++ 2 files changed, 57 insertions(+) create mode 100644 rules/csharp/security/httponly-false-csharp create mode 100644 tests/csharp/httponly-false-csharp-test.yml diff --git a/rules/csharp/security/httponly-false-csharp b/rules/csharp/security/httponly-false-csharp new file mode 100644 index 00000000..af939938 --- /dev/null +++ b/rules/csharp/security/httponly-false-csharp @@ -0,0 +1,48 @@ +id: httponly-false-csharp +language: csharp +severity: warning +message: >- + "Detected a cookie where the `HttpOnly` flag is either missing or + disabled. The `HttpOnly` cookie flag instructs the browser to forbid + client-side JavaScript to read the cookie. If JavaScript interaction is + required, you can ignore this finding. However, set the `HttpOnly` flag to + `true` in all other cases. If this wasn't intentional, it's recommended to + set the HttpOnly flag to true so the cookie will not be accessible through + client-side scripts or to use the Cookie Policy Middleware to globally set + the HttpOnly flag. You can then use the CookieOptions class when + instantiating the cookie, which inherits these settings and will require + future developers to have to explicitly override them on a case-by-case + basis if needed. This approach ensures cookies are secure by default." +note: >- + [CWE-1004] Sensitive Cookie Without 'HttpOnly' Flag" + [REFERENCES] + - https://learn.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-8.0#cookie-policy-middleware + - https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.cookieoptions + - https://owasp.org/Top10/A05_2021-Security_Misconfiguration + + +ast-grep-essentials: true + +rule: + kind: boolean_literal + pattern: $LITERAL + follows: + regex: ^=$ + follows: + kind: member_access_expression + inside: + kind: assignment_expression + all: + - has: + kind: member_access_expression + nthChild: 1 + regex: \.Cookie$ + - has: + kind: identifier + nthChild: 2 + regex: ^HttpOnly$ + +constraints: + LITERAL: + regex: ^false$ + diff --git a/tests/csharp/httponly-false-csharp-test.yml b/tests/csharp/httponly-false-csharp-test.yml new file mode 100644 index 00000000..e29a7eab --- /dev/null +++ b/tests/csharp/httponly-false-csharp-test.yml @@ -0,0 +1,9 @@ +id: httponly-false-csharp +valid: + - | + myHttpOnlyCookie.HttpOnly = true; + - | + options.Cookie.HttpOnly = true; +invalid: + - | + options.Cookie.HttpOnly = false; From 2c5ea88476cdca70b993026ce65cb1435e602119 Mon Sep 17 00:00:00 2001 From: ESS ENN Date: Wed, 22 Jan 2025 19:00:33 +0530 Subject: [PATCH 3/6] use-of-md5-digest-utils-java --- .../security/use-of-md5-digest-utils-java.yml | 42 +++++++++++++++++++ .../use-of-md5-digest-utils-java-snapshot.yml | 29 +++++++++++++ .../use-of-md5-digest-utils-java-test.yml | 7 ++++ 3 files changed, 78 insertions(+) create mode 100644 rules/java/security/use-of-md5-digest-utils-java.yml create mode 100644 tests/__snapshots__/use-of-md5-digest-utils-java-snapshot.yml create mode 100644 tests/java/use-of-md5-digest-utils-java-test.yml diff --git a/rules/java/security/use-of-md5-digest-utils-java.yml b/rules/java/security/use-of-md5-digest-utils-java.yml new file mode 100644 index 00000000..553bac8a --- /dev/null +++ b/rules/java/security/use-of-md5-digest-utils-java.yml @@ -0,0 +1,42 @@ +id: use-of-md5-digest-utils-java +language: java +severity: warning +message: >- + 'Detected MD5 hash algorithm which is considered insecure. MD5 is not + collision resistant and is therefore not suitable as a cryptographic + signature. Use HMAC instead.' +note: >- + [CWE-328] Use of Weak Hash + [REFERENCES] + - https://owasp.org/Top10/A02_2021-Cryptographic_Failures + +ast-grep-essentials: true + +rule: + kind: identifier + regex: ^getMd5Digest$ + nthChild: 2 + precedes: + nthChild: 3 + kind: argument_list + not: + has: + nthChild: 1 + inside: + kind: method_invocation + nthChild: 1 + inside: + kind: method_invocation + all: + - has: + kind: identifier + nthChild: 2 + regex: ^digest$ + - has: + kind: argument_list + nthChild: 3 + - not: + has: + stopBy: end + kind: ERROR + diff --git a/tests/__snapshots__/use-of-md5-digest-utils-java-snapshot.yml b/tests/__snapshots__/use-of-md5-digest-utils-java-snapshot.yml new file mode 100644 index 00000000..2e74b70e --- /dev/null +++ b/tests/__snapshots__/use-of-md5-digest-utils-java-snapshot.yml @@ -0,0 +1,29 @@ +id: use-of-md5-digest-utils-java +snapshots: + ? | + byte[] hashValue = DigestUtils.getMd5Digest().digest(password.getBytes()); + : labels: + - source: getMd5Digest + style: primary + start: 31 + end: 43 + - source: digest + style: secondary + start: 46 + end: 52 + - source: (password.getBytes()) + style: secondary + start: 52 + end: 73 + - source: DigestUtils.getMd5Digest().digest(password.getBytes()) + style: secondary + start: 19 + end: 73 + - source: DigestUtils.getMd5Digest() + style: secondary + start: 19 + end: 45 + - source: () + style: secondary + start: 43 + end: 45 diff --git a/tests/java/use-of-md5-digest-utils-java-test.yml b/tests/java/use-of-md5-digest-utils-java-test.yml new file mode 100644 index 00000000..769a4b52 --- /dev/null +++ b/tests/java/use-of-md5-digest-utils-java-test.yml @@ -0,0 +1,7 @@ +id: use-of-md5-digest-utils-java +valid: + - | + byte[] hashValue = DigestUtils.getSha512Digest().digest(password.getBytes()); +invalid: + - | + byte[] hashValue = DigestUtils.getMd5Digest().digest(password.getBytes()); From d3067f11ba31741fd738392f2d2efb1702116dcf Mon Sep 17 00:00:00 2001 From: ESS ENN Date: Wed, 22 Jan 2025 19:05:33 +0530 Subject: [PATCH 4/6] removing use-of-md5-digest-utils and httponly-false-csharp --- rules/csharp/security/httponly-false-csharp | 48 ------------------- .../security/use-of-md5-digest-utils-java.yml | 42 ---------------- tests/csharp/httponly-false-csharp-test.yml | 9 ---- .../use-of-md5-digest-utils-java-test.yml | 7 --- 4 files changed, 106 deletions(-) delete mode 100644 rules/csharp/security/httponly-false-csharp delete mode 100644 rules/java/security/use-of-md5-digest-utils-java.yml delete mode 100644 tests/csharp/httponly-false-csharp-test.yml delete mode 100644 tests/java/use-of-md5-digest-utils-java-test.yml diff --git a/rules/csharp/security/httponly-false-csharp b/rules/csharp/security/httponly-false-csharp deleted file mode 100644 index af939938..00000000 --- a/rules/csharp/security/httponly-false-csharp +++ /dev/null @@ -1,48 +0,0 @@ -id: httponly-false-csharp -language: csharp -severity: warning -message: >- - "Detected a cookie where the `HttpOnly` flag is either missing or - disabled. The `HttpOnly` cookie flag instructs the browser to forbid - client-side JavaScript to read the cookie. If JavaScript interaction is - required, you can ignore this finding. However, set the `HttpOnly` flag to - `true` in all other cases. If this wasn't intentional, it's recommended to - set the HttpOnly flag to true so the cookie will not be accessible through - client-side scripts or to use the Cookie Policy Middleware to globally set - the HttpOnly flag. You can then use the CookieOptions class when - instantiating the cookie, which inherits these settings and will require - future developers to have to explicitly override them on a case-by-case - basis if needed. This approach ensures cookies are secure by default." -note: >- - [CWE-1004] Sensitive Cookie Without 'HttpOnly' Flag" - [REFERENCES] - - https://learn.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-8.0#cookie-policy-middleware - - https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.cookieoptions - - https://owasp.org/Top10/A05_2021-Security_Misconfiguration - - -ast-grep-essentials: true - -rule: - kind: boolean_literal - pattern: $LITERAL - follows: - regex: ^=$ - follows: - kind: member_access_expression - inside: - kind: assignment_expression - all: - - has: - kind: member_access_expression - nthChild: 1 - regex: \.Cookie$ - - has: - kind: identifier - nthChild: 2 - regex: ^HttpOnly$ - -constraints: - LITERAL: - regex: ^false$ - diff --git a/rules/java/security/use-of-md5-digest-utils-java.yml b/rules/java/security/use-of-md5-digest-utils-java.yml deleted file mode 100644 index 553bac8a..00000000 --- a/rules/java/security/use-of-md5-digest-utils-java.yml +++ /dev/null @@ -1,42 +0,0 @@ -id: use-of-md5-digest-utils-java -language: java -severity: warning -message: >- - 'Detected MD5 hash algorithm which is considered insecure. MD5 is not - collision resistant and is therefore not suitable as a cryptographic - signature. Use HMAC instead.' -note: >- - [CWE-328] Use of Weak Hash - [REFERENCES] - - https://owasp.org/Top10/A02_2021-Cryptographic_Failures - -ast-grep-essentials: true - -rule: - kind: identifier - regex: ^getMd5Digest$ - nthChild: 2 - precedes: - nthChild: 3 - kind: argument_list - not: - has: - nthChild: 1 - inside: - kind: method_invocation - nthChild: 1 - inside: - kind: method_invocation - all: - - has: - kind: identifier - nthChild: 2 - regex: ^digest$ - - has: - kind: argument_list - nthChild: 3 - - not: - has: - stopBy: end - kind: ERROR - diff --git a/tests/csharp/httponly-false-csharp-test.yml b/tests/csharp/httponly-false-csharp-test.yml deleted file mode 100644 index e29a7eab..00000000 --- a/tests/csharp/httponly-false-csharp-test.yml +++ /dev/null @@ -1,9 +0,0 @@ -id: httponly-false-csharp -valid: - - | - myHttpOnlyCookie.HttpOnly = true; - - | - options.Cookie.HttpOnly = true; -invalid: - - | - options.Cookie.HttpOnly = false; diff --git a/tests/java/use-of-md5-digest-utils-java-test.yml b/tests/java/use-of-md5-digest-utils-java-test.yml deleted file mode 100644 index 769a4b52..00000000 --- a/tests/java/use-of-md5-digest-utils-java-test.yml +++ /dev/null @@ -1,7 +0,0 @@ -id: use-of-md5-digest-utils-java -valid: - - | - byte[] hashValue = DigestUtils.getSha512Digest().digest(password.getBytes()); -invalid: - - | - byte[] hashValue = DigestUtils.getMd5Digest().digest(password.getBytes()); From 62ff00adee50a1a305a9ca712ac4b64510d91bbd Mon Sep 17 00:00:00 2001 From: ESS-ENN Date: Mon, 31 Mar 2025 12:07:52 +0530 Subject: [PATCH 5/6] world-writable-file-c --- rules/c/security/world-writable-file-c.yml | 328 ++++++++++++++++++ .../world-writable-file-c-snapshot.yml | 79 +++++ tests/c/world-writable-file-c-test.yml | 37 ++ 3 files changed, 444 insertions(+) create mode 100644 rules/c/security/world-writable-file-c.yml create mode 100644 tests/__snapshots__/world-writable-file-c-snapshot.yml create mode 100644 tests/c/world-writable-file-c-test.yml diff --git a/rules/c/security/world-writable-file-c.yml b/rules/c/security/world-writable-file-c.yml new file mode 100644 index 00000000..a514fd7d --- /dev/null +++ b/rules/c/security/world-writable-file-c.yml @@ -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 diff --git a/tests/__snapshots__/world-writable-file-c-snapshot.yml b/tests/__snapshots__/world-writable-file-c-snapshot.yml new file mode 100644 index 00000000..10da9622 --- /dev/null +++ b/tests/__snapshots__/world-writable-file-c-snapshot.yml @@ -0,0 +1,79 @@ +id: world-writable-file-c +snapshots: + ? | + void test_octal_bad() { + mode_t mode = 0666; + chmod("/tmp/foo", mode); + int fd = open_log(); + fchmod(fd, mode); + int dirfd = open_log_dir(); + fchmodat(dirfd, "log", mode, AT_SYMLINK_NOFOLLOW); + open("log", O_CREAT, mode); + openat(fd, "log", O_CREAT, mode); + creat("log", mode); + } + : labels: + - source: mode + style: primary + start: 66 + end: 70 + - source: mode + style: secondary + start: 33 + end: 37 + - source: '0666' + style: secondary + start: 40 + end: 44 + - source: mode = 0666 + style: secondary + start: 33 + end: 44 + - source: mode_t mode = 0666; + style: secondary + start: 26 + end: 45 + - source: mode_t mode = 0666; + style: secondary + start: 26 + end: 45 + - source: chmod("/tmp/foo", mode) + style: secondary + start: 48 + end: 71 + - source: chmod + style: secondary + start: 48 + end: 53 + - source: ("/tmp/foo", mode) + style: secondary + start: 53 + end: 71 + ? | + void test_symbol_direct_bad() { + chmod("/tmp/foo", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + int fd = open_log(); + fchmod(fd, S_IROTH | S_IWOTH | S_IRUSR | S_IWUSR); + int dirfd = open_log_dir(); + fchmodat(dirfd, "log", S_IWOTH); + open("log", O_CREAT, S_IWUSR | S_IWOTH); + openat(fd, "log", O_CREAT, S_IWOTH | S_IUSR | S_IGRP); + creat("log", S_IWOTH); + } + : labels: + - source: S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH + style: primary + start: 52 + end: 109 + - source: chmod("/tmp/foo", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) + style: secondary + start: 34 + end: 110 + - source: chmod + style: secondary + start: 34 + end: 39 + - source: ("/tmp/foo", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) + style: secondary + start: 39 + end: 110 diff --git a/tests/c/world-writable-file-c-test.yml b/tests/c/world-writable-file-c-test.yml new file mode 100644 index 00000000..5053e399 --- /dev/null +++ b/tests/c/world-writable-file-c-test.yml @@ -0,0 +1,37 @@ +id: world-writable-file-c +valid: + - | + void test_symbol_direct_good() { + chmod("/tmp/foo", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); + int fd = open_log(); + fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); + int dirfd = open_log_dir(); + fchmodat(dirfd, "log", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, AT_SYMLINK_NOFOLLOW); + open("log", O_CREAT, mode); + openat(fd, "log", O_CREAT, mode); + creat("log", mode); + } +invalid: + - | + void test_octal_bad() { + mode_t mode = 0666; + chmod("/tmp/foo", mode); + int fd = open_log(); + fchmod(fd, mode); + int dirfd = open_log_dir(); + fchmodat(dirfd, "log", mode, AT_SYMLINK_NOFOLLOW); + open("log", O_CREAT, mode); + openat(fd, "log", O_CREAT, mode); + creat("log", mode); + } + - | + void test_symbol_direct_bad() { + chmod("/tmp/foo", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + int fd = open_log(); + fchmod(fd, S_IROTH | S_IWOTH | S_IRUSR | S_IWUSR); + int dirfd = open_log_dir(); + fchmodat(dirfd, "log", S_IWOTH); + open("log", O_CREAT, S_IWUSR | S_IWOTH); + openat(fd, "log", O_CREAT, S_IWOTH | S_IUSR | S_IGRP); + creat("log", S_IWOTH); + } From ce381a689f24dc2b0c98b114c3e2cb7474700330 Mon Sep 17 00:00:00 2001 From: ESS-ENN Date: Mon, 31 Mar 2025 12:10:32 +0530 Subject: [PATCH 6/6] world-writable-file-cpp --- .../cpp/security/world-writable-file-cpp.yml | 329 ++++++++++++++++++ .../world-writable-file-cpp-snapshot.yml | 79 +++++ tests/cpp/world-writable-file-cpp-test.yml | 37 ++ 3 files changed, 445 insertions(+) create mode 100644 rules/cpp/security/world-writable-file-cpp.yml create mode 100644 tests/__snapshots__/world-writable-file-cpp-snapshot.yml create mode 100644 tests/cpp/world-writable-file-cpp-test.yml diff --git a/rules/cpp/security/world-writable-file-cpp.yml b/rules/cpp/security/world-writable-file-cpp.yml new file mode 100644 index 00000000..d6bc7177 --- /dev/null +++ b/rules/cpp/security/world-writable-file-cpp.yml @@ -0,0 +1,329 @@ +id: world-writable-file-cpp +language: cpp +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 diff --git a/tests/__snapshots__/world-writable-file-cpp-snapshot.yml b/tests/__snapshots__/world-writable-file-cpp-snapshot.yml new file mode 100644 index 00000000..54f1344e --- /dev/null +++ b/tests/__snapshots__/world-writable-file-cpp-snapshot.yml @@ -0,0 +1,79 @@ +id: world-writable-file-cpp +snapshots: + ? | + void test_octal_bad() { + mode_t mode = 0666; + chmod("/tmp/foo", mode); + int fd = open_log(); + fchmod(fd, mode); + int dirfd = open_log_dir(); + fchmodat(dirfd, "log", mode, AT_SYMLINK_NOFOLLOW); + open("log", O_CREAT, mode); + openat(fd, "log", O_CREAT, mode); + creat("log", mode); + } + : labels: + - source: mode + style: primary + start: 66 + end: 70 + - source: mode + style: secondary + start: 33 + end: 37 + - source: '0666' + style: secondary + start: 40 + end: 44 + - source: mode = 0666 + style: secondary + start: 33 + end: 44 + - source: mode_t mode = 0666; + style: secondary + start: 26 + end: 45 + - source: mode_t mode = 0666; + style: secondary + start: 26 + end: 45 + - source: chmod("/tmp/foo", mode) + style: secondary + start: 48 + end: 71 + - source: chmod + style: secondary + start: 48 + end: 53 + - source: ("/tmp/foo", mode) + style: secondary + start: 53 + end: 71 + ? | + void test_symbol_direct_bad() { + chmod("/tmp/foo", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + int fd = open_log(); + fchmod(fd, S_IROTH | S_IWOTH | S_IRUSR | S_IWUSR); + int dirfd = open_log_dir(); + fchmodat(dirfd, "log", S_IWOTH); + open("log", O_CREAT, S_IWUSR | S_IWOTH); + openat(fd, "log", O_CREAT, S_IWOTH | S_IUSR | S_IGRP); + creat("log", S_IWOTH); + } + : labels: + - source: S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH + style: primary + start: 52 + end: 109 + - source: chmod("/tmp/foo", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) + style: secondary + start: 34 + end: 110 + - source: chmod + style: secondary + start: 34 + end: 39 + - source: ("/tmp/foo", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) + style: secondary + start: 39 + end: 110 diff --git a/tests/cpp/world-writable-file-cpp-test.yml b/tests/cpp/world-writable-file-cpp-test.yml new file mode 100644 index 00000000..9892771a --- /dev/null +++ b/tests/cpp/world-writable-file-cpp-test.yml @@ -0,0 +1,37 @@ +id: world-writable-file-cpp +valid: + - | + void test_symbol_direct_good() { + chmod("/tmp/foo", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); + int fd = open_log(); + fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); + int dirfd = open_log_dir(); + fchmodat(dirfd, "log", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, AT_SYMLINK_NOFOLLOW); + open("log", O_CREAT, mode); + openat(fd, "log", O_CREAT, mode); + creat("log", mode); + } +invalid: + - | + void test_octal_bad() { + mode_t mode = 0666; + chmod("/tmp/foo", mode); + int fd = open_log(); + fchmod(fd, mode); + int dirfd = open_log_dir(); + fchmodat(dirfd, "log", mode, AT_SYMLINK_NOFOLLOW); + open("log", O_CREAT, mode); + openat(fd, "log", O_CREAT, mode); + creat("log", mode); + } + - | + void test_symbol_direct_bad() { + chmod("/tmp/foo", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + int fd = open_log(); + fchmod(fd, S_IROTH | S_IWOTH | S_IRUSR | S_IWUSR); + int dirfd = open_log_dir(); + fchmodat(dirfd, "log", S_IWOTH); + open("log", O_CREAT, S_IWUSR | S_IWOTH); + openat(fd, "log", O_CREAT, S_IWOTH | S_IUSR | S_IGRP); + creat("log", S_IWOTH); + }