Skip to content

Commit f4a87e7

Browse files
kaberummakynes
authored andcommitted
netfilter: synproxy: fix BUG_ON triggered by corrupt TCP packets
TCP packets hitting the SYN proxy through the SYNPROXY target are not validated by TCP conntrack. When th->doff is below 5, an underflow happens when calculating the options length, causing skb_header_pointer() to return NULL and triggering the BUG_ON(). Handle this case gracefully by checking for NULL instead of using BUG_ON(). Reported-by: Martin Topholm <mph@one.com> Tested-by: Martin Topholm <mph@one.com> Signed-off-by: Patrick McHardy <kaber@trash.net> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent d1ee4fe commit f4a87e7

4 files changed

Lines changed: 22 additions & 12 deletions

File tree

‎include/net/netfilter/nf_conntrack_synproxy.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct synproxy_options {
5656

5757
struct tcphdr;
5858
struct xt_synproxy_info;
59-
extern void synproxy_parse_options(const struct sk_buff *skb, unsigned int doff,
59+
extern bool synproxy_parse_options(const struct sk_buff *skb, unsigned int doff,
6060
const struct tcphdr *th,
6161
struct synproxy_options *opts);
6262
extern unsigned int synproxy_options_size(const struct synproxy_options *opts);

‎net/ipv4/netfilter/ipt_SYNPROXY.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ synproxy_tg4(struct sk_buff *skb, const struct xt_action_param *par)
267267
if (th == NULL)
268268
return NF_DROP;
269269

270-
synproxy_parse_options(skb, par->thoff, th, &opts);
270+
if (!synproxy_parse_options(skb, par->thoff, th, &opts))
271+
return NF_DROP;
271272

272273
if (th->syn && !(th->ack || th->fin || th->rst)) {
273274
/* Initial SYN from client */
@@ -350,7 +351,8 @@ static unsigned int ipv4_synproxy_hook(unsigned int hooknum,
350351

351352
/* fall through */
352353
case TCP_CONNTRACK_SYN_SENT:
353-
synproxy_parse_options(skb, thoff, th, &opts);
354+
if (!synproxy_parse_options(skb, thoff, th, &opts))
355+
return NF_DROP;
354356

355357
if (!th->syn && th->ack &&
356358
CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
@@ -373,7 +375,9 @@ static unsigned int ipv4_synproxy_hook(unsigned int hooknum,
373375
if (!th->syn || !th->ack)
374376
break;
375377

376-
synproxy_parse_options(skb, thoff, th, &opts);
378+
if (!synproxy_parse_options(skb, thoff, th, &opts))
379+
return NF_DROP;
380+
377381
if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
378382
synproxy->tsoff = opts.tsval - synproxy->its;
379383

‎net/ipv6/netfilter/ip6t_SYNPROXY.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ synproxy_tg6(struct sk_buff *skb, const struct xt_action_param *par)
282282
if (th == NULL)
283283
return NF_DROP;
284284

285-
synproxy_parse_options(skb, par->thoff, th, &opts);
285+
if (!synproxy_parse_options(skb, par->thoff, th, &opts))
286+
return NF_DROP;
286287

287288
if (th->syn && !(th->ack || th->fin || th->rst)) {
288289
/* Initial SYN from client */
@@ -372,7 +373,8 @@ static unsigned int ipv6_synproxy_hook(unsigned int hooknum,
372373

373374
/* fall through */
374375
case TCP_CONNTRACK_SYN_SENT:
375-
synproxy_parse_options(skb, thoff, th, &opts);
376+
if (!synproxy_parse_options(skb, thoff, th, &opts))
377+
return NF_DROP;
376378

377379
if (!th->syn && th->ack &&
378380
CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
@@ -395,7 +397,9 @@ static unsigned int ipv6_synproxy_hook(unsigned int hooknum,
395397
if (!th->syn || !th->ack)
396398
break;
397399

398-
synproxy_parse_options(skb, thoff, th, &opts);
400+
if (!synproxy_parse_options(skb, thoff, th, &opts))
401+
return NF_DROP;
402+
399403
if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
400404
synproxy->tsoff = opts.tsval - synproxy->its;
401405

‎net/netfilter/nf_synproxy_core.c‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@
2424
int synproxy_net_id;
2525
EXPORT_SYMBOL_GPL(synproxy_net_id);
2626

27-
void
27+
bool
2828
synproxy_parse_options(const struct sk_buff *skb, unsigned int doff,
2929
const struct tcphdr *th, struct synproxy_options *opts)
3030
{
3131
int length = (th->doff * 4) - sizeof(*th);
3232
u8 buf[40], *ptr;
3333

3434
ptr = skb_header_pointer(skb, doff + sizeof(*th), length, buf);
35-
BUG_ON(ptr == NULL);
35+
if (ptr == NULL)
36+
return false;
3637

3738
opts->options = 0;
3839
while (length > 0) {
@@ -41,16 +42,16 @@ synproxy_parse_options(const struct sk_buff *skb, unsigned int doff,
4142

4243
switch (opcode) {
4344
case TCPOPT_EOL:
44-
return;
45+
return true;
4546
case TCPOPT_NOP:
4647
length--;
4748
continue;
4849
default:
4950
opsize = *ptr++;
5051
if (opsize < 2)
51-
return;
52+
return true;
5253
if (opsize > length)
53-
return;
54+
return true;
5455

5556
switch (opcode) {
5657
case TCPOPT_MSS:
@@ -84,6 +85,7 @@ synproxy_parse_options(const struct sk_buff *skb, unsigned int doff,
8485
length -= opsize;
8586
}
8687
}
88+
return true;
8789
}
8890
EXPORT_SYMBOL_GPL(synproxy_parse_options);
8991

0 commit comments

Comments
 (0)