Skip to content

Commit ec0ce56

Browse files
committed
C++: Model getaddrinfo as flow source
1 parent 8f9a73e commit ec0ce56

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

cpp/ql/lib/semmle/code/cpp/models/implementations/Inet.qll

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import semmle.code.cpp.models.interfaces.Taint
22
import semmle.code.cpp.models.interfaces.Alias
33
import semmle.code.cpp.models.interfaces.ArrayFunction
4+
import semmle.code.cpp.models.interfaces.FlowSource
45

56
private class InetNtoa extends TaintFunction {
67
InetNtoa() { hasGlobalName("inet_ntoa") }
@@ -142,3 +143,21 @@ private class Gethostbyaddr extends TaintFunction, ArrayFunction {
142143

143144
override predicate hasArrayWithNullTerminator(int bufParam) { bufParam = 0 }
144145
}
146+
147+
private class Getaddrinfo extends TaintFunction, ArrayFunction, RemoteFlowSourceFunction {
148+
Getaddrinfo() { hasGlobalName("getaddrinfo") }
149+
150+
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
151+
input.isParameterDeref([0 .. 2]) and
152+
output.isParameterDeref(3)
153+
}
154+
155+
override predicate hasArrayInput(int bufParam) { bufParam in [0, 1] }
156+
157+
override predicate hasArrayWithNullTerminator(int bufParam) { bufParam in [0, 1] }
158+
159+
override predicate hasRemoteFlowSource(FunctionOutput output, string description) {
160+
output.isParameterDeref(3) and
161+
description = "Address returned by " + this.getName()
162+
}
163+
}

cpp/ql/test/library-tests/dataflow/source-sink-tests/sources-and-sinks.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,13 @@ void test_scanf(FILE *stream, int *d, char *buf) {
4040
scanf("%d %s", d, buf); // $ local_source=40:18 local_source=40:21
4141
fscanf(stream, "%d %s", d, buf); // $ remote_source=41:27 remote_source=41:30
4242
}
43+
44+
struct addrinfo;
45+
46+
int getaddrinfo(const char *hostname, const char *servname,
47+
const struct addrinfo *hints, struct addrinfo **res);
48+
49+
void test_inet(char *hostname, char *servname, struct addrinfo *hints) {
50+
addrinfo *res;
51+
int ret = getaddrinfo(hostname, servname, hints, &res); // $ remote_source
52+
}

0 commit comments

Comments
 (0)