|
| 1 | +/** Definitions for reasoning about whether files are closed. */ |
| 2 | + |
| 3 | +import python |
| 4 | +import semmle.python.dataflow.new.internal.DataFlowDispatch |
| 5 | +import semmle.python.ApiGraphs |
| 6 | + |
| 7 | +/** A CFG node where a file is opened. */ |
| 8 | +abstract class FileOpenSource extends DataFlow::CfgNode { } |
| 9 | + |
| 10 | +/** A call to the builtin `open` or `os.open`. */ |
| 11 | +class FileOpenCall extends FileOpenSource { |
| 12 | + FileOpenCall() { |
| 13 | + this = [API::builtin("open").getACall(), API::moduleImport("os").getMember("open").getACall()] |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +private DataFlow::TypeTrackingNode fileOpenInstance(DataFlow::TypeTracker t) { |
| 18 | + t.start() and |
| 19 | + result instanceof FileOpenSource |
| 20 | + or |
| 21 | + exists(DataFlow::TypeTracker t2 | result = fileOpenInstance(t2).track(t2, t)) |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * A call that returns an instance of an open file object. |
| 26 | + * This includes calls to methods that transitively call `open` or similar. |
| 27 | + */ |
| 28 | +class FileOpen extends DataFlow::CallCfgNode { |
| 29 | + FileOpen() { fileOpenInstance(DataFlow::TypeTracker::end()).flowsTo(this) } |
| 30 | +} |
| 31 | + |
| 32 | +/** A call that may wrap a file object in a wrapper class or `os.fdopen`. */ |
| 33 | +class FileWrapperCall extends DataFlow::CallCfgNode { |
| 34 | + DataFlow::Node wrapped; |
| 35 | + |
| 36 | + FileWrapperCall() { |
| 37 | + wrapped = this.getArg(_).getALocalSource() and |
| 38 | + this.getFunction() = classTracker(_) |
| 39 | + or |
| 40 | + wrapped = this.getArg(0) and |
| 41 | + this = API::moduleImport("os").getMember("fdopen").getACall() |
| 42 | + or |
| 43 | + wrapped = this.getArg(0) and |
| 44 | + this = API::moduleImport("django").getMember("http").getMember("FileResponse").getACall() |
| 45 | + } |
| 46 | + |
| 47 | + /** Gets the file that this call wraps. */ |
| 48 | + DataFlow::Node getWrapped() { result = wrapped } |
| 49 | +} |
| 50 | + |
| 51 | +/** A node where a file is closed. */ |
| 52 | +abstract class FileClose extends DataFlow::CfgNode { |
| 53 | + /** Holds if this file close will occur if an exception is thrown at `raises`. */ |
| 54 | + predicate guardsExceptions(DataFlow::CfgNode raises) { |
| 55 | + this.asCfgNode() = raises.asCfgNode().getAnExceptionalSuccessor().getASuccessor*() |
| 56 | + or |
| 57 | + // The expression is after the close call. |
| 58 | + // This also covers the body of a `with` statement. |
| 59 | + raises.asCfgNode() = this.asCfgNode().getASuccessor*() |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** A call to the `.close()` method of a file object. */ |
| 64 | +class FileCloseCall extends FileClose { |
| 65 | + FileCloseCall() { exists(DataFlow::MethodCallNode mc | mc.calls(this, "close")) } |
| 66 | +} |
| 67 | + |
| 68 | +/** A call to `os.close`. */ |
| 69 | +class OsCloseCall extends FileClose { |
| 70 | + OsCloseCall() { this = API::moduleImport("os").getMember("close").getACall().getArg(0) } |
| 71 | +} |
| 72 | + |
| 73 | +/** A `with` statement. */ |
| 74 | +class WithStatement extends FileClose { |
| 75 | + WithStatement() { this.asExpr() = any(With w).getContextExpr() } |
| 76 | +} |
| 77 | + |
| 78 | +/** Holds if an exception may be raised at `raises` if `file` is a file object. */ |
| 79 | +private predicate mayRaiseWithFile(DataFlow::CfgNode file, DataFlow::CfgNode raises) { |
| 80 | + // Currently just consider any method called on `file`; e.g. `file.write()`; as potentially raising an exception |
| 81 | + raises.(DataFlow::MethodCallNode).getObject() = file and |
| 82 | + not file instanceof FileOpen and |
| 83 | + not file instanceof FileClose |
| 84 | +} |
| 85 | + |
| 86 | +/** Holds if data flows from `nodeFrom` to `nodeTo` in one step that also includes file wrapper classes. */ |
| 87 | +private predicate fileAdditionalLocalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { |
| 88 | + exists(FileWrapperCall fw | nodeFrom = fw.getWrapped() and nodeTo = fw) |
| 89 | +} |
| 90 | + |
| 91 | +private predicate fileLocalFlowHelper0( |
| 92 | + DataFlow::LocalSourceNode nodeFrom, DataFlow::LocalSourceNode nodeTo |
| 93 | +) { |
| 94 | + exists(DataFlow::Node nodeMid | |
| 95 | + nodeFrom.flowsTo(nodeMid) and fileAdditionalLocalFlowStep(nodeMid, nodeTo) |
| 96 | + ) |
| 97 | +} |
| 98 | + |
| 99 | +private predicate fileLocalFlowHelper1( |
| 100 | + DataFlow::LocalSourceNode nodeFrom, DataFlow::LocalSourceNode nodeTo |
| 101 | +) { |
| 102 | + fileLocalFlowHelper0*(nodeFrom, nodeTo) |
| 103 | +} |
| 104 | + |
| 105 | +/** Holds if data flows from `source` to `sink`, including file wrapper classes. */ |
| 106 | +pragma[inline] |
| 107 | +private predicate fileLocalFlow(FileOpen source, DataFlow::Node sink) { |
| 108 | + exists(DataFlow::LocalSourceNode mid | fileLocalFlowHelper1(source, mid) and mid.flowsTo(sink)) |
| 109 | +} |
| 110 | + |
| 111 | +/** Holds if the file opened at `fo` is closed. */ |
| 112 | +predicate fileIsClosed(FileOpen fo) { exists(FileClose fc | fileLocalFlow(fo, fc)) } |
| 113 | + |
| 114 | +/** Holds if the file opened at `fo` is returned to the caller. This makes the caller responsible for closing the file. */ |
| 115 | +predicate fileIsReturned(FileOpen fo) { |
| 116 | + exists(Return ret, Expr retVal | |
| 117 | + ( |
| 118 | + retVal = ret.getValue() |
| 119 | + or |
| 120 | + retVal = ret.getValue().(List).getAnElt() |
| 121 | + or |
| 122 | + retVal = ret.getValue().(Tuple).getAnElt() |
| 123 | + ) and |
| 124 | + fileLocalFlow(fo, DataFlow::exprNode(retVal)) |
| 125 | + ) |
| 126 | +} |
| 127 | + |
| 128 | +/** Holds if the file opened at `fo` is stored in a field. We assume that another method is then responsible for closing the file. */ |
| 129 | +predicate fileIsStoredInField(FileOpen fo) { |
| 130 | + exists(DataFlow::AttrWrite aw | fileLocalFlow(fo, aw.getValue())) |
| 131 | +} |
| 132 | + |
| 133 | +/** Holds if the file opened at `fo` is not closed, and is expected to be closed. */ |
| 134 | +predicate fileNotClosed(FileOpen fo) { |
| 135 | + not fileIsClosed(fo) and |
| 136 | + not fileIsReturned(fo) and |
| 137 | + not fileIsStoredInField(fo) |
| 138 | +} |
| 139 | + |
| 140 | +predicate fileMayNotBeClosedOnException(FileOpen fo, DataFlow::Node raises) { |
| 141 | + fileIsClosed(fo) and |
| 142 | + exists(DataFlow::CfgNode fileRaised | |
| 143 | + mayRaiseWithFile(fileRaised, raises) and |
| 144 | + fileLocalFlow(fo, fileRaised) and |
| 145 | + not exists(FileClose fc | |
| 146 | + fileLocalFlow(fo, fc) and |
| 147 | + fc.guardsExceptions(raises) |
| 148 | + ) |
| 149 | + ) |
| 150 | +} |
0 commit comments