|
| 1 | +/** |
| 2 | + * @name Missing error check |
| 3 | + * @description When a function returns a pointer alongside an error value, one should normally |
| 4 | + * assume that the pointer may be nil until either the pointer or error has been checked. |
| 5 | + * @kind problem |
| 6 | + * @problem.severity warning |
| 7 | + * @id go/missing-error-check |
| 8 | + * @tags reliability |
| 9 | + * correctness |
| 10 | + * logic |
| 11 | + * @precision high |
| 12 | + */ |
| 13 | + |
| 14 | +import go |
| 15 | + |
| 16 | +/** |
| 17 | + * Holds if `node` is a reference to the `nil` builtin constant. |
| 18 | + */ |
| 19 | +predicate isNil(DataFlow::Node node) { node = Builtin::nil().getARead() } |
| 20 | + |
| 21 | +/** |
| 22 | + * Matches if `call` may return a nil pointer alongside an error value. |
| 23 | + * |
| 24 | + * This is both an over- and under-estimate: over in that we assume opaque functions may use this |
| 25 | + * convention, and under in that functions with bodies are only recognized if they use a literal |
| 26 | + * `nil` for the pointer return value at some return site. |
| 27 | + */ |
| 28 | +predicate calleeMayReturnNilWithError(DataFlow::CallNode call) { |
| 29 | + not exists(call.getACallee()) |
| 30 | + or |
| 31 | + exists(FuncDef callee | callee = call.getACallee() | |
| 32 | + not exists(callee.getBody()) |
| 33 | + or |
| 34 | + exists(IR::ReturnInstruction ret, DataFlow::Node ptrReturn, DataFlow::Node errReturn | |
| 35 | + callee = ret.getRoot() and |
| 36 | + ptrReturn = DataFlow::instructionNode(ret.getResult(0)) and |
| 37 | + errReturn = DataFlow::instructionNode(ret.getResult(1)) and |
| 38 | + isNil(ptrReturn) and |
| 39 | + not isNil(errReturn) |
| 40 | + ) |
| 41 | + ) |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Matches if `type` is a pointer, slice or interface type, or an alias for such a type. |
| 46 | + */ |
| 47 | +predicate isDereferenceableType(Type maybePointer) { |
| 48 | + exists(Type t | t = maybePointer.getUnderlyingType() | |
| 49 | + t instanceof PointerType or t instanceof SliceType or t instanceof InterfaceType |
| 50 | + ) |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Matches if `instruction` checks `value`. |
| 55 | + * |
| 56 | + * We consider testing value for equality (against anything), passing it as a parameter to |
| 57 | + * a function call, switching on either its value or its type or casting it to constitute a |
| 58 | + * check. |
| 59 | + */ |
| 60 | +predicate checksValue(IR::Instruction instruction, DataFlow::SsaNode value) { |
| 61 | + exists(DataFlow::InstructionNode instNode | instNode.asInstruction() = instruction | |
| 62 | + instNode.(DataFlow::CallNode).getAnArgument() = value.getAUse() or |
| 63 | + instNode.(DataFlow::EqualityTestNode).getAnOperand() = value.getAUse() |
| 64 | + ) |
| 65 | + or |
| 66 | + value.getAUse().asInstruction() = instruction and |
| 67 | + ( |
| 68 | + exists(ExpressionSwitchStmt s | instruction.(IR::EvalInstruction).getExpr() = s.getExpr()) |
| 69 | + or |
| 70 | + // This case accounts for both a type-switch or cast used to check `value` |
| 71 | + exists(TypeAssertExpr e | instruction.(IR::EvalInstruction).getExpr() = e.getExpr()) |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Matches if `call` is a function returning (`ptr`, `err`) where `ptr` may be nil, and neither |
| 77 | + * `ptr` not `err` has been checked for validity as of `node`. |
| 78 | + * |
| 79 | + * This is initially true of any callsite that may call either an opaque function or a user-defined |
| 80 | + * function that may return (nil, error), and is true of any downstream control-flow node where a |
| 81 | + * check has not certainly been made against either `ptr` or `err`. |
| 82 | + */ |
| 83 | +predicate returnUncheckedAtNode( |
| 84 | + DataFlow::CallNode call, ControlFlow::Node node, DataFlow::SsaNode ptr, DataFlow::SsaNode err |
| 85 | +) { |
| 86 | + ( |
| 87 | + // Base case: check that `ptr` and `err` have appropriate types, and that the callee may return |
| 88 | + // a nil pointer with an error. |
| 89 | + ptr.getAPredecessor() = call.getResult(0) and |
| 90 | + err.getAPredecessor() = call.getResult(1) and |
| 91 | + call.asInstruction() = node and |
| 92 | + isDereferenceableType(ptr.getType()) and |
| 93 | + err.getType().implements(Builtin::error().getType().getUnderlyingType()) and |
| 94 | + calleeMayReturnNilWithError(call) |
| 95 | + or |
| 96 | + // Recursive case: check that some predecessor is missing a check, and `node` does not itself |
| 97 | + // check either `ptr` or `err`. |
| 98 | + // localFlow is used to permit checks via either an SSA phi node or ordinary assignment. |
| 99 | + returnUncheckedAtNode(call, node.getAPredecessor(), ptr, err) and |
| 100 | + not exists(DataFlow::SsaNode checked | |
| 101 | + DataFlow::localFlow(ptr, checked) or DataFlow::localFlow(err, checked) |
| 102 | + | |
| 103 | + checksValue(node, checked) |
| 104 | + ) |
| 105 | + ) |
| 106 | +} |
| 107 | + |
| 108 | +from |
| 109 | + DataFlow::CallNode call, DataFlow::SsaNode ptr, DataFlow::SsaNode err, |
| 110 | + DataFlow::PointerDereferenceNode deref, ControlFlow::Node derefNode |
| 111 | +where |
| 112 | + // `derefNode` is a control-flow node corresponding to `deref` |
| 113 | + deref.getOperand().asInstruction() = derefNode and |
| 114 | + // neither `ptr` nor `err`, the return values of `call`, have been checked as of `derefNode` |
| 115 | + returnUncheckedAtNode(call, derefNode, ptr, err) and |
| 116 | + // `deref` dereferences `ptr` |
| 117 | + deref.getOperand() = ptr.getAUse() |
| 118 | +select deref.getOperand(), |
| 119 | + ptr.getSourceVariable() + " may be nil here, because $@ may not have been checked.", err, |
| 120 | + err.getSourceVariable().toString() |
0 commit comments