Skip to content

Commit 9f69c32

Browse files
Florian Westphalgregkh
authored andcommitted
netfilter: nf_conntrack_sip: don't use simple_strtoul
[ Upstream commit 8cf6809 ] Replace unsafe port parsing in epaddr_len(), ct_sip_parse_header_uri(), and ct_sip_parse_request() with a new sip_parse_port() helper that validates each digit against the buffer limit, eliminating the use of simple_strtoul() which assumes NUL-terminated strings. The previous code dereferenced pointers without bounds checks after sip_parse_addr() and relied on simple_strtoul() on non-NUL-terminated skb data. A port that reaches the buffer limit without a trailing character is also rejected as malformed. Also get rid of all simple_strtoul() usage in conntrack, prefer a stricter version instead. There are intentional changes: - Bail out if number is > UINT_MAX and indicate a failure, same for too long sequences. While we do accept 05535 as port 5535, we will not accept e.g. 'sip:10.0.0.1:005060'. While its syntactically valid under RFC 3261, we should restrict this to not waste cycles when presented with malformed packets with 64k '0' characters. - Force base 10 in ct_sip_parse_numerical_param(). This is used to fetch 'expire=' and 'rports='; both are expected to use base-10. - In nf_nat_sip.c, only accept the parsed value if its within the 1k-64k range. - epaddr_len now returns 0 if the port is invalid, as it already does for invalid ip addresses. This is intentional. nf_conntrack_sip performs lots of guesswork to find the right parts of the message to parse. Being stricter could break existing setups. Connection tracking helpers are designed to allow traffic to pass, not to block it. Based on an earlier patch from Jenny Guanni Qu <qguanni@gmail.com>. Fixes: 05e3ced ("[NETFILTER]: nf_conntrack_sip: introduce SIP-URI parsing helper") Reported-by: Klaudia Kloc <klaudia@vidocsecurity.com> Reported-by: Dawid Moczadło <dawid@vidocsecurity.com> Reported-by: Jenny Guanni Qu <qguanni@gmail.com>. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent b130a6e commit 9f69c32

2 files changed

Lines changed: 119 additions & 34 deletions

File tree

net/netfilter/nf_conntrack_sip.c

Lines changed: 118 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,57 @@ static int sip_parse_addr(const struct nf_conn *ct, const char *cp,
181181
return 1;
182182
}
183183

184+
/* Parse optional port number after IP address.
185+
* Returns false on malformed input, true otherwise.
186+
* If port is non-NULL, stores parsed port in network byte order.
187+
* If no port is present, sets *port to default SIP port.
188+
*/
189+
static bool sip_parse_port(const char *dptr, const char **endp,
190+
const char *limit, __be16 *port)
191+
{
192+
unsigned int p = 0;
193+
int len = 0;
194+
195+
if (dptr >= limit)
196+
return false;
197+
198+
if (*dptr != ':') {
199+
if (port)
200+
*port = htons(SIP_PORT);
201+
if (endp)
202+
*endp = dptr;
203+
return true;
204+
}
205+
206+
dptr++; /* skip ':' */
207+
208+
while (dptr < limit && isdigit(*dptr)) {
209+
p = p * 10 + (*dptr - '0');
210+
dptr++;
211+
len++;
212+
if (len > 5) /* max "65535" */
213+
return false;
214+
}
215+
216+
if (len == 0)
217+
return false;
218+
219+
/* reached limit while parsing port */
220+
if (dptr >= limit)
221+
return false;
222+
223+
if (p < 1024 || p > 65535)
224+
return false;
225+
226+
if (port)
227+
*port = htons(p);
228+
229+
if (endp)
230+
*endp = dptr;
231+
232+
return true;
233+
}
234+
184235
/* skip ip address. returns its length. */
185236
static int epaddr_len(const struct nf_conn *ct, const char *dptr,
186237
const char *limit, int *shift)
@@ -193,11 +244,8 @@ static int epaddr_len(const struct nf_conn *ct, const char *dptr,
193244
return 0;
194245
}
195246

196-
/* Port number */
197-
if (*dptr == ':') {
198-
dptr++;
199-
dptr += digits_len(ct, dptr, limit, shift);
200-
}
247+
if (!sip_parse_port(dptr, &dptr, limit, NULL))
248+
return 0;
201249
return dptr - aux;
202250
}
203251

@@ -228,6 +276,51 @@ static int skp_epaddr_len(const struct nf_conn *ct, const char *dptr,
228276
return epaddr_len(ct, dptr, limit, shift);
229277
}
230278

279+
/* simple_strtoul stops after first non-number character.
280+
* But as we're not dealing with c-strings, we can't rely on
281+
* hitting \r,\n,\0 etc. before moving past end of buffer.
282+
*
283+
* This is a variant of simple_strtoul, but doesn't require
284+
* a c-string.
285+
*
286+
* If value exceeds UINT_MAX, 0 is returned.
287+
*/
288+
static unsigned int sip_strtouint(const char *cp, unsigned int len, char **endp)
289+
{
290+
const unsigned int max = sizeof("4294967295");
291+
unsigned int olen = len;
292+
const char *s = cp;
293+
u64 result = 0;
294+
295+
if (len > max)
296+
len = max;
297+
298+
while (olen > 0 && isdigit(*s)) {
299+
unsigned int value;
300+
301+
if (len == 0)
302+
goto err;
303+
304+
value = *s - '0';
305+
result = result * 10 + value;
306+
307+
if (result > UINT_MAX)
308+
goto err;
309+
s++;
310+
len--;
311+
olen--;
312+
}
313+
314+
if (endp)
315+
*endp = (char *)s;
316+
317+
return result;
318+
err:
319+
if (endp)
320+
*endp = (char *)cp;
321+
return 0;
322+
}
323+
231324
/* Parse a SIP request line of the form:
232325
*
233326
* Request-Line = Method SP Request-URI SP SIP-Version CRLF
@@ -241,7 +334,6 @@ int ct_sip_parse_request(const struct nf_conn *ct,
241334
{
242335
const char *start = dptr, *limit = dptr + datalen, *end;
243336
unsigned int mlen;
244-
unsigned int p;
245337
int shift = 0;
246338

247339
/* Skip method and following whitespace */
@@ -267,14 +359,8 @@ int ct_sip_parse_request(const struct nf_conn *ct,
267359

268360
if (!sip_parse_addr(ct, dptr, &end, addr, limit, true))
269361
return -1;
270-
if (end < limit && *end == ':') {
271-
end++;
272-
p = simple_strtoul(end, (char **)&end, 10);
273-
if (p < 1024 || p > 65535)
274-
return -1;
275-
*port = htons(p);
276-
} else
277-
*port = htons(SIP_PORT);
362+
if (!sip_parse_port(end, &end, limit, port))
363+
return -1;
278364

279365
if (end == dptr)
280366
return 0;
@@ -509,7 +595,6 @@ int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr,
509595
union nf_inet_addr *addr, __be16 *port)
510596
{
511597
const char *c, *limit = dptr + datalen;
512-
unsigned int p;
513598
int ret;
514599

515600
ret = ct_sip_walk_headers(ct, dptr, dataoff ? *dataoff : 0, datalen,
@@ -520,14 +605,8 @@ int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr,
520605

521606
if (!sip_parse_addr(ct, dptr + *matchoff, &c, addr, limit, true))
522607
return -1;
523-
if (*c == ':') {
524-
c++;
525-
p = simple_strtoul(c, (char **)&c, 10);
526-
if (p < 1024 || p > 65535)
527-
return -1;
528-
*port = htons(p);
529-
} else
530-
*port = htons(SIP_PORT);
608+
if (!sip_parse_port(c, &c, limit, port))
609+
return -1;
531610

532611
if (dataoff)
533612
*dataoff = c - dptr;
@@ -609,7 +688,7 @@ int ct_sip_parse_numerical_param(const struct nf_conn *ct, const char *dptr,
609688
return 0;
610689

611690
start += strlen(name);
612-
*val = simple_strtoul(start, &end, 0);
691+
*val = sip_strtouint(start, limit - start, (char **)&end);
613692
if (start == end)
614693
return -1;
615694
if (matchoff && matchlen) {
@@ -1065,6 +1144,8 @@ static int process_sdp(struct sk_buff *skb, unsigned int protoff,
10651144

10661145
mediaoff = sdpoff;
10671146
for (i = 0; i < ARRAY_SIZE(sdp_media_types); ) {
1147+
char *end;
1148+
10681149
if (ct_sip_get_sdp_header(ct, *dptr, mediaoff, *datalen,
10691150
SDP_HDR_MEDIA, SDP_HDR_UNSPEC,
10701151
&mediaoff, &medialen) <= 0)
@@ -1080,8 +1161,8 @@ static int process_sdp(struct sk_buff *skb, unsigned int protoff,
10801161
mediaoff += t->len;
10811162
medialen -= t->len;
10821163

1083-
port = simple_strtoul(*dptr + mediaoff, NULL, 10);
1084-
if (port == 0)
1164+
port = sip_strtouint(*dptr + mediaoff, *datalen - mediaoff, (char **)&end);
1165+
if (port == 0 || *dptr + mediaoff == end)
10851166
continue;
10861167
if (port < 1024 || port > 65535) {
10871168
nf_ct_helper_log(skb, ct, "wrong port %u", port);
@@ -1255,7 +1336,7 @@ static int process_register_request(struct sk_buff *skb, unsigned int protoff,
12551336
*/
12561337
if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
12571338
&matchoff, &matchlen) > 0)
1258-
expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1339+
expires = sip_strtouint(*dptr + matchoff, *datalen - matchoff, NULL);
12591340

12601341
ret = ct_sip_parse_header_uri(ct, *dptr, NULL, *datalen,
12611342
SIP_HDR_CONTACT, NULL,
@@ -1359,7 +1440,7 @@ static int process_register_response(struct sk_buff *skb, unsigned int protoff,
13591440

13601441
if (ct_sip_get_header(ct, *dptr, 0, *datalen, SIP_HDR_EXPIRES,
13611442
&matchoff, &matchlen) > 0)
1362-
expires = simple_strtoul(*dptr + matchoff, NULL, 10);
1443+
expires = sip_strtouint(*dptr + matchoff, *datalen - matchoff, NULL);
13631444

13641445
while (1) {
13651446
unsigned int c_expires = expires;
@@ -1419,10 +1500,12 @@ static int process_sip_response(struct sk_buff *skb, unsigned int protoff,
14191500
struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
14201501
unsigned int matchoff, matchlen, matchend;
14211502
unsigned int code, cseq, i;
1503+
char *end;
14221504

14231505
if (*datalen < strlen("SIP/2.0 200"))
14241506
return NF_ACCEPT;
1425-
code = simple_strtoul(*dptr + strlen("SIP/2.0 "), NULL, 10);
1507+
code = sip_strtouint(*dptr + strlen("SIP/2.0 "),
1508+
*datalen - strlen("SIP/2.0 "), NULL);
14261509
if (!code) {
14271510
nf_ct_helper_log(skb, ct, "cannot get code");
14281511
return NF_DROP;
@@ -1433,8 +1516,8 @@ static int process_sip_response(struct sk_buff *skb, unsigned int protoff,
14331516
nf_ct_helper_log(skb, ct, "cannot parse cseq");
14341517
return NF_DROP;
14351518
}
1436-
cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
1437-
if (!cseq && *(*dptr + matchoff) != '0') {
1519+
cseq = sip_strtouint(*dptr + matchoff, *datalen - matchoff, (char **)&end);
1520+
if (*dptr + matchoff == end) {
14381521
nf_ct_helper_log(skb, ct, "cannot get cseq");
14391522
return NF_DROP;
14401523
}
@@ -1483,6 +1566,7 @@ static int process_sip_request(struct sk_buff *skb, unsigned int protoff,
14831566

14841567
for (i = 0; i < ARRAY_SIZE(sip_handlers); i++) {
14851568
const struct sip_handler *handler;
1569+
char *end;
14861570

14871571
handler = &sip_handlers[i];
14881572
if (handler->request == NULL)
@@ -1499,8 +1583,8 @@ static int process_sip_request(struct sk_buff *skb, unsigned int protoff,
14991583
nf_ct_helper_log(skb, ct, "cannot parse cseq");
15001584
return NF_DROP;
15011585
}
1502-
cseq = simple_strtoul(*dptr + matchoff, NULL, 10);
1503-
if (!cseq && *(*dptr + matchoff) != '0') {
1586+
cseq = sip_strtouint(*dptr + matchoff, *datalen - matchoff, (char **)&end);
1587+
if (*dptr + matchoff == end) {
15041588
nf_ct_helper_log(skb, ct, "cannot get cseq");
15051589
return NF_DROP;
15061590
}
@@ -1576,7 +1660,7 @@ static int sip_help_tcp(struct sk_buff *skb, unsigned int protoff,
15761660
&matchoff, &matchlen) <= 0)
15771661
break;
15781662

1579-
clen = simple_strtoul(dptr + matchoff, (char **)&end, 10);
1663+
clen = sip_strtouint(dptr + matchoff, datalen - matchoff, (char **)&end);
15801664
if (dptr + matchoff == end)
15811665
break;
15821666

net/netfilter/nf_nat_sip.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ static unsigned int nf_nat_sip(struct sk_buff *skb, unsigned int protoff,
246246
if (ct_sip_parse_numerical_param(ct, *dptr, matchend, *datalen,
247247
"rport=", &poff, &plen,
248248
&n) > 0 &&
249+
n >= 1024 && n <= 65535 &&
249250
htons(n) == ct->tuplehash[dir].tuple.dst.u.udp.port &&
250251
htons(n) != ct->tuplehash[!dir].tuple.src.u.udp.port) {
251252
__be16 p = ct->tuplehash[!dir].tuple.src.u.udp.port;

0 commit comments

Comments
 (0)