|
| 1 | +/** |
| 2 | + * @name Compiler Removal Of Code To Clear Buffers |
| 3 | + * @description --Using the memset function to clear private data as a final expression when working with a variable is potentially dangerous because the compiler can optimize this call. |
| 4 | + * --For some compilers, optimization is also possible when using calls to free memory after the memset function. |
| 5 | + * --To clear it, you need to use the RtlSecureZeroMemory or memset_s functions, or compilation flags that exclude optimization of memset calls (-fno-builtin-memset). |
| 6 | + * @kind problem |
| 7 | + * @id cpp/compiler-removal-of-code-to-clear-buffers |
| 8 | + * @problem.severity warning |
| 9 | + * @precision medium |
| 10 | + * @tags security |
| 11 | + * external/cwe/cwe-14 |
| 12 | + */ |
| 13 | + |
| 14 | +import cpp |
| 15 | +import semmle.code.cpp.dataflow.DataFlow |
| 16 | + |
| 17 | +/** |
| 18 | + * A call to `memset` , for some local variable. |
| 19 | + */ |
| 20 | +class CompilerRemovaMemset extends FunctionCall { |
| 21 | + CompilerRemovaMemset() { |
| 22 | + this.getTarget().hasName("memset") and |
| 23 | + exists(DataFlow::Node source, DataFlow::Node sink, LocalVariable isv, Expr exp | |
| 24 | + DataFlow::localFlow(source, sink) and |
| 25 | + this.getArgument(0) = isv.getAnAccess() and |
| 26 | + source.asExpr() = exp and |
| 27 | + exp.getLocation().getEndLine() < this.getArgument(0).getLocation().getStartLine() and |
| 28 | + sink.asExpr() = this.getArgument(0) |
| 29 | + ) |
| 30 | + } |
| 31 | + |
| 32 | + predicate isExistsAllocForThisVariable() { |
| 33 | + exists(FunctionCall alloc, Variable v | |
| 34 | + alloc = v.getAnAssignedValue() and |
| 35 | + this.getArgument(0) = v.getAnAccess() and |
| 36 | + alloc.getASuccessor+() = this |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + predicate isExistsFreeForThisVariable() { |
| 41 | + exists(FunctionCall free, Variable v | |
| 42 | + free instanceof DeallocationExpr and |
| 43 | + this.getArgument(0) = v.getAnAccess() and |
| 44 | + free.getArgument(0) = v.getAnAccess() and |
| 45 | + this.getASuccessor+() = free |
| 46 | + ) |
| 47 | + } |
| 48 | + |
| 49 | + predicate isExistsCallWithThisVariableExcludingDeallocationCalls() { |
| 50 | + exists(FunctionCall fc, Variable v | |
| 51 | + not fc instanceof DeallocationExpr and |
| 52 | + this.getArgument(0) = v.getAnAccess() and |
| 53 | + fc.getAnArgument() = v.getAnAccess() and |
| 54 | + this.getASuccessor+() = fc |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + predicate isVariableUseAfterMemsetExcludingCalls() { |
| 59 | + exists(DataFlow::Node source, DataFlow::Node sink, LocalVariable isv, Expr exp | |
| 60 | + DataFlow::localFlow(source, sink) and |
| 61 | + this.getArgument(0) = isv.getAnAccess() and |
| 62 | + source.asExpr() = isv.getAnAccess() and |
| 63 | + exp.getLocation().getStartLine() > this.getArgument(2).getLocation().getEndLine() and |
| 64 | + not exp.getParent() instanceof FunctionCall and |
| 65 | + sink.asExpr() = exp |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + predicate isVariableUseBoundWithArgumentFunction() { |
| 70 | + exists(DataFlow::Node source, DataFlow::Node sink, LocalVariable isv, Parameter p, Expr exp | |
| 71 | + DataFlow::localFlow(source, sink) and |
| 72 | + this.getArgument(0) = isv.getAnAccess() and |
| 73 | + this.getEnclosingFunction().getAParameter() = p and |
| 74 | + exp.getAChild*() = p.getAnAccess() and |
| 75 | + source.asExpr() = exp and |
| 76 | + sink.asExpr() = isv.getAnAccess() |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + predicate isVariableUseBoundWithGlobalVariable() { |
| 81 | + exists( |
| 82 | + DataFlow::Node source, DataFlow::Node sink, LocalVariable isv, GlobalVariable gv, Expr exp |
| 83 | + | |
| 84 | + DataFlow::localFlow(source, sink) and |
| 85 | + this.getArgument(0) = isv.getAnAccess() and |
| 86 | + exp.getAChild*() = gv.getAnAccess() and |
| 87 | + source.asExpr() = exp and |
| 88 | + sink.asExpr() = isv.getAnAccess() |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + predicate isExistsCompilationFlagsBlockingRemoval() { |
| 93 | + exists(Compilation c | |
| 94 | + c.getAFileCompiled() = this.getFile() and |
| 95 | + c.getAnArgument() = "-fno-builtin-memset" |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + predicate isUseVCCompilation() { |
| 100 | + exists(Compilation c | |
| 101 | + c.getAFileCompiled() = this.getFile() and |
| 102 | + ( |
| 103 | + c.getArgument(2).toString().matches("%gcc%") or |
| 104 | + c.getArgument(2).toString().matches("%g++%") or |
| 105 | + c.getArgument(2).toString().matches("%clang%") or |
| 106 | + c.getArgument(2).toString() = "--force-recompute" |
| 107 | + ) |
| 108 | + ) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +from CompilerRemovaMemset fc |
| 113 | +where |
| 114 | + not (fc.isExistsAllocForThisVariable() and not fc.isExistsFreeForThisVariable()) and |
| 115 | + not (fc.isExistsFreeForThisVariable() and not fc.isUseVCCompilation()) and |
| 116 | + not fc.isVariableUseAfterMemsetExcludingCalls() and |
| 117 | + not fc.isExistsCallWithThisVariableExcludingDeallocationCalls() and |
| 118 | + not fc.isVariableUseBoundWithArgumentFunction() and |
| 119 | + not fc.isVariableUseBoundWithGlobalVariable() and |
| 120 | + not fc.isExistsCompilationFlagsBlockingRemoval() |
| 121 | +select fc.getArgument(0), "this variable will not be cleared" |
0 commit comments