|
1 | 1 | /** |
2 | | - * @name TODO |
| 2 | + * @name Missing return-value check for a scanf-like function |
3 | 3 | * @description TODO |
4 | 4 | * @kind problem |
5 | 5 | * @problem.severity warning |
6 | 6 | * @security-severity TODO |
7 | 7 | * @precision TODO |
8 | 8 | * @id cpp/missing-check-scanf |
9 | | - * @tags TODO |
| 9 | + * @tags security |
10 | 10 | */ |
11 | 11 |
|
12 | 12 | import cpp |
13 | 13 | import semmle.code.cpp.commons.Scanf |
| 14 | +import semmle.code.cpp.controlflow.Guards |
| 15 | +import semmle.code.cpp.dataflow.DataFlow |
| 16 | +import semmle.code.cpp.ir.IR |
| 17 | +import semmle.code.cpp.ir.ValueNumbering |
14 | 18 |
|
15 | | -from ScanfFunction scanf, FunctionCall fc |
| 19 | +/** An expression appearing as an output argument to a `scanf`-like call */ |
| 20 | +class ScanfOutput extends Expr { |
| 21 | + ScanfFunctionCall call; |
| 22 | + int varargIndex; |
| 23 | + Instruction instr; |
| 24 | + ValueNumber valNum; |
| 25 | + |
| 26 | + ScanfOutput() { |
| 27 | + this = call.getArgument(call.getTarget().getNumberOfParameters() + varargIndex) and |
| 28 | + varargIndex >= 0 and |
| 29 | + instr.getUnconvertedResultExpression() = this and |
| 30 | + valueNumber(instr) = valNum and |
| 31 | + // The following line is a kludge to prohibit more than one associated `instr` field, |
| 32 | + // as would occur, for example, when `this` is an access to an array variable. |
| 33 | + not instr instanceof ConvertInstruction |
| 34 | + } |
| 35 | + |
| 36 | + ScanfFunctionCall getCall() { result = call } |
| 37 | + |
| 38 | + /** |
| 39 | + * Any subsequent use of this argument should be surrounded by a |
| 40 | + * check ensuring that the `scanf`-like function has returned a value |
| 41 | + * equal to at least `getMinimumGuardConstant()`. |
| 42 | + */ |
| 43 | + int getMinimumGuardConstant() { |
| 44 | + result = |
| 45 | + varargIndex + 1 - |
| 46 | + count(ScanfFormatLiteral f, int n | |
| 47 | + n <= varargIndex and f.getUse() = call and f.parseConvSpec(n, _, _, _, "n") |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + predicate hasGuardedAccess(Access e, boolean isGuarded) { |
| 52 | + e = this.getAnAccess() and |
| 53 | + if |
| 54 | + exists(int value, int minGuard | minGuard = this.getMinimumGuardConstant() | |
| 55 | + e.getBasicBlock() = blockGuardedBy(value, "==", call) and minGuard <= value |
| 56 | + or |
| 57 | + e.getBasicBlock() = blockGuardedBy(value, "<", call) and minGuard - 1 <= value |
| 58 | + or |
| 59 | + e.getBasicBlock() = blockGuardedBy(value, "<=", call) and minGuard <= value |
| 60 | + ) |
| 61 | + then isGuarded = true |
| 62 | + else isGuarded = false |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Get a subsequent access of the same underlying storage, |
| 67 | + * but before it gets reset or reused in another `scanf` call. |
| 68 | + */ |
| 69 | + Access getAnAccess() { |
| 70 | + exists(Instruction dst | |
| 71 | + this.bigStep(instr) = dst and |
| 72 | + dst.getAst() = result and |
| 73 | + valueNumber(dst) = valNum |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + private Instruction bigStep(Instruction i) { |
| 78 | + result = this.smallStep(i) |
| 79 | + or |
| 80 | + exists(Instruction j | j = this.bigStep(i) | result = this.smallStep(j)) |
| 81 | + } |
| 82 | + |
| 83 | + private Instruction smallStep(Instruction i) { |
| 84 | + instr.getASuccessor*() = i and |
| 85 | + i.getASuccessor() = result and |
| 86 | + not this.isBarrier(result) |
| 87 | + } |
| 88 | + |
| 89 | + private predicate isBarrier(Instruction i) { |
| 90 | + valueNumber(i) = valNum and |
| 91 | + exists(Expr e | i.getAst() = e | |
| 92 | + i = any(StoreInstruction s).getDestinationAddress() |
| 93 | + or |
| 94 | + [e, e.getParent().(AddressOfExpr)] instanceof ScanfOutput |
| 95 | + ) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/** Returns a block guarded by the assertion of $value $op $call */ |
| 100 | +BasicBlock blockGuardedBy(int value, string op, ScanfFunctionCall call) { |
| 101 | + exists(GuardCondition g, Expr left, Expr right | |
| 102 | + right = g.getAChild() and |
| 103 | + value = left.getValue().toInt() and |
| 104 | + DataFlow::localExprFlow(call, right) |
| 105 | + | |
| 106 | + g.ensuresEq(left, right, 0, result, true) and op = "==" |
| 107 | + or |
| 108 | + g.ensuresLt(left, right, 0, result, true) and op = "<" |
| 109 | + or |
| 110 | + g.ensuresLt(left, right, 1, result, true) and op = "<=" |
| 111 | + ) |
| 112 | +} |
| 113 | + |
| 114 | +from ScanfOutput output, ScanfFunctionCall call, ScanfFunction fun, Access access |
16 | 115 | where |
17 | | - fc.getTarget() = scanf and |
18 | | - fc instanceof ExprInVoidContext |
19 | | -select fc, "This is a call to scanf." |
| 116 | + call.getTarget() = fun and |
| 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 $@() returns at least " + output.getMinimumGuardConstant() |
| 122 | + + ".", access, access.toString(), call, fun.getName() |
0 commit comments