|
| 1 | +/** |
| 2 | + * Provides implementation classes modeling `sscanf`, `fscanf` and various similar |
| 3 | + * functions. See `semmle.code.cpp.models.Models` for usage information. |
| 4 | + */ |
| 5 | + |
| 6 | +import semmle.code.cpp.Function |
| 7 | +import semmle.code.cpp.commons.Scanf |
| 8 | +import semmle.code.cpp.models.interfaces.ArrayFunction |
| 9 | +import semmle.code.cpp.models.interfaces.Taint |
| 10 | +import semmle.code.cpp.models.interfaces.Alias |
| 11 | +import semmle.code.cpp.models.interfaces.SideEffect |
| 12 | + |
| 13 | +/** |
| 14 | + * The standard function `sscanf`, `fscanf` and its assorted variants |
| 15 | + */ |
| 16 | +private class SscanfModel extends ArrayFunction, TaintFunction, AliasFunction, SideEffectFunction { |
| 17 | + SscanfModel() { this instanceof Sscanf or this instanceof Fscanf or this instanceof Snscanf } |
| 18 | + |
| 19 | + override predicate hasArrayWithNullTerminator(int bufParam) { |
| 20 | + bufParam = this.(ScanfFunction).getFormatParameterIndex() |
| 21 | + or |
| 22 | + not this instanceof Fscanf and |
| 23 | + bufParam = this.(ScanfFunction).getInputParameterIndex() |
| 24 | + } |
| 25 | + |
| 26 | + override predicate hasArrayInput(int bufParam) { hasArrayWithNullTerminator(bufParam) } |
| 27 | + |
| 28 | + private int getLengthParameterIndex() { result = this.(Snscanf).getInputLengthParameterIndex() } |
| 29 | + |
| 30 | + private int getLocaleParameterIndex() { |
| 31 | + this.getName().matches("%\\_l") and |
| 32 | + ( |
| 33 | + if exists(getLengthParameterIndex()) |
| 34 | + then result = getLengthParameterIndex() + 2 |
| 35 | + else result = 2 |
| 36 | + ) |
| 37 | + } |
| 38 | + |
| 39 | + private int getArgsStartPosition() { result = this.getNumberOfParameters() } |
| 40 | + |
| 41 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 42 | + input.isParameterDeref(this.(ScanfFunction).getInputParameterIndex()) and |
| 43 | + output.isParameterDeref(any(int i | i >= getArgsStartPosition())) |
| 44 | + } |
| 45 | + |
| 46 | + override predicate parameterNeverEscapes(int index) { |
| 47 | + index = [0 .. max(getACallToThisFunction().getNumberOfArguments())] |
| 48 | + } |
| 49 | + |
| 50 | + override predicate parameterEscapesOnlyViaReturn(int index) { none() } |
| 51 | + |
| 52 | + override predicate parameterIsAlwaysReturned(int index) { none() } |
| 53 | + |
| 54 | + override predicate hasOnlySpecificReadSideEffects() { any() } |
| 55 | + |
| 56 | + override predicate hasOnlySpecificWriteSideEffects() { any() } |
| 57 | + |
| 58 | + override predicate hasSpecificWriteSideEffect(ParameterIndex i, boolean buffer, boolean mustWrite) { |
| 59 | + i >= getArgsStartPosition() and |
| 60 | + buffer = true and |
| 61 | + mustWrite = true |
| 62 | + } |
| 63 | + |
| 64 | + override predicate hasSpecificReadSideEffect(ParameterIndex i, boolean buffer) { |
| 65 | + buffer = true and |
| 66 | + i = |
| 67 | + [ |
| 68 | + this.(ScanfFunction).getInputParameterIndex(), |
| 69 | + this.(ScanfFunction).getFormatParameterIndex(), getLocaleParameterIndex() |
| 70 | + ] |
| 71 | + } |
| 72 | +} |
0 commit comments