|
| 1 | +/** |
| 2 | + * @name Boost_asio TLS Settings Misconfiguration |
| 3 | + * @description Using TLS or SSLv23 protool from the boost::asio library, but not disabling deprecated protocols or disabling minimum-recommended protocols |
| 4 | + * @kind problem |
| 5 | + * @problem.severity error |
| 6 | + * @id cpp/boost/tls_settings_misconfiguration |
| 7 | + * @tags security |
| 8 | + */ |
| 9 | + |
| 10 | +import cpp |
| 11 | +import semmle.code.cpp.security.boostorg.asio.protocols |
| 12 | + |
| 13 | +class ExistsAnyFlowConfig extends DataFlow::Configuration { |
| 14 | + ExistsAnyFlowConfig() { |
| 15 | + this = "ExistsAnyFlowConfig" |
| 16 | + } |
| 17 | + |
| 18 | + override predicate isSource(DataFlow::Node source) { |
| 19 | + any() |
| 20 | + } |
| 21 | + |
| 22 | + override predicate isSink(DataFlow::Node sink) { |
| 23 | + any() |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +bindingset[flag] |
| 28 | +predicate isOptionSet( ConstructorCall cc, int flag, FunctionCall fcSetOptions) { |
| 29 | + exists( BoostorgAsio::SslContextFlowsToSetOptionConfig config, ExistsAnyFlowConfig testConfig, Expr optionsSink | |
| 30 | + config.hasFlow(DataFlow::exprNode(cc), DataFlow::exprNode(optionsSink)) and |
| 31 | + exists( VariableAccess contextSetOptions | |
| 32 | + testConfig.hasFlow(DataFlow::exprNode(cc), DataFlow::exprNode(contextSetOptions)) and |
| 33 | + ( |
| 34 | + exists( BoostorgAsio::SslSetOptionsFunction f | |
| 35 | + f.getACallToThisFunction() = fcSetOptions | |
| 36 | + contextSetOptions = fcSetOptions.getQualifier() and |
| 37 | + forall( Expr optionArgument, BoostorgAsio::SslOptionConfig optionArgConfig, Expr optionArgumentSource | |
| 38 | + optionArgument = fcSetOptions.getArgument(0) and |
| 39 | + optionArgConfig.hasFlow(DataFlow::exprNode(optionArgumentSource), DataFlow::exprNode(optionArgument)) | |
| 40 | + optionArgument.getValue().toInt().bitShiftRight(16).bitAnd(flag) = flag |
| 41 | + ) |
| 42 | + ) |
| 43 | + ) |
| 44 | + ) |
| 45 | + ) |
| 46 | +} |
| 47 | + |
| 48 | +bindingset[flag] |
| 49 | +predicate isOptionNotSet( ConstructorCall cc, int flag ) { |
| 50 | + not exists( BoostorgAsio::SslContextFlowsToSetOptionConfig config, ExistsAnyFlowConfig testConfig, Expr optionsSink | |
| 51 | + config.hasFlow(DataFlow::exprNode(cc), DataFlow::exprNode(optionsSink)) and |
| 52 | + exists( VariableAccess contextSetOptions | |
| 53 | + testConfig.hasFlow(DataFlow::exprNode(cc), DataFlow::exprNode(contextSetOptions)) and |
| 54 | + ( |
| 55 | + exists( FunctionCall fcSetOptions, BoostorgAsio::SslSetOptionsFunction f | |
| 56 | + f.getACallToThisFunction() = fcSetOptions | |
| 57 | + contextSetOptions = fcSetOptions.getQualifier() and |
| 58 | + forall( Expr optionArgument, BoostorgAsio::SslOptionConfig optionArgConfig, Expr optionArgumentSource | |
| 59 | + optionArgument = fcSetOptions.getArgument(0) and |
| 60 | + optionArgConfig.hasFlow(DataFlow::exprNode(optionArgumentSource), DataFlow::exprNode(optionArgument)) | |
| 61 | + optionArgument.getValue().toInt().bitShiftRight(16).bitAnd(flag) = flag |
| 62 | + ) |
| 63 | + ) |
| 64 | + ) |
| 65 | + ) |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +from |
| 70 | + BoostorgAsio::SslContextCallTlsProtocolConfig configConstructor, |
| 71 | + BoostorgAsio::SslContextFlowsToSetOptionConfig config, |
| 72 | + Expr protocolSource, Expr protocolSink, |
| 73 | + ConstructorCall cc, |
| 74 | + Expr e, string msg |
| 75 | +where |
| 76 | + configConstructor.hasFlow(DataFlow::exprNode(protocolSource), DataFlow::exprNode(protocolSink)) and |
| 77 | + cc.getArgument(0)= protocolSink and ( |
| 78 | + ( |
| 79 | + BoostorgAsio::isExprSslV23BoostProtocol(protocolSource) and |
| 80 | + not exists( Expr optionsSink | |
| 81 | + config.hasFlow(DataFlow::exprNode(cc), DataFlow::exprNode(optionsSink)) and |
| 82 | + isOptionSet(cc, BoostorgAsio::getShiftedSslOptionsNoSsl3(), _) and |
| 83 | + isOptionSet(cc, BoostorgAsio::getShiftedSslOptionsNoTls1(), _) and |
| 84 | + isOptionSet(cc, BoostorgAsio::getShiftedSslOptionsNoTls1_1(), _) and |
| 85 | + isOptionNotSet(cc, BoostorgAsio::getShiftedSslOptionsNoTls1_2()) |
| 86 | + ) |
| 87 | + ) or |
| 88 | + ( |
| 89 | + BoostorgAsio::isExprTlsBoostProtocol(protocolSource) and |
| 90 | + not BoostorgAsio::isExprSslV23BoostProtocol(protocolSource) and |
| 91 | + not exists( Expr optionsSink | |
| 92 | + config.hasFlow(DataFlow::exprNode(cc), DataFlow::exprNode(optionsSink)) and |
| 93 | + isOptionSet(cc, BoostorgAsio::getShiftedSslOptionsNoTls1(), _) and |
| 94 | + isOptionSet(cc, BoostorgAsio::getShiftedSslOptionsNoTls1_1(), _) and |
| 95 | + isOptionNotSet(cc, BoostorgAsio::getShiftedSslOptionsNoTls1_2()) |
| 96 | + ) |
| 97 | + ) |
| 98 | + ) and ( |
| 99 | + ( |
| 100 | + BoostorgAsio::isExprSslV23BoostProtocol(protocolSource) and |
| 101 | + isOptionNotSet(cc, BoostorgAsio::getShiftedSslOptionsNoSsl3()) and |
| 102 | + e =cc and |
| 103 | + msg = "no_sslv3 has not been set" |
| 104 | + ) or |
| 105 | + ( |
| 106 | + isOptionNotSet(cc, BoostorgAsio::getShiftedSslOptionsNoTls1()) and |
| 107 | + e =cc and |
| 108 | + msg = "no_tlsv1 has not been set" |
| 109 | + ) or |
| 110 | + ( |
| 111 | + isOptionNotSet(cc, BoostorgAsio::getShiftedSslOptionsNoTls1_1()) and |
| 112 | + e =cc and |
| 113 | + msg = "no_tlsv1_1 has not been set" |
| 114 | + ) or |
| 115 | + ( |
| 116 | + isOptionSet(cc, BoostorgAsio::getShiftedSslOptionsNoTls1_2(), e) and |
| 117 | + msg = "no_tlsv1_2 was set" |
| 118 | + ) |
| 119 | + ) |
| 120 | +select cc, "Usage of $@ with protocol $@ is not configured correctly: The option $@.", |
| 121 | + cc, "boost::asio::ssl::context::context", |
| 122 | + protocolSource, protocolSource.toString(), |
| 123 | + e, msg |
| 124 | + |
0 commit comments