Skip to content

Commit a2dac3a

Browse files
committed
C++: Move remote flow sink test and also handle local and remote sinks
1 parent 01d8ad9 commit a2dac3a

File tree

7 files changed

+84
-45
lines changed

7 files changed

+84
-45
lines changed

cpp/ql/test/library-tests/dataflow/DefaultTaintTracking/annotate_sinks_only/defaulttainttracking.cpp

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
#include "../shared.h"
22

3-
4-
5-
6-
7-
8-
9-
10-
11-
123
int main() {
13-
14-
15-
164
sink(_strdup(getenv("VAR"))); // $ ir MISSING: ast
175
sink(strdup(getenv("VAR"))); // $ ast,ir
186
sink(unmodeled_function(getenv("VAR"))); // clean by assumption
@@ -59,9 +47,6 @@ void test_outparams() {
5947
sink(p2); // $ ir MISSING: ast
6048
}
6149

62-
63-
64-
6550
struct XY {
6651
int x;
6752
int y;
@@ -230,24 +215,17 @@ void test_recv() {
230215

231216
// --- send and related functions ---
232217

233-
int send(int, const void*, int, int);
234-
235-
void test_send(char* buffer, int length) {
236-
send(0, buffer, length, 0); // $ remote
237-
}
238-
239218
struct iovec {
240219
void *iov_base;
241220
unsigned iov_len;
242221
};
243222

244223
int readv(int, const struct iovec*, int);
245-
int writev(int, const struct iovec*, int);
246224

247225
void sink(const iovec* iovs);
248226
void sink(iovec);
249227

250-
int test_readv_and_writev(iovec* iovs) {
228+
void test_readv_and_writev(iovec* iovs) {
251229
readv(0, iovs, 16);
252230
sink(iovs); // $ast,ir
253231
sink(iovs[0]); // $ast,ir
@@ -256,6 +234,4 @@ int test_readv_and_writev(iovec* iovs) {
256234
char* p = (char*)iovs[1].iov_base;
257235
sink(p); // $ MISSING: ast,ir
258236
sink(*p); // $ MISSING: ast,ir
259-
260-
writev(0, iovs, 16); // $ remote
261237
}

cpp/ql/test/library-tests/dataflow/DefaultTaintTracking/annotate_sinks_only/remote-flow-sink.ql

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/** This tests that we are able to detect local flow sources. */
2+
3+
import cpp
4+
import TestUtilities.InlineExpectationsTest
5+
import semmle.code.cpp.security.FlowSources
6+
7+
class LocalFlowSourceTest extends InlineExpectationsTest {
8+
LocalFlowSourceTest() { this = "LocalFlowSourceTest" }
9+
10+
override string getARelevantTag() { result = "local_source" }
11+
12+
override predicate hasActualResult(Location location, string element, string tag, string value) {
13+
tag = "local_source" and
14+
value = "" and
15+
exists(LocalFlowSource node |
16+
location = node.getLocation() and
17+
element = node.toString()
18+
)
19+
}
20+
}

cpp/ql/test/library-tests/dataflow/source-sink-tests/remote-flow.expected

Whitespace-only changes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/** This tests that we are able to detect remote flow sources and sinks. */
2+
3+
import cpp
4+
import TestUtilities.InlineExpectationsTest
5+
import semmle.code.cpp.security.FlowSources
6+
7+
class RemoteFlowSourceTest extends InlineExpectationsTest {
8+
RemoteFlowSourceTest() { this = "RemoteFlowSourceTest" }
9+
10+
override string getARelevantTag() { result = "remote_source" }
11+
12+
override predicate hasActualResult(Location location, string element, string tag, string value) {
13+
tag = "remote_source" and
14+
value = "" and
15+
exists(RemoteFlowSource node |
16+
location = node.getLocation() and
17+
element = node.toString()
18+
)
19+
}
20+
}
21+
22+
class RemoteFlowSinkTest extends InlineExpectationsTest {
23+
RemoteFlowSinkTest() { this = "RemoteFlowSinkTest" }
24+
25+
override string getARelevantTag() { result = "remote_sink" }
26+
27+
override predicate hasActualResult(Location location, string element, string tag, string value) {
28+
tag = "remote_sink" and
29+
value = "" and
30+
exists(RemoteFlowSink node |
31+
location = node.getLocation() and
32+
element = node.toString()
33+
)
34+
}
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
char *getenv(const char *name);
2+
char *secure_getenv(const char *name);
3+
wchar_t *_wgetenv(const wchar_t *name);
4+
5+
void test_getenv() {
6+
void *var1 = getenv("VAR"); // $ local_source
7+
void *var2 = secure_getenv("VAR"); // $ local_source
8+
void *var3 = _wgetenv(L"VAR"); // $ local_source
9+
}
10+
11+
int send(int, const void*, int, int);
12+
13+
void test_send(char* buffer, int length) {
14+
send(0, buffer, length, 0); // $ remote_sink
15+
}
16+
17+
struct iovec {
18+
void *iov_base;
19+
unsigned iov_len;
20+
};
21+
22+
int readv(int, const struct iovec*, int);
23+
int writev(int, const struct iovec*, int);
24+
25+
void test_readv_and_writev(iovec* iovs) {
26+
readv(0, iovs, 16); // $ remote_source
27+
writev(0, iovs, 16); // $ remote_sink
28+
}

0 commit comments

Comments
 (0)