From f307ca8bed47dcadcc1b94cb42adc89495e49ff7 Mon Sep 17 00:00:00 2001 From: chanelyoung Date: Tue, 1 Dec 2026 13:56:14 -0800 Subject: [PATCH 1/7] Java: add canonical direct-parent containment regressions Add safe and adversarial helper fixtures, recording the current two false-positive diagnostics before updating containment recognition. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 07dbce6b-e7a3-48b2-a656-bddbcb83db5b --- .../canonical-containment/DirectParent.java | 140 ++++++++++++++++++ .../canonical-containment/test.expected | 2 + .../canonical-containment/test.ql | 13 ++ 3 files changed, 155 insertions(+) create mode 100644 java/ql/test/library-tests/canonical-containment/DirectParent.java create mode 100644 java/ql/test/library-tests/canonical-containment/test.expected create mode 100644 java/ql/test/library-tests/canonical-containment/test.ql diff --git a/java/ql/test/library-tests/canonical-containment/DirectParent.java b/java/ql/test/library-tests/canonical-containment/DirectParent.java new file mode 100644 index 000000000000..293be55ebc25 --- /dev/null +++ b/java/ql/test/library-tests/canonical-containment/DirectParent.java @@ -0,0 +1,140 @@ +import java.io.File; +import java.io.IOException; + +class DirectParent { + static String source() { return null; } + static void sink(Object value) {} + + static File child(String base, String name) throws IOException { + File directory = new File(base).getCanonicalFile(); + File candidate = new File(directory, name).getCanonicalFile(); + if (!directory.equals(candidate.getParentFile())) + return null; + return candidate; + } + + static File reversed(String base, String name) throws IOException { + final File directory; + final File candidate; + try { + directory = new File(base).getCanonicalFile(); + candidate = new File(directory, name).getCanonicalFile(); + } catch (IOException ex) { + return null; + } + if (candidate.getParentFile().equals(directory)) { + return candidate; + } + return null; + } + + static void safe() throws IOException { + File file = child("/srv/data", source()); + if (file != null) + sink(file); // Safe: only the canonical direct child is returned. + File other = reversed("/srv/data", source()); + if (other != null) + sink(other); // Safe: reversed equality and failure returning null. + } + + static void untrustedDirectory() throws IOException { + sink(child(source(), "fixed")); // $ hasTaintFlow + sink(child(source(), source())); // $ hasTaintFlow + sink(reversed(source(), "fixed")); // $ hasTaintFlow + } + + static File normalizedOnly(String base, String name) throws IOException { + File directory = new File(base).getCanonicalFile(); + File candidate = new File(directory, name).getCanonicalFile(); + return candidate; + } + + static File failure(String base, String name) throws IOException { + File directory = new File(base).getCanonicalFile(); + File candidate = new File(directory, name).getCanonicalFile(); + if (!directory.equals(candidate.getParentFile())) + return candidate; + return null; + } + + static File ignored(String base, String name) throws IOException { + File directory = new File(base).getCanonicalFile(); + File candidate = new File(directory, name).getCanonicalFile(); + directory.equals(candidate.getParentFile()); + return candidate; + } + + static File fallthrough(String base, String name) throws IOException { + File directory = new File(base).getCanonicalFile(); + File candidate = new File(directory, name).getCanonicalFile(); + if (directory.equals(candidate.getParentFile())) + return candidate; + return candidate; + } + + static File lexical(String base, String name) { + File directory = new File(base); + // A lexical direct child can be a symlink to a file outside directory. + File candidate = new File(directory, name); + if (!directory.equals(candidate.getParentFile())) + return null; + return candidate; + } + + static File wrongCandidate(String base, String name) throws IOException { + File directory = new File(base).getCanonicalFile(); + File checked = new File(directory, "fixed").getCanonicalFile(); + File candidate = new File(directory, name).getCanonicalFile(); + if (!directory.equals(checked.getParentFile())) + return null; + return candidate; + } + + static File reassigned(String base, String name) throws IOException { + File directory = new File(base).getCanonicalFile(); + File candidate = new File(directory, "fixed").getCanonicalFile(); + if (!directory.equals(candidate.getParentFile())) + return null; + candidate = new File(name).getCanonicalFile(); + return candidate; + } + + static File merged(String base, String name, boolean choose) throws IOException { + File directory = new File(base).getCanonicalFile(); + File candidate = new File(directory, name).getCanonicalFile(); + if (!directory.equals(candidate.getParentFile())) + return null; + if (choose) + candidate = new File(name).getCanonicalFile(); + return candidate; + } + + static File derivedDirectory(String name) throws IOException { + File directory = new File(name).getCanonicalFile(); + File candidate = new File(directory, "fixed").getCanonicalFile(); + if (!directory.equals(candidate.getParentFile())) + return null; + return candidate; + } + + static File directoryFromCandidate(String name) throws IOException { + File candidate = new File(name).getCanonicalFile(); + File directory = candidate.getParentFile().getCanonicalFile(); + if (!directory.equals(candidate.getParentFile())) + return null; + return candidate; + } + + static void unsafe() throws IOException { + sink(normalizedOnly("/srv/data", source())); // $ hasTaintFlow + sink(failure("/srv/data", source())); // $ hasTaintFlow + sink(ignored("/srv/data", source())); // $ hasTaintFlow + sink(fallthrough("/srv/data", source())); // $ hasTaintFlow + sink(lexical("/srv/data", source())); // $ hasTaintFlow + sink(wrongCandidate("/srv/data", source())); // $ hasTaintFlow + sink(reassigned("/srv/data", source())); // $ hasTaintFlow + sink(merged("/srv/data", source(), true)); // $ hasTaintFlow + sink(derivedDirectory(source())); // $ hasTaintFlow + sink(directoryFromCandidate(source())); // $ hasTaintFlow + } +} diff --git a/java/ql/test/library-tests/canonical-containment/test.expected b/java/ql/test/library-tests/canonical-containment/test.expected new file mode 100644 index 000000000000..dd7c17b37236 --- /dev/null +++ b/java/ql/test/library-tests/canonical-containment/test.expected @@ -0,0 +1,2 @@ +| DirectParent.java:34:18:34:21 | file | Unexpected result: hasTaintFlow | +| DirectParent.java:37:18:37:22 | other | Unexpected result: hasTaintFlow | diff --git a/java/ql/test/library-tests/canonical-containment/test.ql b/java/ql/test/library-tests/canonical-containment/test.ql new file mode 100644 index 000000000000..03ecd32be802 --- /dev/null +++ b/java/ql/test/library-tests/canonical-containment/test.ql @@ -0,0 +1,13 @@ +import java +import semmle.code.java.security.PathSanitizer +import utils.test.InlineFlowTest + +module ContainmentConfig implements DataFlow::ConfigSig { + predicate isSource(DataFlow::Node source) { DefaultFlowConfig::isSource(source) } + + predicate isSink(DataFlow::Node sink) { DefaultFlowConfig::isSink(sink) } + + predicate isBarrier(DataFlow::Node node) { node instanceof PathInjectionSanitizer } +} + +import TaintFlowTest From 9892f3171c6f4b9400fdaa8c228d63828d908152 Mon Sep 17 00:00:00 2001 From: chanelyoung Date: Tue, 1 Dec 2026 13:56:16 -0800 Subject: [PATCH 2/7] Java: recognize canonical direct-parent containment helpers Recognize successful canonical parent equality before returning the same candidate. Preserve the allowed-directory parameter's taint at helper calls. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 07dbce6b-e7a3-48b2-a656-bddbcb83db5b --- .../2026-12-01-canonical-direct-parent.md | 4 + .../code/java/security/PathSanitizer.qll | 94 +++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 java/ql/lib/change-notes/2026-12-01-canonical-direct-parent.md diff --git a/java/ql/lib/change-notes/2026-12-01-canonical-direct-parent.md b/java/ql/lib/change-notes/2026-12-01-canonical-direct-parent.md new file mode 100644 index 000000000000..be0c18c02746 --- /dev/null +++ b/java/ql/lib/change-notes/2026-12-01-canonical-direct-parent.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* Path injection analysis now recognizes static helpers that return a canonical file only after checking that its parent equals the canonical allowed directory. Taint from the allowed directory is preserved. diff --git a/java/ql/lib/semmle/code/java/security/PathSanitizer.qll b/java/ql/lib/semmle/code/java/security/PathSanitizer.qll index b4344c50cea3..d8e053d0ed44 100644 --- a/java/ql/lib/semmle/code/java/security/PathSanitizer.qll +++ b/java/ql/lib/semmle/code/java/security/PathSanitizer.qll @@ -46,6 +46,100 @@ class ExactPathMatchSanitizer extends PathInjectionSanitizer { } } +/** A canonical file constructed without calling an application-defined override. */ +private class CanonicalFileCall extends MethodCall { + CanonicalFileCall() { + this.getMethod().hasQualifiedName("java.io", "File", "getCanonicalFile") and + this.getQualifier().(ClassInstanceExpr).getConstructedType() instanceof TypeFile + } +} + +/** + * Holds if `read` reads the local definition initialized to `value`. + * Unlike may-flow, this does not accept a phi that also contains an unchecked file. + */ +private predicate readsFileValue(Expr read, Expr value) { + exists(SsaExplicitWrite def | + def.getSourceVariable().getVariable() instanceof LocalScopeVariable and + def.getDefiningExpr().(VariableAssign).getSource() = value and + read = def.getARead() + ) +} + +/** + * Holds if `directory` is canonicalized directly from a parameter of `helper`. + * Restricting the origin lets us preserve that parameter's taint at calls to the helper. + */ +private predicate containmentDirectory( + CanonicalFileCall directory, Method helper, Parameter parameter +) { + exists(ClassInstanceExpr file, SsaParameterInit def | + file = directory.getQualifier() and + file.getNumArgument() = 1 and + file.getArgument(0) = def.getARead() and + parameter = def.getParameter() and + parameter.getCallable() = helper + ) +} + +/** + * Holds if `guard` compares the parent of the canonical `candidate` to `directory`. + */ +private predicate canonicalParentGuard( + Guard guard, CanonicalFileCall candidate, CanonicalFileCall directory +) { + exists(MethodCall equals, MethodCall parent, Expr allowed | + guard = equals and + equals.getMethod().hasQualifiedName("java.io", "File", "equals") and + parent.getMethod().hasQualifiedName("java.io", "File", "getParentFile") and + readsFileValue(parent.getQualifier(), candidate) and + readsFileValue(allowed, directory) + | + equals.getQualifier() = parent and equals.getArgument(0) = allowed + or + equals.getQualifier() = allowed and equals.getArgument(0) = parent + ) +} + +/** + * Holds if `ret` returns the unchanged canonical candidate on a successful containment check. + * Only static helpers with a directory parameter are modeled. In particular, we do not + * assume that an arbitrary expression producing the allowed directory is trusted. + */ +private predicate canonicalContainmentReturn(ReturnStmt ret, Parameter directoryParameter) { + exists(Method helper, CanonicalFileCall candidate, CanonicalFileCall directory, Guard guard | + helper = ret.getEnclosingCallable() and + helper.isStatic() and + containmentDirectory(directory, helper, directoryParameter) and + readsFileValue(ret.getExpr(), candidate) and + canonicalParentGuard(guard, candidate, directory) and + guard.controls(ret.getBasicBlock(), true) + ) +} + +/** A canonical file returned only after a successful containment check. */ +private class CanonicalContainmentSanitizer extends PathInjectionSanitizer { + CanonicalContainmentSanitizer() { + this.asExpr() = any(ReturnStmt ret | canonicalContainmentReturn(ret, _)).getExpr() + } +} + +/** + * Preserve the allowed directory's influence on a containment helper's result. + * The successful return removes the candidate's influence, not the directory's: + * choosing an attacker-controlled allowed directory is still path injection. + */ +private class ContainmentDirectoryTaintStep extends AdditionalTaintStep { + override predicate step(DataFlow::Node n1, DataFlow::Node n2) { + exists(ReturnStmt ret, Parameter parameter, MethodCall call | + canonicalContainmentReturn(ret, parameter) and + call.getMethod() = ret.getEnclosingCallable() and + n1.asExpr() = call.getArgument(parameter.getPosition()) and + n2.asExpr() = call + ) + } +} + abstract private class PathGuard extends Guard { abstract Expr getCheckedExpr(); } From 4bac8414812d5b93266d90ea3e63a3e4686559bc Mon Sep 17 00:00:00 2001 From: chanelyoung Date: Tue, 1 Dec 2026 13:56:16 -0800 Subject: [PATCH 3/7] Java: update direct-parent containment expected results Remove the two pre-fix false-positive diagnostics after canonical parent validation is recognized. Unsafe cases retain their inline taint expectations. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 07dbce6b-e7a3-48b2-a656-bddbcb83db5b --- java/ql/test/library-tests/canonical-containment/test.expected | 2 -- 1 file changed, 2 deletions(-) diff --git a/java/ql/test/library-tests/canonical-containment/test.expected b/java/ql/test/library-tests/canonical-containment/test.expected index dd7c17b37236..e69de29bb2d1 100644 --- a/java/ql/test/library-tests/canonical-containment/test.expected +++ b/java/ql/test/library-tests/canonical-containment/test.expected @@ -1,2 +0,0 @@ -| DirectParent.java:34:18:34:21 | file | Unexpected result: hasTaintFlow | -| DirectParent.java:37:18:37:22 | other | Unexpected result: hasTaintFlow | From 3d619c07f9bd5f772310ff15d153d6867a7959ab Mon Sep 17 00:00:00 2001 From: chanelyoung Date: Tue, 1 Dec 2026 13:56:18 -0800 Subject: [PATCH 4/7] Java: add canonical ancestor-loop containment regressions Add safe and adversarial parent-walking helpers, recording four false-positive and four missed-taint diagnostics before adding ancestor-loop recognition. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 07dbce6b-e7a3-48b2-a656-bddbcb83db5b --- .../canonical-containment/AncestorLoop.java | 188 ++++++++++++++++++ .../canonical-containment/test.expected | 8 + 2 files changed, 196 insertions(+) create mode 100644 java/ql/test/library-tests/canonical-containment/AncestorLoop.java diff --git a/java/ql/test/library-tests/canonical-containment/AncestorLoop.java b/java/ql/test/library-tests/canonical-containment/AncestorLoop.java new file mode 100644 index 000000000000..ca2fb135bf07 --- /dev/null +++ b/java/ql/test/library-tests/canonical-containment/AncestorLoop.java @@ -0,0 +1,188 @@ +import java.io.File; +import java.io.IOException; +import java.util.function.BiFunction; + +class AncestorLoop { + static String source() { return null; } + static void sink(Object value) {} + + static File descendant(String path, String base) { + try { + File directory = new File(base).getCanonicalFile(); + File candidate = new File(path).getCanonicalFile(); + for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) { + if (directory.equals(parent)) { + return candidate; + } + } + return null; + } catch (IOException ex) { + return null; + } + } + + static File whileLoop(String path, String base) throws IOException { + File directory = new File(base).getCanonicalFile(); + File candidate = new File(path).getCanonicalFile(); + File parent = candidate.getParentFile(); + while (parent != null) { + if (parent.equals(directory)) + return candidate; + parent = parent.getParentFile(); + } + return null; + } + + static File genericLoop(String path, String base) throws IOException { + File directory = new File(base).getCanonicalFile(); + File candidate = new File(path).getCanonicalFile(); + for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) { + if (directory.equals(parent)) + return candidate; + } + return null; + } + + static void safe() throws IOException { + File file = descendant(source(), "/srv/data"); + if (file != null) + sink(file); // Safe: a strict canonical descendant, possibly nested. + sink(whileLoop(source(), "/srv/data")); // Safe: equivalent while loop. + sink(AncestorLoop.genericLoop(source(), "/srv/data")); // Safe + BiFunction resolve = AncestorLoop::descendant; + sink(resolve.apply(source(), "/srv/data")); // Safe + } + + static void untrustedDirectory() throws IOException { + sink(descendant("/srv/data/fixed", source())); // $ hasTaintFlow + sink(descendant(source(), source())); // $ hasTaintFlow + sink(whileLoop("/srv/data/fixed", source())); // $ hasTaintFlow + sink(AncestorLoop.genericLoop("/srv/data/fixed", source())); // $ hasTaintFlow + BiFunction resolve = AncestorLoop::descendant; + sink(resolve.apply("/srv/data/fixed", source())); // $ hasTaintFlow + } + + static File fallthrough(String path, String base) throws IOException { + File directory = new File(base).getCanonicalFile(); + File candidate = new File(path).getCanonicalFile(); + for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) { + if (directory.equals(parent)) + return candidate; + } + return candidate; + } + + static File failure(String path, String base) throws IOException { + File directory = new File(base).getCanonicalFile(); + File candidate = new File(path).getCanonicalFile(); + for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) { + if (!directory.equals(parent)) + return candidate; + } + return null; + } + + static File injectedParent(String path, String base, boolean inject) throws IOException { + File directory = new File(base).getCanonicalFile(); + File candidate = new File(path).getCanonicalFile(); + for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) { + if (inject) + parent = directory; + if (directory.equals(parent)) + return candidate; + } + return null; + } + + static File resetOnBackedge(String path, String base, boolean inject) throws IOException { + File directory = new File(base).getCanonicalFile(); + File candidate = new File(path).getCanonicalFile(); + File parent = candidate.getParentFile(); + while (parent != null) { + if (directory.equals(parent)) + return candidate; + if (inject) + parent = directory; + else + parent = parent.getParentFile(); + } + return null; + } + + static File wrongCandidate(String path, String base) throws IOException { + File directory = new File(base).getCanonicalFile(); + File candidate = new File(path).getCanonicalFile(); + File other = new File(directory, "fixed").getCanonicalFile(); + for (File parent = other.getParentFile(); parent != null; parent = parent.getParentFile()) { + if (directory.equals(parent)) + return candidate; + } + return null; + } + + static File reassigned(String path, String base) throws IOException { + File directory = new File(base).getCanonicalFile(); + File candidate = new File(directory, "fixed").getCanonicalFile(); + for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) { + if (directory.equals(parent)) { + candidate = new File(path).getCanonicalFile(); + return candidate; + } + } + return null; + } + + static File lexical(String path, String base) { + File directory = new File(base); + File candidate = new File(path); + for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) { + if (directory.equals(parent)) + return candidate; + } + return null; + } + + static File noGuard(String path, String base) throws IOException { + File directory = new File(base).getCanonicalFile(); + File candidate = new File(path).getCanonicalFile(); + for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) { + directory.equals(parent); + return candidate; + } + return null; + } + + static File siblingPrefix(String path, String base) throws IOException { + File directory = new File(base).getCanonicalFile(); + File candidate = new File(path).getCanonicalFile(); + for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) { + if (parent.toString().startsWith(directory.toString())) + return candidate; + } + return null; + } + + static File derivedDirectory(String path) throws IOException { + File candidate = new File(path).getCanonicalFile(); + File directory = candidate.getParentFile().getCanonicalFile(); + for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) { + if (directory.equals(parent)) + return candidate; + } + return null; + } + + static void unsafe() throws IOException { + sink(fallthrough(source(), "/srv/data")); // $ hasTaintFlow + sink(failure(source(), "/srv/data")); // $ hasTaintFlow + sink(injectedParent(source(), "/srv/data", true)); // $ hasTaintFlow + sink(resetOnBackedge(source(), "/srv/data", true)); // $ hasTaintFlow + sink(wrongCandidate(source(), "/srv/data")); // $ hasTaintFlow + sink(reassigned(source(), "/srv/data")); // $ hasTaintFlow + sink(lexical(source(), "/srv/data")); // $ hasTaintFlow + sink(noGuard(source(), "/srv/data")); // $ hasTaintFlow + // Existing prefix sanitizers accept this; parent-walking recognition must not expand it. + sink(siblingPrefix(source(), "/srv/data")); // $ MISSING: hasTaintFlow + sink(derivedDirectory(source())); // $ hasTaintFlow + } +} diff --git a/java/ql/test/library-tests/canonical-containment/test.expected b/java/ql/test/library-tests/canonical-containment/test.expected index e69de29bb2d1..f65c4bbe4f53 100644 --- a/java/ql/test/library-tests/canonical-containment/test.expected +++ b/java/ql/test/library-tests/canonical-containment/test.expected @@ -0,0 +1,8 @@ +| AncestorLoop.java:49:18:49:21 | file | Unexpected result: hasTaintFlow | +| AncestorLoop.java:50:14:50:45 | whileLoop(...) | Unexpected result: hasTaintFlow | +| AncestorLoop.java:51:14:51:68 | genericLoop(...) | Unexpected result: hasTaintFlow | +| AncestorLoop.java:53:14:53:49 | apply(...) | Unexpected result: hasTaintFlow | +| AncestorLoop.java:57:56:57:72 | // $ hasTaintFlow | Missing result: hasTaintFlow | +| AncestorLoop.java:59:55:59:71 | // $ hasTaintFlow | Missing result: hasTaintFlow | +| AncestorLoop.java:60:78:60:94 | // $ hasTaintFlow | Missing result: hasTaintFlow | +| AncestorLoop.java:62:59:62:75 | // $ hasTaintFlow | Missing result: hasTaintFlow | From 1d4e0ad52c9fd7160d83677915d923cab2eb6f38 Mon Sep 17 00:00:00 2001 From: chanelyoung Date: Tue, 1 Dec 2026 13:56:19 -0800 Subject: [PATCH 5/7] Java: recognize canonical ancestor-loop containment helpers Require all loop SSA-phi inputs to be candidate-parent initialization or self-parent update, with successful equality controlling return of the unchanged candidate. Preserve directory taint for generic calls and method references. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 07dbce6b-e7a3-48b2-a656-bddbcb83db5b --- .../2026-12-01-canonical-ancestor-loop.md | 4 ++ .../code/java/security/PathSanitizer.qll | 53 ++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 java/ql/lib/change-notes/2026-12-01-canonical-ancestor-loop.md diff --git a/java/ql/lib/change-notes/2026-12-01-canonical-ancestor-loop.md b/java/ql/lib/change-notes/2026-12-01-canonical-ancestor-loop.md new file mode 100644 index 000000000000..757dd7bb3e6d --- /dev/null +++ b/java/ql/lib/change-notes/2026-12-01-canonical-ancestor-loop.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* Path injection analysis now recognizes static helpers that return an unchanged canonical file after a canonical allowed directory is found by walking its parents. Recognition checks every input of the loop's SSA phi and preserves taint from the allowed directory. diff --git a/java/ql/lib/semmle/code/java/security/PathSanitizer.qll b/java/ql/lib/semmle/code/java/security/PathSanitizer.qll index d8e053d0ed44..e93ea909a2fb 100644 --- a/java/ql/lib/semmle/code/java/security/PathSanitizer.qll +++ b/java/ql/lib/semmle/code/java/security/PathSanitizer.qll @@ -101,6 +101,51 @@ private predicate canonicalParentGuard( ) } +/** + * Holds if `parent` reads a loop phi initialized to the parent of `candidate` + * and updated only by taking its own parent. Every phi input must be accounted + * for: an existential may-flow path through a loop is not sufficient. + */ +private predicate canonicalAncestor(Expr parent, CanonicalFileCall candidate) { + exists( + SsaPhiDefinition phi, SsaExplicitWrite init, SsaExplicitWrite update, MethodCall firstParent, + MethodCall nextParent + | + parent = phi.getARead() and + phi.getSourceVariable().getVariable() instanceof LocalScopeVariable and + init.getSourceVariable() = phi.getSourceVariable() and + update.getSourceVariable() = phi.getSourceVariable() and + init = phi.getAnInput() and + update = phi.getAnInput() and + init != update and + forall(SsaDefinition input | input = phi.getAnInput() | input = [init, update]) and + init.getDefiningExpr().(VariableAssign).getSource() = firstParent and + firstParent.getMethod().hasQualifiedName("java.io", "File", "getParentFile") and + readsFileValue(firstParent.getQualifier(), candidate) and + update.getDefiningExpr().(VariableAssign).getSource() = nextParent and + nextParent.getMethod().hasQualifiedName("java.io", "File", "getParentFile") and + nextParent.getQualifier() = phi.getARead() + ) +} + +/** + * Holds if `guard` compares a canonical ancestor of `candidate` to `directory`. + */ +private predicate canonicalAncestorGuard( + Guard guard, CanonicalFileCall candidate, CanonicalFileCall directory +) { + exists(MethodCall equals, Expr parent, Expr allowed | + guard = equals and + equals.getMethod().hasQualifiedName("java.io", "File", "equals") and + canonicalAncestor(parent, candidate) and + readsFileValue(allowed, directory) + | + equals.getQualifier() = parent and equals.getArgument(0) = allowed + or + equals.getQualifier() = allowed and equals.getArgument(0) = parent + ) +} + /** * Holds if `ret` returns the unchanged canonical candidate on a successful containment check. * Only static helpers with a directory parameter are modeled. In particular, we do not @@ -112,7 +157,11 @@ private predicate canonicalContainmentReturn(ReturnStmt ret, Parameter directory helper.isStatic() and containmentDirectory(directory, helper, directoryParameter) and readsFileValue(ret.getExpr(), candidate) and - canonicalParentGuard(guard, candidate, directory) and + ( + canonicalParentGuard(guard, candidate, directory) + or + canonicalAncestorGuard(guard, candidate, directory) + ) and guard.controls(ret.getBasicBlock(), true) ) } @@ -133,7 +182,7 @@ private class ContainmentDirectoryTaintStep extends AdditionalTaintStep { override predicate step(DataFlow::Node n1, DataFlow::Node n2) { exists(ReturnStmt ret, Parameter parameter, MethodCall call | canonicalContainmentReturn(ret, parameter) and - call.getMethod() = ret.getEnclosingCallable() and + call.getMethod().getSourceDeclaration() = ret.getEnclosingCallable() and n1.asExpr() = call.getArgument(parameter.getPosition()) and n2.asExpr() = call ) From 2867f6fe192f01aa1bc250440207fb53b7a6fea5 Mon Sep 17 00:00:00 2001 From: chanelyoung Date: Tue, 1 Dec 2026 13:56:20 -0800 Subject: [PATCH 6/7] Java: update ancestor-loop containment expected results Remove the four false-positive and four missed-taint diagnostics after ancestor-loop recognition and directory-taint propagation are updated. Unsafe cases and the documented pre-existing lexical-prefix gap retain their inline expectations. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 07dbce6b-e7a3-48b2-a656-bddbcb83db5b --- .../library-tests/canonical-containment/test.expected | 8 -------- 1 file changed, 8 deletions(-) diff --git a/java/ql/test/library-tests/canonical-containment/test.expected b/java/ql/test/library-tests/canonical-containment/test.expected index f65c4bbe4f53..e69de29bb2d1 100644 --- a/java/ql/test/library-tests/canonical-containment/test.expected +++ b/java/ql/test/library-tests/canonical-containment/test.expected @@ -1,8 +0,0 @@ -| AncestorLoop.java:49:18:49:21 | file | Unexpected result: hasTaintFlow | -| AncestorLoop.java:50:14:50:45 | whileLoop(...) | Unexpected result: hasTaintFlow | -| AncestorLoop.java:51:14:51:68 | genericLoop(...) | Unexpected result: hasTaintFlow | -| AncestorLoop.java:53:14:53:49 | apply(...) | Unexpected result: hasTaintFlow | -| AncestorLoop.java:57:56:57:72 | // $ hasTaintFlow | Missing result: hasTaintFlow | -| AncestorLoop.java:59:55:59:71 | // $ hasTaintFlow | Missing result: hasTaintFlow | -| AncestorLoop.java:60:78:60:94 | // $ hasTaintFlow | Missing result: hasTaintFlow | -| AncestorLoop.java:62:59:62:75 | // $ hasTaintFlow | Missing result: hasTaintFlow | From a6ebfd30d412d657ebb4e335804f632e51a256cb Mon Sep 17 00:00:00 2001 From: chanelyoung Date: Tue, 1 Dec 2026 14:40:40 -0800 Subject: [PATCH 7/7] Java: narrow containment regressions to reported false positives Remove change notes and retain the reported helper patterns with closely related unsafe cases. Focused canonical-containment tests pass. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 07dbce6b-e7a3-48b2-a656-bddbcb83db5b --- .../2026-12-01-canonical-ancestor-loop.md | 4 - .../2026-12-01-canonical-direct-parent.md | 4 - .../canonical-containment/AncestorLoop.java | 158 +----------------- .../canonical-containment/DirectParent.java | 121 +------------- 4 files changed, 13 insertions(+), 274 deletions(-) delete mode 100644 java/ql/lib/change-notes/2026-12-01-canonical-ancestor-loop.md delete mode 100644 java/ql/lib/change-notes/2026-12-01-canonical-direct-parent.md diff --git a/java/ql/lib/change-notes/2026-12-01-canonical-ancestor-loop.md b/java/ql/lib/change-notes/2026-12-01-canonical-ancestor-loop.md deleted file mode 100644 index 757dd7bb3e6d..000000000000 --- a/java/ql/lib/change-notes/2026-12-01-canonical-ancestor-loop.md +++ /dev/null @@ -1,4 +0,0 @@ ---- -category: minorAnalysis ---- -* Path injection analysis now recognizes static helpers that return an unchanged canonical file after a canonical allowed directory is found by walking its parents. Recognition checks every input of the loop's SSA phi and preserves taint from the allowed directory. diff --git a/java/ql/lib/change-notes/2026-12-01-canonical-direct-parent.md b/java/ql/lib/change-notes/2026-12-01-canonical-direct-parent.md deleted file mode 100644 index be0c18c02746..000000000000 --- a/java/ql/lib/change-notes/2026-12-01-canonical-direct-parent.md +++ /dev/null @@ -1,4 +0,0 @@ ---- -category: minorAnalysis ---- -* Path injection analysis now recognizes static helpers that return a canonical file only after checking that its parent equals the canonical allowed directory. Taint from the allowed directory is preserved. diff --git a/java/ql/test/library-tests/canonical-containment/AncestorLoop.java b/java/ql/test/library-tests/canonical-containment/AncestorLoop.java index ca2fb135bf07..5a6e8f94864d 100644 --- a/java/ql/test/library-tests/canonical-containment/AncestorLoop.java +++ b/java/ql/test/library-tests/canonical-containment/AncestorLoop.java @@ -1,6 +1,5 @@ import java.io.File; import java.io.IOException; -import java.util.function.BiFunction; class AncestorLoop { static String source() { return null; } @@ -21,47 +20,6 @@ static File descendant(String path, String base) { } } - static File whileLoop(String path, String base) throws IOException { - File directory = new File(base).getCanonicalFile(); - File candidate = new File(path).getCanonicalFile(); - File parent = candidate.getParentFile(); - while (parent != null) { - if (parent.equals(directory)) - return candidate; - parent = parent.getParentFile(); - } - return null; - } - - static File genericLoop(String path, String base) throws IOException { - File directory = new File(base).getCanonicalFile(); - File candidate = new File(path).getCanonicalFile(); - for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) { - if (directory.equals(parent)) - return candidate; - } - return null; - } - - static void safe() throws IOException { - File file = descendant(source(), "/srv/data"); - if (file != null) - sink(file); // Safe: a strict canonical descendant, possibly nested. - sink(whileLoop(source(), "/srv/data")); // Safe: equivalent while loop. - sink(AncestorLoop.genericLoop(source(), "/srv/data")); // Safe - BiFunction resolve = AncestorLoop::descendant; - sink(resolve.apply(source(), "/srv/data")); // Safe - } - - static void untrustedDirectory() throws IOException { - sink(descendant("/srv/data/fixed", source())); // $ hasTaintFlow - sink(descendant(source(), source())); // $ hasTaintFlow - sink(whileLoop("/srv/data/fixed", source())); // $ hasTaintFlow - sink(AncestorLoop.genericLoop("/srv/data/fixed", source())); // $ hasTaintFlow - BiFunction resolve = AncestorLoop::descendant; - sink(resolve.apply("/srv/data/fixed", source())); // $ hasTaintFlow - } - static File fallthrough(String path, String base) throws IOException { File directory = new File(base).getCanonicalFile(); File candidate = new File(path).getCanonicalFile(); @@ -72,117 +30,11 @@ static File fallthrough(String path, String base) throws IOException { return candidate; } - static File failure(String path, String base) throws IOException { - File directory = new File(base).getCanonicalFile(); - File candidate = new File(path).getCanonicalFile(); - for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) { - if (!directory.equals(parent)) - return candidate; - } - return null; - } - - static File injectedParent(String path, String base, boolean inject) throws IOException { - File directory = new File(base).getCanonicalFile(); - File candidate = new File(path).getCanonicalFile(); - for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) { - if (inject) - parent = directory; - if (directory.equals(parent)) - return candidate; - } - return null; - } - - static File resetOnBackedge(String path, String base, boolean inject) throws IOException { - File directory = new File(base).getCanonicalFile(); - File candidate = new File(path).getCanonicalFile(); - File parent = candidate.getParentFile(); - while (parent != null) { - if (directory.equals(parent)) - return candidate; - if (inject) - parent = directory; - else - parent = parent.getParentFile(); - } - return null; - } - - static File wrongCandidate(String path, String base) throws IOException { - File directory = new File(base).getCanonicalFile(); - File candidate = new File(path).getCanonicalFile(); - File other = new File(directory, "fixed").getCanonicalFile(); - for (File parent = other.getParentFile(); parent != null; parent = parent.getParentFile()) { - if (directory.equals(parent)) - return candidate; - } - return null; - } - - static File reassigned(String path, String base) throws IOException { - File directory = new File(base).getCanonicalFile(); - File candidate = new File(directory, "fixed").getCanonicalFile(); - for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) { - if (directory.equals(parent)) { - candidate = new File(path).getCanonicalFile(); - return candidate; - } - } - return null; - } - - static File lexical(String path, String base) { - File directory = new File(base); - File candidate = new File(path); - for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) { - if (directory.equals(parent)) - return candidate; - } - return null; - } - - static File noGuard(String path, String base) throws IOException { - File directory = new File(base).getCanonicalFile(); - File candidate = new File(path).getCanonicalFile(); - for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) { - directory.equals(parent); - return candidate; - } - return null; - } - - static File siblingPrefix(String path, String base) throws IOException { - File directory = new File(base).getCanonicalFile(); - File candidate = new File(path).getCanonicalFile(); - for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) { - if (parent.toString().startsWith(directory.toString())) - return candidate; - } - return null; - } - - static File derivedDirectory(String path) throws IOException { - File candidate = new File(path).getCanonicalFile(); - File directory = candidate.getParentFile().getCanonicalFile(); - for (File parent = candidate.getParentFile(); parent != null; parent = parent.getParentFile()) { - if (directory.equals(parent)) - return candidate; - } - return null; - } - - static void unsafe() throws IOException { + static void test() throws IOException { + File file = descendant(source(), "/srv/data"); + if (file != null) + sink(file); // Safe: a canonical ancestor must match the allowed directory. sink(fallthrough(source(), "/srv/data")); // $ hasTaintFlow - sink(failure(source(), "/srv/data")); // $ hasTaintFlow - sink(injectedParent(source(), "/srv/data", true)); // $ hasTaintFlow - sink(resetOnBackedge(source(), "/srv/data", true)); // $ hasTaintFlow - sink(wrongCandidate(source(), "/srv/data")); // $ hasTaintFlow - sink(reassigned(source(), "/srv/data")); // $ hasTaintFlow - sink(lexical(source(), "/srv/data")); // $ hasTaintFlow - sink(noGuard(source(), "/srv/data")); // $ hasTaintFlow - // Existing prefix sanitizers accept this; parent-walking recognition must not expand it. - sink(siblingPrefix(source(), "/srv/data")); // $ MISSING: hasTaintFlow - sink(derivedDirectory(source())); // $ hasTaintFlow + sink(descendant("/srv/data/fixed", source())); // $ hasTaintFlow } } diff --git a/java/ql/test/library-tests/canonical-containment/DirectParent.java b/java/ql/test/library-tests/canonical-containment/DirectParent.java index 293be55ebc25..90263807258f 100644 --- a/java/ql/test/library-tests/canonical-containment/DirectParent.java +++ b/java/ql/test/library-tests/canonical-containment/DirectParent.java @@ -5,15 +5,7 @@ class DirectParent { static String source() { return null; } static void sink(Object value) {} - static File child(String base, String name) throws IOException { - File directory = new File(base).getCanonicalFile(); - File candidate = new File(directory, name).getCanonicalFile(); - if (!directory.equals(candidate.getParentFile())) - return null; - return candidate; - } - - static File reversed(String base, String name) throws IOException { + static File child(String base, String name) { final File directory; final File candidate; try { @@ -22,119 +14,22 @@ static File reversed(String base, String name) throws IOException { } catch (IOException ex) { return null; } - if (candidate.getParentFile().equals(directory)) { - return candidate; - } - return null; - } - - static void safe() throws IOException { - File file = child("/srv/data", source()); - if (file != null) - sink(file); // Safe: only the canonical direct child is returned. - File other = reversed("/srv/data", source()); - if (other != null) - sink(other); // Safe: reversed equality and failure returning null. - } - - static void untrustedDirectory() throws IOException { - sink(child(source(), "fixed")); // $ hasTaintFlow - sink(child(source(), source())); // $ hasTaintFlow - sink(reversed(source(), "fixed")); // $ hasTaintFlow - } - - static File normalizedOnly(String base, String name) throws IOException { - File directory = new File(base).getCanonicalFile(); - File candidate = new File(directory, name).getCanonicalFile(); - return candidate; - } - - static File failure(String base, String name) throws IOException { - File directory = new File(base).getCanonicalFile(); - File candidate = new File(directory, name).getCanonicalFile(); - if (!directory.equals(candidate.getParentFile())) - return candidate; - return null; - } - - static File ignored(String base, String name) throws IOException { - File directory = new File(base).getCanonicalFile(); - File candidate = new File(directory, name).getCanonicalFile(); - directory.equals(candidate.getParentFile()); - return candidate; - } - - static File fallthrough(String base, String name) throws IOException { - File directory = new File(base).getCanonicalFile(); - File candidate = new File(directory, name).getCanonicalFile(); - if (directory.equals(candidate.getParentFile())) - return candidate; - return candidate; - } - - static File lexical(String base, String name) { - File directory = new File(base); - // A lexical direct child can be a symlink to a file outside directory. - File candidate = new File(directory, name); - if (!directory.equals(candidate.getParentFile())) - return null; - return candidate; - } - - static File wrongCandidate(String base, String name) throws IOException { - File directory = new File(base).getCanonicalFile(); - File checked = new File(directory, "fixed").getCanonicalFile(); - File candidate = new File(directory, name).getCanonicalFile(); - if (!directory.equals(checked.getParentFile())) - return null; - return candidate; - } - - static File reassigned(String base, String name) throws IOException { - File directory = new File(base).getCanonicalFile(); - File candidate = new File(directory, "fixed").getCanonicalFile(); if (!directory.equals(candidate.getParentFile())) return null; - candidate = new File(name).getCanonicalFile(); return candidate; } - static File merged(String base, String name, boolean choose) throws IOException { + static File unchecked(String base, String name) throws IOException { File directory = new File(base).getCanonicalFile(); File candidate = new File(directory, name).getCanonicalFile(); - if (!directory.equals(candidate.getParentFile())) - return null; - if (choose) - candidate = new File(name).getCanonicalFile(); - return candidate; - } - - static File derivedDirectory(String name) throws IOException { - File directory = new File(name).getCanonicalFile(); - File candidate = new File(directory, "fixed").getCanonicalFile(); - if (!directory.equals(candidate.getParentFile())) - return null; return candidate; } - static File directoryFromCandidate(String name) throws IOException { - File candidate = new File(name).getCanonicalFile(); - File directory = candidate.getParentFile().getCanonicalFile(); - if (!directory.equals(candidate.getParentFile())) - return null; - return candidate; - } - - static void unsafe() throws IOException { - sink(normalizedOnly("/srv/data", source())); // $ hasTaintFlow - sink(failure("/srv/data", source())); // $ hasTaintFlow - sink(ignored("/srv/data", source())); // $ hasTaintFlow - sink(fallthrough("/srv/data", source())); // $ hasTaintFlow - sink(lexical("/srv/data", source())); // $ hasTaintFlow - sink(wrongCandidate("/srv/data", source())); // $ hasTaintFlow - sink(reassigned("/srv/data", source())); // $ hasTaintFlow - sink(merged("/srv/data", source(), true)); // $ hasTaintFlow - sink(derivedDirectory(source())); // $ hasTaintFlow - sink(directoryFromCandidate(source())); // $ hasTaintFlow + static void test() throws IOException { + File file = child("/srv/data", source()); + if (file != null) + sink(file); // Safe: the canonical parent must match the allowed directory. + sink(unchecked("/srv/data", source())); // $ hasTaintFlow + sink(child(source(), "fixed")); // $ hasTaintFlow } }