|
| 1 | +/** |
| 2 | + * Provides classes representing various flow sources for taint tracking. |
| 3 | + */ |
| 4 | + |
| 5 | +import cpp |
| 6 | +import semmle.code.cpp.ir.dataflow.DataFlow |
| 7 | +private import semmle.code.cpp.ir.IR |
| 8 | + |
| 9 | +/** A data flow source of remote user input. */ |
| 10 | +abstract class RemoteFlowSource extends DataFlow::Node { |
| 11 | + /** Gets a string that describes the type of this remote flow source. */ |
| 12 | + abstract string getSourceType(); |
| 13 | +} |
| 14 | + |
| 15 | +class FileDescriptorTaintedCallSource extends RemoteFlowSource { |
| 16 | + FileDescriptorTaintedCallSource() { |
| 17 | + asExpr().(Call).getTarget().hasGlobalName(["fgets", "gets"]) |
| 18 | + } |
| 19 | + |
| 20 | + override string getSourceType() { result = "Data read from a FILE* or file descriptor" } |
| 21 | +} |
| 22 | + |
| 23 | +class FileTaintedParameterSource extends RemoteFlowSource, DataFlow::DefinitionByReferenceNode { |
| 24 | + FileTaintedParameterSource() { |
| 25 | + exists(string fname, int arg | |
| 26 | + getParameter().getFunction().hasGlobalOrStdName(fname) and |
| 27 | + getParameter().getIndex() = arg |
| 28 | + | |
| 29 | + fname = "fread" and arg = 0 |
| 30 | + or |
| 31 | + fname = "fgets" and arg = 0 |
| 32 | + or |
| 33 | + fname = "fgetws" and arg = 0 |
| 34 | + or |
| 35 | + fname = "gets" and arg = 0 |
| 36 | + or |
| 37 | + fname = "scanf" and arg >= 1 |
| 38 | + or |
| 39 | + fname = "fscanf" and arg >= 2 |
| 40 | + ) |
| 41 | + or |
| 42 | + exists(string fname, int arg | |
| 43 | + getParameter().getFunction().hasGlobalOrStdName(fname) and |
| 44 | + getParameter().getIndex() = arg |
| 45 | + | |
| 46 | + fname = "read" and arg = 1 |
| 47 | + or |
| 48 | + fname = "getaddrinfo" and arg = 3 |
| 49 | + or |
| 50 | + fname = "recv" and arg = 1 |
| 51 | + or |
| 52 | + fname = "recvfrom" and |
| 53 | + (arg = 1 or arg = 4 or arg = 5) |
| 54 | + or |
| 55 | + fname = "recvmsg" and arg = 1 |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + override string getSourceType() { result = "Data read from a FILE* or file descriptor" } |
| 60 | +} |
0 commit comments