From 8fb597e77dbba9a2d757c778b5c55126b1607665 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 31 Oct 2026 03:40:01 +0000 Subject: [PATCH 1/5] Initial plan From 289a221f10ec2f2255bb75591fb198244c53f6d6 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 31 Oct 2026 03:59:33 +0000 Subject: [PATCH 2/5] Add cpp/tainted-divisor-without-zero-check query and tests Co-authored-by: felickz <1760475+felickz@users.noreply.github.com> --- .../TaintedDivisorWithoutZeroCheck.ql | 120 +++++++++++++++ .../ArgvAccessWithoutArgcCheck.qlref | 2 + .../ArgvAccessWithoutArgcCheck/bad_main.c | 17 +++ .../bad_main_late_check.c | 10 ++ .../bad_main_unrelated_check.c | 10 ++ .../good_main_early_return.c | 17 +++ .../good_main_guarded.cpp | 13 ++ .../ArgvAccessWithoutArgcCheck/good_main_ne.c | 8 + .../ArgvAccessWithoutArgcCheck/other.c | 7 + .../TaintedDivisorWithoutZeroCheck.expected | 46 ++++++ .../TaintedDivisorWithoutZeroCheck.qlref | 2 + .../TaintedDivisorWithoutZeroCheck/test.c | 141 ++++++++++++++++++ 12 files changed, 393 insertions(+) create mode 100644 languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.ql create mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.qlref create mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/bad_main.c create mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/bad_main_late_check.c create mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/bad_main_unrelated_check.c create mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/good_main_early_return.c create mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/good_main_guarded.cpp create mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/good_main_ne.c create mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/other.c create mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.expected create mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.qlref create mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/test.c diff --git a/languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.ql b/languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.ql new file mode 100644 index 00000000..a3d2938c --- /dev/null +++ b/languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.ql @@ -0,0 +1,120 @@ +/** + * @name Untrusted divisor without zero check + * @description Using a value read from a file, socket, environment variable or + * command line as the divisor of an integer division or remainder + * without first checking it against zero allows an attacker to + * crash the program with a divide-by-zero fault. + * @kind path-problem + * @problem.severity warning + * @security-severity 5.3 + * @precision medium + * @id cpp/tainted-divisor-without-zero-check + * @tags security + * reliability + * external/cwe/cwe-369 + */ + +import cpp +import semmle.code.cpp.ir.dataflow.TaintTracking +import semmle.code.cpp.security.FlowSources +import semmle.code.cpp.controlflow.IRGuards +import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis +import TaintedDivisorFlow::PathGraph + +/** Gets the divisor operand of an integer division or remainder operation `op`. */ +Expr getIntegerDivisor(Expr op) { + ( + result = op.(DivExpr).getRightOperand() or + result = op.(RemExpr).getRightOperand() or + result = op.(AssignDivExpr).getRValue() or + result = op.(AssignRemExpr).getRValue() + ) and + op.getUnspecifiedType() instanceof IntegralType +} + +/** + * Holds if `divisor` can be shown to be non-zero by range analysis, for + * example `(x % 10) + 1`. + */ +predicate isProvablyNonZero(Expr divisor) { + lowerBound(divisor.getFullyConverted()) > 0 or + upperBound(divisor.getFullyConverted()) < 0 +} + +/** + * Holds if `g` is a check that validates `e` on the `branch` edge. Any + * comparison of `e`, or a test of `e` for truthiness, is treated as + * validation since the exact non-zero bound is often expressed indirectly + * (for example `if (height <= 0) return;`). + */ +predicate validatesValue(IRGuardCondition g, Expr e, boolean branch) { + exists(ComparisonOperation cmp | + cmp = g.getUnconvertedResultExpression() and + e = cmp.getAnOperand() and + branch = [true, false] + ) + or + e = g.getUnconvertedResultExpression() and + not e instanceof ComparisonOperation and + branch = true + or + exists(NotExpr not_ | + not_ = g.getUnconvertedResultExpression() and + e = not_.getOperand() and + branch = false + ) +} + +/** + * Holds if `fieldRead` reads a field of an aggregate `base` that was written + * as a whole (for example by `fread(&img, ...)` or `recv(s, &hdr, ...)`). + */ +predicate aggregateToFieldStep(DataFlow::Node aggregate, DataFlow::Node fieldRead) { + exists(FieldAccess fa, VariableAccess base | + fieldRead.asExpr() = fa and + ( + base = fa.getQualifier() + or + // `p->f` where `p` points to the aggregate. + base = fa.getQualifier().(PointerDereferenceExpr).getOperand() + ) and + ( + aggregate.asIndirectExpr() = base + or + aggregate.asExpr() = base and base.getUnspecifiedType() instanceof Class + ) + ) +} + +module TaintedDivisorConfig implements DataFlow::ConfigSig { + predicate isSource(DataFlow::Node source) { source instanceof FlowSource } + + predicate isSink(DataFlow::Node sink) { + exists(Expr divisor | + divisor = getIntegerDivisor(_) and + sink.asExpr() = divisor and + not isProvablyNonZero(divisor) + ) + } + + predicate isBarrier(DataFlow::Node node) { + node = DataFlow::BarrierGuard::getABarrierNode() + } + + predicate isAdditionalFlowStep(DataFlow::Node n1, DataFlow::Node n2) { + aggregateToFieldStep(n1, n2) + } +} + +module TaintedDivisorFlow = TaintTracking::Global; + +from + TaintedDivisorFlow::PathNode source, TaintedDivisorFlow::PathNode sink, Expr op, + FlowSource flowSource +where + TaintedDivisorFlow::flowPath(source, sink) and + sink.getNode().asExpr() = getIntegerDivisor(op) and + flowSource = source.getNode() +select sink.getNode(), source, sink, + "This divisor depends on $@ and is not checked against zero before the $@.", flowSource, + flowSource.getSourceType(), op, "division" diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.qlref b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.qlref new file mode 100644 index 00000000..4cc980d2 --- /dev/null +++ b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.qlref @@ -0,0 +1,2 @@ +query: InputValidationNullNumeric/ArgvAccessWithoutArgcCheck.ql +postprocess: utils/test/InlineExpectationsTestQuery.ql diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/bad_main.c b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/bad_main.c new file mode 100644 index 00000000..b1894e4c --- /dev/null +++ b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/bad_main.c @@ -0,0 +1,17 @@ +typedef struct _FILE FILE; +FILE *fopen(const char *path, const char *mode); +int printf(const char *fmt, ...); + +int ProcessImage(char *filename) { + FILE *fp = fopen(filename, "r"); + return fp != 0; +} + +// argv[1] is used without checking argc: running the program without +// arguments passes NULL to ProcessImage/fopen. +int main(int argc, char **argv) { + ProcessImage(argv[1]); // $ Alert + printf("%s\n", *(argv + 2)); // $ Alert + printf("%s\n", argv[0]); + return 0; +} diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/bad_main_late_check.c b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/bad_main_late_check.c new file mode 100644 index 00000000..8f754f3c --- /dev/null +++ b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/bad_main_late_check.c @@ -0,0 +1,10 @@ +int printf(const char *fmt, ...); + +// argc is only checked after argv[1] has already been read. +int main(int argc, char **argv) { + char *file = argv[1]; // $ Alert + if (argc < 2) + return 1; + printf("%s\n", file); + return 0; +} diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/bad_main_unrelated_check.c b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/bad_main_unrelated_check.c new file mode 100644 index 00000000..7184579e --- /dev/null +++ b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/bad_main_unrelated_check.c @@ -0,0 +1,10 @@ +int printf(const char *fmt, ...); + +// The check on argc guards a different access only. +int main(int argc, char **argv) { + if (argc > 2) { + printf("%s\n", argv[2]); + } + printf("%s\n", argv[1]); // $ Alert + return 0; +} diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/good_main_early_return.c b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/good_main_early_return.c new file mode 100644 index 00000000..102079cd --- /dev/null +++ b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/good_main_early_return.c @@ -0,0 +1,17 @@ +typedef struct _FILE FILE; +FILE *fopen(const char *path, const char *mode); +int printf(const char *fmt, ...); +void exit(int status); + +static void usage(const char *prog) { + printf("usage: %s \n", prog); + exit(1); +} + +int main(int argc, char *argv[]) { + if (argc < 2) { + usage(argv[0]); + } + FILE *fp = fopen(argv[1], "r"); + return fp != 0; +} diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/good_main_guarded.cpp b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/good_main_guarded.cpp new file mode 100644 index 00000000..526cbc8b --- /dev/null +++ b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/good_main_guarded.cpp @@ -0,0 +1,13 @@ +int printf(const char *fmt, ...); + +int main(int argc, char **argv) { + if (argc >= 3) { + printf("%s %s\n", argv[1], argv[2]); + } + const char *name = argc > 1 ? argv[1] : "default"; + printf("%s\n", name); + for (int i = 1; i < argc; i++) { + printf("%s\n", argv[i]); + } + return 0; +} diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/good_main_ne.c b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/good_main_ne.c new file mode 100644 index 00000000..42cd35e5 --- /dev/null +++ b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/good_main_ne.c @@ -0,0 +1,8 @@ +int printf(const char *fmt, ...); + +int main(int argc, char **argv) { + if (argc != 2) + return 1; + printf("%s\n", argv[1]); + return 0; +} diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/other.c b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/other.c new file mode 100644 index 00000000..c312b31b --- /dev/null +++ b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/other.c @@ -0,0 +1,7 @@ +int printf(const char *fmt, ...); + +// Not main: argv-like parameters of other functions are out of scope. +int helper(int argc, char **argv) { + printf("%s\n", argv[1]); + return 0; +} diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.expected b/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.expected new file mode 100644 index 00000000..b2973b10 --- /dev/null +++ b/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.expected @@ -0,0 +1,46 @@ +#select +| test.c:25:33:25:38 | height | test.c:24:16:24:19 | fread output argument | test.c:25:33:25:38 | height | This divisor depends on $@ and is not checked against zero before the $@. | test.c:24:16:24:19 | fread output argument | string read by fread | test.c:25:17:25:38 | ... / ... | division | +| test.c:37:24:37:29 | height | test.c:36:9:36:9 | fread output argument | test.c:37:24:37:29 | height | This divisor depends on $@ and is not checked against zero before the $@. | test.c:36:9:36:9 | fread output argument | string read by fread | test.c:37:10:37:29 | ... / ... | division | +| test.c:44:27:44:32 | height | test.c:43:9:43:12 | fread output argument | test.c:44:27:44:32 | height | This divisor depends on $@ and is not checked against zero before the $@. | test.c:43:9:43:12 | fread output argument | string read by fread | test.c:44:11:44:32 | ... % ... | division | +| test.c:46:12:46:17 | height | test.c:43:9:43:12 | fread output argument | test.c:46:12:46:17 | height | This divisor depends on $@ and is not checked against zero before the $@. | test.c:43:9:43:12 | fread output argument | string read by fread | test.c:46:3:46:17 | ... /= ... | division | +| test.c:58:24:58:27 | data | test.c:55:18:55:22 | recv output argument | test.c:58:24:58:27 | data | This divisor depends on $@ and is not checked against zero before the $@. | test.c:55:18:55:22 | recv output argument | buffer read by recv | test.c:58:18:58:27 | ... / ... | division | +| test.c:63:24:63:24 | n | test.c:62:16:62:30 | *call to getenv | test.c:63:24:63:24 | n | This divisor depends on $@ and is not checked against zero before the $@. | test.c:62:16:62:30 | *call to getenv | an environment variable | test.c:63:18:63:24 | ... % ... | division | +edges +| test.c:24:16:24:19 | fread output argument | test.c:25:17:25:19 | *img | provenance | | +| test.c:25:17:25:19 | *img | test.c:25:29:25:31 | *img | provenance | | +| test.c:25:29:25:31 | *img | test.c:25:17:25:19 | *img | provenance | | +| test.c:25:29:25:31 | *img | test.c:25:33:25:38 | height | provenance | Config | +| test.c:36:9:36:9 | fread output argument | test.c:37:10:37:10 | *p | provenance | | +| test.c:37:10:37:10 | *p | test.c:37:21:37:21 | *p | provenance | | +| test.c:37:21:37:21 | *p | test.c:37:24:37:29 | height | provenance | Config | +| test.c:43:9:43:12 | fread output argument | test.c:44:11:44:13 | *img | provenance | | +| test.c:44:11:44:13 | *img | test.c:44:23:44:25 | *img | provenance | | +| test.c:44:23:44:25 | *img | test.c:44:27:44:32 | height | provenance | Config | +| test.c:44:23:44:25 | *img | test.c:46:8:46:10 | *img | provenance | | +| test.c:46:8:46:10 | *img | test.c:46:12:46:17 | height | provenance | Config | +| test.c:55:18:55:22 | recv output argument | test.c:56:5:56:22 | ... = ... | provenance | TaintFunction | +| test.c:56:5:56:22 | ... = ... | test.c:58:24:58:27 | data | provenance | | +| test.c:62:11:62:14 | call to atoi | test.c:63:24:63:24 | n | provenance | | +| test.c:62:16:62:30 | *call to getenv | test.c:62:11:62:14 | call to atoi | provenance | TaintFunction | +nodes +| test.c:24:16:24:19 | fread output argument | semmle.label | fread output argument | +| test.c:25:17:25:19 | *img | semmle.label | *img | +| test.c:25:29:25:31 | *img | semmle.label | *img | +| test.c:25:33:25:38 | height | semmle.label | height | +| test.c:36:9:36:9 | fread output argument | semmle.label | fread output argument | +| test.c:37:10:37:10 | *p | semmle.label | *p | +| test.c:37:21:37:21 | *p | semmle.label | *p | +| test.c:37:24:37:29 | height | semmle.label | height | +| test.c:43:9:43:12 | fread output argument | semmle.label | fread output argument | +| test.c:44:11:44:13 | *img | semmle.label | *img | +| test.c:44:23:44:25 | *img | semmle.label | *img | +| test.c:44:27:44:32 | height | semmle.label | height | +| test.c:46:8:46:10 | *img | semmle.label | *img | +| test.c:46:12:46:17 | height | semmle.label | height | +| test.c:55:18:55:22 | recv output argument | semmle.label | recv output argument | +| test.c:56:5:56:22 | ... = ... | semmle.label | ... = ... | +| test.c:58:24:58:27 | data | semmle.label | data | +| test.c:62:11:62:14 | call to atoi | semmle.label | call to atoi | +| test.c:62:16:62:30 | *call to getenv | semmle.label | *call to getenv | +| test.c:63:24:63:24 | n | semmle.label | n | +subpaths diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.qlref b/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.qlref new file mode 100644 index 00000000..6a4d61f6 --- /dev/null +++ b/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.qlref @@ -0,0 +1,2 @@ +query: InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.ql +postprocess: utils/test/InlineExpectationsTestQuery.ql diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/test.c b/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/test.c new file mode 100644 index 00000000..aa9cb8a8 --- /dev/null +++ b/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/test.c @@ -0,0 +1,141 @@ +typedef unsigned long size_t; +typedef struct _FILE FILE; +FILE *fopen(const char *path, const char *mode); +size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); +int fclose(FILE *stream); +int recv(int s, char *buf, int len, int flags); +int atoi(const char *s); +char *getenv(const char *name); +int printf(const char *fmt, ...); + +struct Image { + char header[4]; + int width; + int height; + char data[10]; +}; + +// Pattern from an image parser: the height field is read from a file and +// used as a divisor without a zero check. +int process_image_bad(const char *filename) { + FILE *fp = fopen(filename, "r"); + struct Image img; + int total = 0; + while (fread(&img, sizeof(img), 1, fp) > 0) { // $ Source + int ratio = img.width / img.height; // $ Alert + total += ratio; + } + fclose(fp); + return total; +} + +// Same pattern through a pointer to the struct. +int process_image_ptr_bad(FILE *fp) { + struct Image img; + struct Image *p = &img; + fread(p, sizeof(img), 1, fp); // $ Source + return p->width / p->height; // $ Alert +} + +// Remainder operator and compound assignment. +int process_image_rem_bad(FILE *fp) { + struct Image img; + fread(&img, sizeof(img), 1, fp); // $ Source + int x = img.width % img.height; // $ Alert + int y = 1000; + y /= img.height; // $ Alert + return x + y; +} + +// Juliet-style: an integer is received from a socket, converted with atoi and +// used as a divisor. +void socket_bad(int sock) { + char input[16]; + int data = 0; + if (recv(sock, input, sizeof(input) - 1, 0) > 0) { // $ Source + data = atoi(input); + } + printf("%d\n", 100 / data); // $ Alert +} + +void env_bad() { + int n = atoi(getenv("COUNT")); // $ Source + printf("%d\n", 100 % n); // $ Alert +} + +// --- Safe counterparts: no alerts expected below --- + +int process_image_good(const char *filename) { + FILE *fp = fopen(filename, "r"); + struct Image img; + int total = 0; + while (fread(&img, sizeof(img), 1, fp) > 0) { + if (img.height == 0) + continue; + total += img.width / img.height; + } + fclose(fp); + return total; +} + +int process_image_good2(FILE *fp) { + struct Image img; + fread(&img, sizeof(img), 1, fp); + if (img.height != 0) { + return img.width / img.height; + } + return 0; +} + +int process_image_good3(FILE *fp) { + struct Image img; + fread(&img, sizeof(img), 1, fp); + if (img.height <= 0) + return -1; + return img.width / img.height; +} + +int process_image_good4(FILE *fp) { + struct Image img; + fread(&img, sizeof(img), 1, fp); + return img.height ? img.width / img.height : 0; +} + +void socket_good(int sock) { + char input[16]; + int data = 0; + if (recv(sock, input, sizeof(input) - 1, 0) > 0) { + data = atoi(input); + } + if (data != 0) { + printf("%d\n", 100 / data); + } +} + +// Tainted value used as dividend, not as divisor. +void socket_dividend_good(int sock) { + char input[16]; + recv(sock, input, sizeof(input) - 1, 0); + int data = atoi(input); + printf("%d\n", data / 7); +} + +// Untainted divisor. +int constant_divisor_good(int a, int b) { + return a / b; +} + +// Floating point division does not trap on zero. +double float_good(FILE *fp) { + double d; + fread(&d, sizeof(d), 1, fp); + return 1.0 / d; +} + +// Divisor made non-zero by arithmetic. +void socket_plus_one_good(int sock) { + char input[16]; + recv(sock, input, sizeof(input) - 1, 0); + unsigned int data = (unsigned int)atoi(input) % 10u; + printf("%u\n", 100u / (data + 1u)); +} From 4109588b01a2966143a7621dab892ff249247966 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 31 Oct 2026 04:08:41 +0000 Subject: [PATCH 3/5] Add argv/argc and unchecked privilege-change queries; gate struct field step; add query help Co-authored-by: felickz <1760475+felickz@users.noreply.github.com> --- .../ArgvAccessWithoutArgcCheck.md | 40 +++++ .../ArgvAccessWithoutArgcCheck.ql | 157 ++++++++++++++++++ .../TaintedDivisorWithoutZeroCheck.md | 38 +++++ .../TaintedDivisorWithoutZeroCheck.ql | 37 ++++- .../UncheckedPrivilegeChangeReturn.md | 37 +++++ .../UncheckedPrivilegeChangeReturn.ql | 45 +++++ .../ArgvAccessWithoutArgcCheck.expected | 4 + .../TaintedDivisorWithoutZeroCheck/test.c | 8 + .../UncheckedPrivilegeChangeReturn.expected | 10 ++ .../UncheckedPrivilegeChangeReturn.qlref | 2 + .../UncheckedPrivilegeChangeReturn/test.c | 69 ++++++++ 11 files changed, 440 insertions(+), 7 deletions(-) create mode 100644 languages/cpp/custom/src/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck.md create mode 100644 languages/cpp/custom/src/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck.ql create mode 100644 languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.md create mode 100644 languages/cpp/custom/src/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn.md create mode 100644 languages/cpp/custom/src/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn.ql create mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.expected create mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/UncheckedPrivilegeChangeReturn.expected create mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/UncheckedPrivilegeChangeReturn.qlref create mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/test.c diff --git a/languages/cpp/custom/src/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck.md b/languages/cpp/custom/src/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck.md new file mode 100644 index 00000000..c13e0690 --- /dev/null +++ b/languages/cpp/custom/src/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck.md @@ -0,0 +1,40 @@ +# Command-line argument accessed without checking argument count + +## Overview + +The C and C++ standards guarantee that `argv[argc]` is a NULL pointer. A program that reads `argv[n]` without first checking that `argc > n` therefore receives NULL (or reads past the end of the argument vector) when it is started with fewer arguments than expected. Passing that pointer on, for example to `fopen` or `strcpy`, dereferences NULL and crashes the program (CWE-476). + +The query reports reads of `argv[n]` and `*(argv + n)` in `main` with a constant index `n >= 1` that are not protected by a check of `argc`. An access is considered protected when it is only evaluated on one branch of a condition that reads `argc`, when a preceding `if` statement that reads `argc` leaves the current path (by returning, exiting, or calling a function that may exit), or when `argc` is passed to a function such as `getopt` before the access. + +## Recommendation + +Check `argc` before reading command-line arguments and print a usage message when too few arguments were supplied. + +## Example + +The following program passes `argv[1]` to `ProcessImage` without checking `argc`: + +```c +int main(int argc, char **argv) { + ProcessImage(argv[1]); // BAD: NULL when run without arguments + return 0; +} +``` + +The corrected version checks the argument count first: + +```c +int main(int argc, char **argv) { + if (argc < 2) { + fprintf(stderr, "usage: %s \n", argv[0]); + return 1; + } + ProcessImage(argv[1]); // GOOD + return 0; +} +``` + +## References + +- Common Weakness Enumeration: [CWE-476: NULL Pointer Dereference](https://cwe.mitre.org/data/definitions/476.html). +- Common Weakness Enumeration: [CWE-20: Improper Input Validation](https://cwe.mitre.org/data/definitions/20.html). diff --git a/languages/cpp/custom/src/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck.ql b/languages/cpp/custom/src/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck.ql new file mode 100644 index 00000000..bda94542 --- /dev/null +++ b/languages/cpp/custom/src/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck.ql @@ -0,0 +1,157 @@ +/** + * @name Command-line argument accessed without checking argument count + * @description Reading `argv[n]` in `main` without first checking that `argc` + * is greater than `n` reads a NULL pointer (or past the end of + * the argument vector) when the program is started with too few + * arguments. + * @kind problem + * @problem.severity warning + * @security-severity 3.3 + * @precision medium + * @id cpp/argv-access-without-argc-check + * @tags security + * reliability + * external/cwe/cwe-476 + * external/cwe/cwe-20 + */ + +import cpp +import semmle.code.cpp.controlflow.Dominance + +/** A `main` (or `wmain`) function with `argc` and `argv` parameters. */ +class MainFunction extends Function { + MainFunction() { + this.hasGlobalName(["main", "wmain"]) and + this.getNumberOfParameters() >= 2 + } + + Parameter getArgc() { result = this.getParameter(0) } + + Parameter getArgv() { result = this.getParameter(1) } +} + +/** + * An access to `argv[index]` in `main` with a constant `index` of at least 1, + * written either as `argv[index]` or as `*(argv + index)`. + */ +class ConstantArgvAccess extends Expr { + MainFunction main; + int index; + + ConstantArgvAccess() { + exists(Expr base, Expr offset | + ( + base = this.(ArrayExpr).getArrayBase() and + offset = this.(ArrayExpr).getArrayOffset() + or + exists(PointerAddExpr add | + add = this.(PointerDereferenceExpr).getOperand() and + base = add.getLeftOperand() and + offset = add.getRightOperand() + ) + ) and + base.(VariableAccess).getTarget() = main.getArgv() and + index = offset.getValue().toInt() and + index >= 1 + ) and + // `&argv[n]` does not read the element. + not this.getParent() instanceof AddressOfExpr and + not this.isUnevaluated() + } + + MainFunction getMain() { result = main } + + int getIndex() { result = index } +} + +/** Holds if `e` is an expression that reads `argc` of `main`. */ +predicate isArgcRead(Expr e, MainFunction main) { e.(VariableAccess).getTarget() = main.getArgc() } + +/** + * Holds if function `f` (transitively) calls a function that terminates the + * process, such as `exit` or `abort`. + */ +predicate mayTerminate(Function f) { + f.hasGlobalOrStdName([ + "exit", "_exit", "_Exit", "abort", "quick_exit", "err", "errx", "verr", "verrx" + ]) + or + exists(FunctionCall call | + call.getEnclosingFunction() = f and + mayTerminate(call.getTarget()) + ) +} + +/** Holds if statement `s` contains a statement or call that leaves the current path. */ +predicate leavesPath(Stmt s) { + exists(Stmt inner | inner.getParentStmt*() = s | + inner instanceof ReturnStmt or + inner instanceof GotoStmt or + inner instanceof BreakStmt or + inner instanceof ContinueStmt + ) + or + exists(FunctionCall call | call.getEnclosingStmt().getParentStmt*() = s | + mayTerminate(call.getTarget()) + ) + or + exists(ThrowExpr t | t.getEnclosingStmt().getParentStmt*() = s) +} + +/** + * Holds if `access` is only evaluated on one branch of a condition `cond`, + * for example inside an `if`, loop, `?:` or the right operand of `&&`/`||`. + * + * This is purely syntactic so that it also works when several programs in + * the same database define `main` (whose definitions are then merged). + */ +predicate isConditionallyEvaluated(ConstantArgvAccess access, Expr cond) { + exists(IfStmt s | cond = s.getCondition() | access.getEnclosingStmt().getParentStmt+() = s) + or + exists(Loop l | cond = l.getCondition() | + access.getEnclosingStmt().getParentStmt+() = l.getStmt() + ) + or + exists(ConditionalExpr c | cond = c.getCondition() | + access.getParent*() = [c.getThen(), c.getElse()] + ) + or + exists(BinaryLogicalOperation op | cond = op.getLeftOperand() | + access.getParent*() = op.getRightOperand() + ) +} + +/** + * Holds if `access` is protected by a check of `argc`: either the access is + * only reached on one branch of a condition that reads `argc`, or a preceding + * `if` statement that reads `argc` leaves the current path (by returning, + * exiting or similar) when the check fails. + */ +predicate isArgcChecked(ConstantArgvAccess access) { + exists(Expr cond, Expr argc | + isConditionallyEvaluated(access, cond) and + isArgcRead(argc, access.getMain()) and + argc.getParent*() = cond + ) + or + exists(IfStmt ifStmt, Expr argc | + isArgcRead(argc, access.getMain()) and + argc.getParent*() = ifStmt.getCondition() and + (leavesPath(ifStmt.getThen()) or leavesPath(ifStmt.getElse())) and + dominates(argc, access) + ) + or + // `argc` is validated by a helper, for example `check_args(argc, argv)`, or + // passed to an option parser such as `getopt`. + exists(FunctionCall call, Expr argc | + isArgcRead(argc, access.getMain()) and + argc = call.getAnArgument() and + dominates(call, access) + ) +} + +from ConstantArgvAccess access +where not isArgcChecked(access) +select access, + "argv[" + access.getIndex() + "] is read without checking that argc is greater than " + + access.getIndex() + "; it is NULL when the program is run with fewer arguments." diff --git a/languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.md b/languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.md new file mode 100644 index 00000000..201e07f1 --- /dev/null +++ b/languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.md @@ -0,0 +1,38 @@ +# Untrusted divisor without zero check + +## Overview + +Integer division and remainder by zero is undefined behavior in C and C++ and, on most platforms, terminates the process with a fault such as `SIGFPE`. When the divisor comes from untrusted input (a file, a network socket, an environment variable or the command line) and is not checked against zero, an attacker can crash the program by supplying `0`. This is a denial-of-service vulnerability (CWE-369). + +The query tracks untrusted data to the divisor of `/`, `%`, `/=` and `%=` on integer types. Fields read from a structure that was filled as a whole (for example `fread(&img, sizeof(img), 1, fp)`) are treated as untrusted. Any comparison of the divisor, or a truthiness test such as `if (n)`, is treated as validation, and divisors that range analysis shows to be non-zero (such as `(x % 10) + 1`) are not reported. + +## Recommendation + +Check every divisor that is derived from external input before using it, and reject or handle the zero case explicitly. + +## Example + +The following example reads an image header from a file and divides by its `height` field without checking it: + +```c +struct Image img; +while (fread(&img, sizeof(img), 1, fp) > 0) { + int ratio = img.width / img.height; // BAD: height may be 0 +} +``` + +The corrected version rejects a zero height before the division: + +```c +struct Image img; +while (fread(&img, sizeof(img), 1, fp) > 0) { + if (img.height == 0) + continue; + int ratio = img.width / img.height; // GOOD +} +``` + +## References + +- Common Weakness Enumeration: [CWE-369: Divide By Zero](https://cwe.mitre.org/data/definitions/369.html). +- SEI CERT C Coding Standard: [INT33-C. Ensure that division and remainder operations do not result in divide-by-zero errors](https://wiki.sei.cmu.edu/confluence/display/c/INT33-C.+Ensure+that+division+and+remainder+operations+do+not+result+in+divide-by-zero+errors). diff --git a/languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.ql b/languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.ql index a3d2938c..b69c103e 100644 --- a/languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.ql +++ b/languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.ql @@ -19,6 +19,7 @@ import semmle.code.cpp.ir.dataflow.TaintTracking import semmle.code.cpp.security.FlowSources import semmle.code.cpp.controlflow.IRGuards import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis +import semmle.code.cpp.controlflow.Dominance import TaintedDivisorFlow::PathGraph /** Gets the divisor operand of an integer division or remainder operation `op`. */ @@ -65,19 +66,41 @@ predicate validatesValue(IRGuardCondition g, Expr e, boolean branch) { ) } +/** + * Holds if `fa` accesses field `f` of the aggregate denoted by `base`, either + * directly (`s.f`, `p->f`) or through an explicit dereference (`(*p).f`). + */ +predicate fieldAccessOf(FieldAccess fa, VariableAccess base, Field f) { + f = fa.getTarget() and + ( + base = fa.getQualifier() + or + base = fa.getQualifier().(PointerDereferenceExpr).getOperand() + ) +} + +/** + * Holds if field `f` of variable `v` is assigned by `assign`, which dominates + * the field read `fa`, so that `fa` no longer reads the originally written + * value. + */ +predicate overwrittenBefore(FieldAccess fa, Variable v, Field f) { + exists(Assignment assign, VariableAccess otherBase | + fieldAccessOf(assign.getLValue(), otherBase, f) and + otherBase.getTarget() = v and + dominates(assign, fa) + ) +} + /** * Holds if `fieldRead` reads a field of an aggregate `base` that was written * as a whole (for example by `fread(&img, ...)` or `recv(s, &hdr, ...)`). */ predicate aggregateToFieldStep(DataFlow::Node aggregate, DataFlow::Node fieldRead) { - exists(FieldAccess fa, VariableAccess base | + exists(FieldAccess fa, VariableAccess base, Field f | fieldRead.asExpr() = fa and - ( - base = fa.getQualifier() - or - // `p->f` where `p` points to the aggregate. - base = fa.getQualifier().(PointerDereferenceExpr).getOperand() - ) and + fieldAccessOf(fa, base, f) and + not overwrittenBefore(fa, base.getTarget(), f) and ( aggregate.asIndirectExpr() = base or diff --git a/languages/cpp/custom/src/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn.md b/languages/cpp/custom/src/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn.md new file mode 100644 index 00000000..4142ad08 --- /dev/null +++ b/languages/cpp/custom/src/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn.md @@ -0,0 +1,37 @@ +# Unchecked return value of privilege-changing function + +## Overview + +Functions that change the security context of a thread or process, such as the Windows `ImpersonateSelf`, `ImpersonateLoggedOnUser` and `RevertToSelf`, or the POSIX `setuid`, `setgid` and `setresuid`, can fail. If the program ignores the return value, it continues to run in a different security context than it expects. For example, after a failed `ImpersonateSelf` the thread still runs with the (possibly privileged) process token, and after a failed `setuid` the process keeps its elevated privileges (CWE-252, CWE-273). + +The query reports calls to these functions whose result is discarded. + +## Recommendation + +Always check the return value of privilege-changing functions and stop processing (or abort) when the call fails. + +## Example + +The following code ignores the result of `ImpersonateSelf`: + +```c +ImpersonateSelf(SecurityImpersonation); // BAD +do_restricted_work(); +RevertToSelf(); // BAD +``` + +The corrected version stops when either call fails: + +```c +if (!ImpersonateSelf(SecurityImpersonation)) + exit(1); +do_restricted_work(); +if (!RevertToSelf()) + exit(1); +``` + +## References + +- Common Weakness Enumeration: [CWE-252: Unchecked Return Value](https://cwe.mitre.org/data/definitions/252.html). +- Common Weakness Enumeration: [CWE-273: Improper Check for Dropped Privileges](https://cwe.mitre.org/data/definitions/273.html). +- SEI CERT C Coding Standard: [POS37-C. Ensure that privilege relinquishment is successful](https://wiki.sei.cmu.edu/confluence/display/c/POS37-C.+Ensure+that+privilege+relinquishment+is+successful). diff --git a/languages/cpp/custom/src/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn.ql b/languages/cpp/custom/src/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn.ql new file mode 100644 index 00000000..69ba3658 --- /dev/null +++ b/languages/cpp/custom/src/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn.ql @@ -0,0 +1,45 @@ +/** + * @name Unchecked return value of privilege-changing function + * @description Ignoring the result of a call that impersonates, reverts or + * drops privileges (such as `ImpersonateSelf` or `setuid`) means + * the program keeps running in an unexpected security context + * when the call fails. + * @kind problem + * @problem.severity warning + * @security-severity 6.4 + * @precision high + * @id cpp/unchecked-privilege-change-return + * @tags security + * external/cwe/cwe-252 + * external/cwe/cwe-273 + */ + +import cpp + +/** A function that changes the security context of the current thread or process. */ +class PrivilegeChangeFunction extends Function { + PrivilegeChangeFunction() { + this.hasGlobalName([ + // Windows impersonation and token APIs + "ImpersonateSelf", "ImpersonateLoggedOnUser", "ImpersonateNamedPipeClient", + "ImpersonateAnonymousToken", "ImpersonateDdeClientWindow", "RpcImpersonateClient", + "CoImpersonateClient", "RevertToSelf", "RpcRevertToSelf", "CoRevertToSelf", + "SetThreadToken", + // POSIX credential APIs + "setuid", "setgid", "seteuid", "setegid", "setreuid", "setregid", "setresuid", "setresgid", + "setgroups", "initgroups", "setfsuid", "setfsgid" + ]) + } +} + +from FunctionCall call, PrivilegeChangeFunction f +where + call.getTarget() = f and + ( + call instanceof ExprInVoidContext + or + call.getFullyConverted().(Conversion).getUnspecifiedType() instanceof VoidType + ) +select call, + "The return value of $@ is not checked, so a failure leaves the program in the wrong security context.", + f, f.getName() diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.expected b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.expected new file mode 100644 index 00000000..c4055d2c --- /dev/null +++ b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.expected @@ -0,0 +1,4 @@ +| bad_main.c:13:16:13:22 | access to array | argv[1] is read without checking that argc is greater than 1; it is NULL when the program is run with fewer arguments. | +| bad_main.c:14:18:14:28 | * ... | argv[2] is read without checking that argc is greater than 2; it is NULL when the program is run with fewer arguments. | +| bad_main_late_check.c:5:16:5:22 | access to array | argv[1] is read without checking that argc is greater than 1; it is NULL when the program is run with fewer arguments. | +| bad_main_unrelated_check.c:8:18:8:24 | access to array | argv[1] is read without checking that argc is greater than 1; it is NULL when the program is run with fewer arguments. | diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/test.c b/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/test.c index aa9cb8a8..73637137 100644 --- a/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/test.c +++ b/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/test.c @@ -139,3 +139,11 @@ void socket_plus_one_good(int sock) { unsigned int data = (unsigned int)atoi(input) % 10u; printf("%u\n", 100u / (data + 1u)); } + +// The field is overwritten with a constant before the division. +int process_image_overwrite_good(FILE *fp) { + struct Image img; + fread(&img, sizeof(img), 1, fp); + img.height = 4; + return img.width / img.height; +} diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/UncheckedPrivilegeChangeReturn.expected b/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/UncheckedPrivilegeChangeReturn.expected new file mode 100644 index 00000000..a68d11ee --- /dev/null +++ b/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/UncheckedPrivilegeChangeReturn.expected @@ -0,0 +1,10 @@ +| test.c:22:3:22:17 | call to ImpersonateSelf | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:7:6:7:20 | ImpersonateSelf | ImpersonateSelf | +| test.c:24:3:24:14 | call to RevertToSelf | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:10:6:10:17 | RevertToSelf | RevertToSelf | +| test.c:28:3:28:25 | call to ImpersonateLoggedOnUser | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:8:6:8:28 | ImpersonateLoggedOnUser | ImpersonateLoggedOnUser | +| test.c:29:3:29:28 | call to ImpersonateNamedPipeClient | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:9:6:9:31 | ImpersonateNamedPipeClient | ImpersonateNamedPipeClient | +| test.c:30:3:30:16 | call to SetThreadToken | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:11:6:11:19 | SetThreadToken | SetThreadToken | +| test.c:34:3:34:8 | call to setgid | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:13:5:13:10 | setgid | setgid | +| test.c:35:3:35:8 | call to setuid | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:12:5:12:10 | setuid | setuid | +| test.c:36:3:36:9 | call to seteuid | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:14:5:14:11 | seteuid | seteuid | +| test.c:37:3:37:11 | call to setresuid | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:15:5:15:13 | setresuid | setresuid | +| test.c:68:9:68:14 | call to setuid | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:12:5:12:10 | setuid | setuid | diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/UncheckedPrivilegeChangeReturn.qlref b/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/UncheckedPrivilegeChangeReturn.qlref new file mode 100644 index 00000000..cac04002 --- /dev/null +++ b/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/UncheckedPrivilegeChangeReturn.qlref @@ -0,0 +1,2 @@ +query: InputValidationNullNumeric/UncheckedPrivilegeChangeReturn.ql +postprocess: utils/test/InlineExpectationsTestQuery.ql diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/test.c b/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/test.c new file mode 100644 index 00000000..10721c30 --- /dev/null +++ b/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/test.c @@ -0,0 +1,69 @@ +typedef int BOOL; +typedef void *HANDLE; +typedef unsigned int uid_t; +typedef unsigned int gid_t; +typedef enum { SecurityAnonymous, SecurityIdentification, SecurityImpersonation } SECURITY_IMPERSONATION_LEVEL; + +BOOL ImpersonateSelf(SECURITY_IMPERSONATION_LEVEL level); +BOOL ImpersonateLoggedOnUser(HANDLE token); +BOOL ImpersonateNamedPipeClient(HANDLE pipe); +BOOL RevertToSelf(void); +BOOL SetThreadToken(HANDLE *thread, HANDLE token); +int setuid(uid_t uid); +int setgid(gid_t gid); +int seteuid(uid_t uid); +int setresuid(uid_t r, uid_t e, uid_t s); +int printf(const char *fmt, ...); +void exit(int status); + +// Juliet-style: the result of ImpersonateSelf is ignored, so on failure the +// thread keeps running with the process token. +void impersonate_bad() { + ImpersonateSelf(SecurityImpersonation); // $ Alert + printf("doing work\n"); + RevertToSelf(); // $ Alert +} + +void impersonate_user_bad(HANDLE token, HANDLE pipe) { + ImpersonateLoggedOnUser(token); // $ Alert + ImpersonateNamedPipeClient(pipe); // $ Alert + SetThreadToken(0, token); // $ Alert +} + +void drop_privileges_bad(uid_t uid, gid_t gid) { + setgid(gid); // $ Alert + setuid(uid); // $ Alert + seteuid(uid); // $ Alert + setresuid(uid, uid, uid); // $ Alert +} + +// --- Safe counterparts: no alerts expected below --- + +void impersonate_good() { + if (!ImpersonateSelf(SecurityImpersonation)) { + exit(1); + } + printf("doing work\n"); + if (!RevertToSelf()) { + exit(1); + } +} + +void drop_privileges_good(uid_t uid, gid_t gid) { + if (setgid(gid) != 0) + exit(1); + if (setuid(uid) != 0) + exit(1); + int r = seteuid(uid); + if (r == -1) + exit(1); +} + +BOOL impersonate_return_good(HANDLE token) { + return ImpersonateLoggedOnUser(token); +} + +// Explicitly discarding the result is still unsafe for privilege changes. +void explicit_discard_bad(uid_t uid) { + (void)setuid(uid); // $ Alert +} From c380b1c4c132cc6bd3ad446ed98e6495464364e8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 2 Nov 2026 20:25:27 +0000 Subject: [PATCH 4/5] Address targeted C++ review feedback Co-authored-by: felickz <1760475+felickz@users.noreply.github.com> --- .../ArgvAccessWithoutArgcCheck.ql | 7 +- .../TaintedDivisorWithoutZeroCheck.ql | 10 ++- .../UncheckedPrivilegeChangeReturn.ql | 2 +- .../ArgvAccessWithoutArgcCheck.actual | 5 ++ .../ArgvAccessWithoutArgcCheck/bad_main.c | 3 + .../TaintedDivisorWithoutZeroCheck.actual | 65 +++++++++++++++++++ .../TaintedDivisorWithoutZeroCheck/test.c | 8 +++ .../UncheckedPrivilegeChangeReturn.actual | 10 +++ .../UncheckedPrivilegeChangeReturn/test.c | 12 ++++ 9 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.actual create mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.actual create mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/UncheckedPrivilegeChangeReturn.actual diff --git a/languages/cpp/custom/src/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck.ql b/languages/cpp/custom/src/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck.ql index bda94542..2dcd6660 100644 --- a/languages/cpp/custom/src/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck.ql +++ b/languages/cpp/custom/src/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck.ql @@ -131,7 +131,12 @@ predicate isArgcChecked(ConstantArgvAccess access) { exists(Expr cond, Expr argc | isConditionallyEvaluated(access, cond) and isArgcRead(argc, access.getMain()) and - argc.getParent*() = cond + argc.getParent*() = cond and + ( + not exists(Loop l | cond = l.getCondition()) + or + dominates(argc, access) + ) ) or exists(IfStmt ifStmt, Expr argc | diff --git a/languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.ql b/languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.ql index b69c103e..701fdc85 100644 --- a/languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.ql +++ b/languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.ql @@ -81,14 +81,18 @@ predicate fieldAccessOf(FieldAccess fa, VariableAccess base, Field f) { /** * Holds if field `f` of variable `v` is assigned by `assign`, which dominates - * the field read `fa`, so that `fa` no longer reads the originally written - * value. + * the field read `fa` or is itself the assignment's lvalue. */ predicate overwrittenBefore(FieldAccess fa, Variable v, Field f) { exists(Assignment assign, VariableAccess otherBase | fieldAccessOf(assign.getLValue(), otherBase, f) and otherBase.getTarget() = v and - dominates(assign, fa) + exists(FunctionCall write | + write.getTarget().hasGlobalName(["fread", "recv"]) and + write.passesByReference(_, otherBase) and + dominates(write, assign) + ) and + (dominates(assign, fa) or fa = assign.getLValue()) ) } diff --git a/languages/cpp/custom/src/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn.ql b/languages/cpp/custom/src/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn.ql index 69ba3658..90a7d689 100644 --- a/languages/cpp/custom/src/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn.ql +++ b/languages/cpp/custom/src/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn.ql @@ -27,7 +27,7 @@ class PrivilegeChangeFunction extends Function { "SetThreadToken", // POSIX credential APIs "setuid", "setgid", "seteuid", "setegid", "setreuid", "setregid", "setresuid", "setresgid", - "setgroups", "initgroups", "setfsuid", "setfsgid" + "setgroups", "initgroups" ]) } } diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.actual b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.actual new file mode 100644 index 00000000..626fc287 --- /dev/null +++ b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.actual @@ -0,0 +1,5 @@ +| bad_main.c:13:16:13:22 | access to array | argv[1] is read without checking that argc is greater than 1; it is NULL when the program is run with fewer arguments. | +| bad_main.c:14:18:14:28 | * ... | argv[2] is read without checking that argc is greater than 2; it is NULL when the program is run with fewer arguments. | +| bad_main.c:17:18:17:24 | access to array | argv[1] is read without checking that argc is greater than 1; it is NULL when the program is run with fewer arguments. | +| bad_main_late_check.c:5:16:5:22 | access to array | argv[1] is read without checking that argc is greater than 1; it is NULL when the program is run with fewer arguments. | +| bad_main_unrelated_check.c:8:18:8:24 | access to array | argv[1] is read without checking that argc is greater than 1; it is NULL when the program is run with fewer arguments. | diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/bad_main.c b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/bad_main.c index b1894e4c..4171d51f 100644 --- a/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/bad_main.c +++ b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/bad_main.c @@ -13,5 +13,8 @@ int main(int argc, char **argv) { ProcessImage(argv[1]); // $ Alert printf("%s\n", *(argv + 2)); // $ Alert printf("%s\n", argv[0]); + do { + ProcessImage(argv[1]); // $ Alert + } while (argc > 1); return 0; } diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.actual b/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.actual new file mode 100644 index 00000000..8938b345 --- /dev/null +++ b/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.actual @@ -0,0 +1,65 @@ +#select +| test.c:25:33:25:38 | height | test.c:24:16:24:19 | fread output argument | test.c:25:33:25:38 | height | This divisor depends on $@ and is not checked against zero before the $@. | test.c:24:16:24:19 | fread output argument | string read by fread | test.c:25:17:25:38 | ... / ... | division | +| test.c:37:24:37:29 | height | test.c:36:9:36:9 | fread output argument | test.c:37:24:37:29 | height | This divisor depends on $@ and is not checked against zero before the $@. | test.c:36:9:36:9 | fread output argument | string read by fread | test.c:37:10:37:29 | ... / ... | division | +| test.c:44:27:44:32 | height | test.c:43:9:43:12 | fread output argument | test.c:44:27:44:32 | height | This divisor depends on $@ and is not checked against zero before the $@. | test.c:43:9:43:12 | fread output argument | string read by fread | test.c:44:11:44:32 | ... % ... | division | +| test.c:46:12:46:17 | height | test.c:43:9:43:12 | fread output argument | test.c:46:12:46:17 | height | This divisor depends on $@ and is not checked against zero before the $@. | test.c:43:9:43:12 | fread output argument | string read by fread | test.c:46:3:46:17 | ... /= ... | division | +| test.c:58:24:58:27 | data | test.c:55:18:55:22 | recv output argument | test.c:58:24:58:27 | data | This divisor depends on $@ and is not checked against zero before the $@. | test.c:55:18:55:22 | recv output argument | buffer read by recv | test.c:58:18:58:27 | ... / ... | division | +| test.c:63:24:63:24 | n | test.c:62:16:62:30 | *call to getenv | test.c:63:24:63:24 | n | This divisor depends on $@ and is not checked against zero before the $@. | test.c:62:16:62:30 | *call to getenv | an environment variable | test.c:63:18:63:24 | ... % ... | division | +| test.c:148:26:148:31 | height | test.c:146:9:146:12 | fread output argument | test.c:148:26:148:31 | height | This divisor depends on $@ and is not checked against zero before the $@. | test.c:146:9:146:12 | fread output argument | string read by fread | test.c:148:10:148:31 | ... / ... | division | +| test.c:156:20:156:25 | height | test.c:155:9:155:12 | fread output argument | test.c:156:20:156:25 | height | This divisor depends on $@ and is not checked against zero before the $@. | test.c:155:9:155:12 | fread output argument | string read by fread | test.c:156:10:156:25 | ... / ... | division | +edges +| test.c:24:16:24:19 | fread output argument | test.c:25:17:25:19 | *img | provenance | | +| test.c:25:17:25:19 | *img | test.c:25:29:25:31 | *img | provenance | | +| test.c:25:29:25:31 | *img | test.c:25:17:25:19 | *img | provenance | | +| test.c:25:29:25:31 | *img | test.c:25:33:25:38 | height | provenance | Config | +| test.c:36:9:36:9 | fread output argument | test.c:37:10:37:10 | *p | provenance | | +| test.c:37:10:37:10 | *p | test.c:37:21:37:21 | *p | provenance | | +| test.c:37:21:37:21 | *p | test.c:37:24:37:29 | height | provenance | Config | +| test.c:43:9:43:12 | fread output argument | test.c:44:11:44:13 | *img | provenance | | +| test.c:44:11:44:13 | *img | test.c:44:23:44:25 | *img | provenance | | +| test.c:44:23:44:25 | *img | test.c:44:27:44:32 | height | provenance | Config | +| test.c:44:23:44:25 | *img | test.c:46:8:46:10 | *img | provenance | | +| test.c:46:8:46:10 | *img | test.c:46:12:46:17 | height | provenance | Config | +| test.c:55:18:55:22 | recv output argument | test.c:56:5:56:22 | ... = ... | provenance | TaintFunction | +| test.c:56:5:56:22 | ... = ... | test.c:58:24:58:27 | data | provenance | | +| test.c:62:11:62:14 | call to atoi | test.c:63:24:63:24 | n | provenance | | +| test.c:62:16:62:30 | *call to getenv | test.c:62:11:62:14 | call to atoi | provenance | TaintFunction | +| test.c:146:9:146:12 | fread output argument | test.c:147:3:147:5 | *img | provenance | | +| test.c:147:3:147:5 | *img | test.c:148:10:148:12 | *img | provenance | | +| test.c:148:10:148:12 | *img | test.c:148:22:148:24 | *img | provenance | | +| test.c:148:22:148:24 | *img | test.c:148:26:148:31 | height | provenance | Config | +| test.c:155:9:155:12 | fread output argument | test.c:156:16:156:18 | *img | provenance | | +| test.c:156:16:156:18 | *img | test.c:156:20:156:25 | height | provenance | Config | +nodes +| test.c:24:16:24:19 | fread output argument | semmle.label | fread output argument | +| test.c:25:17:25:19 | *img | semmle.label | *img | +| test.c:25:29:25:31 | *img | semmle.label | *img | +| test.c:25:33:25:38 | height | semmle.label | height | +| test.c:36:9:36:9 | fread output argument | semmle.label | fread output argument | +| test.c:37:10:37:10 | *p | semmle.label | *p | +| test.c:37:21:37:21 | *p | semmle.label | *p | +| test.c:37:24:37:29 | height | semmle.label | height | +| test.c:43:9:43:12 | fread output argument | semmle.label | fread output argument | +| test.c:44:11:44:13 | *img | semmle.label | *img | +| test.c:44:23:44:25 | *img | semmle.label | *img | +| test.c:44:27:44:32 | height | semmle.label | height | +| test.c:46:8:46:10 | *img | semmle.label | *img | +| test.c:46:12:46:17 | height | semmle.label | height | +| test.c:55:18:55:22 | recv output argument | semmle.label | recv output argument | +| test.c:56:5:56:22 | ... = ... | semmle.label | ... = ... | +| test.c:58:24:58:27 | data | semmle.label | data | +| test.c:62:11:62:14 | call to atoi | semmle.label | call to atoi | +| test.c:62:16:62:30 | *call to getenv | semmle.label | *call to getenv | +| test.c:63:24:63:24 | n | semmle.label | n | +| test.c:146:9:146:12 | fread output argument | semmle.label | fread output argument | +| test.c:147:3:147:5 | *img | semmle.label | *img | +| test.c:148:10:148:12 | *img | semmle.label | *img | +| test.c:148:22:148:24 | *img | semmle.label | *img | +| test.c:148:26:148:31 | height | semmle.label | height | +| test.c:155:9:155:12 | fread output argument | semmle.label | fread output argument | +| test.c:156:16:156:18 | *img | semmle.label | *img | +| test.c:156:20:156:25 | height | semmle.label | height | +subpaths +testFailures +| test.c:146:9:146:12 | fread output argument | Unexpected result: Source | +| test.c:148:26:148:31 | height | Unexpected result: Alert | diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/test.c b/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/test.c index 73637137..3de78a47 100644 --- a/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/test.c +++ b/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/test.c @@ -147,3 +147,11 @@ int process_image_overwrite_good(FILE *fp) { img.height = 4; return img.width / img.height; } + +// A field assignment before the input read does not overwrite the tainted value. +int process_image_assign_before_read_bad(FILE *fp) { + struct Image img; + img.height = 1; + fread(&img, sizeof(img), 1, fp); // $ Source + return 100 / img.height; // $ Alert +} diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/UncheckedPrivilegeChangeReturn.actual b/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/UncheckedPrivilegeChangeReturn.actual new file mode 100644 index 00000000..c9214057 --- /dev/null +++ b/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/UncheckedPrivilegeChangeReturn.actual @@ -0,0 +1,10 @@ +| test.c:25:3:25:17 | call to ImpersonateSelf | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:8:6:8:20 | ImpersonateSelf | ImpersonateSelf | +| test.c:27:3:27:14 | call to RevertToSelf | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:11:6:11:17 | RevertToSelf | RevertToSelf | +| test.c:31:3:31:25 | call to ImpersonateLoggedOnUser | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:9:6:9:28 | ImpersonateLoggedOnUser | ImpersonateLoggedOnUser | +| test.c:32:3:32:28 | call to ImpersonateNamedPipeClient | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:10:6:10:31 | ImpersonateNamedPipeClient | ImpersonateNamedPipeClient | +| test.c:33:3:33:16 | call to SetThreadToken | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:12:6:12:19 | SetThreadToken | SetThreadToken | +| test.c:37:3:37:8 | call to setgid | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:14:5:14:10 | setgid | setgid | +| test.c:38:3:38:8 | call to setuid | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:13:5:13:10 | setuid | setuid | +| test.c:39:3:39:9 | call to seteuid | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:15:5:15:11 | seteuid | seteuid | +| test.c:40:3:40:11 | call to setresuid | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:16:5:16:13 | setresuid | setresuid | +| test.c:71:9:71:14 | call to setuid | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:13:5:13:10 | setuid | setuid | diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/test.c b/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/test.c index 10721c30..070112e3 100644 --- a/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/test.c +++ b/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/test.c @@ -2,6 +2,7 @@ typedef int BOOL; typedef void *HANDLE; typedef unsigned int uid_t; typedef unsigned int gid_t; +typedef unsigned int fsid_t; typedef enum { SecurityAnonymous, SecurityIdentification, SecurityImpersonation } SECURITY_IMPERSONATION_LEVEL; BOOL ImpersonateSelf(SECURITY_IMPERSONATION_LEVEL level); @@ -13,6 +14,8 @@ int setuid(uid_t uid); int setgid(gid_t gid); int seteuid(uid_t uid); int setresuid(uid_t r, uid_t e, uid_t s); +int setfsuid(fsid_t fsuid); +int setfsgid(fsid_t fsgid); int printf(const char *fmt, ...); void exit(int status); @@ -67,3 +70,12 @@ BOOL impersonate_return_good(HANDLE token) { void explicit_discard_bad(uid_t uid) { (void)setuid(uid); // $ Alert } + +void filesystem_ids_good(fsid_t fsuid, fsid_t fsgid) { + setfsuid(fsuid); + if (setfsuid((fsid_t)-1) != fsuid) + exit(1); + setfsgid(fsgid); + if (setfsgid((fsid_t)-1) != fsgid) + exit(1); +} From 87d9fb3b0c1cd3cac19a7dfd5e49719a2e3960b0 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 2 Nov 2026 22:18:03 +0000 Subject: [PATCH 5/5] Fix aggregate overwrite and update C++ expectations Co-authored-by: felickz <1760475+felickz@users.noreply.github.com> --- .../TaintedDivisorWithoutZeroCheck.ql | 21 +++--- .../ArgvAccessWithoutArgcCheck.actual | 5 -- .../ArgvAccessWithoutArgcCheck.expected | 1 + .../TaintedDivisorWithoutZeroCheck.actual | 65 ------------------- .../TaintedDivisorWithoutZeroCheck.expected | 6 ++ .../UncheckedPrivilegeChangeReturn.actual | 10 --- 6 files changed, 20 insertions(+), 88 deletions(-) delete mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.actual delete mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.actual delete mode 100644 languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/UncheckedPrivilegeChangeReturn.actual diff --git a/languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.ql b/languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.ql index 701fdc85..9d7c2951 100644 --- a/languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.ql +++ b/languages/cpp/custom/src/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck.ql @@ -83,15 +83,20 @@ predicate fieldAccessOf(FieldAccess fa, VariableAccess base, Field f) { * Holds if field `f` of variable `v` is assigned by `assign`, which dominates * the field read `fa` or is itself the assignment's lvalue. */ -predicate overwrittenBefore(FieldAccess fa, Variable v, Field f) { - exists(Assignment assign, VariableAccess otherBase | +predicate overwrittenBefore( + DataFlow::Node aggregate, VariableAccess base, FieldAccess fa, Field f +) { + exists(Assignment assign, VariableAccess otherBase, FunctionCall write, VariableAccess writeBase | fieldAccessOf(assign.getLValue(), otherBase, f) and - otherBase.getTarget() = v and - exists(FunctionCall write | - write.getTarget().hasGlobalName(["fread", "recv"]) and - write.passesByReference(_, otherBase) and - dominates(write, assign) + otherBase.getTarget() = base.getTarget() and + ( + aggregate.asIndirectExpr().(VariableAccess).getTarget() = base.getTarget() + or aggregate.asExpr().(VariableAccess).getTarget() = base.getTarget() ) and + write.getTarget().hasGlobalName(["fread", "recv"]) and + writeBase.getParent*() = write.getAnArgument() and + writeBase.getTarget() = base.getTarget() and + dominates(write, assign) and (dominates(assign, fa) or fa = assign.getLValue()) ) } @@ -104,7 +109,7 @@ predicate aggregateToFieldStep(DataFlow::Node aggregate, DataFlow::Node fieldRea exists(FieldAccess fa, VariableAccess base, Field f | fieldRead.asExpr() = fa and fieldAccessOf(fa, base, f) and - not overwrittenBefore(fa, base.getTarget(), f) and + not overwrittenBefore(aggregate, base, fa, f) and ( aggregate.asIndirectExpr() = base or diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.actual b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.actual deleted file mode 100644 index 626fc287..00000000 --- a/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.actual +++ /dev/null @@ -1,5 +0,0 @@ -| bad_main.c:13:16:13:22 | access to array | argv[1] is read without checking that argc is greater than 1; it is NULL when the program is run with fewer arguments. | -| bad_main.c:14:18:14:28 | * ... | argv[2] is read without checking that argc is greater than 2; it is NULL when the program is run with fewer arguments. | -| bad_main.c:17:18:17:24 | access to array | argv[1] is read without checking that argc is greater than 1; it is NULL when the program is run with fewer arguments. | -| bad_main_late_check.c:5:16:5:22 | access to array | argv[1] is read without checking that argc is greater than 1; it is NULL when the program is run with fewer arguments. | -| bad_main_unrelated_check.c:8:18:8:24 | access to array | argv[1] is read without checking that argc is greater than 1; it is NULL when the program is run with fewer arguments. | diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.expected b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.expected index c4055d2c..626fc287 100644 --- a/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.expected +++ b/languages/cpp/custom/test/InputValidationNullNumeric/ArgvAccessWithoutArgcCheck/ArgvAccessWithoutArgcCheck.expected @@ -1,4 +1,5 @@ | bad_main.c:13:16:13:22 | access to array | argv[1] is read without checking that argc is greater than 1; it is NULL when the program is run with fewer arguments. | | bad_main.c:14:18:14:28 | * ... | argv[2] is read without checking that argc is greater than 2; it is NULL when the program is run with fewer arguments. | +| bad_main.c:17:18:17:24 | access to array | argv[1] is read without checking that argc is greater than 1; it is NULL when the program is run with fewer arguments. | | bad_main_late_check.c:5:16:5:22 | access to array | argv[1] is read without checking that argc is greater than 1; it is NULL when the program is run with fewer arguments. | | bad_main_unrelated_check.c:8:18:8:24 | access to array | argv[1] is read without checking that argc is greater than 1; it is NULL when the program is run with fewer arguments. | diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.actual b/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.actual deleted file mode 100644 index 8938b345..00000000 --- a/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.actual +++ /dev/null @@ -1,65 +0,0 @@ -#select -| test.c:25:33:25:38 | height | test.c:24:16:24:19 | fread output argument | test.c:25:33:25:38 | height | This divisor depends on $@ and is not checked against zero before the $@. | test.c:24:16:24:19 | fread output argument | string read by fread | test.c:25:17:25:38 | ... / ... | division | -| test.c:37:24:37:29 | height | test.c:36:9:36:9 | fread output argument | test.c:37:24:37:29 | height | This divisor depends on $@ and is not checked against zero before the $@. | test.c:36:9:36:9 | fread output argument | string read by fread | test.c:37:10:37:29 | ... / ... | division | -| test.c:44:27:44:32 | height | test.c:43:9:43:12 | fread output argument | test.c:44:27:44:32 | height | This divisor depends on $@ and is not checked against zero before the $@. | test.c:43:9:43:12 | fread output argument | string read by fread | test.c:44:11:44:32 | ... % ... | division | -| test.c:46:12:46:17 | height | test.c:43:9:43:12 | fread output argument | test.c:46:12:46:17 | height | This divisor depends on $@ and is not checked against zero before the $@. | test.c:43:9:43:12 | fread output argument | string read by fread | test.c:46:3:46:17 | ... /= ... | division | -| test.c:58:24:58:27 | data | test.c:55:18:55:22 | recv output argument | test.c:58:24:58:27 | data | This divisor depends on $@ and is not checked against zero before the $@. | test.c:55:18:55:22 | recv output argument | buffer read by recv | test.c:58:18:58:27 | ... / ... | division | -| test.c:63:24:63:24 | n | test.c:62:16:62:30 | *call to getenv | test.c:63:24:63:24 | n | This divisor depends on $@ and is not checked against zero before the $@. | test.c:62:16:62:30 | *call to getenv | an environment variable | test.c:63:18:63:24 | ... % ... | division | -| test.c:148:26:148:31 | height | test.c:146:9:146:12 | fread output argument | test.c:148:26:148:31 | height | This divisor depends on $@ and is not checked against zero before the $@. | test.c:146:9:146:12 | fread output argument | string read by fread | test.c:148:10:148:31 | ... / ... | division | -| test.c:156:20:156:25 | height | test.c:155:9:155:12 | fread output argument | test.c:156:20:156:25 | height | This divisor depends on $@ and is not checked against zero before the $@. | test.c:155:9:155:12 | fread output argument | string read by fread | test.c:156:10:156:25 | ... / ... | division | -edges -| test.c:24:16:24:19 | fread output argument | test.c:25:17:25:19 | *img | provenance | | -| test.c:25:17:25:19 | *img | test.c:25:29:25:31 | *img | provenance | | -| test.c:25:29:25:31 | *img | test.c:25:17:25:19 | *img | provenance | | -| test.c:25:29:25:31 | *img | test.c:25:33:25:38 | height | provenance | Config | -| test.c:36:9:36:9 | fread output argument | test.c:37:10:37:10 | *p | provenance | | -| test.c:37:10:37:10 | *p | test.c:37:21:37:21 | *p | provenance | | -| test.c:37:21:37:21 | *p | test.c:37:24:37:29 | height | provenance | Config | -| test.c:43:9:43:12 | fread output argument | test.c:44:11:44:13 | *img | provenance | | -| test.c:44:11:44:13 | *img | test.c:44:23:44:25 | *img | provenance | | -| test.c:44:23:44:25 | *img | test.c:44:27:44:32 | height | provenance | Config | -| test.c:44:23:44:25 | *img | test.c:46:8:46:10 | *img | provenance | | -| test.c:46:8:46:10 | *img | test.c:46:12:46:17 | height | provenance | Config | -| test.c:55:18:55:22 | recv output argument | test.c:56:5:56:22 | ... = ... | provenance | TaintFunction | -| test.c:56:5:56:22 | ... = ... | test.c:58:24:58:27 | data | provenance | | -| test.c:62:11:62:14 | call to atoi | test.c:63:24:63:24 | n | provenance | | -| test.c:62:16:62:30 | *call to getenv | test.c:62:11:62:14 | call to atoi | provenance | TaintFunction | -| test.c:146:9:146:12 | fread output argument | test.c:147:3:147:5 | *img | provenance | | -| test.c:147:3:147:5 | *img | test.c:148:10:148:12 | *img | provenance | | -| test.c:148:10:148:12 | *img | test.c:148:22:148:24 | *img | provenance | | -| test.c:148:22:148:24 | *img | test.c:148:26:148:31 | height | provenance | Config | -| test.c:155:9:155:12 | fread output argument | test.c:156:16:156:18 | *img | provenance | | -| test.c:156:16:156:18 | *img | test.c:156:20:156:25 | height | provenance | Config | -nodes -| test.c:24:16:24:19 | fread output argument | semmle.label | fread output argument | -| test.c:25:17:25:19 | *img | semmle.label | *img | -| test.c:25:29:25:31 | *img | semmle.label | *img | -| test.c:25:33:25:38 | height | semmle.label | height | -| test.c:36:9:36:9 | fread output argument | semmle.label | fread output argument | -| test.c:37:10:37:10 | *p | semmle.label | *p | -| test.c:37:21:37:21 | *p | semmle.label | *p | -| test.c:37:24:37:29 | height | semmle.label | height | -| test.c:43:9:43:12 | fread output argument | semmle.label | fread output argument | -| test.c:44:11:44:13 | *img | semmle.label | *img | -| test.c:44:23:44:25 | *img | semmle.label | *img | -| test.c:44:27:44:32 | height | semmle.label | height | -| test.c:46:8:46:10 | *img | semmle.label | *img | -| test.c:46:12:46:17 | height | semmle.label | height | -| test.c:55:18:55:22 | recv output argument | semmle.label | recv output argument | -| test.c:56:5:56:22 | ... = ... | semmle.label | ... = ... | -| test.c:58:24:58:27 | data | semmle.label | data | -| test.c:62:11:62:14 | call to atoi | semmle.label | call to atoi | -| test.c:62:16:62:30 | *call to getenv | semmle.label | *call to getenv | -| test.c:63:24:63:24 | n | semmle.label | n | -| test.c:146:9:146:12 | fread output argument | semmle.label | fread output argument | -| test.c:147:3:147:5 | *img | semmle.label | *img | -| test.c:148:10:148:12 | *img | semmle.label | *img | -| test.c:148:22:148:24 | *img | semmle.label | *img | -| test.c:148:26:148:31 | height | semmle.label | height | -| test.c:155:9:155:12 | fread output argument | semmle.label | fread output argument | -| test.c:156:16:156:18 | *img | semmle.label | *img | -| test.c:156:20:156:25 | height | semmle.label | height | -subpaths -testFailures -| test.c:146:9:146:12 | fread output argument | Unexpected result: Source | -| test.c:148:26:148:31 | height | Unexpected result: Alert | diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.expected b/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.expected index b2973b10..79997277 100644 --- a/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.expected +++ b/languages/cpp/custom/test/InputValidationNullNumeric/TaintedDivisorWithoutZeroCheck/TaintedDivisorWithoutZeroCheck.expected @@ -5,6 +5,7 @@ | test.c:46:12:46:17 | height | test.c:43:9:43:12 | fread output argument | test.c:46:12:46:17 | height | This divisor depends on $@ and is not checked against zero before the $@. | test.c:43:9:43:12 | fread output argument | string read by fread | test.c:46:3:46:17 | ... /= ... | division | | test.c:58:24:58:27 | data | test.c:55:18:55:22 | recv output argument | test.c:58:24:58:27 | data | This divisor depends on $@ and is not checked against zero before the $@. | test.c:55:18:55:22 | recv output argument | buffer read by recv | test.c:58:18:58:27 | ... / ... | division | | test.c:63:24:63:24 | n | test.c:62:16:62:30 | *call to getenv | test.c:63:24:63:24 | n | This divisor depends on $@ and is not checked against zero before the $@. | test.c:62:16:62:30 | *call to getenv | an environment variable | test.c:63:18:63:24 | ... % ... | division | +| test.c:156:20:156:25 | height | test.c:155:9:155:12 | fread output argument | test.c:156:20:156:25 | height | This divisor depends on $@ and is not checked against zero before the $@. | test.c:155:9:155:12 | fread output argument | string read by fread | test.c:156:10:156:25 | ... / ... | division | edges | test.c:24:16:24:19 | fread output argument | test.c:25:17:25:19 | *img | provenance | | | test.c:25:17:25:19 | *img | test.c:25:29:25:31 | *img | provenance | | @@ -22,6 +23,8 @@ edges | test.c:56:5:56:22 | ... = ... | test.c:58:24:58:27 | data | provenance | | | test.c:62:11:62:14 | call to atoi | test.c:63:24:63:24 | n | provenance | | | test.c:62:16:62:30 | *call to getenv | test.c:62:11:62:14 | call to atoi | provenance | TaintFunction | +| test.c:155:9:155:12 | fread output argument | test.c:156:16:156:18 | *img | provenance | | +| test.c:156:16:156:18 | *img | test.c:156:20:156:25 | height | provenance | Config | nodes | test.c:24:16:24:19 | fread output argument | semmle.label | fread output argument | | test.c:25:17:25:19 | *img | semmle.label | *img | @@ -43,4 +46,7 @@ nodes | test.c:62:11:62:14 | call to atoi | semmle.label | call to atoi | | test.c:62:16:62:30 | *call to getenv | semmle.label | *call to getenv | | test.c:63:24:63:24 | n | semmle.label | n | +| test.c:155:9:155:12 | fread output argument | semmle.label | fread output argument | +| test.c:156:16:156:18 | *img | semmle.label | *img | +| test.c:156:20:156:25 | height | semmle.label | height | subpaths diff --git a/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/UncheckedPrivilegeChangeReturn.actual b/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/UncheckedPrivilegeChangeReturn.actual deleted file mode 100644 index c9214057..00000000 --- a/languages/cpp/custom/test/InputValidationNullNumeric/UncheckedPrivilegeChangeReturn/UncheckedPrivilegeChangeReturn.actual +++ /dev/null @@ -1,10 +0,0 @@ -| test.c:25:3:25:17 | call to ImpersonateSelf | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:8:6:8:20 | ImpersonateSelf | ImpersonateSelf | -| test.c:27:3:27:14 | call to RevertToSelf | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:11:6:11:17 | RevertToSelf | RevertToSelf | -| test.c:31:3:31:25 | call to ImpersonateLoggedOnUser | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:9:6:9:28 | ImpersonateLoggedOnUser | ImpersonateLoggedOnUser | -| test.c:32:3:32:28 | call to ImpersonateNamedPipeClient | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:10:6:10:31 | ImpersonateNamedPipeClient | ImpersonateNamedPipeClient | -| test.c:33:3:33:16 | call to SetThreadToken | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:12:6:12:19 | SetThreadToken | SetThreadToken | -| test.c:37:3:37:8 | call to setgid | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:14:5:14:10 | setgid | setgid | -| test.c:38:3:38:8 | call to setuid | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:13:5:13:10 | setuid | setuid | -| test.c:39:3:39:9 | call to seteuid | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:15:5:15:11 | seteuid | seteuid | -| test.c:40:3:40:11 | call to setresuid | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:16:5:16:13 | setresuid | setresuid | -| test.c:71:9:71:14 | call to setuid | The return value of $@ is not checked, so a failure leaves the program in the wrong security context. | test.c:13:5:13:10 | setuid | setuid |