Skip to content

Commit 09cd168

Browse files
author
ihsinme
committed
create new branchihsinme-patch-88 in fork
1 parent 28dca3f commit 09cd168

File tree

6 files changed

+145
-0
lines changed

6 files changed

+145
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
...
2+
SSL_shutdown(ssl);
3+
SSL_shutdown(ssl); // BAD
4+
...
5+
switch ((ret = SSL_shutdown(ssl))) {
6+
case 1:
7+
break;
8+
case 0:
9+
ERR_clear_error();
10+
if (-1 != (ret = SSL_shutdown(ssl))) break; // GOOD
11+
...
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>Incorrect closing of the connection leads to the creation of different states for the server and client, which can be exploited by an attacker.</p>
7+
8+
</overview>
9+
10+
<example>
11+
<p>The following example shows the incorrect and correct usage of function SSL_shutdown.</p>
12+
<sample src="DangerousUseSSL_shutdown.cpp" />
13+
14+
</example>
15+
<references>
16+
17+
<li>
18+
CERT Coding Standard:
19+
<a href="https://wiki.sei.cmu.edu/confluence/display/c/EXP12-C.+Do+not+ignore+values+returned+by+functions">EXP12-C. Do not ignore values returned by functions - SEI CERT C Coding Standard - Confluence</a>.
20+
</li>
21+
22+
</references>
23+
</qhelp>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @name Dangerous use SSL_shutdown.
3+
* @description Incorrect closing of the connection leads to the creation of different states for the server and client, which can be exploited by an attacker.
4+
* @kind problem
5+
* @id cpp/dangerous-use-of-ssl_shutdown
6+
* @problem.severity warning
7+
* @precision medium
8+
* @tags correctness
9+
* security
10+
* external/cwe/cwe-670
11+
*/
12+
13+
import cpp
14+
import semmle.code.cpp.commons.Exclusions
15+
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
16+
17+
from FunctionCall fc, FunctionCall fc1
18+
where
19+
fc != fc1 and
20+
fc.getASuccessor+() = fc1 and
21+
fc.getTarget().hasName("SSL_shutdown") and
22+
fc1.getTarget().hasName("SSL_shutdown") and
23+
fc1 instanceof ExprInVoidContext and
24+
(
25+
globalValueNumber(fc.getArgument(0)) = globalValueNumber(fc1.getArgument(0)) or
26+
fc.getArgument(0).(VariableAccess).getTarget() = fc1.getArgument(0).(VariableAccess).getTarget()
27+
) and
28+
not exists(FunctionCall fctmp |
29+
fctmp.getTarget().hasName("SSL_free") and
30+
fc.getASuccessor+() = fctmp and
31+
fctmp.getASuccessor+() = fc1
32+
)
33+
select fc, "You need to handle the return value SSL_shutdown"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.cpp:45:20:45:31 | call to SSL_shutdown | You need to handle the return value SSL_shutdown |
2+
| test.cpp:61:11:61:22 | call to SSL_shutdown | You need to handle the return value SSL_shutdown |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
experimental/Security/CWE/CWE-670/DangerousUseSSL_shutdown.ql
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// it's not exact, but it's enough for an example
2+
typedef int SSL;
3+
4+
5+
int SSL_shutdown(SSL *ssl);
6+
int SSL_get_error(const SSL *ssl, int ret);
7+
void ERR_clear_error(void);
8+
void print_error(char *buff,int code);
9+
10+
int gootTest1(SSL *ssl)
11+
{
12+
int ret;
13+
switch ((ret = SSL_shutdown(ssl))) {
14+
case 1:
15+
break;
16+
case 0:
17+
ERR_clear_error();
18+
if ((ret = SSL_shutdown(ssl)) == 1) break; // GOOD
19+
default:
20+
print_error("error shutdown",
21+
SSL_get_error(ssl, ret));
22+
return -1;
23+
}
24+
return 0;
25+
}
26+
int gootTest2(SSL *ssl)
27+
{
28+
int ret;
29+
switch ((ret = SSL_shutdown(ssl))) {
30+
case 1:
31+
break;
32+
case 0:
33+
ERR_clear_error();
34+
if (-1 != (ret = SSL_shutdown(ssl))) break; // GOOD
35+
default:
36+
print_error("error shutdown",
37+
SSL_get_error(ssl, ret));
38+
return -1;
39+
}
40+
return 0;
41+
}
42+
int badTest1(SSL *ssl)
43+
{
44+
int ret;
45+
switch ((ret = SSL_shutdown(ssl))) {
46+
case 1:
47+
break;
48+
case 0:
49+
SSL_shutdown(ssl); // BAD
50+
break;
51+
default:
52+
print_error("error shutdown",
53+
SSL_get_error(ssl, ret));
54+
return -1;
55+
}
56+
return 0;
57+
}
58+
int badTest2(SSL *ssl)
59+
{
60+
int ret;
61+
ret = SSL_shutdown(ssl);
62+
switch (ret) {
63+
case 1:
64+
break;
65+
case 0:
66+
SSL_shutdown(ssl); // BAD
67+
break;
68+
default:
69+
print_error("error shutdown",
70+
SSL_get_error(ssl, ret));
71+
return -1;
72+
}
73+
return 0;
74+
}
75+

0 commit comments

Comments
 (0)