|
| 1 | +/** |
| 2 | + * @name Missing return-value check for a 'scanf'-like function |
| 3 | + * @description Failing to check that a call to 'scanf' actually writes to an |
| 4 | + * output variable can lead to unexpected behavior at reading time. |
| 5 | + * @kind problem |
| 6 | + * @problem.severity warning |
| 7 | + * @security-severity 7.5 |
| 8 | + * @precision medium |
| 9 | + * @id cpp/missing-check-scanf |
| 10 | + * @tags security |
| 11 | + * correctness |
| 12 | + * external/cwe/cwe-252 |
| 13 | + * external/cwe/cwe-253 |
| 14 | + */ |
| 15 | + |
| 16 | +import cpp |
| 17 | +import semmle.code.cpp.commons.Scanf |
| 18 | +import semmle.code.cpp.controlflow.Guards |
| 19 | +import semmle.code.cpp.dataflow.DataFlow |
| 20 | +import semmle.code.cpp.ir.IR |
| 21 | +import semmle.code.cpp.ir.ValueNumbering |
| 22 | + |
| 23 | +/** An expression appearing as an output argument to a `scanf`-like call */ |
| 24 | +class ScanfOutput extends Expr { |
| 25 | + ScanfFunctionCall call; |
| 26 | + int varargIndex; |
| 27 | + Instruction instr; |
| 28 | + ValueNumber valNum; |
| 29 | + |
| 30 | + ScanfOutput() { |
| 31 | + this = call.getOutputArgument(varargIndex).getFullyConverted() and |
| 32 | + instr.getConvertedResultExpression() = this and |
| 33 | + valueNumber(instr) = valNum |
| 34 | + } |
| 35 | + |
| 36 | + ScanfFunctionCall getCall() { result = call } |
| 37 | + |
| 38 | + /** |
| 39 | + * Returns the smallest possible `scanf` return value that would indicate |
| 40 | + * success in writing this output argument. |
| 41 | + */ |
| 42 | + int getMinimumGuardConstant() { |
| 43 | + result = |
| 44 | + varargIndex + 1 - |
| 45 | + count(ScanfFormatLiteral f, int n | |
| 46 | + // Special case: %n writes to an argument without reading any input. |
| 47 | + // It does not increase the count returned by `scanf`. |
| 48 | + n <= varargIndex and f.getUse() = call and f.getConversionChar(n) = "n" |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + predicate hasGuardedAccess(Access e, boolean isGuarded) { |
| 53 | + e = this.getAnAccess() and |
| 54 | + if |
| 55 | + exists(int value, int minGuard | minGuard = this.getMinimumGuardConstant() | |
| 56 | + e.getBasicBlock() = blockGuardedBy(value, "==", call) and minGuard <= value |
| 57 | + or |
| 58 | + e.getBasicBlock() = blockGuardedBy(value, "<", call) and minGuard - 1 <= value |
| 59 | + or |
| 60 | + e.getBasicBlock() = blockGuardedBy(value, "<=", call) and minGuard <= value |
| 61 | + ) |
| 62 | + then isGuarded = true |
| 63 | + else isGuarded = false |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get a subsequent access of the same underlying storage, |
| 68 | + * but before it gets reset or reused in another `scanf` call. |
| 69 | + */ |
| 70 | + Access getAnAccess() { |
| 71 | + exists(Instruction dst | |
| 72 | + this.bigStep() = dst and |
| 73 | + dst.getAst() = result and |
| 74 | + valueNumber(dst) = valNum |
| 75 | + ) |
| 76 | + } |
| 77 | + |
| 78 | + private Instruction bigStep() { |
| 79 | + result = this.smallStep(instr) |
| 80 | + or |
| 81 | + exists(Instruction i | i = this.bigStep() | result = this.smallStep(i)) |
| 82 | + } |
| 83 | + |
| 84 | + private Instruction smallStep(Instruction i) { |
| 85 | + instr.getASuccessor*() = i and |
| 86 | + i.getASuccessor() = result and |
| 87 | + not this.isBarrier(result) |
| 88 | + } |
| 89 | + |
| 90 | + private predicate isBarrier(Instruction i) { |
| 91 | + valueNumber(i) = valNum and |
| 92 | + exists(Expr e | i.getAst() = e | |
| 93 | + i = any(StoreInstruction s).getDestinationAddress() |
| 94 | + or |
| 95 | + [e, e.getParent().(AddressOfExpr)] instanceof ScanfOutput |
| 96 | + ) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/** Returns a block guarded by the assertion of `value op call` */ |
| 101 | +BasicBlock blockGuardedBy(int value, string op, ScanfFunctionCall call) { |
| 102 | + exists(GuardCondition g, Expr left, Expr right | |
| 103 | + right = g.getAChild() and |
| 104 | + value = left.getValue().toInt() and |
| 105 | + DataFlow::localExprFlow(call, right) |
| 106 | + | |
| 107 | + g.ensuresEq(left, right, 0, result, true) and op = "==" |
| 108 | + or |
| 109 | + g.ensuresLt(left, right, 0, result, true) and op = "<" |
| 110 | + or |
| 111 | + g.ensuresLt(left, right, 1, result, true) and op = "<=" |
| 112 | + ) |
| 113 | +} |
| 114 | + |
| 115 | +from ScanfOutput output, ScanfFunctionCall call, Access access |
| 116 | +where |
| 117 | + output.getCall() = call and |
| 118 | + output.hasGuardedAccess(access, false) |
| 119 | +select access, |
| 120 | + "$@ is read here, but may not have been written. " + |
| 121 | + "It should be guarded by a check that the $@ returns at least " + |
| 122 | + output.getMinimumGuardConstant() + ".", access, access.toString(), call, call.toString() |
0 commit comments