|
| 1 | +/** |
| 2 | + * @name Potential file system race condition |
| 3 | + * @description Separately checking the state of a file before operating |
| 4 | + * on it may allow an attacker to modify the file between |
| 5 | + * the two operations. |
| 6 | + * @kind problem |
| 7 | + * @problem.severity warning |
| 8 | + * @security-severity 7.7 |
| 9 | + * @precision medium |
| 10 | + * @id js/file-system-race |
| 11 | + * @tags security |
| 12 | + * external/cwe/cwe-367 |
| 13 | + */ |
| 14 | + |
| 15 | +import javascript |
| 16 | + |
| 17 | +/** |
| 18 | + * A call that checks a property of some file. |
| 19 | + */ |
| 20 | +class FileCheck extends DataFlow::CallNode { |
| 21 | + FileCheck() { |
| 22 | + this = |
| 23 | + NodeJSLib::FS::moduleMember([ |
| 24 | + "open", "openSync", "exists", "existsSync", "stat", "statSync", "lstat", "lstatSync", |
| 25 | + "fstat", "fstatSync", "access", "accessSync" |
| 26 | + ]).getACall() |
| 27 | + } |
| 28 | + |
| 29 | + DataFlow::Node getPathArgument() { result = this.getArgument(0) } |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * A call that modifies or otherwise interacts with a file. |
| 34 | + */ |
| 35 | +class FileUse extends DataFlow::CallNode { |
| 36 | + FileUse() { |
| 37 | + this = |
| 38 | + NodeJSLib::FS::moduleMember([ |
| 39 | + // these are the six methods that accept file paths and file descriptors |
| 40 | + "readFile", "readFileSync", "writeFile", "writeFileSync", "appendFile", "appendFileSync", |
| 41 | + // don't use "open" after e.g. "access" |
| 42 | + "open", "openSync" |
| 43 | + ]).getACall() |
| 44 | + } |
| 45 | + |
| 46 | + DataFlow::Node getPathArgument() { result = this.getArgument(0) } |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Gets a reference to a file-handle from a call to `open` or `openSync`. |
| 51 | + */ |
| 52 | +DataFlow::SourceNode getAFileHandle(DataFlow::TypeTracker t) { |
| 53 | + t.start() and |
| 54 | + ( |
| 55 | + result = NodeJSLib::FS::moduleMember("openSync").getACall() |
| 56 | + or |
| 57 | + result = |
| 58 | + NodeJSLib::FS::moduleMember("open") |
| 59 | + .getACall() |
| 60 | + .getLastArgument() |
| 61 | + .getAFunctionValue() |
| 62 | + .getParameter(1) |
| 63 | + ) |
| 64 | + or |
| 65 | + exists(DataFlow::TypeTracker t2 | result = getAFileHandle(t2).track(t2, t)) |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Holds if `check` and `use` operate on the same file. |
| 70 | + */ |
| 71 | +predicate checkAndUseOnSame(FileCheck check, FileUse use) { |
| 72 | + exists(string path | |
| 73 | + check.getPathArgument().mayHaveStringValue(path) and |
| 74 | + use.getPathArgument().mayHaveStringValue(path) |
| 75 | + ) |
| 76 | + or |
| 77 | + AccessPath::getAnAliasedSourceNode(check.getPathArgument()).flowsTo(use.getPathArgument()) |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Holds if `check` happens before `use`. |
| 82 | + */ |
| 83 | +pragma[inline] |
| 84 | +predicate useAfterCheck(FileCheck check, FileUse use) { |
| 85 | + check.getCallback(_).getFunction() = use.getContainer() |
| 86 | + or |
| 87 | + exists(BasicBlock bb | |
| 88 | + check.getBasicBlock() = bb and |
| 89 | + use.getBasicBlock() = bb and |
| 90 | + exists(int i, int j | |
| 91 | + bb.getNode(i) = check.asExpr() and |
| 92 | + bb.getNode(j) = use.asExpr() and |
| 93 | + i < j |
| 94 | + ) |
| 95 | + ) |
| 96 | + or |
| 97 | + check.getBasicBlock().getASuccessor+() = use.getBasicBlock() |
| 98 | +} |
| 99 | + |
| 100 | +from FileCheck check, FileUse use |
| 101 | +where |
| 102 | + checkAndUseOnSame(check, use) and |
| 103 | + useAfterCheck(check, use) and |
| 104 | + not getAFileHandle(DataFlow::TypeTracker::end()).flowsTo(use.getPathArgument()) |
| 105 | +select use, "The file may have changed since it $@.", check, "was checked" |
0 commit comments