diff --git a/csharp/ql/lib/change-notes/2026-11-27-midblock-guards.md b/csharp/ql/lib/change-notes/2026-11-27-midblock-guards.md new file mode 100644 index 000000000000..283f951f0f29 --- /dev/null +++ b/csharp/ql/lib/change-notes/2026-11-27-midblock-guards.md @@ -0,0 +1,8 @@ +--- +category: minorAnalysis +--- +* The Guards library has been improved to make better inferences related to + conjunctions and disjunctions. This increases precision for a number of queries. + In particular, `cs/constant-comparison` yields more true positives, and + `cs/dereferenced-value-may-be-null` yield fewer false positives. + `cs/user-controlled-bypass` may also yield more results. diff --git a/csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll b/csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll index 031a48906869..2c214b11e904 100644 --- a/csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll +++ b/csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll @@ -22,6 +22,10 @@ private module GuardsInput implements SharedGuards::InputSig ... | true | Logic.java:37:17:39:5 | { ... } | +| Logic.java:44:9:44:10 | !... | false | Logic.java:43:23:43:31 | Normal Exit | +| Logic.java:44:9:44:10 | !... | true | Logic.java:44:33:44:35 | msg | | Logic.java:44:10:44:10 | b | false | Logic.java:44:33:44:35 | msg | | Logic.java:44:10:44:10 | b | true | Logic.java:43:23:43:31 | Normal Exit | | Logic.java:48:5:48:22 | checkTrue(...) | exception | Logic.java:47:23:47:32 | Exceptional Exit | @@ -68,6 +70,8 @@ | Logic.java:53:21:53:26 | g(...) | false | Logic.java:54:17:56:5 | { ... } | | Logic.java:53:21:53:26 | g(...) | false | Logic.java:57:5:57:18 | var ...; | | Logic.java:54:9:54:14 | ... > ... | true | Logic.java:54:17:56:5 | { ... } | +| Logic.java:61:9:61:10 | !... | false | Logic.java:60:23:60:31 | Normal Exit | +| Logic.java:61:9:61:10 | !... | true | Logic.java:61:33:61:35 | msg | | Logic.java:61:10:61:10 | b | false | Logic.java:61:33:61:35 | msg | | Logic.java:61:10:61:10 | b | true | Logic.java:60:23:60:31 | Normal Exit | | Logic.java:65:5:65:22 | checkTrue(...) | exception | Logic.java:64:23:64:32 | Exceptional Exit | diff --git a/shared/controlflow/codeql/controlflow/Guards.qll b/shared/controlflow/codeql/controlflow/Guards.qll index 7550363f0ea2..45f1f1175beb 100644 --- a/shared/controlflow/codeql/controlflow/Guards.qll +++ b/shared/controlflow/codeql/controlflow/Guards.qll @@ -78,6 +78,11 @@ signature module InputSig; @@ -1057,6 +1087,13 @@ module Make< ) } + private predicate guardControlsBlock(Guard guard, BasicBlock bb, GuardValue v) { + exists(Guard g0, GuardValue v0 | + midBlockGuard(g0, bb, v0) and + BranchImplies::guardControls(guard, v, g0, v0) + ) + } + /** * Holds if `def` evaluating to `v` controls the control-flow branch * edge from `bb1` to `bb2`. That is, following the edge from `bb1` to @@ -1080,6 +1117,12 @@ module Make< dominatingEdge(guard, succ) and succ.dominates(bb) ) + or + exists(Guard g0, BasicBlock guardbb, GuardValue v0 | + midBlockGuard(g0, guardbb, v0) and + BranchImplies::ssaControls(def, v, g0, v0) and + guardbb.dominates(bb) + ) } private import DisjunctiveGuard @@ -1159,6 +1202,37 @@ module Make< tgtGuard.getBasicBlock() = bb } + pragma[nomagic] + private predicate relevantValue1(SsaDefinition def, GuardValue v1) { + DisjunctImplies::ssaControls(def, v1, _, _) + } + + pragma[nomagic] + private predicate relevantValue2(SsaDefinition def, GuardValue v2) { + ssaControlsGuard(def, v2, _, _, _) + } + + pragma[nomagic] + private predicate relevantDisjointValues(SsaDefinition def, GuardValue v1, GuardValue v2) { + relevantValue1(def, v1) and + relevantValue2(def, v2) and + disjointValues(v1, v2) + } + + /** + * Holds if `tgtGuard` evaluating to `tgtVal` implies that `def` + * evaluates to a value disjoint from `v`. The basic block of `tgtGuard` is `bb`. + */ + pragma[nomagic] + private predicate ssaControlsGuard2( + SsaDefinition def, GuardValue v, Guard tgtGuard, GuardValue tgtVal, BasicBlock bb + ) { + exists(GuardValue v2 | + ssaControlsGuard(def, v2, tgtGuard, tgtVal, bb) and + relevantDisjointValues(def, v, v2) + ) + } + /** * Holds if `tgtGuard` evaluating to `tgtVal` implies that `guard` * evaluates to `v`. @@ -1167,13 +1241,11 @@ module Make< predicate disjunctiveGuardControls( Guard guard, GuardValue v, Guard tgtGuard, GuardValue tgtVal ) { - exists(SsaDefinition def, GuardValue v1, GuardValue v2, BasicBlock bb | + exists(SsaDefinition def, GuardValue v1, BasicBlock bb | // If `def==v1 || guard==v` controls `bb`, ssaDisjunctionControls(def, v1, guard, v, bb) and - // and `tgtGuard==tgtVal` in `bb` implies `def==v2`, - ssaControlsGuard(def, v2, tgtGuard, tgtVal, bb) and - // and `v1` and `v2` are disjoint, - disjointValues(v1, v2) + // and `tgtGuard==tgtVal` in `bb` implies `def` having some value `v2` disjoint from `v1`, + ssaControlsGuard2(def, v1, tgtGuard, tgtVal, bb) // then assuming `tgtGuard==tgtVal` it follows that `def` cannot be `v1` // and therefore we must have `guard==v`. ) @@ -1532,6 +1604,11 @@ module Make< dominatingEdge(guard, succ) and succ.dominates(bb) ) + or + exists(BasicBlock guard | + guardControlsBlock(this, guard, v) and + guard.dominates(bb) + ) } /**