Skip to content

Commit 7d1f10b

Browse files
authored
Merge pull request github#11627 from jketema/getaddrinfo
C++: Model `getaddrinfo` as flow source
2 parents 3b9b9b3 + 2095f11 commit 7d1f10b

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `getaddrinfo` function is now recognized as a flow source.

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

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
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 {
6-
InetNtoa() { hasGlobalName("inet_ntoa") }
7+
InetNtoa() { this.hasGlobalName("inet_ntoa") }
78

89
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
910
input.isParameter(0) and
@@ -12,7 +13,7 @@ private class InetNtoa extends TaintFunction {
1213
}
1314

1415
private class InetAton extends TaintFunction, ArrayFunction {
15-
InetAton() { hasGlobalName("inet_aton") }
16+
InetAton() { this.hasGlobalName("inet_aton") }
1617

1718
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
1819
input.isParameterDeref(0) and
@@ -32,7 +33,7 @@ private class InetAton extends TaintFunction, ArrayFunction {
3233
}
3334

3435
private class InetAddr extends TaintFunction, ArrayFunction, AliasFunction {
35-
InetAddr() { hasGlobalName("inet_addr") }
36+
InetAddr() { this.hasGlobalName("inet_addr") }
3637

3738
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
3839
input.isParameterDeref(0) and
@@ -51,7 +52,7 @@ private class InetAddr extends TaintFunction, ArrayFunction, AliasFunction {
5152
}
5253

5354
private class InetNetwork extends TaintFunction, ArrayFunction {
54-
InetNetwork() { hasGlobalName("inet_network") }
55+
InetNetwork() { this.hasGlobalName("inet_network") }
5556

5657
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
5758
input.isParameterDeref(0) and
@@ -64,7 +65,7 @@ private class InetNetwork extends TaintFunction, ArrayFunction {
6465
}
6566

6667
private class InetMakeaddr extends TaintFunction {
67-
InetMakeaddr() { hasGlobalName("inet_makeaddr") }
68+
InetMakeaddr() { this.hasGlobalName("inet_makeaddr") }
6869

6970
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
7071
(
@@ -76,7 +77,7 @@ private class InetMakeaddr extends TaintFunction {
7677
}
7778

7879
private class InetLnaof extends TaintFunction {
79-
InetLnaof() { hasGlobalName("inet_lnaof") }
80+
InetLnaof() { this.hasGlobalName("inet_lnaof") }
8081

8182
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
8283
input.isParameter(0) and
@@ -85,7 +86,7 @@ private class InetLnaof extends TaintFunction {
8586
}
8687

8788
private class InetNetof extends TaintFunction {
88-
InetNetof() { hasGlobalName("inet_netof") }
89+
InetNetof() { this.hasGlobalName("inet_netof") }
8990

9091
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
9192
input.isParameter(0) and
@@ -94,7 +95,7 @@ private class InetNetof extends TaintFunction {
9495
}
9596

9697
private class InetPton extends TaintFunction, ArrayFunction {
97-
InetPton() { hasGlobalName("inet_pton") }
98+
InetPton() { this.hasGlobalName("inet_pton") }
9899

99100
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
100101
(
@@ -114,7 +115,7 @@ private class InetPton extends TaintFunction, ArrayFunction {
114115
}
115116

116117
private class Gethostbyname extends TaintFunction, ArrayFunction {
117-
Gethostbyname() { hasGlobalName("gethostbyname") }
118+
Gethostbyname() { this.hasGlobalName("gethostbyname") }
118119

119120
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
120121
input.isParameterDeref(0) and
@@ -127,7 +128,7 @@ private class Gethostbyname extends TaintFunction, ArrayFunction {
127128
}
128129

129130
private class Gethostbyaddr extends TaintFunction, ArrayFunction {
130-
Gethostbyaddr() { hasGlobalName("gethostbyaddr") }
131+
Gethostbyaddr() { this.hasGlobalName("gethostbyaddr") }
131132

132133
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
133134
(
@@ -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() { this.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)