Skip to content

[NEW QUERY] C/C++: untrusted divisor, argv without argc check, unchecked privilege change (InputValidationNullNumeric) - #305

Draft
felickz with Copilot wants to merge 5 commits into
mainfrom
copilot/cpp-query-create-input-validation-null-numeric
Draft

felickz with Copilot wants to merge 5 commits into
mainfrom
copilot/cpp-query-create-input-validation-null-numeric

Conversation

Copilot AI commented Oct 31, 2026 •

Copy link
Copy Markdown

AI Scan reported 19 C/C++ findings (CWE-252/369/457/476/571) that default CodeQL misses. This PR adds three queries in languages/cpp/custom/src/InputValidationNullNumeric/ that cover 14 of them. For the other five, I ran the existing CodeQL queries on small reproductions (see References).

📝 Query Information

  • Language: cpp
  • Query ID:
    • cpp/tainted-divisor-without-zero-check (path-problem, precision medium)
    • cpp/argv-access-without-argc-check (problem, precision medium)
    • cpp/unchecked-privilege-change-return (problem, precision high)
  • Category: security
  • Severity: warning (security-severity 5.3 / 3.3 / 6.4)
  • CWE/CVE (if applicable): CWE-369; CWE-476, CWE-20; CWE-252, CWE-273

🎯 Description

What This Query Detects

  • TaintedDivisorWithoutZeroCheck: integer /, %, /= and %= where the divisor comes from a FlowSource (e.g. fread, recv, getenv, argv; read is a modeled source but untested).
    • Struct fields read after the whole struct was filled (fread(&img, ...) then img.height) count as tainted. This needs an extra flow step from the struct to the field. The step is skipped when an assignment to the same field dominates the read.
    • Any comparison of the divisor, or a truthiness test (if (n), !n), counts as validation. This favors precision over recall.
    • Divisors that SimpleRangeAnalysis proves non-zero are excluded.
  • ArgvAccessWithoutArgcCheck: argv[n] or *(argv + n) in main/wmain, with a constant n >= 1, where no argc check comes first. &argv[n] and unevaluated contexts are ignored. The access counts as checked if any of these hold:
    • it sits on one branch of an if, loop, ?: or &&/|| whose condition reads argc;
    • an earlier if on argc dominates it and leaves the path (return/goto/break/continue/throw, or a call that may reach exit/abort/err*);
    • argc is passed to a call that dominates it (e.g. getopt, a validation helper).
  • UncheckedPrivilegeChangeReturn: Windows impersonation and token APIs (ImpersonateSelf, RevertToSelf, SetThreadToken, ...) and POSIX credential APIs (setuid, setgid, setresuid, setgroups, ...) called in void context, including (void)setuid(x).

ArgvAccessWithoutArgcCheck works from the AST and dominance only, not IR guards. When several translation units define main (as in Damn_Vulnerable_C_Program), CodeQL merges them into one Function, and GuardCondition.controls then returns nothing.

Example Vulnerable Code

while (fread(&img, sizeof(img), 1, fp) > 0) {
  int ratio = img.width / img.height;   // cpp/tainted-divisor-without-zero-check
}

int main(int argc, char **argv) {
  ProcessImage(argv[1]);                // cpp/argv-access-without-argc-check
}

ImpersonateSelf(SecurityImpersonation); // cpp/unchecked-privilege-change-return

Example Safe Code

if (img.height == 0) continue;
int ratio = img.width / img.height;

if (argc < 2) usage(argv[0]);           // usage() calls exit()
ProcessImage(argv[1]);

if (!ImpersonateSelf(SecurityImpersonation)) exit(1);

🧪 Testing

  • Positive test cases included
  • Negative test cases included
  • Edge cases covered: field overwritten before division; float division; range-proven non-zero divisor; argc checked after the access; a guard on argc that covers a different index; ternary guard; for (i = 1; i < argc; i++); non-main functions; explicit (void) discard
  • All tests pass

Tests are in languages/cpp/custom/test/InputValidationNullNumeric/<Query>/ and use inline expectations (// $ Alert / // $ Source).

📋 Checklist

  • Query compiles without errors
  • Documentation complete (.md and .qhelp): .md help only, no .qhelp
  • Metadata properly set (@name, @id, @kind, etc.)
  • Tests validate query behavior
  • No false positives in test cases

🔗 References

The other five findings, checked against the existing codeql/cpp-queries 1.9.2 queries:

Pattern Existing query result
Juliet char_01 (NULL assigned, then dereferenced) Reported by cpp/missing-null-test
Juliet null_check_after_deref Reported by cpp/redundant-null-check-simple and cpp/missing-null-test
CWE-571 size4 = size3 + 100; if (size4 > size3) Not reported by cpp/constant-comparison
CWE-457 read of an uninitialized local array Not reported by cpp/uninitialized-local
CWE-252 read() result not checked for -1, then used as size_t Not covered

No new queries were added for the last three. Each is a single finding, and a rule for it would be heuristic.


Note: This query was developed using Test-Driven Development methodology.

Copilot AI and others added 2 commits October 31, 2026 03:59
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
…ld step; add query help

Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Copilot AI changed the title [WIP] Add new query for missing input validation and checks [NEW QUERY] C/C++: untrusted divisor, argv without argc check, unchecked privilege change (InputValidationNullNumeric) Oct 31, 2026
Copilot AI requested a review from felickz October 31, 2026 04:11
@felickz
felickz requested a balanced review from Copilot November 2, 2026 18:35

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical query-modeling flaws can hide unsafe divisions and argv accesses, while incorrect filesystem-ID return handling can flag properly verified privilege changes.

4 open issues
What changed in this PR

Adds three security queries to the custom C/C++ CodeQL pack, addressing untrusted divisors, unchecked command-line argument access, and discarded privilege-change results from issue #254.

Changes:

  • Adds divisor taint tracking, argument-count check detection, and privilege-change API checks.
  • Documents each query’s behavior, recommendations, and examples.
  • Adds positive and negative fixtures with inline expectations and expected results.
File Description
languages/​cpp/​custom/​test/​InputValidationNullNumeric/​UncheckedPrivilegeChangeReturn/​UncheckedPrivilegeChangeReturn.qlref Connects privilege-change tests to the query.
languages/​cpp/​custom/​test/​InputValidationNullNumeric/​UncheckedPrivilegeChangeReturn/​UncheckedPrivilegeChangeReturn.expected Records expected privilege-change alerts.
languages/​cpp/​custom/​test/​InputValidationNullNumeric/​UncheckedPrivilegeChangeReturn/​test.c Covers discarded and checked privilege-change results.
languages/​cpp/​custom/​test/​InputValidationNullNumeric/​TaintedDivisorWithoutZeroCheck/​test.c Covers input sources, divisor checks, and field overwrites.
languages/​cpp/​custom/​test/​InputValidationNullNumeric/​TaintedDivisorWithoutZeroCheck/​TaintedDivisorWithoutZeroCheck.qlref Connects divisor tests to the query.
languages/​cpp/​custom/​test/​InputValidationNullNumeric/​TaintedDivisorWithoutZeroCheck/​TaintedDivisorWithoutZeroCheck.expected Records divisor alerts and flow paths.
languages/​cpp/​custom/​test/​InputValidationNullNumeric/​ArgvAccessWithoutArgcCheck/​other.c Checks exclusion of non-entry-point functions.
languages/​cpp/​custom/​test/​InputValidationNullNumeric/​ArgvAccessWithoutArgcCheck/​good_main_ne.c Covers an exact argument-count check.
languages/​cpp/​custom/​test/​InputValidationNullNumeric/​ArgvAccessWithoutArgcCheck/​good_main_guarded.cpp Covers guarded argument access patterns.
languages/​cpp/​custom/​test/​InputValidationNullNumeric/​ArgvAccessWithoutArgcCheck/​good_main_early_return.c Covers a terminating usage helper.
languages/​cpp/​custom/​test/​InputValidationNullNumeric/​ArgvAccessWithoutArgcCheck/​bad_main.c Covers unchecked indexed and pointer-based access.
languages/​cpp/​custom/​test/​InputValidationNullNumeric/​ArgvAccessWithoutArgcCheck/​bad_main_unrelated_check.c Covers access outside an argument-count guard.
languages/​cpp/​custom/​test/​InputValidationNullNumeric/​ArgvAccessWithoutArgcCheck/​bad_main_late_check.c Covers argument-count checks after access.
languages/​cpp/​custom/​test/​InputValidationNullNumeric/​ArgvAccessWithoutArgcCheck/​ArgvAccessWithoutArgcCheck.qlref Connects argument-access tests to the query.
languages/​cpp/​custom/​test/​InputValidationNullNumeric/​ArgvAccessWithoutArgcCheck/​ArgvAccessWithoutArgcCheck.expected Records expected argument-access alerts.
languages/​cpp/​custom/​src/​InputValidationNullNumeric/​UncheckedPrivilegeChangeReturn.ql Detects discarded privilege-change results.
languages/​cpp/​custom/​src/​InputValidationNullNumeric/​UncheckedPrivilegeChangeReturn.md Documents privilege-change checks and remediation.
languages/​cpp/​custom/​src/​InputValidationNullNumeric/​TaintedDivisorWithoutZeroCheck.ql Tracks untrusted input into integer divisors.
languages/​cpp/​custom/​src/​InputValidationNullNumeric/​TaintedDivisorWithoutZeroCheck.md Documents divisor detection and zero checks.
languages/​cpp/​custom/​src/​InputValidationNullNumeric/​ArgvAccessWithoutArgcCheck.ql Detects constant argument accesses without preceding checks.
languages/​cpp/​custom/​src/​InputValidationNullNumeric/​ArgvAccessWithoutArgcCheck.md Documents argument-access detection and validation.

Review effort: Balanced

Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Query Create]: CWE-252, CWE-369, CWE-457, CWE-476, CWE-571 MEDIUM - C/C++: new query for missing input validation, NULL and numeric checks

3 participants