Skip to content

Commit e696eaa

Browse files
committed
C++: Fix false positives involving STDIN_FILENO.
1 parent 3ba9e80 commit e696eaa

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

cpp/ql/src/Security/CWE/CWE-311/CleartextTransmission.ql

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ import semmle.code.cpp.valuenumbering.GlobalValueNumbering
2121
* A function call that sends or receives data over a network.
2222
*/
2323
abstract class NetworkSendRecv extends FunctionCall {
24+
/**
25+
* Gets the expression for the socket or similar object used for sending or
26+
* receiving data.
27+
*/
28+
abstract Expr getSocketExpr();
29+
2430
/**
2531
* Gets the expression for the buffer to be sent from / received into.
2632
*/
@@ -38,6 +44,8 @@ class NetworkSend extends NetworkSendRecv {
3844
.hasGlobalName(["send", "sendto", "sendmsg", "write", "writev", "pwritev", "pwritev2"])
3945
}
4046

47+
override Expr getSocketExpr() { result = this.getArgument(0) }
48+
4149
override Expr getDataExpr() { result = this.getArgument(1) }
4250
}
4351

@@ -52,9 +60,15 @@ class NetworkRecv extends NetworkSendRecv {
5260
])
5361
}
5462

63+
override Expr getSocketExpr() { result = this.getArgument(0) }
64+
5565
override Expr getDataExpr() { result = this.getArgument(1) }
5666
}
5767

5868
from NetworkSendRecv transmission, SensitiveExpr e
59-
where DataFlow::localFlow(DataFlow::exprNode(e), DataFlow::exprNode(transmission.getDataExpr()))
69+
where
70+
DataFlow::localFlow(DataFlow::exprNode(e), DataFlow::exprNode(transmission.getDataExpr())) and
71+
not exists(Zero zero |
72+
DataFlow::localFlow(DataFlow::exprNode(zero), DataFlow::exprNode(transmission.getSocketExpr()))
73+
)
6074
select transmission, e

cpp/ql/test/query-tests/Security/CWE/CWE-311/semmle/tests/CleartextTransmission.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@
55
| test3.cpp:70:3:70:6 | call to send | test3.cpp:68:21:68:29 | password1 |
66
| test3.cpp:77:3:77:6 | call to recv | test3.cpp:75:15:75:22 | password |
77
| test3.cpp:95:3:95:6 | call to read | test3.cpp:95:12:95:19 | password |
8-
| test3.cpp:102:3:102:6 | call to read | test3.cpp:102:12:102:19 | password |

cpp/ql/test/query-tests/Security/CWE/CWE-311/semmle/tests/test3.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void test_read()
9999
char password[256];
100100
int fd = STDIN_FILENO;
101101

102-
read(fd, password, 256); // GOOD: `password` is received from stdin, not a network socket [FALSE POSITIVE]
102+
read(fd, password, 256); // GOOD: `password` is received from stdin, not a network socket
103103
}
104104
}
105105

0 commit comments

Comments
 (0)