1 /*
2 * Packet matching code.
3 *
4 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
5 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
6 * Copyright (C) 2006-2010 Patrick McHardy <kaber@trash.net>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/cache.h>
14 #include <linux/capability.h>
15 #include <linux/skbuff.h>
16 #include <linux/kmod.h>
17 #include <linux/vmalloc.h>
18 #include <linux/netdevice.h>
19 #include <linux/module.h>
20 #include <linux/icmp.h>
21 #include <net/ip.h>
22 #include <net/compat.h>
23 #include <asm/uaccess.h>
24 #include <linux/mutex.h>
25 #include <linux/proc_fs.h>
26 #include <linux/err.h>
27 #include <linux/cpumask.h>
28
29 #include <linux/netfilter/x_tables.h>
30 #include <linux/netfilter_ipv4/ip_tables.h>
31 #include <net/netfilter/nf_log.h>
32 #include "../../netfilter/xt_repldata.h"
33
34 MODULE_LICENSE("GPL");
35 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
36 MODULE_DESCRIPTION("IPv4 packet filter");
37
38 #ifdef CONFIG_NETFILTER_DEBUG
39 #define IP_NF_ASSERT(x) WARN_ON(!(x))
40 #else
41 #define IP_NF_ASSERT(x)
42 #endif
43
ipt_alloc_initial_table(const struct xt_table * info)44 void *ipt_alloc_initial_table(const struct xt_table *info)
45 {
46 return xt_alloc_initial_table(ipt, IPT);
47 }
48 EXPORT_SYMBOL_GPL(ipt_alloc_initial_table);
49
50 /* Returns whether matches rule or not. */
51 /* Performance critical - called for every packet */
52 static inline bool
ip_packet_match(const struct iphdr * ip,const char * indev,const char * outdev,const struct ipt_ip * ipinfo,int isfrag)53 ip_packet_match(const struct iphdr *ip,
54 const char *indev,
55 const char *outdev,
56 const struct ipt_ip *ipinfo,
57 int isfrag)
58 {
59 unsigned long ret;
60
61 if (NF_INVF(ipinfo, IPT_INV_SRCIP,
62 (ip->saddr & ipinfo->smsk.s_addr) != ipinfo->src.s_addr) ||
63 NF_INVF(ipinfo, IPT_INV_DSTIP,
64 (ip->daddr & ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr))
65 return false;
66
67 ret = ifname_compare_aligned(indev, ipinfo->iniface, ipinfo->iniface_mask);
68
69 if (NF_INVF(ipinfo, IPT_INV_VIA_IN, ret != 0))
70 return false;
71
72 ret = ifname_compare_aligned(outdev, ipinfo->outiface, ipinfo->outiface_mask);
73
74 if (NF_INVF(ipinfo, IPT_INV_VIA_OUT, ret != 0))
75 return false;
76
77 /* Check specific protocol */
78 if (ipinfo->proto &&
79 NF_INVF(ipinfo, IPT_INV_PROTO, ip->protocol != ipinfo->proto))
80 return false;
81
82 /* If we have a fragment rule but the packet is not a fragment
83 * then we return zero */
84 if (NF_INVF(ipinfo, IPT_INV_FRAG,
85 (ipinfo->flags & IPT_F_FRAG) && !isfrag))
86 return false;
87
88 return true;
89 }
90
91 static bool
ip_checkentry(const struct ipt_ip * ip)92 ip_checkentry(const struct ipt_ip *ip)
93 {
94 if (ip->flags & ~IPT_F_MASK)
95 return false;
96 if (ip->invflags & ~IPT_INV_MASK)
97 return false;
98 return true;
99 }
100
101 static unsigned int
ipt_error(struct sk_buff * skb,const struct xt_action_param * par)102 ipt_error(struct sk_buff *skb, const struct xt_action_param *par)
103 {
104 net_info_ratelimited("error: `%s'\n", (const char *)par->targinfo);
105
106 return NF_DROP;
107 }
108
109 /* Performance critical */
110 static inline struct ipt_entry *
get_entry(const void * base,unsigned int offset)111 get_entry(const void *base, unsigned int offset)
112 {
113 return (struct ipt_entry *)(base + offset);
114 }
115
116 /* All zeroes == unconditional rule. */
117 /* Mildly perf critical (only if packet tracing is on) */
unconditional(const struct ipt_entry * e)118 static inline bool unconditional(const struct ipt_entry *e)
119 {
120 static const struct ipt_ip uncond;
121
122 return e->target_offset == sizeof(struct ipt_entry) &&
123 memcmp(&e->ip, &uncond, sizeof(uncond)) == 0;
124 }
125
126 /* for const-correctness */
127 static inline const struct xt_entry_target *
ipt_get_target_c(const struct ipt_entry * e)128 ipt_get_target_c(const struct ipt_entry *e)
129 {
130 return ipt_get_target((struct ipt_entry *)e);
131 }
132
133 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
134 static const char *const hooknames[] = {
135 [NF_INET_PRE_ROUTING] = "PREROUTING",
136 [NF_INET_LOCAL_IN] = "INPUT",
137 [NF_INET_FORWARD] = "FORWARD",
138 [NF_INET_LOCAL_OUT] = "OUTPUT",
139 [NF_INET_POST_ROUTING] = "POSTROUTING",
140 };
141
142 enum nf_ip_trace_comments {
143 NF_IP_TRACE_COMMENT_RULE,
144 NF_IP_TRACE_COMMENT_RETURN,
145 NF_IP_TRACE_COMMENT_POLICY,
146 };
147
148 static const char *const comments[] = {
149 [NF_IP_TRACE_COMMENT_RULE] = "rule",
150 [NF_IP_TRACE_COMMENT_RETURN] = "return",
151 [NF_IP_TRACE_COMMENT_POLICY] = "policy",
152 };
153
154 static struct nf_loginfo trace_loginfo = {
155 .type = NF_LOG_TYPE_LOG,
156 .u = {
157 .log = {
158 .level = 4,
159 .logflags = NF_LOG_DEFAULT_MASK,
160 },
161 },
162 };
163
164 /* Mildly perf critical (only if packet tracing is on) */
165 static inline int
get_chainname_rulenum(const struct ipt_entry * s,const struct ipt_entry * e,const char * hookname,const char ** chainname,const char ** comment,unsigned int * rulenum)166 get_chainname_rulenum(const struct ipt_entry *s, const struct ipt_entry *e,
167 const char *hookname, const char **chainname,
168 const char **comment, unsigned int *rulenum)
169 {
170 const struct xt_standard_target *t = (void *)ipt_get_target_c(s);
171
172 if (strcmp(t->target.u.kernel.target->name, XT_ERROR_TARGET) == 0) {
173 /* Head of user chain: ERROR target with chainname */
174 *chainname = t->target.data;
175 (*rulenum) = 0;
176 } else if (s == e) {
177 (*rulenum)++;
178
179 if (unconditional(s) &&
180 strcmp(t->target.u.kernel.target->name,
181 XT_STANDARD_TARGET) == 0 &&
182 t->verdict < 0) {
183 /* Tail of chains: STANDARD target (return/policy) */
184 *comment = *chainname == hookname
185 ? comments[NF_IP_TRACE_COMMENT_POLICY]
186 : comments[NF_IP_TRACE_COMMENT_RETURN];
187 }
188 return 1;
189 } else
190 (*rulenum)++;
191
192 return 0;
193 }
194
trace_packet(struct net * net,const struct sk_buff * skb,unsigned int hook,const struct net_device * in,const struct net_device * out,const char * tablename,const struct xt_table_info * private,const struct ipt_entry * e)195 static void trace_packet(struct net *net,
196 const struct sk_buff *skb,
197 unsigned int hook,
198 const struct net_device *in,
199 const struct net_device *out,
200 const char *tablename,
201 const struct xt_table_info *private,
202 const struct ipt_entry *e)
203 {
204 const struct ipt_entry *root;
205 const char *hookname, *chainname, *comment;
206 const struct ipt_entry *iter;
207 unsigned int rulenum = 0;
208
209 root = get_entry(private->entries, private->hook_entry[hook]);
210
211 hookname = chainname = hooknames[hook];
212 comment = comments[NF_IP_TRACE_COMMENT_RULE];
213
214 xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
215 if (get_chainname_rulenum(iter, e, hookname,
216 &chainname, &comment, &rulenum) != 0)
217 break;
218
219 nf_log_trace(net, AF_INET, hook, skb, in, out, &trace_loginfo,
220 "TRACE: %s:%s:%s:%u ",
221 tablename, chainname, comment, rulenum);
222 }
223 #endif
224
225 static inline
ipt_next_entry(const struct ipt_entry * entry)226 struct ipt_entry *ipt_next_entry(const struct ipt_entry *entry)
227 {
228 return (void *)entry + entry->next_offset;
229 }
230
231 /* Returns one of the generic firewall policies, like NF_ACCEPT. */
232 unsigned int
ipt_do_table(struct sk_buff * skb,const struct nf_hook_state * state,struct xt_table * table)233 ipt_do_table(struct sk_buff *skb,
234 const struct nf_hook_state *state,
235 struct xt_table *table)
236 {
237 unsigned int hook = state->hook;
238 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
239 const struct iphdr *ip;
240 /* Initializing verdict to NF_DROP keeps gcc happy. */
241 unsigned int verdict = NF_DROP;
242 const char *indev, *outdev;
243 const void *table_base;
244 struct ipt_entry *e, **jumpstack;
245 unsigned int stackidx, cpu;
246 const struct xt_table_info *private;
247 struct xt_action_param acpar;
248 unsigned int addend;
249
250 /* Initialization */
251 stackidx = 0;
252 ip = ip_hdr(skb);
253 indev = state->in ? state->in->name : nulldevname;
254 outdev = state->out ? state->out->name : nulldevname;
255 /* We handle fragments by dealing with the first fragment as
256 * if it was a normal packet. All other fragments are treated
257 * normally, except that they will NEVER match rules that ask
258 * things we don't know, ie. tcp syn flag or ports). If the
259 * rule is also a fragment-specific rule, non-fragments won't
260 * match it. */
261 acpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
262 acpar.thoff = ip_hdrlen(skb);
263 acpar.hotdrop = false;
264 acpar.net = state->net;
265 acpar.in = state->in;
266 acpar.out = state->out;
267 acpar.family = NFPROTO_IPV4;
268 acpar.hooknum = hook;
269
270 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
271 local_bh_disable();
272 addend = xt_write_recseq_begin();
273 private = table->private;
274 cpu = smp_processor_id();
275 /*
276 * Ensure we load private-> members after we've fetched the base
277 * pointer.
278 */
279 smp_read_barrier_depends();
280 table_base = private->entries;
281 jumpstack = (struct ipt_entry **)private->jumpstack[cpu];
282
283 /* Switch to alternate jumpstack if we're being invoked via TEE.
284 * TEE issues XT_CONTINUE verdict on original skb so we must not
285 * clobber the jumpstack.
286 *
287 * For recursion via REJECT or SYNPROXY the stack will be clobbered
288 * but it is no problem since absolute verdict is issued by these.
289 */
290 if (static_key_false(&xt_tee_enabled))
291 jumpstack += private->stacksize * __this_cpu_read(nf_skb_duplicated);
292
293 e = get_entry(table_base, private->hook_entry[hook]);
294
295 do {
296 const struct xt_entry_target *t;
297 const struct xt_entry_match *ematch;
298 struct xt_counters *counter;
299
300 IP_NF_ASSERT(e);
301 if (!ip_packet_match(ip, indev, outdev,
302 &e->ip, acpar.fragoff)) {
303 no_match:
304 e = ipt_next_entry(e);
305 continue;
306 }
307
308 xt_ematch_foreach(ematch, e) {
309 acpar.match = ematch->u.kernel.match;
310 acpar.matchinfo = ematch->data;
311 if (!acpar.match->match(skb, &acpar))
312 goto no_match;
313 }
314
315 counter = xt_get_this_cpu_counter(&e->counters);
316 ADD_COUNTER(*counter, skb->len, 1);
317
318 t = ipt_get_target(e);
319 IP_NF_ASSERT(t->u.kernel.target);
320
321 #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
322 /* The packet is traced: log it */
323 if (unlikely(skb->nf_trace))
324 trace_packet(state->net, skb, hook, state->in,
325 state->out, table->name, private, e);
326 #endif
327 /* Standard target? */
328 if (!t->u.kernel.target->target) {
329 int v;
330
331 v = ((struct xt_standard_target *)t)->verdict;
332 if (v < 0) {
333 /* Pop from stack? */
334 if (v != XT_RETURN) {
335 verdict = (unsigned int)(-v) - 1;
336 break;
337 }
338 if (stackidx == 0) {
339 e = get_entry(table_base,
340 private->underflow[hook]);
341 } else {
342 e = jumpstack[--stackidx];
343 e = ipt_next_entry(e);
344 }
345 continue;
346 }
347 if (table_base + v != ipt_next_entry(e) &&
348 !(e->ip.flags & IPT_F_GOTO)) {
349 if (unlikely(stackidx >= private->stacksize)) {
350 verdict = NF_DROP;
351 break;
352 }
353 jumpstack[stackidx++] = e;
354 }
355
356 e = get_entry(table_base, v);
357 continue;
358 }
359
360 acpar.target = t->u.kernel.target;
361 acpar.targinfo = t->data;
362
363 verdict = t->u.kernel.target->target(skb, &acpar);
364 /* Target might have changed stuff. */
365 ip = ip_hdr(skb);
366 if (verdict == XT_CONTINUE)
367 e = ipt_next_entry(e);
368 else
369 /* Verdict */
370 break;
371 } while (!acpar.hotdrop);
372
373 xt_write_recseq_end(addend);
374 local_bh_enable();
375
376 if (acpar.hotdrop)
377 return NF_DROP;
378 else return verdict;
379 }
380
381 /* Figures out from what hook each rule can be called: returns 0 if
382 there are loops. Puts hook bitmask in comefrom. */
383 static int
mark_source_chains(const struct xt_table_info * newinfo,unsigned int valid_hooks,void * entry0,unsigned int * offsets)384 mark_source_chains(const struct xt_table_info *newinfo,
385 unsigned int valid_hooks, void *entry0,
386 unsigned int *offsets)
387 {
388 unsigned int hook;
389
390 /* No recursion; use packet counter to save back ptrs (reset
391 to 0 as we leave), and comefrom to save source hook bitmask */
392 for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
393 unsigned int pos = newinfo->hook_entry[hook];
394 struct ipt_entry *e = (struct ipt_entry *)(entry0 + pos);
395
396 if (!(valid_hooks & (1 << hook)))
397 continue;
398
399 /* Set initial back pointer. */
400 e->counters.pcnt = pos;
401
402 for (;;) {
403 const struct xt_standard_target *t
404 = (void *)ipt_get_target_c(e);
405 int visited = e->comefrom & (1 << hook);
406
407 if (e->comefrom & (1 << NF_INET_NUMHOOKS))
408 return 0;
409
410 e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
411
412 /* Unconditional return/END. */
413 if ((unconditional(e) &&
414 (strcmp(t->target.u.user.name,
415 XT_STANDARD_TARGET) == 0) &&
416 t->verdict < 0) || visited) {
417 unsigned int oldpos, size;
418
419 if ((strcmp(t->target.u.user.name,
420 XT_STANDARD_TARGET) == 0) &&
421 t->verdict < -NF_MAX_VERDICT - 1)
422 return 0;
423
424 /* Return: backtrack through the last
425 big jump. */
426 do {
427 e->comefrom ^= (1<<NF_INET_NUMHOOKS);
428 oldpos = pos;
429 pos = e->counters.pcnt;
430 e->counters.pcnt = 0;
431
432 /* We're at the start. */
433 if (pos == oldpos)
434 goto next;
435
436 e = (struct ipt_entry *)
437 (entry0 + pos);
438 } while (oldpos == pos + e->next_offset);
439
440 /* Move along one */
441 size = e->next_offset;
442 e = (struct ipt_entry *)
443 (entry0 + pos + size);
444 if (pos + size >= newinfo->size)
445 return 0;
446 e->counters.pcnt = pos;
447 pos += size;
448 } else {
449 int newpos = t->verdict;
450
451 if (strcmp(t->target.u.user.name,
452 XT_STANDARD_TARGET) == 0 &&
453 newpos >= 0) {
454 /* This a jump; chase it. */
455 if (!xt_find_jump_offset(offsets, newpos,
456 newinfo->number))
457 return 0;
458 e = (struct ipt_entry *)
459 (entry0 + newpos);
460 } else {
461 /* ... this is a fallthru */
462 newpos = pos + e->next_offset;
463 if (newpos >= newinfo->size)
464 return 0;
465 }
466 e = (struct ipt_entry *)
467 (entry0 + newpos);
468 e->counters.pcnt = pos;
469 pos = newpos;
470 }
471 }
472 next: ;
473 }
474 return 1;
475 }
476
cleanup_match(struct xt_entry_match * m,struct net * net)477 static void cleanup_match(struct xt_entry_match *m, struct net *net)
478 {
479 struct xt_mtdtor_param par;
480
481 par.net = net;
482 par.match = m->u.kernel.match;
483 par.matchinfo = m->data;
484 par.family = NFPROTO_IPV4;
485 if (par.match->destroy != NULL)
486 par.match->destroy(&par);
487 module_put(par.match->me);
488 }
489
490 static int
check_match(struct xt_entry_match * m,struct xt_mtchk_param * par)491 check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
492 {
493 const struct ipt_ip *ip = par->entryinfo;
494
495 par->match = m->u.kernel.match;
496 par->matchinfo = m->data;
497
498 return xt_check_match(par, m->u.match_size - sizeof(*m),
499 ip->proto, ip->invflags & IPT_INV_PROTO);
500 }
501
502 static int
find_check_match(struct xt_entry_match * m,struct xt_mtchk_param * par)503 find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
504 {
505 struct xt_match *match;
506 int ret;
507
508 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
509 m->u.user.revision);
510 if (IS_ERR(match))
511 return PTR_ERR(match);
512 m->u.kernel.match = match;
513
514 ret = check_match(m, par);
515 if (ret)
516 goto err;
517
518 return 0;
519 err:
520 module_put(m->u.kernel.match->me);
521 return ret;
522 }
523
check_target(struct ipt_entry * e,struct net * net,const char * name)524 static int check_target(struct ipt_entry *e, struct net *net, const char *name)
525 {
526 struct xt_entry_target *t = ipt_get_target(e);
527 struct xt_tgchk_param par = {
528 .net = net,
529 .table = name,
530 .entryinfo = e,
531 .target = t->u.kernel.target,
532 .targinfo = t->data,
533 .hook_mask = e->comefrom,
534 .family = NFPROTO_IPV4,
535 };
536
537 return xt_check_target(&par, t->u.target_size - sizeof(*t),
538 e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
539 }
540
541 static int
find_check_entry(struct ipt_entry * e,struct net * net,const char * name,unsigned int size,struct xt_percpu_counter_alloc_state * alloc_state)542 find_check_entry(struct ipt_entry *e, struct net *net, const char *name,
543 unsigned int size,
544 struct xt_percpu_counter_alloc_state *alloc_state)
545 {
546 struct xt_entry_target *t;
547 struct xt_target *target;
548 int ret;
549 unsigned int j;
550 struct xt_mtchk_param mtpar;
551 struct xt_entry_match *ematch;
552
553 if (!xt_percpu_counter_alloc(alloc_state, &e->counters))
554 return -ENOMEM;
555
556 j = 0;
557 mtpar.net = net;
558 mtpar.table = name;
559 mtpar.entryinfo = &e->ip;
560 mtpar.hook_mask = e->comefrom;
561 mtpar.family = NFPROTO_IPV4;
562 xt_ematch_foreach(ematch, e) {
563 ret = find_check_match(ematch, &mtpar);
564 if (ret != 0)
565 goto cleanup_matches;
566 ++j;
567 }
568
569 t = ipt_get_target(e);
570 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
571 t->u.user.revision);
572 if (IS_ERR(target)) {
573 ret = PTR_ERR(target);
574 goto cleanup_matches;
575 }
576 t->u.kernel.target = target;
577
578 ret = check_target(e, net, name);
579 if (ret)
580 goto err;
581
582 return 0;
583 err:
584 module_put(t->u.kernel.target->me);
585 cleanup_matches:
586 xt_ematch_foreach(ematch, e) {
587 if (j-- == 0)
588 break;
589 cleanup_match(ematch, net);
590 }
591
592 xt_percpu_counter_free(&e->counters);
593
594 return ret;
595 }
596
check_underflow(const struct ipt_entry * e)597 static bool check_underflow(const struct ipt_entry *e)
598 {
599 const struct xt_entry_target *t;
600 unsigned int verdict;
601
602 if (!unconditional(e))
603 return false;
604 t = ipt_get_target_c(e);
605 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
606 return false;
607 verdict = ((struct xt_standard_target *)t)->verdict;
608 verdict = -verdict - 1;
609 return verdict == NF_DROP || verdict == NF_ACCEPT;
610 }
611
612 static int
check_entry_size_and_hooks(struct ipt_entry * e,struct xt_table_info * newinfo,const unsigned char * base,const unsigned char * limit,const unsigned int * hook_entries,const unsigned int * underflows,unsigned int valid_hooks)613 check_entry_size_and_hooks(struct ipt_entry *e,
614 struct xt_table_info *newinfo,
615 const unsigned char *base,
616 const unsigned char *limit,
617 const unsigned int *hook_entries,
618 const unsigned int *underflows,
619 unsigned int valid_hooks)
620 {
621 unsigned int h;
622 int err;
623
624 if ((unsigned long)e % __alignof__(struct ipt_entry) != 0 ||
625 (unsigned char *)e + sizeof(struct ipt_entry) >= limit ||
626 (unsigned char *)e + e->next_offset > limit)
627 return -EINVAL;
628
629 if (e->next_offset
630 < sizeof(struct ipt_entry) + sizeof(struct xt_entry_target))
631 return -EINVAL;
632
633 if (!ip_checkentry(&e->ip))
634 return -EINVAL;
635
636 err = xt_check_entry_offsets(e, e->elems, e->target_offset,
637 e->next_offset);
638 if (err)
639 return err;
640
641 /* Check hooks & underflows */
642 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
643 if (!(valid_hooks & (1 << h)))
644 continue;
645 if ((unsigned char *)e - base == hook_entries[h])
646 newinfo->hook_entry[h] = hook_entries[h];
647 if ((unsigned char *)e - base == underflows[h]) {
648 if (!check_underflow(e))
649 return -EINVAL;
650
651 newinfo->underflow[h] = underflows[h];
652 }
653 }
654
655 /* Clear counters and comefrom */
656 e->counters = ((struct xt_counters) { 0, 0 });
657 e->comefrom = 0;
658 return 0;
659 }
660
661 static void
cleanup_entry(struct ipt_entry * e,struct net * net)662 cleanup_entry(struct ipt_entry *e, struct net *net)
663 {
664 struct xt_tgdtor_param par;
665 struct xt_entry_target *t;
666 struct xt_entry_match *ematch;
667
668 /* Cleanup all matches */
669 xt_ematch_foreach(ematch, e)
670 cleanup_match(ematch, net);
671 t = ipt_get_target(e);
672
673 par.net = net;
674 par.target = t->u.kernel.target;
675 par.targinfo = t->data;
676 par.family = NFPROTO_IPV4;
677 if (par.target->destroy != NULL)
678 par.target->destroy(&par);
679 module_put(par.target->me);
680 xt_percpu_counter_free(&e->counters);
681 }
682
683 /* Checks and translates the user-supplied table segment (held in
684 newinfo) */
685 static int
translate_table(struct net * net,struct xt_table_info * newinfo,void * entry0,const struct ipt_replace * repl)686 translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
687 const struct ipt_replace *repl)
688 {
689 struct xt_percpu_counter_alloc_state alloc_state = { 0 };
690 struct ipt_entry *iter;
691 unsigned int *offsets;
692 unsigned int i;
693 int ret = 0;
694
695 newinfo->size = repl->size;
696 newinfo->number = repl->num_entries;
697
698 /* Init all hooks to impossible value. */
699 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
700 newinfo->hook_entry[i] = 0xFFFFFFFF;
701 newinfo->underflow[i] = 0xFFFFFFFF;
702 }
703
704 offsets = xt_alloc_entry_offsets(newinfo->number);
705 if (!offsets)
706 return -ENOMEM;
707 i = 0;
708 /* Walk through entries, checking offsets. */
709 xt_entry_foreach(iter, entry0, newinfo->size) {
710 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
711 entry0 + repl->size,
712 repl->hook_entry,
713 repl->underflow,
714 repl->valid_hooks);
715 if (ret != 0)
716 goto out_free;
717 if (i < repl->num_entries)
718 offsets[i] = (void *)iter - entry0;
719 ++i;
720 if (strcmp(ipt_get_target(iter)->u.user.name,
721 XT_ERROR_TARGET) == 0)
722 ++newinfo->stacksize;
723 }
724
725 ret = -EINVAL;
726 if (i != repl->num_entries)
727 goto out_free;
728
729 /* Check hooks all assigned */
730 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
731 /* Only hooks which are valid */
732 if (!(repl->valid_hooks & (1 << i)))
733 continue;
734 if (newinfo->hook_entry[i] == 0xFFFFFFFF)
735 goto out_free;
736 if (newinfo->underflow[i] == 0xFFFFFFFF)
737 goto out_free;
738 }
739
740 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
741 ret = -ELOOP;
742 goto out_free;
743 }
744 kvfree(offsets);
745
746 /* Finally, each sanity check must pass */
747 i = 0;
748 xt_entry_foreach(iter, entry0, newinfo->size) {
749 ret = find_check_entry(iter, net, repl->name, repl->size,
750 &alloc_state);
751 if (ret != 0)
752 break;
753 ++i;
754 }
755
756 if (ret != 0) {
757 xt_entry_foreach(iter, entry0, newinfo->size) {
758 if (i-- == 0)
759 break;
760 cleanup_entry(iter, net);
761 }
762 return ret;
763 }
764
765 return ret;
766 out_free:
767 kvfree(offsets);
768 return ret;
769 }
770
771 static void
get_counters(const struct xt_table_info * t,struct xt_counters counters[])772 get_counters(const struct xt_table_info *t,
773 struct xt_counters counters[])
774 {
775 struct ipt_entry *iter;
776 unsigned int cpu;
777 unsigned int i;
778
779 for_each_possible_cpu(cpu) {
780 seqcount_t *s = &per_cpu(xt_recseq, cpu);
781
782 i = 0;
783 xt_entry_foreach(iter, t->entries, t->size) {
784 struct xt_counters *tmp;
785 u64 bcnt, pcnt;
786 unsigned int start;
787
788 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
789 do {
790 start = read_seqcount_begin(s);
791 bcnt = tmp->bcnt;
792 pcnt = tmp->pcnt;
793 } while (read_seqcount_retry(s, start));
794
795 ADD_COUNTER(counters[i], bcnt, pcnt);
796 ++i; /* macro does multi eval of i */
797 }
798 }
799 }
800
alloc_counters(const struct xt_table * table)801 static struct xt_counters *alloc_counters(const struct xt_table *table)
802 {
803 unsigned int countersize;
804 struct xt_counters *counters;
805 const struct xt_table_info *private = table->private;
806
807 /* We need atomic snapshot of counters: rest doesn't change
808 (other than comefrom, which userspace doesn't care
809 about). */
810 countersize = sizeof(struct xt_counters) * private->number;
811 counters = vzalloc(countersize);
812
813 if (counters == NULL)
814 return ERR_PTR(-ENOMEM);
815
816 get_counters(private, counters);
817
818 return counters;
819 }
820
821 static int
copy_entries_to_user(unsigned int total_size,const struct xt_table * table,void __user * userptr)822 copy_entries_to_user(unsigned int total_size,
823 const struct xt_table *table,
824 void __user *userptr)
825 {
826 unsigned int off, num;
827 const struct ipt_entry *e;
828 struct xt_counters *counters;
829 const struct xt_table_info *private = table->private;
830 int ret = 0;
831 const void *loc_cpu_entry;
832
833 counters = alloc_counters(table);
834 if (IS_ERR(counters))
835 return PTR_ERR(counters);
836
837 loc_cpu_entry = private->entries;
838 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
839 ret = -EFAULT;
840 goto free_counters;
841 }
842
843 /* FIXME: use iterator macros --RR */
844 /* ... then go back and fix counters and names */
845 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
846 unsigned int i;
847 const struct xt_entry_match *m;
848 const struct xt_entry_target *t;
849
850 e = (struct ipt_entry *)(loc_cpu_entry + off);
851 if (copy_to_user(userptr + off
852 + offsetof(struct ipt_entry, counters),
853 &counters[num],
854 sizeof(counters[num])) != 0) {
855 ret = -EFAULT;
856 goto free_counters;
857 }
858
859 for (i = sizeof(struct ipt_entry);
860 i < e->target_offset;
861 i += m->u.match_size) {
862 m = (void *)e + i;
863
864 if (copy_to_user(userptr + off + i
865 + offsetof(struct xt_entry_match,
866 u.user.name),
867 m->u.kernel.match->name,
868 strlen(m->u.kernel.match->name)+1)
869 != 0) {
870 ret = -EFAULT;
871 goto free_counters;
872 }
873 }
874
875 t = ipt_get_target_c(e);
876 if (copy_to_user(userptr + off + e->target_offset
877 + offsetof(struct xt_entry_target,
878 u.user.name),
879 t->u.kernel.target->name,
880 strlen(t->u.kernel.target->name)+1) != 0) {
881 ret = -EFAULT;
882 goto free_counters;
883 }
884 }
885
886 free_counters:
887 vfree(counters);
888 return ret;
889 }
890
891 #ifdef CONFIG_COMPAT
compat_standard_from_user(void * dst,const void * src)892 static void compat_standard_from_user(void *dst, const void *src)
893 {
894 int v = *(compat_int_t *)src;
895
896 if (v > 0)
897 v += xt_compat_calc_jump(AF_INET, v);
898 memcpy(dst, &v, sizeof(v));
899 }
900
compat_standard_to_user(void __user * dst,const void * src)901 static int compat_standard_to_user(void __user *dst, const void *src)
902 {
903 compat_int_t cv = *(int *)src;
904
905 if (cv > 0)
906 cv -= xt_compat_calc_jump(AF_INET, cv);
907 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
908 }
909
compat_calc_entry(const struct ipt_entry * e,const struct xt_table_info * info,const void * base,struct xt_table_info * newinfo)910 static int compat_calc_entry(const struct ipt_entry *e,
911 const struct xt_table_info *info,
912 const void *base, struct xt_table_info *newinfo)
913 {
914 const struct xt_entry_match *ematch;
915 const struct xt_entry_target *t;
916 unsigned int entry_offset;
917 int off, i, ret;
918
919 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
920 entry_offset = (void *)e - base;
921 xt_ematch_foreach(ematch, e)
922 off += xt_compat_match_offset(ematch->u.kernel.match);
923 t = ipt_get_target_c(e);
924 off += xt_compat_target_offset(t->u.kernel.target);
925 newinfo->size -= off;
926 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
927 if (ret)
928 return ret;
929
930 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
931 if (info->hook_entry[i] &&
932 (e < (struct ipt_entry *)(base + info->hook_entry[i])))
933 newinfo->hook_entry[i] -= off;
934 if (info->underflow[i] &&
935 (e < (struct ipt_entry *)(base + info->underflow[i])))
936 newinfo->underflow[i] -= off;
937 }
938 return 0;
939 }
940
compat_table_info(const struct xt_table_info * info,struct xt_table_info * newinfo)941 static int compat_table_info(const struct xt_table_info *info,
942 struct xt_table_info *newinfo)
943 {
944 struct ipt_entry *iter;
945 const void *loc_cpu_entry;
946 int ret;
947
948 if (!newinfo || !info)
949 return -EINVAL;
950
951 /* we dont care about newinfo->entries */
952 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
953 newinfo->initial_entries = 0;
954 loc_cpu_entry = info->entries;
955 xt_compat_init_offsets(AF_INET, info->number);
956 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
957 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
958 if (ret != 0)
959 return ret;
960 }
961 return 0;
962 }
963 #endif
964
get_info(struct net * net,void __user * user,const int * len,int compat)965 static int get_info(struct net *net, void __user *user,
966 const int *len, int compat)
967 {
968 char name[XT_TABLE_MAXNAMELEN];
969 struct xt_table *t;
970 int ret;
971
972 if (*len != sizeof(struct ipt_getinfo))
973 return -EINVAL;
974
975 if (copy_from_user(name, user, sizeof(name)) != 0)
976 return -EFAULT;
977
978 name[XT_TABLE_MAXNAMELEN-1] = '\0';
979 #ifdef CONFIG_COMPAT
980 if (compat)
981 xt_compat_lock(AF_INET);
982 #endif
983 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
984 "iptable_%s", name);
985 if (!IS_ERR_OR_NULL(t)) {
986 struct ipt_getinfo info;
987 const struct xt_table_info *private = t->private;
988 #ifdef CONFIG_COMPAT
989 struct xt_table_info tmp;
990
991 if (compat) {
992 ret = compat_table_info(private, &tmp);
993 xt_compat_flush_offsets(AF_INET);
994 private = &tmp;
995 }
996 #endif
997 memset(&info, 0, sizeof(info));
998 info.valid_hooks = t->valid_hooks;
999 memcpy(info.hook_entry, private->hook_entry,
1000 sizeof(info.hook_entry));
1001 memcpy(info.underflow, private->underflow,
1002 sizeof(info.underflow));
1003 info.num_entries = private->number;
1004 info.size = private->size;
1005 strcpy(info.name, name);
1006
1007 if (copy_to_user(user, &info, *len) != 0)
1008 ret = -EFAULT;
1009 else
1010 ret = 0;
1011
1012 xt_table_unlock(t);
1013 module_put(t->me);
1014 } else
1015 ret = t ? PTR_ERR(t) : -ENOENT;
1016 #ifdef CONFIG_COMPAT
1017 if (compat)
1018 xt_compat_unlock(AF_INET);
1019 #endif
1020 return ret;
1021 }
1022
1023 static int
get_entries(struct net * net,struct ipt_get_entries __user * uptr,const int * len)1024 get_entries(struct net *net, struct ipt_get_entries __user *uptr,
1025 const int *len)
1026 {
1027 int ret;
1028 struct ipt_get_entries get;
1029 struct xt_table *t;
1030
1031 if (*len < sizeof(get))
1032 return -EINVAL;
1033 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1034 return -EFAULT;
1035 if (*len != sizeof(struct ipt_get_entries) + get.size)
1036 return -EINVAL;
1037 get.name[sizeof(get.name) - 1] = '\0';
1038
1039 t = xt_find_table_lock(net, AF_INET, get.name);
1040 if (!IS_ERR_OR_NULL(t)) {
1041 const struct xt_table_info *private = t->private;
1042 if (get.size == private->size)
1043 ret = copy_entries_to_user(private->size,
1044 t, uptr->entrytable);
1045 else
1046 ret = -EAGAIN;
1047
1048 module_put(t->me);
1049 xt_table_unlock(t);
1050 } else
1051 ret = t ? PTR_ERR(t) : -ENOENT;
1052
1053 return ret;
1054 }
1055
1056 static int
__do_replace(struct net * net,const char * name,unsigned int valid_hooks,struct xt_table_info * newinfo,unsigned int num_counters,void __user * counters_ptr)1057 __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
1058 struct xt_table_info *newinfo, unsigned int num_counters,
1059 void __user *counters_ptr)
1060 {
1061 int ret;
1062 struct xt_table *t;
1063 struct xt_table_info *oldinfo;
1064 struct xt_counters *counters;
1065 struct ipt_entry *iter;
1066
1067 ret = 0;
1068 counters = vzalloc(num_counters * sizeof(struct xt_counters));
1069 if (!counters) {
1070 ret = -ENOMEM;
1071 goto out;
1072 }
1073
1074 t = try_then_request_module(xt_find_table_lock(net, AF_INET, name),
1075 "iptable_%s", name);
1076 if (IS_ERR_OR_NULL(t)) {
1077 ret = t ? PTR_ERR(t) : -ENOENT;
1078 goto free_newinfo_counters_untrans;
1079 }
1080
1081 /* You lied! */
1082 if (valid_hooks != t->valid_hooks) {
1083 ret = -EINVAL;
1084 goto put_module;
1085 }
1086
1087 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1088 if (!oldinfo)
1089 goto put_module;
1090
1091 /* Update module usage count based on number of rules */
1092 if ((oldinfo->number > oldinfo->initial_entries) ||
1093 (newinfo->number <= oldinfo->initial_entries))
1094 module_put(t->me);
1095 if ((oldinfo->number > oldinfo->initial_entries) &&
1096 (newinfo->number <= oldinfo->initial_entries))
1097 module_put(t->me);
1098
1099 /* Get the old counters, and synchronize with replace */
1100 get_counters(oldinfo, counters);
1101
1102 /* Decrease module usage counts and free resource */
1103 xt_entry_foreach(iter, oldinfo->entries, oldinfo->size)
1104 cleanup_entry(iter, net);
1105
1106 xt_free_table_info(oldinfo);
1107 if (copy_to_user(counters_ptr, counters,
1108 sizeof(struct xt_counters) * num_counters) != 0) {
1109 /* Silent error, can't fail, new table is already in place */
1110 net_warn_ratelimited("iptables: counters copy to user failed while replacing table\n");
1111 }
1112 vfree(counters);
1113 xt_table_unlock(t);
1114 return ret;
1115
1116 put_module:
1117 module_put(t->me);
1118 xt_table_unlock(t);
1119 free_newinfo_counters_untrans:
1120 vfree(counters);
1121 out:
1122 return ret;
1123 }
1124
1125 static int
do_replace(struct net * net,const void __user * user,unsigned int len)1126 do_replace(struct net *net, const void __user *user, unsigned int len)
1127 {
1128 int ret;
1129 struct ipt_replace tmp;
1130 struct xt_table_info *newinfo;
1131 void *loc_cpu_entry;
1132 struct ipt_entry *iter;
1133
1134 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1135 return -EFAULT;
1136
1137 /* overflow check */
1138 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1139 return -ENOMEM;
1140 if (tmp.num_counters == 0)
1141 return -EINVAL;
1142
1143 tmp.name[sizeof(tmp.name)-1] = 0;
1144
1145 newinfo = xt_alloc_table_info(tmp.size);
1146 if (!newinfo)
1147 return -ENOMEM;
1148
1149 loc_cpu_entry = newinfo->entries;
1150 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1151 tmp.size) != 0) {
1152 ret = -EFAULT;
1153 goto free_newinfo;
1154 }
1155
1156 ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
1157 if (ret != 0)
1158 goto free_newinfo;
1159
1160 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1161 tmp.num_counters, tmp.counters);
1162 if (ret)
1163 goto free_newinfo_untrans;
1164 return 0;
1165
1166 free_newinfo_untrans:
1167 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1168 cleanup_entry(iter, net);
1169 free_newinfo:
1170 xt_free_table_info(newinfo);
1171 return ret;
1172 }
1173
1174 static int
do_add_counters(struct net * net,const void __user * user,unsigned int len,int compat)1175 do_add_counters(struct net *net, const void __user *user,
1176 unsigned int len, int compat)
1177 {
1178 unsigned int i;
1179 struct xt_counters_info tmp;
1180 struct xt_counters *paddc;
1181 struct xt_table *t;
1182 const struct xt_table_info *private;
1183 int ret = 0;
1184 struct ipt_entry *iter;
1185 unsigned int addend;
1186
1187 paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
1188 if (IS_ERR(paddc))
1189 return PTR_ERR(paddc);
1190
1191 t = xt_find_table_lock(net, AF_INET, tmp.name);
1192 if (IS_ERR_OR_NULL(t)) {
1193 ret = t ? PTR_ERR(t) : -ENOENT;
1194 goto free;
1195 }
1196
1197 local_bh_disable();
1198 private = t->private;
1199 if (private->number != tmp.num_counters) {
1200 ret = -EINVAL;
1201 goto unlock_up_free;
1202 }
1203
1204 i = 0;
1205 addend = xt_write_recseq_begin();
1206 xt_entry_foreach(iter, private->entries, private->size) {
1207 struct xt_counters *tmp;
1208
1209 tmp = xt_get_this_cpu_counter(&iter->counters);
1210 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
1211 ++i;
1212 }
1213 xt_write_recseq_end(addend);
1214 unlock_up_free:
1215 local_bh_enable();
1216 xt_table_unlock(t);
1217 module_put(t->me);
1218 free:
1219 vfree(paddc);
1220
1221 return ret;
1222 }
1223
1224 #ifdef CONFIG_COMPAT
1225 struct compat_ipt_replace {
1226 char name[XT_TABLE_MAXNAMELEN];
1227 u32 valid_hooks;
1228 u32 num_entries;
1229 u32 size;
1230 u32 hook_entry[NF_INET_NUMHOOKS];
1231 u32 underflow[NF_INET_NUMHOOKS];
1232 u32 num_counters;
1233 compat_uptr_t counters; /* struct xt_counters * */
1234 struct compat_ipt_entry entries[0];
1235 };
1236
1237 static int
compat_copy_entry_to_user(struct ipt_entry * e,void __user ** dstptr,unsigned int * size,struct xt_counters * counters,unsigned int i)1238 compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
1239 unsigned int *size, struct xt_counters *counters,
1240 unsigned int i)
1241 {
1242 struct xt_entry_target *t;
1243 struct compat_ipt_entry __user *ce;
1244 u_int16_t target_offset, next_offset;
1245 compat_uint_t origsize;
1246 const struct xt_entry_match *ematch;
1247 int ret = 0;
1248
1249 origsize = *size;
1250 ce = (struct compat_ipt_entry __user *)*dstptr;
1251 if (copy_to_user(ce, e, sizeof(struct ipt_entry)) != 0 ||
1252 copy_to_user(&ce->counters, &counters[i],
1253 sizeof(counters[i])) != 0)
1254 return -EFAULT;
1255
1256 *dstptr += sizeof(struct compat_ipt_entry);
1257 *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1258
1259 xt_ematch_foreach(ematch, e) {
1260 ret = xt_compat_match_to_user(ematch, dstptr, size);
1261 if (ret != 0)
1262 return ret;
1263 }
1264 target_offset = e->target_offset - (origsize - *size);
1265 t = ipt_get_target(e);
1266 ret = xt_compat_target_to_user(t, dstptr, size);
1267 if (ret)
1268 return ret;
1269 next_offset = e->next_offset - (origsize - *size);
1270 if (put_user(target_offset, &ce->target_offset) != 0 ||
1271 put_user(next_offset, &ce->next_offset) != 0)
1272 return -EFAULT;
1273 return 0;
1274 }
1275
1276 static int
compat_find_calc_match(struct xt_entry_match * m,const struct ipt_ip * ip,int * size)1277 compat_find_calc_match(struct xt_entry_match *m,
1278 const struct ipt_ip *ip,
1279 int *size)
1280 {
1281 struct xt_match *match;
1282
1283 match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
1284 m->u.user.revision);
1285 if (IS_ERR(match))
1286 return PTR_ERR(match);
1287
1288 m->u.kernel.match = match;
1289 *size += xt_compat_match_offset(match);
1290 return 0;
1291 }
1292
compat_release_entry(struct compat_ipt_entry * e)1293 static void compat_release_entry(struct compat_ipt_entry *e)
1294 {
1295 struct xt_entry_target *t;
1296 struct xt_entry_match *ematch;
1297
1298 /* Cleanup all matches */
1299 xt_ematch_foreach(ematch, e)
1300 module_put(ematch->u.kernel.match->me);
1301 t = compat_ipt_get_target(e);
1302 module_put(t->u.kernel.target->me);
1303 }
1304
1305 static int
check_compat_entry_size_and_hooks(struct compat_ipt_entry * e,struct xt_table_info * newinfo,unsigned int * size,const unsigned char * base,const unsigned char * limit)1306 check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
1307 struct xt_table_info *newinfo,
1308 unsigned int *size,
1309 const unsigned char *base,
1310 const unsigned char *limit)
1311 {
1312 struct xt_entry_match *ematch;
1313 struct xt_entry_target *t;
1314 struct xt_target *target;
1315 unsigned int entry_offset;
1316 unsigned int j;
1317 int ret, off;
1318
1319 if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0 ||
1320 (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit ||
1321 (unsigned char *)e + e->next_offset > limit)
1322 return -EINVAL;
1323
1324 if (e->next_offset < sizeof(struct compat_ipt_entry) +
1325 sizeof(struct compat_xt_entry_target))
1326 return -EINVAL;
1327
1328 if (!ip_checkentry(&e->ip))
1329 return -EINVAL;
1330
1331 ret = xt_compat_check_entry_offsets(e, e->elems,
1332 e->target_offset, e->next_offset);
1333 if (ret)
1334 return ret;
1335
1336 off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1337 entry_offset = (void *)e - (void *)base;
1338 j = 0;
1339 xt_ematch_foreach(ematch, e) {
1340 ret = compat_find_calc_match(ematch, &e->ip, &off);
1341 if (ret != 0)
1342 goto release_matches;
1343 ++j;
1344 }
1345
1346 t = compat_ipt_get_target(e);
1347 target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
1348 t->u.user.revision);
1349 if (IS_ERR(target)) {
1350 ret = PTR_ERR(target);
1351 goto release_matches;
1352 }
1353 t->u.kernel.target = target;
1354
1355 off += xt_compat_target_offset(target);
1356 *size += off;
1357 ret = xt_compat_add_offset(AF_INET, entry_offset, off);
1358 if (ret)
1359 goto out;
1360
1361 return 0;
1362
1363 out:
1364 module_put(t->u.kernel.target->me);
1365 release_matches:
1366 xt_ematch_foreach(ematch, e) {
1367 if (j-- == 0)
1368 break;
1369 module_put(ematch->u.kernel.match->me);
1370 }
1371 return ret;
1372 }
1373
1374 static void
compat_copy_entry_from_user(struct compat_ipt_entry * e,void ** dstptr,unsigned int * size,struct xt_table_info * newinfo,unsigned char * base)1375 compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
1376 unsigned int *size,
1377 struct xt_table_info *newinfo, unsigned char *base)
1378 {
1379 struct xt_entry_target *t;
1380 struct xt_target *target;
1381 struct ipt_entry *de;
1382 unsigned int origsize;
1383 int h;
1384 struct xt_entry_match *ematch;
1385
1386 origsize = *size;
1387 de = (struct ipt_entry *)*dstptr;
1388 memcpy(de, e, sizeof(struct ipt_entry));
1389 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1390
1391 *dstptr += sizeof(struct ipt_entry);
1392 *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
1393
1394 xt_ematch_foreach(ematch, e)
1395 xt_compat_match_from_user(ematch, dstptr, size);
1396
1397 de->target_offset = e->target_offset - (origsize - *size);
1398 t = compat_ipt_get_target(e);
1399 target = t->u.kernel.target;
1400 xt_compat_target_from_user(t, dstptr, size);
1401
1402 de->next_offset = e->next_offset - (origsize - *size);
1403
1404 for (h = 0; h < NF_INET_NUMHOOKS; h++) {
1405 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1406 newinfo->hook_entry[h] -= origsize - *size;
1407 if ((unsigned char *)de - base < newinfo->underflow[h])
1408 newinfo->underflow[h] -= origsize - *size;
1409 }
1410 }
1411
1412 static int
translate_compat_table(struct net * net,struct xt_table_info ** pinfo,void ** pentry0,const struct compat_ipt_replace * compatr)1413 translate_compat_table(struct net *net,
1414 struct xt_table_info **pinfo,
1415 void **pentry0,
1416 const struct compat_ipt_replace *compatr)
1417 {
1418 unsigned int i, j;
1419 struct xt_table_info *newinfo, *info;
1420 void *pos, *entry0, *entry1;
1421 struct compat_ipt_entry *iter0;
1422 struct ipt_replace repl;
1423 unsigned int size;
1424 int ret;
1425
1426 info = *pinfo;
1427 entry0 = *pentry0;
1428 size = compatr->size;
1429 info->number = compatr->num_entries;
1430
1431 j = 0;
1432 xt_compat_lock(AF_INET);
1433 xt_compat_init_offsets(AF_INET, compatr->num_entries);
1434 /* Walk through entries, checking offsets. */
1435 xt_entry_foreach(iter0, entry0, compatr->size) {
1436 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1437 entry0,
1438 entry0 + compatr->size);
1439 if (ret != 0)
1440 goto out_unlock;
1441 ++j;
1442 }
1443
1444 ret = -EINVAL;
1445 if (j != compatr->num_entries)
1446 goto out_unlock;
1447
1448 ret = -ENOMEM;
1449 newinfo = xt_alloc_table_info(size);
1450 if (!newinfo)
1451 goto out_unlock;
1452
1453 newinfo->number = compatr->num_entries;
1454 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1455 newinfo->hook_entry[i] = compatr->hook_entry[i];
1456 newinfo->underflow[i] = compatr->underflow[i];
1457 }
1458 entry1 = newinfo->entries;
1459 pos = entry1;
1460 size = compatr->size;
1461 xt_entry_foreach(iter0, entry0, compatr->size)
1462 compat_copy_entry_from_user(iter0, &pos, &size,
1463 newinfo, entry1);
1464
1465 /* all module references in entry0 are now gone.
1466 * entry1/newinfo contains a 64bit ruleset that looks exactly as
1467 * generated by 64bit userspace.
1468 *
1469 * Call standard translate_table() to validate all hook_entrys,
1470 * underflows, check for loops, etc.
1471 */
1472 xt_compat_flush_offsets(AF_INET);
1473 xt_compat_unlock(AF_INET);
1474
1475 memcpy(&repl, compatr, sizeof(*compatr));
1476
1477 for (i = 0; i < NF_INET_NUMHOOKS; i++) {
1478 repl.hook_entry[i] = newinfo->hook_entry[i];
1479 repl.underflow[i] = newinfo->underflow[i];
1480 }
1481
1482 repl.num_counters = 0;
1483 repl.counters = NULL;
1484 repl.size = newinfo->size;
1485 ret = translate_table(net, newinfo, entry1, &repl);
1486 if (ret)
1487 goto free_newinfo;
1488
1489 *pinfo = newinfo;
1490 *pentry0 = entry1;
1491 xt_free_table_info(info);
1492 return 0;
1493
1494 free_newinfo:
1495 xt_free_table_info(newinfo);
1496 return ret;
1497 out_unlock:
1498 xt_compat_flush_offsets(AF_INET);
1499 xt_compat_unlock(AF_INET);
1500 xt_entry_foreach(iter0, entry0, compatr->size) {
1501 if (j-- == 0)
1502 break;
1503 compat_release_entry(iter0);
1504 }
1505 return ret;
1506 }
1507
1508 static int
compat_do_replace(struct net * net,void __user * user,unsigned int len)1509 compat_do_replace(struct net *net, void __user *user, unsigned int len)
1510 {
1511 int ret;
1512 struct compat_ipt_replace tmp;
1513 struct xt_table_info *newinfo;
1514 void *loc_cpu_entry;
1515 struct ipt_entry *iter;
1516
1517 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1518 return -EFAULT;
1519
1520 /* overflow check */
1521 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1522 return -ENOMEM;
1523 if (tmp.num_counters == 0)
1524 return -EINVAL;
1525
1526 tmp.name[sizeof(tmp.name)-1] = 0;
1527
1528 newinfo = xt_alloc_table_info(tmp.size);
1529 if (!newinfo)
1530 return -ENOMEM;
1531
1532 loc_cpu_entry = newinfo->entries;
1533 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1534 tmp.size) != 0) {
1535 ret = -EFAULT;
1536 goto free_newinfo;
1537 }
1538
1539 ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
1540 if (ret != 0)
1541 goto free_newinfo;
1542
1543 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1544 tmp.num_counters, compat_ptr(tmp.counters));
1545 if (ret)
1546 goto free_newinfo_untrans;
1547 return 0;
1548
1549 free_newinfo_untrans:
1550 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1551 cleanup_entry(iter, net);
1552 free_newinfo:
1553 xt_free_table_info(newinfo);
1554 return ret;
1555 }
1556
1557 static int
compat_do_ipt_set_ctl(struct sock * sk,int cmd,void __user * user,unsigned int len)1558 compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
1559 unsigned int len)
1560 {
1561 int ret;
1562
1563 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1564 return -EPERM;
1565
1566 switch (cmd) {
1567 case IPT_SO_SET_REPLACE:
1568 ret = compat_do_replace(sock_net(sk), user, len);
1569 break;
1570
1571 case IPT_SO_SET_ADD_COUNTERS:
1572 ret = do_add_counters(sock_net(sk), user, len, 1);
1573 break;
1574
1575 default:
1576 ret = -EINVAL;
1577 }
1578
1579 return ret;
1580 }
1581
1582 struct compat_ipt_get_entries {
1583 char name[XT_TABLE_MAXNAMELEN];
1584 compat_uint_t size;
1585 struct compat_ipt_entry entrytable[0];
1586 };
1587
1588 static int
compat_copy_entries_to_user(unsigned int total_size,struct xt_table * table,void __user * userptr)1589 compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
1590 void __user *userptr)
1591 {
1592 struct xt_counters *counters;
1593 const struct xt_table_info *private = table->private;
1594 void __user *pos;
1595 unsigned int size;
1596 int ret = 0;
1597 unsigned int i = 0;
1598 struct ipt_entry *iter;
1599
1600 counters = alloc_counters(table);
1601 if (IS_ERR(counters))
1602 return PTR_ERR(counters);
1603
1604 pos = userptr;
1605 size = total_size;
1606 xt_entry_foreach(iter, private->entries, total_size) {
1607 ret = compat_copy_entry_to_user(iter, &pos,
1608 &size, counters, i++);
1609 if (ret != 0)
1610 break;
1611 }
1612
1613 vfree(counters);
1614 return ret;
1615 }
1616
1617 static int
compat_get_entries(struct net * net,struct compat_ipt_get_entries __user * uptr,int * len)1618 compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
1619 int *len)
1620 {
1621 int ret;
1622 struct compat_ipt_get_entries get;
1623 struct xt_table *t;
1624
1625 if (*len < sizeof(get))
1626 return -EINVAL;
1627
1628 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1629 return -EFAULT;
1630
1631 if (*len != sizeof(struct compat_ipt_get_entries) + get.size)
1632 return -EINVAL;
1633
1634 get.name[sizeof(get.name) - 1] = '\0';
1635
1636 xt_compat_lock(AF_INET);
1637 t = xt_find_table_lock(net, AF_INET, get.name);
1638 if (!IS_ERR_OR_NULL(t)) {
1639 const struct xt_table_info *private = t->private;
1640 struct xt_table_info info;
1641 ret = compat_table_info(private, &info);
1642 if (!ret && get.size == info.size)
1643 ret = compat_copy_entries_to_user(private->size,
1644 t, uptr->entrytable);
1645 else if (!ret)
1646 ret = -EAGAIN;
1647
1648 xt_compat_flush_offsets(AF_INET);
1649 module_put(t->me);
1650 xt_table_unlock(t);
1651 } else
1652 ret = t ? PTR_ERR(t) : -ENOENT;
1653
1654 xt_compat_unlock(AF_INET);
1655 return ret;
1656 }
1657
1658 static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
1659
1660 static int
compat_do_ipt_get_ctl(struct sock * sk,int cmd,void __user * user,int * len)1661 compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1662 {
1663 int ret;
1664
1665 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1666 return -EPERM;
1667
1668 switch (cmd) {
1669 case IPT_SO_GET_INFO:
1670 ret = get_info(sock_net(sk), user, len, 1);
1671 break;
1672 case IPT_SO_GET_ENTRIES:
1673 ret = compat_get_entries(sock_net(sk), user, len);
1674 break;
1675 default:
1676 ret = do_ipt_get_ctl(sk, cmd, user, len);
1677 }
1678 return ret;
1679 }
1680 #endif
1681
1682 static int
do_ipt_set_ctl(struct sock * sk,int cmd,void __user * user,unsigned int len)1683 do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1684 {
1685 int ret;
1686
1687 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1688 return -EPERM;
1689
1690 switch (cmd) {
1691 case IPT_SO_SET_REPLACE:
1692 ret = do_replace(sock_net(sk), user, len);
1693 break;
1694
1695 case IPT_SO_SET_ADD_COUNTERS:
1696 ret = do_add_counters(sock_net(sk), user, len, 0);
1697 break;
1698
1699 default:
1700 ret = -EINVAL;
1701 }
1702
1703 return ret;
1704 }
1705
1706 static int
do_ipt_get_ctl(struct sock * sk,int cmd,void __user * user,int * len)1707 do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1708 {
1709 int ret;
1710
1711 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1712 return -EPERM;
1713
1714 switch (cmd) {
1715 case IPT_SO_GET_INFO:
1716 ret = get_info(sock_net(sk), user, len, 0);
1717 break;
1718
1719 case IPT_SO_GET_ENTRIES:
1720 ret = get_entries(sock_net(sk), user, len);
1721 break;
1722
1723 case IPT_SO_GET_REVISION_MATCH:
1724 case IPT_SO_GET_REVISION_TARGET: {
1725 struct xt_get_revision rev;
1726 int target;
1727
1728 if (*len != sizeof(rev)) {
1729 ret = -EINVAL;
1730 break;
1731 }
1732 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1733 ret = -EFAULT;
1734 break;
1735 }
1736 rev.name[sizeof(rev.name)-1] = 0;
1737
1738 if (cmd == IPT_SO_GET_REVISION_TARGET)
1739 target = 1;
1740 else
1741 target = 0;
1742
1743 try_then_request_module(xt_find_revision(AF_INET, rev.name,
1744 rev.revision,
1745 target, &ret),
1746 "ipt_%s", rev.name);
1747 break;
1748 }
1749
1750 default:
1751 ret = -EINVAL;
1752 }
1753
1754 return ret;
1755 }
1756
__ipt_unregister_table(struct net * net,struct xt_table * table)1757 static void __ipt_unregister_table(struct net *net, struct xt_table *table)
1758 {
1759 struct xt_table_info *private;
1760 void *loc_cpu_entry;
1761 struct module *table_owner = table->me;
1762 struct ipt_entry *iter;
1763
1764 private = xt_unregister_table(table);
1765
1766 /* Decrease module usage counts and free resources */
1767 loc_cpu_entry = private->entries;
1768 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1769 cleanup_entry(iter, net);
1770 if (private->number > private->initial_entries)
1771 module_put(table_owner);
1772 xt_free_table_info(private);
1773 }
1774
ipt_register_table(struct net * net,const struct xt_table * table,const struct ipt_replace * repl,const struct nf_hook_ops * ops,struct xt_table ** res)1775 int ipt_register_table(struct net *net, const struct xt_table *table,
1776 const struct ipt_replace *repl,
1777 const struct nf_hook_ops *ops, struct xt_table **res)
1778 {
1779 int ret;
1780 struct xt_table_info *newinfo;
1781 struct xt_table_info bootstrap = {0};
1782 void *loc_cpu_entry;
1783 struct xt_table *new_table;
1784
1785 newinfo = xt_alloc_table_info(repl->size);
1786 if (!newinfo)
1787 return -ENOMEM;
1788
1789 loc_cpu_entry = newinfo->entries;
1790 memcpy(loc_cpu_entry, repl->entries, repl->size);
1791
1792 ret = translate_table(net, newinfo, loc_cpu_entry, repl);
1793 if (ret != 0)
1794 goto out_free;
1795
1796 new_table = xt_register_table(net, table, &bootstrap, newinfo);
1797 if (IS_ERR(new_table)) {
1798 ret = PTR_ERR(new_table);
1799 goto out_free;
1800 }
1801
1802 /* set res now, will see skbs right after nf_register_net_hooks */
1803 WRITE_ONCE(*res, new_table);
1804
1805 ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1806 if (ret != 0) {
1807 __ipt_unregister_table(net, new_table);
1808 *res = NULL;
1809 }
1810
1811 return ret;
1812
1813 out_free:
1814 xt_free_table_info(newinfo);
1815 return ret;
1816 }
1817
ipt_unregister_table(struct net * net,struct xt_table * table,const struct nf_hook_ops * ops)1818 void ipt_unregister_table(struct net *net, struct xt_table *table,
1819 const struct nf_hook_ops *ops)
1820 {
1821 nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
1822 __ipt_unregister_table(net, table);
1823 }
1824
1825 /* Returns 1 if the type and code is matched by the range, 0 otherwise */
1826 static inline bool
icmp_type_code_match(u_int8_t test_type,u_int8_t min_code,u_int8_t max_code,u_int8_t type,u_int8_t code,bool invert)1827 icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
1828 u_int8_t type, u_int8_t code,
1829 bool invert)
1830 {
1831 return ((test_type == 0xFF) ||
1832 (type == test_type && code >= min_code && code <= max_code))
1833 ^ invert;
1834 }
1835
1836 static bool
icmp_match(const struct sk_buff * skb,struct xt_action_param * par)1837 icmp_match(const struct sk_buff *skb, struct xt_action_param *par)
1838 {
1839 const struct icmphdr *ic;
1840 struct icmphdr _icmph;
1841 const struct ipt_icmp *icmpinfo = par->matchinfo;
1842
1843 /* Must not be a fragment. */
1844 if (par->fragoff != 0)
1845 return false;
1846
1847 ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
1848 if (ic == NULL) {
1849 /* We've been asked to examine this packet, and we
1850 * can't. Hence, no choice but to drop.
1851 */
1852 par->hotdrop = true;
1853 return false;
1854 }
1855
1856 return icmp_type_code_match(icmpinfo->type,
1857 icmpinfo->code[0],
1858 icmpinfo->code[1],
1859 ic->type, ic->code,
1860 !!(icmpinfo->invflags&IPT_ICMP_INV));
1861 }
1862
icmp_checkentry(const struct xt_mtchk_param * par)1863 static int icmp_checkentry(const struct xt_mtchk_param *par)
1864 {
1865 const struct ipt_icmp *icmpinfo = par->matchinfo;
1866
1867 /* Must specify no unknown invflags */
1868 return (icmpinfo->invflags & ~IPT_ICMP_INV) ? -EINVAL : 0;
1869 }
1870
1871 static struct xt_target ipt_builtin_tg[] __read_mostly = {
1872 {
1873 .name = XT_STANDARD_TARGET,
1874 .targetsize = sizeof(int),
1875 .family = NFPROTO_IPV4,
1876 #ifdef CONFIG_COMPAT
1877 .compatsize = sizeof(compat_int_t),
1878 .compat_from_user = compat_standard_from_user,
1879 .compat_to_user = compat_standard_to_user,
1880 #endif
1881 },
1882 {
1883 .name = XT_ERROR_TARGET,
1884 .target = ipt_error,
1885 .targetsize = XT_FUNCTION_MAXNAMELEN,
1886 .family = NFPROTO_IPV4,
1887 },
1888 };
1889
1890 static struct nf_sockopt_ops ipt_sockopts = {
1891 .pf = PF_INET,
1892 .set_optmin = IPT_BASE_CTL,
1893 .set_optmax = IPT_SO_SET_MAX+1,
1894 .set = do_ipt_set_ctl,
1895 #ifdef CONFIG_COMPAT
1896 .compat_set = compat_do_ipt_set_ctl,
1897 #endif
1898 .get_optmin = IPT_BASE_CTL,
1899 .get_optmax = IPT_SO_GET_MAX+1,
1900 .get = do_ipt_get_ctl,
1901 #ifdef CONFIG_COMPAT
1902 .compat_get = compat_do_ipt_get_ctl,
1903 #endif
1904 .owner = THIS_MODULE,
1905 };
1906
1907 static struct xt_match ipt_builtin_mt[] __read_mostly = {
1908 {
1909 .name = "icmp",
1910 .match = icmp_match,
1911 .matchsize = sizeof(struct ipt_icmp),
1912 .checkentry = icmp_checkentry,
1913 .proto = IPPROTO_ICMP,
1914 .family = NFPROTO_IPV4,
1915 },
1916 };
1917
ip_tables_net_init(struct net * net)1918 static int __net_init ip_tables_net_init(struct net *net)
1919 {
1920 return xt_proto_init(net, NFPROTO_IPV4);
1921 }
1922
ip_tables_net_exit(struct net * net)1923 static void __net_exit ip_tables_net_exit(struct net *net)
1924 {
1925 xt_proto_fini(net, NFPROTO_IPV4);
1926 }
1927
1928 static struct pernet_operations ip_tables_net_ops = {
1929 .init = ip_tables_net_init,
1930 .exit = ip_tables_net_exit,
1931 };
1932
ip_tables_init(void)1933 static int __init ip_tables_init(void)
1934 {
1935 int ret;
1936
1937 ret = register_pernet_subsys(&ip_tables_net_ops);
1938 if (ret < 0)
1939 goto err1;
1940
1941 /* No one else will be downing sem now, so we won't sleep */
1942 ret = xt_register_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
1943 if (ret < 0)
1944 goto err2;
1945 ret = xt_register_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
1946 if (ret < 0)
1947 goto err4;
1948
1949 /* Register setsockopt */
1950 ret = nf_register_sockopt(&ipt_sockopts);
1951 if (ret < 0)
1952 goto err5;
1953
1954 pr_info("(C) 2000-2006 Netfilter Core Team\n");
1955 return 0;
1956
1957 err5:
1958 xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
1959 err4:
1960 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
1961 err2:
1962 unregister_pernet_subsys(&ip_tables_net_ops);
1963 err1:
1964 return ret;
1965 }
1966
ip_tables_fini(void)1967 static void __exit ip_tables_fini(void)
1968 {
1969 nf_unregister_sockopt(&ipt_sockopts);
1970
1971 xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
1972 xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
1973 unregister_pernet_subsys(&ip_tables_net_ops);
1974 }
1975
1976 EXPORT_SYMBOL(ipt_register_table);
1977 EXPORT_SYMBOL(ipt_unregister_table);
1978 EXPORT_SYMBOL(ipt_do_table);
1979 module_init(ip_tables_init);
1980 module_exit(ip_tables_fini);
1981