|
| 1 | +/** |
| 2 | + * @name Find work with changing working directories, with security errors. |
| 3 | + * @description Not validating the return value or pinning the directory can be unsafe. |
| 4 | + * @kind problem |
| 5 | + * @id cpp/work-with-changing-working-directories |
| 6 | + * @problem.severity warning |
| 7 | + * @precision medium |
| 8 | + * @tags correctness |
| 9 | + * security |
| 10 | + * external/cwe/cwe-243 |
| 11 | + * external/cwe/cwe-252 |
| 12 | + */ |
| 13 | + |
| 14 | +import cpp |
| 15 | + |
| 16 | +/** Holds if a `fc` function call is available before or before a `chdir` function call. */ |
| 17 | +predicate inExistsChdir(FunctionCall fcp) { |
| 18 | + exists(FunctionCall fctmp | |
| 19 | + ( |
| 20 | + fctmp.getTarget().hasGlobalOrStdName("chdir") or |
| 21 | + fctmp.getTarget().hasGlobalOrStdName("fchdir") |
| 22 | + ) and |
| 23 | + ( |
| 24 | + fctmp.getASuccessor*() = fcp or |
| 25 | + fcp.getASuccessor*() = fctmp |
| 26 | + ) |
| 27 | + ) |
| 28 | +} |
| 29 | + |
| 30 | +/** Holds if a `fc` function call is available before or before a function call containing a `chdir` call. */ |
| 31 | +predicate outExistsChdir(FunctionCall fcp) { |
| 32 | + exists(FunctionCall fctmp | |
| 33 | + exists(FunctionCall fctmp2 | |
| 34 | + ( |
| 35 | + fctmp2.getTarget().hasGlobalOrStdName("chdir") or |
| 36 | + fctmp2.getTarget().hasGlobalOrStdName("fchdir") |
| 37 | + ) and |
| 38 | + fctmp2.getEnclosingStmt().getParentStmt*() = fctmp.getTarget().getEntryPoint().getChildStmt*() |
| 39 | + ) and |
| 40 | + ( |
| 41 | + fctmp.getASuccessor*() = fcp or |
| 42 | + fcp.getASuccessor*() = fctmp |
| 43 | + ) |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +from FunctionCall fc, string msg |
| 48 | +where |
| 49 | + fc.getTarget().hasGlobalOrStdName("chroot") and |
| 50 | + not inExistsChdir(fc) and |
| 51 | + not outExistsChdir(fc) and |
| 52 | + exists(FunctionCall fctmp | |
| 53 | + fc.getEnclosingStmt().getParentStmt*() = fctmp.getTarget().getEntryPoint().getChildStmt*() and |
| 54 | + not inExistsChdir(fctmp) and |
| 55 | + not outExistsChdir(fctmp) |
| 56 | + ) and |
| 57 | + msg = "Creation of chroot Jail Without Changing Working Directory out" |
| 58 | + or |
| 59 | + ( |
| 60 | + fc.getTarget().hasGlobalOrStdName("chdir") or |
| 61 | + fc.getTarget().hasGlobalOrStdName("fchdir") |
| 62 | + ) and |
| 63 | + not exists(ConditionalStmt cotmp | cotmp.getControllingExpr().getAChild*() = fc) and |
| 64 | + not exists(Loop lptmp | lptmp.getCondition().getAChild*() = fc) and |
| 65 | + not exists(ReturnStmt rttmp | rttmp.getExpr().getAChild*() = fc) and |
| 66 | + not exists(Assignment astmp | astmp.getAChild*() = fc) and |
| 67 | + not exists(Initializer ittmp | ittmp.getExpr().getAChild*() = fc) and |
| 68 | + not fc.isInMacroExpansion() and |
| 69 | + msg = fc.getTarget().getName() + " unchecked return value." |
| 70 | +select fc, msg |
0 commit comments