|
| 1 | +/** |
| 2 | + * @name Checks for suspicious joins |
| 3 | + * @description This query checks for calls to `fd_*_join` where the argument does not match the expected type. |
| 4 | + * @precision high |
| 5 | + * @kind problem |
| 6 | + * @tags security, correctness |
| 7 | + * @id asymmetric-research/join-check |
| 8 | + * @problem.severity warning |
| 9 | + */ |
| 10 | + |
| 11 | +import cpp |
| 12 | + |
| 13 | +class FdJoinCall extends Call { |
| 14 | + FdJoinCall() { |
| 15 | + this.getTarget().getName().matches("fd_%_join") and |
| 16 | + this.getTarget().getParameter(0).getType().(PointerType).getBaseType() instanceof VoidType and |
| 17 | + not this.getType() instanceof VoidType |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +predicate isCompatible(DerivedType t, DerivedType expected) { |
| 22 | + t.getBaseType() = expected.getBaseType() |
| 23 | +} |
| 24 | + |
| 25 | +class RelevantType extends Type { |
| 26 | + RelevantType() { |
| 27 | + not this.(DerivedType).getBaseType() instanceof VoidType and |
| 28 | + not this.(DerivedType).getBaseType() instanceof CharType and |
| 29 | + not this instanceof IntegralType |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +from FdJoinCall call, Expr arg, Type t, Type expectedType |
| 34 | +where |
| 35 | + arg = call.getArgument(0) and |
| 36 | + t = arg.getType() and |
| 37 | + expectedType = call.getType() and |
| 38 | + not isCompatible(t, expectedType) and |
| 39 | + t.getUnspecifiedType() instanceof RelevantType |
| 40 | +select call, "The $@ to $@ has type $@, but the parameter type is $@.", arg, "argument", call, |
| 41 | + call.toString(), t, t.toString(), expectedType, expectedType.toString() |
0 commit comments