|
| 1 | +/** |
| 2 | + * Models the different ways to create paths. Either by using `java.io.File`-related APIs or `java.nio.file.Path`-related APIs. |
| 3 | + */ |
| 4 | + |
| 5 | +import java |
| 6 | +import semmle.code.java.controlflow.Guards |
| 7 | + |
| 8 | +/** Models the creation of a path. */ |
| 9 | +abstract class PathCreation extends Expr { |
| 10 | + /** Gets an input that is used in the creation of this path. */ |
| 11 | + abstract Expr getInput(); |
| 12 | +} |
| 13 | + |
| 14 | +/** Models the `java.nio.file.Paths.get` method. */ |
| 15 | +class PathsGet extends PathCreation, MethodAccess { |
| 16 | + PathsGet() { |
| 17 | + exists(Method m | m = this.getMethod() | |
| 18 | + m.getDeclaringType() instanceof TypePaths and |
| 19 | + m.getName() = "get" |
| 20 | + ) |
| 21 | + } |
| 22 | + |
| 23 | + override Expr getInput() { result = this.getAnArgument() } |
| 24 | +} |
| 25 | + |
| 26 | +/** Models the `java.nio.file.FileSystem.getPath` method. */ |
| 27 | +class FileSystemGetPath extends PathCreation, MethodAccess { |
| 28 | + FileSystemGetPath() { |
| 29 | + exists(Method m | m = this.getMethod() | |
| 30 | + m.getDeclaringType() instanceof TypeFileSystem and |
| 31 | + m.getName() = "getPath" |
| 32 | + ) |
| 33 | + } |
| 34 | + |
| 35 | + override Expr getInput() { result = this.getAnArgument() } |
| 36 | +} |
| 37 | + |
| 38 | +/** Models the `new java.io.File(...)` constructor. */ |
| 39 | +class FileCreation extends PathCreation, ClassInstanceExpr { |
| 40 | + FileCreation() { this.getConstructedType() instanceof TypeFile } |
| 41 | + |
| 42 | + override Expr getInput() { |
| 43 | + result = this.getAnArgument() and |
| 44 | + // Relevant arguments include those that are not a `File`. |
| 45 | + not result.getType() instanceof TypeFile |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/** Models the `java.nio.file.Path.resolveSibling` method. */ |
| 50 | +class PathResolveSiblingCreation extends PathCreation, MethodAccess { |
| 51 | + PathResolveSiblingCreation() { |
| 52 | + exists(Method m | m = this.getMethod() | |
| 53 | + m.getDeclaringType() instanceof TypePath and |
| 54 | + m.getName() = "resolveSibling" |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + override Expr getInput() { |
| 59 | + result = this.getAnArgument() and |
| 60 | + // Relevant arguments are those of type `String`. |
| 61 | + result.getType() instanceof TypeString |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** Models the `java.nio.file.Path.resolve` method. */ |
| 66 | +class PathResolveCreation extends PathCreation, MethodAccess { |
| 67 | + PathResolveCreation() { |
| 68 | + exists(Method m | m = this.getMethod() | |
| 69 | + m.getDeclaringType() instanceof TypePath and |
| 70 | + m.getName() = "resolve" |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + override Expr getInput() { |
| 75 | + result = this.getAnArgument() and |
| 76 | + // Relevant arguments are those of type `String`. |
| 77 | + result.getType() instanceof TypeString |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/** Models the `java.nio.file.Path.of` method. */ |
| 82 | +class PathOfCreation extends PathCreation, MethodAccess { |
| 83 | + PathOfCreation() { |
| 84 | + exists(Method m | m = this.getMethod() | |
| 85 | + m.getDeclaringType() instanceof TypePath and |
| 86 | + m.getName() = "of" |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + override Expr getInput() { result = this.getAnArgument() } |
| 91 | +} |
| 92 | + |
| 93 | +/** Models the `new java.io.FileWriter(...)` constructor. */ |
| 94 | +class FileWriterCreation extends PathCreation, ClassInstanceExpr { |
| 95 | + FileWriterCreation() { this.getConstructedType().hasQualifiedName("java.io", "FileWriter") } |
| 96 | + |
| 97 | + override Expr getInput() { |
| 98 | + result = this.getAnArgument() and |
| 99 | + // Relevant arguments are those of type `String`. |
| 100 | + result.getType() instanceof TypeString |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/** Models the `new java.io.FileReader(...)` constructor. */ |
| 105 | +class FileReaderCreation extends PathCreation, ClassInstanceExpr { |
| 106 | + FileReaderCreation() { this.getConstructedType().hasQualifiedName("java.io", "FileReader") } |
| 107 | + |
| 108 | + override Expr getInput() { |
| 109 | + result = this.getAnArgument() and |
| 110 | + // Relevant arguments are those of type `String`. |
| 111 | + result.getType() instanceof TypeString |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/** Models the `new java.io.FileInputStream(...)` constructor. */ |
| 116 | +class FileInputStreamCreation extends PathCreation, ClassInstanceExpr { |
| 117 | + FileInputStreamCreation() { |
| 118 | + this.getConstructedType().hasQualifiedName("java.io", "FileInputStream") |
| 119 | + } |
| 120 | + |
| 121 | + override Expr getInput() { |
| 122 | + result = this.getAnArgument() and |
| 123 | + // Relevant arguments are those of type `String`. |
| 124 | + result.getType() instanceof TypeString |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +/** Models the `new java.io.FileOutputStream(...)` constructor. */ |
| 129 | +class FileOutputStreamCreation extends PathCreation, ClassInstanceExpr { |
| 130 | + FileOutputStreamCreation() { |
| 131 | + this.getConstructedType().hasQualifiedName("java.io", "FileOutputStream") |
| 132 | + } |
| 133 | + |
| 134 | + override Expr getInput() { |
| 135 | + result = this.getAnArgument() and |
| 136 | + // Relevant arguments are those of type `String`. |
| 137 | + result.getType() instanceof TypeString |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +private predicate inWeakCheck(Expr e) { |
| 142 | + // None of these are sufficient to guarantee that a string is safe. |
| 143 | + exists(MethodAccess m, Method def | m.getQualifier() = e and m.getMethod() = def | |
| 144 | + def.getName() = "startsWith" or |
| 145 | + def.getName() = "endsWith" or |
| 146 | + def.getName() = "isEmpty" or |
| 147 | + def.getName() = "equals" |
| 148 | + ) |
| 149 | + or |
| 150 | + // Checking against `null` has no bearing on path traversal. |
| 151 | + exists(EqualityTest b | b.getAnOperand() = e | b.getAnOperand() instanceof NullLiteral) |
| 152 | +} |
| 153 | + |
| 154 | +// Ignore cases where the variable has been checked somehow, |
| 155 | +// but allow some particularly obviously bad cases. |
| 156 | +predicate guarded(VarAccess e) { |
| 157 | + exists(PathCreation p | e = p.getInput()) and |
| 158 | + exists(ConditionBlock cb, Expr c | |
| 159 | + cb.getCondition().getAChildExpr*() = c and |
| 160 | + c = e.getVariable().getAnAccess() and |
| 161 | + cb.controls(e.getBasicBlock(), true) and |
| 162 | + // Disallow a few obviously bad checks. |
| 163 | + not inWeakCheck(c) |
| 164 | + ) |
| 165 | +} |
0 commit comments