Skip to content

Commit 7814b6e

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: xtables: don't save/restore jumpstack offset
In most cases there is no reentrancy into ip/ip6tables. For skbs sent by REJECT or SYNPROXY targets, there is one level of reentrancy, but its not relevant as those targets issue an absolute verdict, i.e. the jumpstack can be clobbered since its not used after the target issues absolute verdict (ACCEPT, DROP, STOLEN, etc). So the only special case where it is relevant is the TEE target, which returns XT_CONTINUE. This patch changes ip(6)_do_table to always use the jump stack starting from 0. When we detect we're operating on an skb sent via TEE (percpu nf_skb_duplicated is 1) we switch to an alternate stack to leave the original one alone. Since there is no TEE support for arptables, it doesn't need to test if tee is active. The jump stack overflow tests are no longer needed as well -- since ->stacksize is the largest call depth we cannot exceed it. A much better alternative to the external jumpstack would be to just declare a jumps[32] stack on the local stack frame, but that would mean we'd have to reject iptables rulesets that used to work before. Another alternative would be to start rejecting rulesets with a larger call depth, e.g. 1000 -- in this case it would be feasible to allocate the entire stack in the percpu area which would avoid one dereference. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent e7c8899 commit 7814b6e

5 files changed

Lines changed: 48 additions & 49 deletions

File tree

‎include/linux/netfilter/x_tables.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ struct xt_table_info {
222222
* @stacksize jumps (number of user chains) can possibly be made.
223223
*/
224224
unsigned int stacksize;
225-
unsigned int __percpu *stackptr;
226225
void ***jumpstack;
227226

228227
unsigned char entries[0] __aligned(8);

‎net/ipv4/netfilter/arp_tables.c‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ unsigned int arpt_do_table(struct sk_buff *skb,
280280
table_base = private->entries;
281281
jumpstack = (struct arpt_entry **)private->jumpstack[cpu];
282282

283+
/* No TEE support for arptables, so no need to switch to alternate
284+
* stack. All targets that reenter must return absolute verdicts.
285+
*/
283286
e = get_entry(table_base, private->hook_entry[hook]);
284287

285288
acpar.in = state->in;
@@ -325,21 +328,13 @@ unsigned int arpt_do_table(struct sk_buff *skb,
325328
}
326329
if (table_base + v
327330
!= arpt_next_entry(e)) {
328-
329-
if (stackidx >= private->stacksize) {
330-
verdict = NF_DROP;
331-
break;
332-
}
333331
jumpstack[stackidx++] = e;
334332
}
335333

336334
e = get_entry(table_base, v);
337335
continue;
338336
}
339337

340-
/* Targets which reenter must return
341-
* abs. verdicts
342-
*/
343338
acpar.target = t->u.kernel.target;
344339
acpar.targinfo = t->data;
345340
verdict = t->u.kernel.target->target(skb, &acpar);

‎net/ipv4/netfilter/ip_tables.c‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,13 @@ ipt_do_table(struct sk_buff *skb,
296296
const char *indev, *outdev;
297297
const void *table_base;
298298
struct ipt_entry *e, **jumpstack;
299-
unsigned int *stackptr, origptr, cpu;
299+
unsigned int stackidx, cpu;
300300
const struct xt_table_info *private;
301301
struct xt_action_param acpar;
302302
unsigned int addend;
303303

304304
/* Initialization */
305+
stackidx = 0;
305306
ip = ip_hdr(skb);
306307
indev = state->in ? state->in->name : nulldevname;
307308
outdev = state->out ? state->out->name : nulldevname;
@@ -331,13 +332,20 @@ ipt_do_table(struct sk_buff *skb,
331332
smp_read_barrier_depends();
332333
table_base = private->entries;
333334
jumpstack = (struct ipt_entry **)private->jumpstack[cpu];
334-
stackptr = per_cpu_ptr(private->stackptr, cpu);
335-
origptr = *stackptr;
335+
336+
/* Switch to alternate jumpstack if we're being invoked via TEE.
337+
* TEE issues XT_CONTINUE verdict on original skb so we must not
338+
* clobber the jumpstack.
339+
*
340+
* For recursion via REJECT or SYNPROXY the stack will be clobbered
341+
* but it is no problem since absolute verdict is issued by these.
342+
*/
343+
jumpstack += private->stacksize * __this_cpu_read(nf_skb_duplicated);
336344

337345
e = get_entry(table_base, private->hook_entry[hook]);
338346

339-
pr_debug("Entering %s(hook %u); sp at %u (UF %p)\n",
340-
table->name, hook, origptr,
347+
pr_debug("Entering %s(hook %u), UF %p\n",
348+
table->name, hook,
341349
get_entry(table_base, private->underflow[hook]));
342350

343351
do {
@@ -383,28 +391,24 @@ ipt_do_table(struct sk_buff *skb,
383391
verdict = (unsigned int)(-v) - 1;
384392
break;
385393
}
386-
if (*stackptr <= origptr) {
394+
if (stackidx == 0) {
387395
e = get_entry(table_base,
388396
private->underflow[hook]);
389397
pr_debug("Underflow (this is normal) "
390398
"to %p\n", e);
391399
} else {
392-
e = jumpstack[--*stackptr];
400+
e = jumpstack[--stackidx];
393401
pr_debug("Pulled %p out from pos %u\n",
394-
e, *stackptr);
402+
e, stackidx);
395403
e = ipt_next_entry(e);
396404
}
397405
continue;
398406
}
399407
if (table_base + v != ipt_next_entry(e) &&
400408
!(e->ip.flags & IPT_F_GOTO)) {
401-
if (*stackptr >= private->stacksize) {
402-
verdict = NF_DROP;
403-
break;
404-
}
405-
jumpstack[(*stackptr)++] = e;
409+
jumpstack[stackidx++] = e;
406410
pr_debug("Pushed %p into pos %u\n",
407-
e, *stackptr - 1);
411+
e, stackidx - 1);
408412
}
409413

410414
e = get_entry(table_base, v);
@@ -423,9 +427,8 @@ ipt_do_table(struct sk_buff *skb,
423427
/* Verdict */
424428
break;
425429
} while (!acpar.hotdrop);
426-
pr_debug("Exiting %s; resetting sp from %u to %u\n",
427-
__func__, *stackptr, origptr);
428-
*stackptr = origptr;
430+
pr_debug("Exiting %s; sp at %u\n", __func__, stackidx);
431+
429432
xt_write_recseq_end(addend);
430433
local_bh_enable();
431434

‎net/ipv6/netfilter/ip6_tables.c‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,13 @@ ip6t_do_table(struct sk_buff *skb,
324324
const char *indev, *outdev;
325325
const void *table_base;
326326
struct ip6t_entry *e, **jumpstack;
327-
unsigned int *stackptr, origptr, cpu;
327+
unsigned int stackidx, cpu;
328328
const struct xt_table_info *private;
329329
struct xt_action_param acpar;
330330
unsigned int addend;
331331

332332
/* Initialization */
333+
stackidx = 0;
333334
indev = state->in ? state->in->name : nulldevname;
334335
outdev = state->out ? state->out->name : nulldevname;
335336
/* We handle fragments by dealing with the first fragment as
@@ -357,8 +358,15 @@ ip6t_do_table(struct sk_buff *skb,
357358
cpu = smp_processor_id();
358359
table_base = private->entries;
359360
jumpstack = (struct ip6t_entry **)private->jumpstack[cpu];
360-
stackptr = per_cpu_ptr(private->stackptr, cpu);
361-
origptr = *stackptr;
361+
362+
/* Switch to alternate jumpstack if we're being invoked via TEE.
363+
* TEE issues XT_CONTINUE verdict on original skb so we must not
364+
* clobber the jumpstack.
365+
*
366+
* For recursion via REJECT or SYNPROXY the stack will be clobbered
367+
* but it is no problem since absolute verdict is issued by these.
368+
*/
369+
jumpstack += private->stacksize * __this_cpu_read(nf_skb_duplicated);
362370

363371
e = get_entry(table_base, private->hook_entry[hook]);
364372

@@ -406,20 +414,16 @@ ip6t_do_table(struct sk_buff *skb,
406414
verdict = (unsigned int)(-v) - 1;
407415
break;
408416
}
409-
if (*stackptr <= origptr)
417+
if (stackidx == 0)
410418
e = get_entry(table_base,
411419
private->underflow[hook]);
412420
else
413-
e = ip6t_next_entry(jumpstack[--*stackptr]);
421+
e = ip6t_next_entry(jumpstack[--stackidx]);
414422
continue;
415423
}
416424
if (table_base + v != ip6t_next_entry(e) &&
417425
!(e->ipv6.flags & IP6T_F_GOTO)) {
418-
if (*stackptr >= private->stacksize) {
419-
verdict = NF_DROP;
420-
break;
421-
}
422-
jumpstack[(*stackptr)++] = e;
426+
jumpstack[stackidx++] = e;
423427
}
424428

425429
e = get_entry(table_base, v);
@@ -437,8 +441,6 @@ ip6t_do_table(struct sk_buff *skb,
437441
break;
438442
} while (!acpar.hotdrop);
439443

440-
*stackptr = origptr;
441-
442444
xt_write_recseq_end(addend);
443445
local_bh_enable();
444446

‎net/netfilter/x_tables.c‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
6767
[NFPROTO_IPV6] = "ip6",
6868
};
6969

70-
/* Allow this many total (re)entries. */
71-
static const unsigned int xt_jumpstack_multiplier = 2;
72-
7370
/* Registration hooks for targets. */
7471
int xt_register_target(struct xt_target *target)
7572
{
@@ -688,8 +685,6 @@ void xt_free_table_info(struct xt_table_info *info)
688685
kvfree(info->jumpstack);
689686
}
690687

691-
free_percpu(info->stackptr);
692-
693688
kvfree(info);
694689
}
695690
EXPORT_SYMBOL(xt_free_table_info);
@@ -737,10 +732,6 @@ static int xt_jumpstack_alloc(struct xt_table_info *i)
737732
unsigned int size;
738733
int cpu;
739734

740-
i->stackptr = alloc_percpu(unsigned int);
741-
if (i->stackptr == NULL)
742-
return -ENOMEM;
743-
744735
size = sizeof(void **) * nr_cpu_ids;
745736
if (size > PAGE_SIZE)
746737
i->jumpstack = vzalloc(size);
@@ -753,8 +744,17 @@ static int xt_jumpstack_alloc(struct xt_table_info *i)
753744
if (i->stacksize == 0)
754745
return 0;
755746

756-
i->stacksize *= xt_jumpstack_multiplier;
757-
size = sizeof(void *) * i->stacksize;
747+
/* Jumpstack needs to be able to record two full callchains, one
748+
* from the first rule set traversal, plus one table reentrancy
749+
* via -j TEE without clobbering the callchain that brought us to
750+
* TEE target.
751+
*
752+
* This is done by allocating two jumpstacks per cpu, on reentry
753+
* the upper half of the stack is used.
754+
*
755+
* see the jumpstack setup in ipt_do_table() for more details.
756+
*/
757+
size = sizeof(void *) * i->stacksize * 2u;
758758
for_each_possible_cpu(cpu) {
759759
if (size > PAGE_SIZE)
760760
i->jumpstack[cpu] = vmalloc_node(size,

0 commit comments

Comments
 (0)