Repository navigation
Conversation
1 task done
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
There was a problem hiding this comment.
🟡 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>
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


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
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)🎯 Description
What This Query Detects
TaintedDivisorWithoutZeroCheck: integer/,%,/=and%=where the divisor comes from aFlowSource(e.g.fread,recv,getenv,argv;readis a modeled source but untested).fread(&img, ...)thenimg.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.if (n),!n), counts as validation. This favors precision over recall.SimpleRangeAnalysisproves non-zero are excluded.ArgvAccessWithoutArgcCheck:argv[n]or*(argv + n)inmain/wmain, with a constantn >= 1, where noargccheck comes first.&argv[n]and unevaluated contexts are ignored. The access counts as checked if any of these hold:if, loop,?:or&&/||whose condition readsargc;ifonargcdominates it and leaves the path (return/goto/break/continue/throw, or a call that may reachexit/abort/err*);argcis 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).ArgvAccessWithoutArgcCheckworks from the AST and dominance only, not IR guards. When several translation units definemain(as in Damn_Vulnerable_C_Program), CodeQL merges them into oneFunction, andGuardCondition.controlsthen returns nothing.Example Vulnerable Code
Example Safe Code
🧪 Testing
argcchecked after the access; a guard onargcthat covers a different index; ternary guard;for (i = 1; i < argc; i++); non-mainfunctions; explicit(void)discardTests are in
languages/cpp/custom/test/InputValidationNullNumeric/<Query>/and use inline expectations (// $ Alert/// $ Source).📋 Checklist
.mdand.qhelp):.mdhelp only, no.qhelp🔗 References
The other five findings, checked against the existing
codeql/cpp-queries1.9.2 queries:char_01(NULL assigned, then dereferenced)cpp/missing-null-testnull_check_after_derefcpp/redundant-null-check-simpleandcpp/missing-null-testsize4 = size3 + 100; if (size4 > size3)cpp/constant-comparisoncpp/uninitialized-localread()result not checked for -1, then used assize_tNo 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.