|
| 1 | +/** |
| 2 | + * Provides implementation classes modeling `recv` and various similar |
| 3 | + * functions. See `semmle.code.cpp.models.Models` for usage information. |
| 4 | + */ |
| 5 | + |
| 6 | +import semmle.code.cpp.models.interfaces.Taint |
| 7 | +import semmle.code.cpp.models.interfaces.ArrayFunction |
| 8 | +import semmle.code.cpp.models.interfaces.Alias |
| 9 | +import semmle.code.cpp.models.interfaces.FlowSource |
| 10 | +import semmle.code.cpp.models.interfaces.SideEffect |
| 11 | + |
| 12 | +/** The function `recv` and its assorted variants */ |
| 13 | +private class Recv extends AliasFunction, ArrayFunction, SideEffectFunction, RemoteFlowFunction { |
| 14 | + Recv() { |
| 15 | + this.hasGlobalName([ |
| 16 | + "recv", // recv(socket, dest, len, flags) |
| 17 | + "recvfrom", // recvfrom(socket, dest, len, flags, from, fromlen) |
| 18 | + "read", // read(socket, dest, len) |
| 19 | + "pread" // pread(socket, dest, len, offset) |
| 20 | + ]) |
| 21 | + } |
| 22 | + |
| 23 | + override predicate parameterNeverEscapes(int index) { |
| 24 | + this.getParameter(index).getUnspecifiedType() instanceof PointerType |
| 25 | + } |
| 26 | + |
| 27 | + override predicate parameterEscapesOnlyViaReturn(int index) { none() } |
| 28 | + |
| 29 | + override predicate parameterIsAlwaysReturned(int index) { none() } |
| 30 | + |
| 31 | + override predicate hasArrayWithVariableSize(int bufParam, int countParam) { |
| 32 | + bufParam = 1 and countParam = 2 |
| 33 | + } |
| 34 | + |
| 35 | + override predicate hasArrayInput(int bufParam) { this.hasGlobalName("recvfrom") and bufParam = 4 } |
| 36 | + |
| 37 | + override predicate hasArrayOutput(int bufParam) { |
| 38 | + bufParam = 1 |
| 39 | + or |
| 40 | + this.hasGlobalName("recvfrom") and bufParam = 4 |
| 41 | + } |
| 42 | + |
| 43 | + override predicate hasSpecificReadSideEffect(ParameterIndex i, boolean buffer) { |
| 44 | + this.hasGlobalName("recvfrom") and |
| 45 | + ( |
| 46 | + i = 4 and buffer = true |
| 47 | + or |
| 48 | + i = 5 and buffer = false |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + override ParameterIndex getParameterSizeIndex(ParameterIndex i) { i = 1 and result = 2 } |
| 53 | + |
| 54 | + override predicate hasSpecificWriteSideEffect(ParameterIndex i, boolean buffer, boolean mustWrite) { |
| 55 | + i = 1 and buffer = true and mustWrite = false |
| 56 | + or |
| 57 | + this.hasGlobalName("recvfrom") and |
| 58 | + ( |
| 59 | + i = 4 and buffer = true and mustWrite = false |
| 60 | + or |
| 61 | + i = 5 and buffer = false and mustWrite = false |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + override predicate hasOnlySpecificReadSideEffects() { any() } |
| 66 | + |
| 67 | + override predicate hasOnlySpecificWriteSideEffects() { any() } |
| 68 | + |
| 69 | + override predicate hasRemoteFlowSource(FunctionOutput output, string description) { |
| 70 | + output.isParameterDeref(1) and |
| 71 | + description = "Buffer read by " + this.getName() |
| 72 | + } |
| 73 | +} |
0 commit comments