|
| 1 | +/** |
| 2 | + * @name Certificate not checked |
| 3 | + * @description Always check the result of certificate verification after fetching an SSL certificate. |
| 4 | + * @kind problem |
| 5 | + * @problem.severity error |
| 6 | + * @security-severity 7.5 |
| 7 | + * @precision medium |
| 8 | + * @id cpp/certificate-not-checked |
| 9 | + * @tags security |
| 10 | + * external/cwe/cwe-295 |
| 11 | + */ |
| 12 | + |
| 13 | +import cpp |
| 14 | +import semmle.code.cpp.valuenumbering.GlobalValueNumbering |
| 15 | +import semmle.code.cpp.controlflow.IRGuards |
| 16 | + |
| 17 | +/** |
| 18 | + * A call to `SSL_get_peer_certificate`. |
| 19 | + */ |
| 20 | +class SSLGetPeerCertificateCall extends FunctionCall { |
| 21 | + SSLGetPeerCertificateCall() { |
| 22 | + getTarget().getName() = "SSL_get_peer_certificate" // SSL_get_peer_certificate(ssl) |
| 23 | + } |
| 24 | + |
| 25 | + Expr getSSLArgument() { result = getArgument(0) } |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * A call to `SSL_get_verify_result`. |
| 30 | + */ |
| 31 | +class SSLGetVerifyResultCall extends FunctionCall { |
| 32 | + SSLGetVerifyResultCall() { |
| 33 | + getTarget().getName() = "SSL_get_verify_result" // SSL_get_peer_certificate(ssl) |
| 34 | + } |
| 35 | + |
| 36 | + Expr getSSLArgument() { result = getArgument(0) } |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Holds if the SSL object passed into `SSL_get_peer_certificate` is checked with |
| 41 | + * `SSL_get_verify_result` entering `node`. |
| 42 | + */ |
| 43 | +predicate resultIsChecked(SSLGetPeerCertificateCall getCertCall, ControlFlowNode node) { |
| 44 | + exists(Expr ssl, SSLGetVerifyResultCall check | |
| 45 | + ssl = globalValueNumber(getCertCall.getSSLArgument()).getAnExpr() and |
| 46 | + ssl = check.getSSLArgument() and |
| 47 | + node = check |
| 48 | + ) |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Holds if the certificate returned by `SSL_get_peer_certificate` is found to be |
| 53 | + * `0` on the edge `node1` to `node2`. |
| 54 | + */ |
| 55 | +predicate certIsZero( |
| 56 | + SSLGetPeerCertificateCall getCertCall, ControlFlowNode node1, ControlFlowNode node2 |
| 57 | +) { |
| 58 | + exists(Expr cert | cert = globalValueNumber(getCertCall).getAnExpr() | |
| 59 | + exists(GuardCondition guard, Expr zero | |
| 60 | + zero.getValue().toInt() = 0 and |
| 61 | + node1 = guard and |
| 62 | + ( |
| 63 | + // if (cert == zero) { |
| 64 | + guard.comparesEq(cert, zero, 0, true, true) and |
| 65 | + node2 = guard.getATrueSuccessor() |
| 66 | + or |
| 67 | + // if (cert != zero) { } |
| 68 | + guard.comparesEq(cert, zero, 0, false, true) and |
| 69 | + node2 = guard.getAFalseSuccessor() |
| 70 | + ) |
| 71 | + ) |
| 72 | + or |
| 73 | + ( |
| 74 | + // if (cert) { } |
| 75 | + node1 = cert |
| 76 | + or |
| 77 | + // if (!cert) { |
| 78 | + node1.(NotExpr).getAChild() = cert |
| 79 | + ) and |
| 80 | + node2 = node1.getASuccessor() and |
| 81 | + not cert.(GuardCondition).controls(node2, true) // cert may be false |
| 82 | + ) |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Holds if the SSL object passed into `SSL_get_peer_certificate` has not been checked with |
| 87 | + * `SSL_get_verify_result` at `node`. Note that this is only computed at the call to |
| 88 | + * `SSL_get_peer_certificate` and at the start and end of `BasicBlock`s. |
| 89 | + */ |
| 90 | +predicate certNotChecked(SSLGetPeerCertificateCall getCertCall, ControlFlowNode node) { |
| 91 | + // cert is not checked at the call to `SSL_get_peer_certificate` |
| 92 | + node = getCertCall |
| 93 | + or |
| 94 | + exists(BasicBlock bb, int pos | |
| 95 | + // flow to end of a `BasicBlock` |
| 96 | + certNotChecked(getCertCall, bb.getNode(pos)) and |
| 97 | + node = bb.getEnd() and |
| 98 | + // check for barrier node |
| 99 | + not exists(int pos2 | |
| 100 | + pos2 > pos and |
| 101 | + resultIsChecked(getCertCall, bb.getNode(pos2)) |
| 102 | + ) |
| 103 | + ) |
| 104 | + or |
| 105 | + exists(BasicBlock pred, BasicBlock bb | |
| 106 | + // flow from the end of one `BasicBlock` to the beginning of a successor |
| 107 | + certNotChecked(getCertCall, pred.getEnd()) and |
| 108 | + bb = pred.getASuccessor() and |
| 109 | + node = bb.getStart() and |
| 110 | + // check for barrier bb |
| 111 | + not certIsZero(getCertCall, pred.getEnd(), bb.getStart()) |
| 112 | + ) |
| 113 | +} |
| 114 | + |
| 115 | +from SSLGetPeerCertificateCall getCertCall, ControlFlowNode node |
| 116 | +where |
| 117 | + certNotChecked(getCertCall, node) and |
| 118 | + node instanceof Function // (function exit) |
| 119 | +select getCertCall, |
| 120 | + "This " + getCertCall.toString() + " is not followed by a call to SSL_get_verify_result." |
0 commit comments