|
| 1 | +import cpp |
| 2 | +private import semmle.code.cpp.models.Models |
| 3 | +private import semmle.code.cpp.models.interfaces.FormattingFunction |
| 4 | + |
| 5 | +/** |
| 6 | + * Holds if a StringLiteral could be an algorithm literal. |
| 7 | + * Note: this predicate should only consider restrictions with respect to strings only. |
| 8 | + * General restrictions are in the OpenSSLAlgorithmCandidateLiteral class. |
| 9 | + */ |
| 10 | +private predicate isOpenSSLStringLiteralAlgorithmCandidate(StringLiteral s) { |
| 11 | + // 'EC' is a constant that may be used where typical algorithms are specified, |
| 12 | + // but EC specifically means set up a default curve container, that will later be |
| 13 | + //specified explicitly (or if not a default) curve is used. |
| 14 | + s.getValue() != "EC" and |
| 15 | + // Ignore empty strings |
| 16 | + s.getValue() != "" and |
| 17 | + // ignore strings that represent integers, it is possible this could be used for actual |
| 18 | + // algorithms but assuming this is not the common case |
| 19 | + // NOTE: if we were to revert this restriction, we should still consider filtering "0" |
| 20 | + // to be consistent with filtering integer 0 |
| 21 | + not exists(s.getValue().toInt()) and |
| 22 | + // Filter out strings with "%", to filter out format strings |
| 23 | + not s.getValue().matches("%\\%%") and |
| 24 | + // Filter out strings in brackets or braces |
| 25 | + not s.getValue().matches(["[%]", "(%)"]) and |
| 26 | + // Filter out all strings of length 1, since these are not algorithm names |
| 27 | + s.getValue().length() > 1 and |
| 28 | + // Ignore all strings that are in format string calls outputing to a stream (e.g., stdout) |
| 29 | + not exists(FormattingFunctionCall f | |
| 30 | + exists(f.getOutputArgument(true)) and s = f.(Call).getAnArgument() |
| 31 | + ) and |
| 32 | + // Ignore all format string calls where there is no known out param (resulting string) |
| 33 | + // i.e., ignore printf, since it will just ouput a string and not produce a new string |
| 34 | + not exists(FormattingFunctionCall f | |
| 35 | + // Note: using two ways of determining if there is an out param, since I'm not sure |
| 36 | + // which way is canonical |
| 37 | + not exists(f.getOutputArgument(false)) and |
| 38 | + not f.getTarget().(FormattingFunction).hasTaintFlow(_, _) and |
| 39 | + f.(Call).getAnArgument() = s |
| 40 | + ) |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Holds if an IntLiteral could be an algorithm literal. |
| 45 | + * Note: this predicate should only consider restrictions with respect to integers only. |
| 46 | + * General restrictions are in the OpenSSLAlgorithmCandidateLiteral class. |
| 47 | + */ |
| 48 | +private predicate isOpenSSLIntLiteralAlgorithmCandidate(Literal l) { |
| 49 | + exists(l.getValue().toInt()) and |
| 50 | + // Ignore char literals |
| 51 | + not l instanceof CharLiteral and |
| 52 | + not l instanceof StringLiteral and |
| 53 | + // Ignore integer values of 0, commonly referring to NULL only (no known algorithm 0) |
| 54 | + // Also ignore integer values greater than 5000 |
| 55 | + l.getValue().toInt() != 0 and |
| 56 | + // ASSUMPTION, no negative numbers are allowed |
| 57 | + // RATIONALE: this is a performance improvement to avoid having to trace every number |
| 58 | + not exists(UnaryMinusExpr u | u.getOperand() = l) and |
| 59 | + // OPENSSL has a special macro for getting every line, ignore it |
| 60 | + not exists(MacroInvocation mi | mi.getExpr() = l and mi.getMacroName() = "OPENSSL_LINE") and |
| 61 | + // Filter out cases where an int is returned into a pointer, e.g., return NULL; |
| 62 | + not exists(ReturnStmt r | |
| 63 | + r.getExpr() = l and |
| 64 | + r.getEnclosingFunction().getType().getUnspecifiedType() instanceof DerivedType |
| 65 | + ) and |
| 66 | + // A literal as an array index should never be an algorithm |
| 67 | + not exists(ArrayExpr op | op.getArrayOffset() = l) and |
| 68 | + // A literal used in a bitwise operation may be an algorithm, but not a candidate |
| 69 | + // for the purposes of finding applied algorithms |
| 70 | + not exists(BinaryBitwiseOperation op | op.getAnOperand() = l) and |
| 71 | + not exists(AssignBitwiseOperation op | op.getAnOperand() = l) and |
| 72 | + //Filter out cases where an int is assigned or initialized into a pointer, e.g., char* x = NULL; |
| 73 | + not exists(Assignment a | |
| 74 | + a.getRValue() = l and |
| 75 | + a.getLValue().getType().getUnspecifiedType() instanceof DerivedType |
| 76 | + ) and |
| 77 | + not exists(Initializer i | |
| 78 | + i.getExpr() = l and |
| 79 | + i.getDeclaration().getADeclarationEntry().getUnspecifiedType() instanceof DerivedType |
| 80 | + ) and |
| 81 | + // Filter out cases where the literal is used in any kind of arithmetic operation |
| 82 | + not exists(BinaryArithmeticOperation op | op.getAnOperand() = l) and |
| 83 | + not exists(UnaryArithmeticOperation op | op.getOperand() = l) and |
| 84 | + not exists(AssignArithmeticOperation op | op.getAnOperand() = l) |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Any literal that may represent an algorithm for use in an operation, even if an invalid or unknown algorithm. |
| 89 | + * The set of all literals is restricted by this class to cases where there is higher |
| 90 | + * plausibility that the literal is eventually used as an algorithm. |
| 91 | + * Literals are filtered, for example if they are used in a way no indicative of an algorithm use |
| 92 | + * such as in an array index, bitwise operation, or logical operation. |
| 93 | + * Note a case like this: |
| 94 | + * if(algVal == "AES") |
| 95 | + * |
| 96 | + * "AES" may be a legitimate algorithm literal, but the literal will not be used for an operation directly |
| 97 | + * since it is in a equality comparison, hence this case would also be filtered. |
| 98 | + */ |
| 99 | +class OpenSSLAlgorithmCandidateLiteral extends Literal { |
| 100 | + OpenSSLAlgorithmCandidateLiteral() { |
| 101 | + ( |
| 102 | + isOpenSSLIntLiteralAlgorithmCandidate(this) or |
| 103 | + isOpenSSLStringLiteralAlgorithmCandidate(this) |
| 104 | + ) and |
| 105 | + // ********* General filters beyond what is filtered for strings and ints ********* |
| 106 | + // An algorithm literal in a switch case will not be directly applied to an operation. |
| 107 | + not exists(SwitchCase sc | sc.getExpr() = this) and |
| 108 | + // A literal in a logical operation may be an algorithm, but not a candidate |
| 109 | + // for the purposes of finding applied algorithms |
| 110 | + not exists(BinaryLogicalOperation op | op.getAnOperand() = this) and |
| 111 | + not exists(UnaryLogicalOperation op | op.getOperand() = this) and |
| 112 | + // A literal in a comparison operation may be an algorithm, but not a candidate |
| 113 | + // for the purposes of finding applied algorithms |
| 114 | + not exists(ComparisonOperation op | op.getAnOperand() = this) |
| 115 | + } |
| 116 | +} |
0 commit comments