|
| 1 | +/** |
| 2 | + * @name Unsafe use of this in constructor |
| 3 | + * @description A call to a pure virtual function using a 'this' |
| 4 | + * pointer of an object that is under construction |
| 5 | + * may lead to undefined behavior. |
| 6 | + * @kind path-problem |
| 7 | + * @id cpp/unsafe-use-of-this |
| 8 | + * @problem.severity error |
| 9 | + * @precision very-high |
| 10 | + * @tags correctness |
| 11 | + * language-features |
| 12 | + * security |
| 13 | + */ |
| 14 | + |
| 15 | +import cpp |
| 16 | +// We don't actually use the global value numbering library in this query, but without it we end up |
| 17 | +// recomputing the IR. |
| 18 | +private import semmle.code.cpp.valuenumbering.GlobalValueNumbering |
| 19 | +private import semmle.code.cpp.ir.IR |
| 20 | + |
| 21 | +bindingset[n, result] |
| 22 | +int unbind(int n) { result >= n and result <= n } |
| 23 | + |
| 24 | +/** Holds if `p` is the `n`'th parameter of the non-virtual function `f`. */ |
| 25 | +predicate parameterOf(Parameter p, Function f, int n) { |
| 26 | + not f.isVirtual() and f.getParameter(n) = p |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Holds if `instr` is the `n`'th argument to a call to the non-virtual function `f`, and |
| 31 | + * `init` is the corresponding initiazation instruction that receives the value of `instr` in `f`. |
| 32 | + */ |
| 33 | +predicate flowIntoParameter( |
| 34 | + CallInstruction call, Instruction instr, Function f, int n, InitializeParameterInstruction init |
| 35 | +) { |
| 36 | + not f.isVirtual() and |
| 37 | + call.getPositionalArgument(n) = instr and |
| 38 | + f = call.getStaticCallTarget() and |
| 39 | + getEnclosingNonVirtualFunctionInitializeParameter(init, f) and |
| 40 | + init.getParameter().getIndex() = unbind(n) |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Holds if `instr` is an argument to a call to the function `f`, and `init` is the |
| 45 | + * corresponding initialization instruction that receives the value of `instr` in `f`. |
| 46 | + */ |
| 47 | +pragma[noinline] |
| 48 | +predicate getPositionalArgumentInitParam( |
| 49 | + CallInstruction call, Instruction instr, InitializeParameterInstruction init, Function f |
| 50 | +) { |
| 51 | + exists(int n | |
| 52 | + parameterOf(_, f, n) and |
| 53 | + flowIntoParameter(call, instr, f, unbind(n), init) |
| 54 | + ) |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Holds if `instr` is the qualifier to a call to the non-virtual function `f`, and |
| 59 | + * `init` is the corresponding initiazation instruction that receives the value of |
| 60 | + * `instr` in `f`. |
| 61 | + */ |
| 62 | +pragma[noinline] |
| 63 | +predicate getThisArgumentInitParam( |
| 64 | + CallInstruction call, Instruction instr, InitializeParameterInstruction init, Function f |
| 65 | +) { |
| 66 | + not f.isVirtual() and |
| 67 | + call.getStaticCallTarget() = f and |
| 68 | + getEnclosingNonVirtualFunctionInitializeParameter(init, f) and |
| 69 | + call.getThisArgument() = instr and |
| 70 | + init.getIRVariable() instanceof IRThisVariable |
| 71 | +} |
| 72 | + |
| 73 | +/** Holds if `instr` is a `this` pointer used by the call instruction `call`. */ |
| 74 | +predicate isSink(Instruction instr, CallInstruction call) { |
| 75 | + exists(PureVirtualFunction func | |
| 76 | + call.getStaticCallTarget() = func and |
| 77 | + call.getThisArgument() = instr and |
| 78 | + // Weed out implicit calls to destructors of a base class |
| 79 | + not func instanceof Destructor |
| 80 | + ) |
| 81 | +} |
| 82 | + |
| 83 | +/** Holds if `init` initializes the `this` pointer in class `c`. */ |
| 84 | +predicate isSource(InitializeParameterInstruction init, string msg, Class c) { |
| 85 | + ( |
| 86 | + exists(Constructor func | |
| 87 | + not func instanceof CopyConstructor and |
| 88 | + not func instanceof MoveConstructor and |
| 89 | + func = init.getEnclosingFunction() and |
| 90 | + msg = "construction" |
| 91 | + ) |
| 92 | + or |
| 93 | + init.getEnclosingFunction() instanceof Destructor and msg = "destruction" |
| 94 | + ) and |
| 95 | + init.getIRVariable() instanceof IRThisVariable and |
| 96 | + init.getEnclosingFunction().getDeclaringType() = c |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Holds if `instr` flows to a sink (which is a use of the value of `instr` as a `this` pointer). |
| 101 | + */ |
| 102 | +predicate flowsToSink(Instruction instr, Instruction sink) { |
| 103 | + flowsFromSource(instr) and |
| 104 | + ( |
| 105 | + isSink(instr, _) and instr = sink |
| 106 | + or |
| 107 | + exists(Instruction mid | |
| 108 | + successor(instr, mid) and |
| 109 | + flowsToSink(mid, sink) |
| 110 | + ) |
| 111 | + ) |
| 112 | +} |
| 113 | + |
| 114 | +/** Holds if `instr` flows from a source. */ |
| 115 | +predicate flowsFromSource(Instruction instr) { |
| 116 | + isSource(instr, _, _) |
| 117 | + or |
| 118 | + exists(Instruction mid | |
| 119 | + successor(mid, instr) and |
| 120 | + flowsFromSource(mid) |
| 121 | + ) |
| 122 | +} |
| 123 | + |
| 124 | +/** Holds if `f` is the enclosing non-virtual function of `init`. */ |
| 125 | +predicate getEnclosingNonVirtualFunctionInitializeParameter( |
| 126 | + InitializeParameterInstruction init, Function f |
| 127 | +) { |
| 128 | + not f.isVirtual() and |
| 129 | + init.getEnclosingFunction() = f |
| 130 | +} |
| 131 | + |
| 132 | +/** Holds if `f` is the enclosing non-virtual function of `init`. */ |
| 133 | +predicate getEnclosingNonVirtualFunctionInitializeIndirection( |
| 134 | + InitializeIndirectionInstruction init, Function f |
| 135 | +) { |
| 136 | + not f.isVirtual() and |
| 137 | + init.getEnclosingFunction() = f |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * Holds if `instr` is an argument (or argument indirection) to a call, and |
| 142 | + * `succ` is the corresponding initialization instruction in the call target. |
| 143 | + */ |
| 144 | +predicate flowThroughCallable(Instruction instr, Instruction succ) { |
| 145 | + // Flow from an argument to a parameter |
| 146 | + exists(CallInstruction call, InitializeParameterInstruction init | init = succ | |
| 147 | + getPositionalArgumentInitParam(call, instr, init, call.getStaticCallTarget()) |
| 148 | + or |
| 149 | + getThisArgumentInitParam(call, instr, init, call.getStaticCallTarget()) |
| 150 | + ) |
| 151 | + or |
| 152 | + // Flow from argument indirection to parameter indirection |
| 153 | + exists( |
| 154 | + CallInstruction call, ReadSideEffectInstruction read, InitializeIndirectionInstruction init |
| 155 | + | |
| 156 | + init = succ and |
| 157 | + read.getPrimaryInstruction() = call and |
| 158 | + getEnclosingNonVirtualFunctionInitializeIndirection(init, call.getStaticCallTarget()) |
| 159 | + | |
| 160 | + exists(int n | |
| 161 | + read.getSideEffectOperand().getAnyDef() = instr and |
| 162 | + read.getIndex() = n and |
| 163 | + init.getParameter().getIndex() = unbind(n) |
| 164 | + ) |
| 165 | + or |
| 166 | + call.getThisArgument() = instr and |
| 167 | + init.getIRVariable() instanceof IRThisVariable |
| 168 | + ) |
| 169 | +} |
| 170 | + |
| 171 | +/** Holds if `instr` flows to `succ`. */ |
| 172 | +predicate successor(Instruction instr, Instruction succ) { |
| 173 | + succ.(CopyInstruction).getSourceValue() = instr or |
| 174 | + succ.(CheckedConvertOrNullInstruction).getUnary() = instr or |
| 175 | + succ.(ChiInstruction).getTotal() = instr or |
| 176 | + succ.(ConvertInstruction).getUnary() = instr or |
| 177 | + succ.(InheritanceConversionInstruction).getUnary() = instr or |
| 178 | + flowThroughCallable(instr, succ) |
| 179 | +} |
| 180 | + |
| 181 | +/** |
| 182 | + * Holds if: |
| 183 | + * - `source` is an initialization of a `this` pointer of type `sourceClass`, and |
| 184 | + * - `sink` is a use of the `this` pointer, and |
| 185 | + * - `call` invokes a pure virtual function using `sink` as the `this` pointer, and |
| 186 | + * - `msg` is a string describing whether `source` is from a constructor or destructor. |
| 187 | + */ |
| 188 | +predicate flows( |
| 189 | + Instruction source, string msg, Class sourceClass, Instruction sink, CallInstruction call |
| 190 | +) { |
| 191 | + isSource(source, msg, sourceClass) and |
| 192 | + flowsToSink(source, sink) and |
| 193 | + isSink(sink, call) |
| 194 | +} |
| 195 | + |
| 196 | +query predicate edges(Instruction a, Instruction b) { successor(a, b) and flowsToSink(b, _) } |
| 197 | + |
| 198 | +query predicate nodes(Instruction n, string key, string val) { |
| 199 | + flowsToSink(n, _) and |
| 200 | + key = "semmle.label" and |
| 201 | + val = n.toString() |
| 202 | +} |
| 203 | + |
| 204 | +from Instruction source, Instruction sink, CallInstruction call, string msg, Class sourceClass |
| 205 | +where |
| 206 | + flows(source, msg, sourceClass, sink, call) and |
| 207 | + // Only raise an alert if there is no override of the pure virtual function in any base class. |
| 208 | + not exists(Class c | c = sourceClass.getABaseClass*() | |
| 209 | + c.getAMemberFunction().getAnOverriddenFunction() = call.getStaticCallTarget() |
| 210 | + ) |
| 211 | +select call.getUnconvertedResultExpression(), source, sink, |
| 212 | + "Call to pure virtual function during " + msg |
0 commit comments