|
| 1 | +import semmle.code.cpp.models.interfaces.DataFlow |
| 2 | +import semmle.code.cpp.models.interfaces.Taint |
| 3 | +import semmle.code.cpp.models.interfaces.ArrayFunction |
| 4 | +import semmle.code.cpp.models.interfaces.Alias |
| 5 | +import semmle.code.cpp.models.interfaces.SideEffect |
| 6 | + |
| 7 | +/** |
| 8 | + * The standard functions `gets` and `fgets`. |
| 9 | + */ |
| 10 | +class GetsFunction extends DataFlowFunction, TaintFunction, ArrayFunction, AliasFunction, |
| 11 | + SideEffectFunction { |
| 12 | + GetsFunction() { |
| 13 | + exists(string name | hasGlobalOrStdName(name) | |
| 14 | + name = "gets" or // gets(str) |
| 15 | + name = "fgets" or // fgets(str, num, stream) |
| 16 | + name = "fgetws" // fgetws(wstr, num, stream) |
| 17 | + ) |
| 18 | + } |
| 19 | + |
| 20 | + override predicate hasDataFlow(FunctionInput input, FunctionOutput output) { |
| 21 | + input.isParameter(0) and |
| 22 | + output.isReturnValue() |
| 23 | + } |
| 24 | + |
| 25 | + override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) { |
| 26 | + input.isParameter(2) and |
| 27 | + output.isParameterDeref(0) |
| 28 | + } |
| 29 | + |
| 30 | + override predicate parameterNeverEscapes(int index) { index = 2 } |
| 31 | + |
| 32 | + override predicate parameterEscapesOnlyViaReturn(int index) { index = 0 } |
| 33 | + |
| 34 | + override predicate parameterIsAlwaysReturned(int index) { index = 0 } |
| 35 | + |
| 36 | + override predicate hasOnlySpecificReadSideEffects() { any() } |
| 37 | + |
| 38 | + override predicate hasOnlySpecificWriteSideEffects() { any() } |
| 39 | + |
| 40 | + override predicate hasSpecificWriteSideEffect(ParameterIndex i, boolean buffer, boolean mustWrite) { |
| 41 | + i = 0 and |
| 42 | + buffer = true and |
| 43 | + mustWrite = true |
| 44 | + } |
| 45 | +} |
0 commit comments