Skip to content

Commit a065434

Browse files
authored
Merge pull request #16811 from porcupineyhairs/curlssl
CPP: Disabled SSL certificate verification
2 parents ec74595 + ee41e65 commit a065434

File tree

7 files changed

+137
-0
lines changed

7 files changed

+137
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd">
2+
<qhelp>
3+
<overview>
4+
<p>
5+
Disabling verification of the SSL certificate allows man-in-the-middle attacks. A SSL
6+
connection is vulnerable to man-in-the-middle attacks if the certification is not checked
7+
properly. If the peer or the host's certificate verification is not verified, the underlying
8+
SSL communication is insecure.</p>
9+
</overview>
10+
<recommendation>
11+
<p>It is recommended that all communications be done post verification of the host as well as
12+
the
13+
peer.</p>
14+
</recommendation>
15+
<example>
16+
<p>The following snippet disables certification verification by setting the value of <code>
17+
CURLOPT_SSL_VERIFYHOST</code> and <code>CURLOPT_SSL_VERIFYHOST</code> to <code>0</code>:</p>
18+
<sample src="CurlSSLBad.cpp" />
19+
<p>This is bad as the certificates are not verified any more. This can be easily fixed by
20+
setting the values of the options to <code>2</code>. </p>
21+
<sample src="CurlSSLGood.cpp" />
22+
</example>
23+
<references>
24+
<li> Curl Documentation:<a href="https://curl.se/libcurl/c/CURLOPT_SSL_VERIFYHOST.html">
25+
CURLOPT_SSL_VERIFYHOST</a></li>
26+
<li> Curl Documentation:<a href="https://curl.se/libcurl/c/CURLOPT_SSL_VERIFYPEER.html">
27+
CURLOPT_SSL_VERIFYPEER</a></li>
28+
<li> Related CVE: <a href="https://github.com/advisories/GHSA-5r3h-c3r7-9w4h"> CVE-2022-33684</a></li>
29+
<li> Related security advisory: <a
30+
href="https://huntr.com/bounties/42325662-6329-4e04-875a-49e2f5d69f78">
31+
openframeworks/openframeworks
32+
</a></li>
33+
</references>
34+
</qhelp>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @name Disabled certifcate verification
3+
* @description Disabling SSL certificate verification of host or peer could expose the communication to man-in-the-middle(MITM) attacks.
4+
* @kind problem
5+
* @problem.severity warning
6+
* @id cpp/curl-disabled-ssl
7+
* @tags security
8+
* external/cwe/cwe-295
9+
*/
10+
11+
import cpp
12+
import semmle.code.cpp.dataflow.new.TaintTracking
13+
14+
/** Models the `curl_easy_setopt` function call */
15+
private class CurlSetOptCall extends FunctionCall {
16+
CurlSetOptCall() {
17+
exists(FunctionCall fc, Function f |
18+
f.hasGlobalOrStdName("curl_easy_setopt") and
19+
fc.getTarget() = f
20+
|
21+
this = fc
22+
)
23+
}
24+
}
25+
26+
/** Models an access to any enum constant which could affect SSL verification */
27+
private class CurlVerificationConstant extends EnumConstantAccess {
28+
CurlVerificationConstant() {
29+
exists(EnumConstant e | e.getName() = ["CURLOPT_SSL_VERIFYHOST", "CURLOPT_SSL_VERIFYPEER"] |
30+
e.getAnAccess() = this
31+
)
32+
}
33+
}
34+
35+
from CurlSetOptCall c
36+
where
37+
c.getArgument(1) = any(CurlVerificationConstant v) and
38+
c.getArgument(2).getValue() = "0"
39+
select c, "This call disables Secure Socket Layer and could potentially lead to MITM attacks"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
string host = "codeql.com"
2+
void bad(void) {
3+
std::unique_ptr<CURL, void(*)(CURL*)> curl =
4+
std::unique_ptr<CURL, void(*)(CURL*)>(curl_easy_init(), curl_easy_cleanup);
5+
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, 0);
6+
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYHOST, 0);
7+
curl_easy_setopt(curl.get(), CURLOPT_URL, host.c_str());
8+
curl_easy_perform(curl.get());
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
string host = "codeql.com"
2+
void good(void) {
3+
std::unique_ptr<CURL, void(*)(CURL*)> curl =
4+
std::unique_ptr<CURL, void(*)(CURL*)>(curl_easy_init(), curl_easy_cleanup);
5+
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, 2);
6+
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYHOST, 2);
7+
curl_easy_setopt(curl.get(), CURLOPT_URL, host.c_str());
8+
curl_easy_perform(curl.get());
9+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "../../../../../library-tests/string_concat/stl.h"
2+
3+
namespace std{
4+
struct CURL {};
5+
typedef CURL curl;
6+
enum curl_constant{
7+
CURLOPT_URL,
8+
CURLOPT_SSL_VERIFYHOST,
9+
CURLOPT_SSL_VERIFYPEER
10+
};
11+
12+
CURL *curl_easy_init();
13+
void curl_easy_cleanup(CURL *handle);
14+
void curl_easy_perform(CURL *handle);
15+
void curl_easy_setopt(CURL *handle, curl_constant param, int p);
16+
void curl_easy_setopt(CURL *handle, curl_constant param, char* p);
17+
}
18+
19+
20+
using namespace std;
21+
char host[] = "codeql.com";
22+
23+
void bad(void) {
24+
std::unique_ptr<CURL> curl = std::unique_ptr<CURL>(curl_easy_init());
25+
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, 0);
26+
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYHOST, 0);
27+
curl_easy_setopt(curl.get(), CURLOPT_URL, host);
28+
curl_easy_perform(curl.get());
29+
}
30+
31+
void good(void) {
32+
std::unique_ptr<CURL> curl = std::unique_ptr<CURL>(curl_easy_init());
33+
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, 2);
34+
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYHOST, 2);
35+
curl_easy_setopt(curl.get(), CURLOPT_URL, host);
36+
curl_easy_perform(curl.get());
37+
}
38+
39+
int main(int c, char** argv){
40+
bad();
41+
good();
42+
}
43+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| CurlSSL.cpp:25:2:25:17 | call to curl_easy_setopt | This call disables Secure Socket Layer and could potentially lead to MITM attacks |
2+
| CurlSSL.cpp:26:2:26:17 | call to curl_easy_setopt | This call disables Secure Socket Layer and could potentially lead to MITM attacks |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
experimental/Security/CWE/CWE-295/CurlSSL.ql

0 commit comments

Comments
 (0)