|
| 1 | +/** |
| 2 | + * @name Find the wrong use of the umask function. |
| 3 | + * @description Incorrectly evaluated argument to the umask function may have security implications. |
| 4 | + * @kind problem |
| 5 | + * @id cpp/wrong-use-of-the-umask |
| 6 | + * @problem.severity warning |
| 7 | + * @precision medium |
| 8 | + * @tags correctness |
| 9 | + * maintainability |
| 10 | + * security |
| 11 | + * external/cwe/cwe-266 |
| 12 | + * external/cwe/cwe-264 |
| 13 | + * external/cwe/cwe-200 |
| 14 | + * external/cwe/cwe-560 |
| 15 | + * external/cwe/cwe-687 |
| 16 | + */ |
| 17 | + |
| 18 | +import cpp |
| 19 | +import semmle.code.cpp.exprs.BitwiseOperation |
| 20 | +import semmle.code.cpp.valuenumbering.GlobalValueNumbering |
| 21 | + |
| 22 | +/** Holds for a function `f` that has an argument at index `apos` used to set file permissions. */ |
| 23 | +predicate numberArgumentModFunctions(Function f, int apos) { |
| 24 | + f.hasGlobalOrStdName("umask") and apos = 0 |
| 25 | + or |
| 26 | + f.hasGlobalOrStdName("fchmod") and apos = 1 |
| 27 | + or |
| 28 | + f.hasGlobalOrStdName("chmod") and apos = 1 |
| 29 | +} |
| 30 | + |
| 31 | +from FunctionCall fc, string msg |
| 32 | +where |
| 33 | + fc.getTarget().hasGlobalOrStdName("umask") and |
| 34 | + fc.getArgument(0).getValue() = "0" and |
| 35 | + not exists(FunctionCall fctmp | |
| 36 | + fctmp.getTarget().hasGlobalOrStdName("umask") and |
| 37 | + globalValueNumber(fctmp.getArgument(0)) != globalValueNumber(fc.getArgument(0)) |
| 38 | + ) and |
| 39 | + exists(FunctionCall fctmp | |
| 40 | + ( |
| 41 | + fctmp.getTarget().hasGlobalOrStdName("fopen") or |
| 42 | + fctmp.getTarget().hasGlobalOrStdName("open") |
| 43 | + ) and |
| 44 | + fctmp.getNumberOfArguments() = 2 and |
| 45 | + fctmp.getArgument(0).getValue() != "/dev/null" |
| 46 | + ) and |
| 47 | + not exists(FunctionCall fctmp | |
| 48 | + fctmp.getTarget().hasGlobalOrStdName("chmod") or |
| 49 | + fctmp.getTarget().hasGlobalOrStdName("fchmod") |
| 50 | + ) and |
| 51 | + msg = "Using umask (0) may not be safe." |
| 52 | + or |
| 53 | + fc.getTarget().hasGlobalOrStdName("umask") and |
| 54 | + exists(FunctionCall fctmp | |
| 55 | + ( |
| 56 | + fctmp.getTarget().hasGlobalOrStdName("chmod") or |
| 57 | + fctmp.getTarget().hasGlobalOrStdName("fchmod") |
| 58 | + ) and |
| 59 | + ( |
| 60 | + globalValueNumber(fc.getArgument(0)) = globalValueNumber(fctmp.getArgument(1)) and |
| 61 | + fc.getArgument(0).getValue() != "0" |
| 62 | + ) and |
| 63 | + msg = "not use equal argument in umask and " + fctmp.getTarget().getName() + " functions" |
| 64 | + ) |
| 65 | + or |
| 66 | + exists(Expr exptmp, int i | |
| 67 | + numberArgumentModFunctions(fc.getTarget(), i) and |
| 68 | + not exptmp.getAChild*() instanceof FunctionCall and |
| 69 | + not exists(SizeofOperator so | exptmp.getAChild*() = so) and |
| 70 | + not exists(ArrayExpr aetmp | aetmp.getArrayOffset() = exptmp.getAChild*()) and |
| 71 | + exptmp.getAChild*() instanceof BinaryArithmeticOperation and |
| 72 | + not exptmp.getAChild*() instanceof BinaryBitwiseOperation and |
| 73 | + globalValueNumber(exptmp) = globalValueNumber(fc.getArgument(i)) and |
| 74 | + not exptmp.isConstant() and |
| 75 | + msg = "Using arithmetic to compute the mask may not be safe." |
| 76 | + ) |
| 77 | +select fc, msg |
0 commit comments