Skip to content

Commit 122f638

Browse files
committed
C++: Improve recognition of stdin, stdout etc.
1 parent 6896b20 commit 122f638

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,23 @@ abstract class NetworkSendRecv extends FunctionCall {
3636
abstract Expr getDataExpr();
3737

3838
/**
39-
* Holds if any socket used by this call could be a true network socket.
40-
* A zero socket descriptor is standard input, which is not a network
41-
* operation.
39+
* Holds if the socket used by this call could be a true network socket (or
40+
* if no socket is specified). A constant value is likely to indicate standard
41+
* input, standard output or a similar non-network socket.
4242
*/
4343
predicate checkSocket() {
44-
not exists(Zero zero |
45-
DataFlow::localFlow(DataFlow::exprNode(zero), DataFlow::exprNode(getSocketExpr()))
44+
not exists(GVN g |
45+
g = globalValueNumber(getSocketExpr()) and
46+
(
47+
// literal constant
48+
globalValueNumber(any(Literal l)) = g
49+
or
50+
// variable (such as a global) initialized to a literal constant
51+
exists(Variable v |
52+
v.getInitializer().getExpr() instanceof Literal and
53+
g = globalValueNumber(v.getAnAccess())
54+
)
55+
)
4656
)
4757
}
4858
}

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
@@ -196,7 +196,6 @@ subpaths
196196
#select
197197
| test3.cpp:22:3:22:6 | call to send | test3.cpp:22:15:22:23 | password1 | test3.cpp:22:15:22:23 | password1 | This operation transmits 'password1', which may contain unencrypted sensitive data from $@ | test3.cpp:22:15:22:23 | password1 | password1 |
198198
| test3.cpp:26:3:26:6 | call to send | test3.cpp:26:15:26:23 | password2 | test3.cpp:26:15:26:23 | password2 | This operation transmits 'password2', which may contain unencrypted sensitive data from $@ | test3.cpp:26:15:26:23 | password2 | password2 |
199-
| test3.cpp:38:3:38:6 | call to send | test3.cpp:38:23:38:31 | password2 | test3.cpp:38:23:38:31 | password2 | This operation transmits 'password2', which may contain unencrypted sensitive data from $@ | test3.cpp:38:23:38:31 | password2 | password2 |
200199
| test3.cpp:47:3:47:6 | call to recv | test3.cpp:47:15:47:22 | password | test3.cpp:47:15:47:22 | password | This operation receives into 'password', which may put unencrypted sensitive data into $@ | test3.cpp:47:15:47:22 | password | password |
201200
| test3.cpp:55:3:55:6 | call to recv | test3.cpp:55:15:55:22 | password | test3.cpp:55:15:55:22 | password | This operation receives into 'password', which may put unencrypted sensitive data into $@ | test3.cpp:55:15:55:22 | password | password |
202201
| test3.cpp:76:3:76:6 | call to send | test3.cpp:74:21:74:29 | password1 | test3.cpp:76:15:76:17 | ptr | This operation transmits 'ptr', which may contain unencrypted sensitive data from $@ | test3.cpp:74:21:74:29 | password1 | password1 |

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
@@ -35,7 +35,7 @@ void test_send(const char *password1, const char *password2, const char *passwor
3535
}
3636

3737
{
38-
send(stdout_fileno, password2, strlen(password2), val()); // GOOD: `password2` is sent to stdout, not a network socket (this may be an issue but is not within the scope of the `cpp/cleartext-transmission` query) [FALSE POSITIVE]
38+
send(stdout_fileno, password2, strlen(password2), val()); // GOOD: `password2` is sent to stdout, not a network socket (this may be an issue but is not within the scope of the `cpp/cleartext-transmission` query)
3939
}
4040
}
4141

0 commit comments

Comments
 (0)