|
| 1 | +/** |
| 2 | + * @id c/misra/memcmp-used-to-compare-null-terminated-strings |
| 3 | + * @name RULE-21-14: The Standard Library function memcmp shall not be used to compare null terminated strings |
| 4 | + * @description Using memcmp to compare null terminated strings may give unexpected results because |
| 5 | + * memcmp compares by size with no consideration for the null terminator. |
| 6 | + * @kind path-problem |
| 7 | + * @precision very-high |
| 8 | + * @problem.severity error |
| 9 | + * @tags external/misra/id/rule-21-14 |
| 10 | + * maintainability |
| 11 | + * correctness |
| 12 | + * external/misra/obligation/required |
| 13 | + */ |
| 14 | + |
| 15 | +import cpp |
| 16 | +import codingstandards.c.misra |
| 17 | +import codingstandards.c.misra.EssentialTypes |
| 18 | +import semmle.code.cpp.dataflow.TaintTracking |
| 19 | +import DataFlow::PathGraph |
| 20 | + |
| 21 | +// Data flow from a StringLiteral or from an array of characters, to a memcmp call |
| 22 | +class NullTerminatedStringToMemcmpConfiguration extends TaintTracking::Configuration { |
| 23 | + NullTerminatedStringToMemcmpConfiguration() { this = "NullTerminatedStringToMemcmpConfiguration" } |
| 24 | + |
| 25 | + override predicate isSource(DataFlow::Node source) { |
| 26 | + source.asExpr() instanceof StringLiteral |
| 27 | + or |
| 28 | + exists(Variable v, ArrayAggregateLiteral aal | |
| 29 | + aal = v.getInitializer().getExpr() and |
| 30 | + // The array element type is an essentially character type |
| 31 | + getEssentialTypeCategory(aal.getElementType()) = EssentiallyCharacterType() and |
| 32 | + // Includes a null terminator somewhere in the array initializer |
| 33 | + aal.getElementExpr(_).getValue().toInt() = 0 |
| 34 | + | |
| 35 | + // For local variables, use the array aggregate literal as the source |
| 36 | + aal = source.asExpr() |
| 37 | + or |
| 38 | + // ArrayAggregateLiterals used as initializers for global variables are not viable sources |
| 39 | + // for global data flow, so we instead report variable accesses as sources, where the variable |
| 40 | + // is constant or is not assigned in the program |
| 41 | + v instanceof GlobalVariable and |
| 42 | + source.asExpr() = v.getAnAccess() and |
| 43 | + ( |
| 44 | + v.isConst() |
| 45 | + or |
| 46 | + not exists(Expr e | e = v.getAnAssignedValue() and not e = aal) |
| 47 | + ) |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + override predicate isSink(DataFlow::Node sink) { |
| 52 | + exists(FunctionCall memcmp | |
| 53 | + memcmp.getTarget().hasGlobalOrStdName("memcmp") and |
| 54 | + sink.asExpr() = memcmp.getArgument([0, 1]) |
| 55 | + ) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +from |
| 60 | + FunctionCall memcmp, DataFlow::PathNode source, DataFlow::PathNode sink, |
| 61 | + DataFlow::PathNode source1, DataFlow::PathNode arg1, DataFlow::PathNode source2, |
| 62 | + DataFlow::PathNode arg2 |
| 63 | +where |
| 64 | + not isExcluded(memcmp, EssentialTypesPackage::memcmpUsedToCompareNullTerminatedStringsQuery()) and |
| 65 | + memcmp.getTarget().hasGlobalOrStdName("memcmp") and |
| 66 | + arg1.getNode().asExpr() = memcmp.getArgument(0) and |
| 67 | + arg2.getNode().asExpr() = memcmp.getArgument(1) and |
| 68 | + // There is a path from a null-terminated string to each argument |
| 69 | + exists(NullTerminatedStringToMemcmpConfiguration cfg | |
| 70 | + cfg.hasFlowPath(source1, arg1) and |
| 71 | + cfg.hasFlowPath(source2, arg2) |
| 72 | + ) and |
| 73 | + // Produce multiple paths for each result, one for each source/arg pair |
| 74 | + ( |
| 75 | + source = source1 and sink = arg1 |
| 76 | + or |
| 77 | + source = source2 and sink = arg2 |
| 78 | + ) |
| 79 | +select memcmp, source, sink, "memcmp used to compare $@ with $@.", source1, |
| 80 | + "null-terminated string", source2, "null-terminated string" |
0 commit comments