|
| 1 | +/** |
| 2 | + * @name Late Check Of Function Argument |
| 3 | + * @description --Checking the function argument after calling the function itself. |
| 4 | + * --This situation looks suspicious and requires the attention of the developer. |
| 5 | + * --It may be necessary to add validation before calling the function. |
| 6 | + * @kind problem |
| 7 | + * @id cpp/late-check-of-function-argument |
| 8 | + * @problem.severity warning |
| 9 | + * @precision medium |
| 10 | + * @tags correctness |
| 11 | + * security |
| 12 | + * external/cwe/cwe-20 |
| 13 | + */ |
| 14 | + |
| 15 | +import cpp |
| 16 | +import semmle.code.cpp.valuenumbering.GlobalValueNumbering |
| 17 | + |
| 18 | +predicate numberArgument(Function f, int size) { |
| 19 | + f.hasGlobalOrStdName("write") and size = 2 |
| 20 | + or |
| 21 | + f.hasGlobalOrStdName("read") and size = 2 |
| 22 | + or |
| 23 | + f.hasGlobalOrStdName("lseek") and size = 1 |
| 24 | + or |
| 25 | + f.hasGlobalOrStdName("memmove") and size = 2 |
| 26 | + or |
| 27 | + f.hasGlobalOrStdName("memset") and size = 2 |
| 28 | + or |
| 29 | + f.hasGlobalOrStdName("memcpy") and size = 2 |
| 30 | + or |
| 31 | + f.hasGlobalOrStdName("memcmp") and size = 2 |
| 32 | + or |
| 33 | + f.hasGlobalOrStdName("strncat") and size = 2 |
| 34 | + or |
| 35 | + f.hasGlobalOrStdName("strncpy") and size = 2 |
| 36 | + or |
| 37 | + f.hasGlobalOrStdName("strncmp") and size = 2 |
| 38 | + or |
| 39 | + f.hasGlobalOrStdName("snprintf") and size = 1 |
| 40 | + or |
| 41 | + f.hasGlobalOrStdName("strndup") and size = 2 |
| 42 | + or |
| 43 | + f.hasGlobalOrStdName("read") and size = 2 |
| 44 | +} |
| 45 | + |
| 46 | +class IfCompareWithZero extends IfStmt { |
| 47 | + IfCompareWithZero() { this.getCondition().(RelationalOperation).getAChild().getValue() = "0" } |
| 48 | + |
| 49 | + Expr noZerroOperand() { |
| 50 | + if this.getCondition().(RelationalOperation).getGreaterOperand().getValue() = "0" |
| 51 | + then result = this.getCondition().(RelationalOperation).getLesserOperand() |
| 52 | + else result = this.getCondition().(RelationalOperation).getGreaterOperand() |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +from FunctionCall fc, IfCompareWithZero ifc, int na |
| 57 | +where |
| 58 | + numberArgument(fc.getTarget(), na) and |
| 59 | + na >= 0 and |
| 60 | + globalValueNumber(fc.getArgument(na)) = globalValueNumber(ifc.noZerroOperand()) and |
| 61 | + dominates(fc, ifc) and |
| 62 | + not exists(IfStmt ifc1 | |
| 63 | + dominates(ifc1, fc) and |
| 64 | + globalValueNumber(fc.getArgument(na)) = globalValueNumber(ifc1.getCondition().getAChild*()) |
| 65 | + ) |
| 66 | +select fc, "Argument '$@' will be checked later.", fc.getArgument(na), fc.getArgument(na).toString() |
0 commit comments