xref: /linux/net/bridge/netfilter/ebtables.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  ebtables
4  *
5  *  Author:
6  *  Bart De Schuymer		<bdschuym@pandora.be>
7  *
8  *  ebtables.c,v 2.0, July, 2002
9  *
10  *  This code is strongly inspired by the iptables code which is
11  *  Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
12  */
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/kmod.h>
15 #include <linux/module.h>
16 #include <linux/vmalloc.h>
17 #include <linux/netfilter/x_tables.h>
18 #include <linux/netfilter_bridge/ebtables.h>
19 #include <linux/spinlock.h>
20 #include <linux/mutex.h>
21 #include <linux/slab.h>
22 #include <linux/uaccess.h>
23 #include <linux/smp.h>
24 #include <linux/cpumask.h>
25 #include <linux/audit.h>
26 #include <net/sock.h>
27 #include <net/netns/generic.h>
28 /* needed for logical [in,out]-dev filtering */
29 #include "../br_private.h"
30 
31 /* Each cpu has its own set of counters, so there is no need for write_lock in
32  * the softirq
33  * For reading or updating the counters, the user context needs to
34  * get a write_lock
35  */
36 
37 /* The size of each set of counters is altered to get cache alignment */
38 #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
39 #define COUNTER_OFFSET(n) (SMP_ALIGN(n * sizeof(struct ebt_counter)))
40 #define COUNTER_BASE(c, n, cpu) ((struct ebt_counter *)(((char *)c) + \
41 				 COUNTER_OFFSET(n) * cpu))
42 #define MAX_EBT_ENTRIES (((INT_MAX - sizeof(struct ebt_table_info)) / \
43 			 NR_CPUS - SMP_CACHE_BYTES) / sizeof(struct ebt_counter))
44 
45 struct ebt_pernet {
46 	struct list_head tables;
47 	struct list_head dead_tables;
48 };
49 
50 struct ebt_template {
51 	struct list_head list;
52 	char name[EBT_TABLE_MAXNAMELEN];
53 	struct module *owner;
54 	/* called when table is needed in the given netns */
55 	int (*table_init)(struct net *net);
56 };
57 
58 static unsigned int ebt_pernet_id __read_mostly;
59 static LIST_HEAD(template_tables);
60 static DEFINE_MUTEX(ebt_mutex);
61 
62 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
ebt_standard_compat_from_user(void * dst,const void * src)63 static void ebt_standard_compat_from_user(void *dst, const void *src)
64 {
65 	int v = *(compat_int_t *)src;
66 
67 	if (v >= 0)
68 		v += xt_compat_calc_jump(NFPROTO_BRIDGE, v);
69 	memcpy(dst, &v, sizeof(v));
70 }
71 
ebt_standard_compat_to_user(void __user * dst,const void * src)72 static int ebt_standard_compat_to_user(void __user *dst, const void *src)
73 {
74 	compat_int_t cv = *(int *)src;
75 
76 	if (cv >= 0)
77 		cv -= xt_compat_calc_jump(NFPROTO_BRIDGE, cv);
78 	return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
79 }
80 #endif
81 
82 
83 static struct xt_target ebt_standard_target = {
84 	.name       = "standard",
85 	.revision   = 0,
86 	.family     = NFPROTO_BRIDGE,
87 	.targetsize = sizeof(int),
88 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
89 	.compatsize = sizeof(compat_int_t),
90 	.compat_from_user = ebt_standard_compat_from_user,
91 	.compat_to_user =  ebt_standard_compat_to_user,
92 #endif
93 };
94 
95 static inline int
ebt_do_watcher(const struct ebt_entry_watcher * w,struct sk_buff * skb,struct xt_action_param * par)96 ebt_do_watcher(const struct ebt_entry_watcher *w, struct sk_buff *skb,
97 	       struct xt_action_param *par)
98 {
99 	par->target   = w->u.watcher;
100 	par->targinfo = w->data;
101 	w->u.watcher->target(skb, par);
102 	/* watchers don't give a verdict */
103 	return 0;
104 }
105 
106 static inline int
ebt_do_match(struct ebt_entry_match * m,const struct sk_buff * skb,struct xt_action_param * par)107 ebt_do_match(struct ebt_entry_match *m, const struct sk_buff *skb,
108 	     struct xt_action_param *par)
109 {
110 	par->match     = m->u.match;
111 	par->matchinfo = m->data;
112 	return !m->u.match->match(skb, par);
113 }
114 
115 static inline int
ebt_dev_check(const char * entry,const struct net_device * device)116 ebt_dev_check(const char *entry, const struct net_device *device)
117 {
118 	int i = 0;
119 	const char *devname;
120 
121 	if (*entry == '\0')
122 		return 0;
123 	if (!device)
124 		return 1;
125 	devname = device->name;
126 	/* 1 is the wildcard token */
127 	while (entry[i] != '\0' && entry[i] != 1 && entry[i] == devname[i])
128 		i++;
129 	return devname[i] != entry[i] && entry[i] != 1;
130 }
131 
132 /* process standard matches */
133 static inline int
ebt_basic_match(const struct ebt_entry * e,const struct sk_buff * skb,const struct net_device * in,const struct net_device * out)134 ebt_basic_match(const struct ebt_entry *e, const struct sk_buff *skb,
135 		const struct net_device *in, const struct net_device *out)
136 {
137 	const struct ethhdr *h = eth_hdr(skb);
138 	const struct net_bridge_port *p;
139 	__be16 ethproto;
140 
141 	if (skb_vlan_tag_present(skb))
142 		ethproto = htons(ETH_P_8021Q);
143 	else
144 		ethproto = h->h_proto;
145 
146 	if (e->bitmask & EBT_802_3) {
147 		if (NF_INVF(e, EBT_IPROTO, eth_proto_is_802_3(ethproto)))
148 			return 1;
149 	} else if (!(e->bitmask & EBT_NOPROTO) &&
150 		   NF_INVF(e, EBT_IPROTO, e->ethproto != ethproto))
151 		return 1;
152 
153 	if (NF_INVF(e, EBT_IIN, ebt_dev_check(e->in, in)))
154 		return 1;
155 	if (NF_INVF(e, EBT_IOUT, ebt_dev_check(e->out, out)))
156 		return 1;
157 	/* rcu_read_lock()ed by nf_hook_thresh */
158 	if (in && (p = br_port_get_rcu(in)) != NULL &&
159 	    NF_INVF(e, EBT_ILOGICALIN,
160 		    ebt_dev_check(e->logical_in, p->br->dev)))
161 		return 1;
162 	if (out && (p = br_port_get_rcu(out)) != NULL &&
163 	    NF_INVF(e, EBT_ILOGICALOUT,
164 		    ebt_dev_check(e->logical_out, p->br->dev)))
165 		return 1;
166 
167 	if (e->bitmask & EBT_SOURCEMAC) {
168 		if (NF_INVF(e, EBT_ISOURCE,
169 			    !ether_addr_equal_masked(h->h_source, e->sourcemac,
170 						     e->sourcemsk)))
171 			return 1;
172 	}
173 	if (e->bitmask & EBT_DESTMAC) {
174 		if (NF_INVF(e, EBT_IDEST,
175 			    !ether_addr_equal_masked(h->h_dest, e->destmac,
176 						     e->destmsk)))
177 			return 1;
178 	}
179 	return 0;
180 }
181 
182 static inline
ebt_next_entry(const struct ebt_entry * entry)183 struct ebt_entry *ebt_next_entry(const struct ebt_entry *entry)
184 {
185 	return (void *)entry + entry->next_offset;
186 }
187 
188 static inline const struct ebt_entry_target *
ebt_get_target_c(const struct ebt_entry * e)189 ebt_get_target_c(const struct ebt_entry *e)
190 {
191 	return ebt_get_target((struct ebt_entry *)e);
192 }
193 
194 /* Do some firewalling */
ebt_do_table(void * priv,struct sk_buff * skb,const struct nf_hook_state * state)195 unsigned int ebt_do_table(void *priv, struct sk_buff *skb,
196 			  const struct nf_hook_state *state)
197 {
198 	struct ebt_table *table = priv;
199 	unsigned int hook = state->hook;
200 	int i, nentries;
201 	struct ebt_entry *point;
202 	struct ebt_counter *counter_base, *cb_base;
203 	const struct ebt_entry_target *t;
204 	int verdict, sp = 0;
205 	struct ebt_chainstack *cs;
206 	struct ebt_entries *chaininfo;
207 	const char *base;
208 	const struct ebt_table_info *private;
209 	struct xt_action_param acpar;
210 
211 	acpar.state   = state;
212 	acpar.hotdrop = false;
213 
214 	read_lock_bh(&table->lock);
215 	private = table->private;
216 	cb_base = COUNTER_BASE(private->counters, private->nentries,
217 	   smp_processor_id());
218 	if (private->chainstack)
219 		cs = private->chainstack[smp_processor_id()];
220 	else
221 		cs = NULL;
222 	chaininfo = private->hook_entry[hook];
223 	nentries = private->hook_entry[hook]->nentries;
224 	point = (struct ebt_entry *)(private->hook_entry[hook]->data);
225 	counter_base = cb_base + private->hook_entry[hook]->counter_offset;
226 	/* base for chain jumps */
227 	base = private->entries;
228 	i = 0;
229 	while (i < nentries) {
230 		if (ebt_basic_match(point, skb, state->in, state->out))
231 			goto letscontinue;
232 
233 		if (EBT_MATCH_ITERATE(point, ebt_do_match, skb, &acpar) != 0)
234 			goto letscontinue;
235 		if (acpar.hotdrop) {
236 			read_unlock_bh(&table->lock);
237 			return NF_DROP;
238 		}
239 
240 		ADD_COUNTER(*(counter_base + i), skb->len, 1);
241 
242 		/* these should only watch: not modify, nor tell us
243 		 * what to do with the packet
244 		 */
245 		EBT_WATCHER_ITERATE(point, ebt_do_watcher, skb, &acpar);
246 
247 		t = ebt_get_target_c(point);
248 		/* standard target */
249 		if (!t->u.target->target)
250 			verdict = ((struct ebt_standard_target *)t)->verdict;
251 		else {
252 			acpar.target   = t->u.target;
253 			acpar.targinfo = t->data;
254 			verdict = t->u.target->target(skb, &acpar);
255 		}
256 		if (verdict == EBT_ACCEPT) {
257 			read_unlock_bh(&table->lock);
258 			return NF_ACCEPT;
259 		}
260 		if (verdict == EBT_DROP) {
261 			read_unlock_bh(&table->lock);
262 			return NF_DROP;
263 		}
264 		if (verdict == EBT_RETURN) {
265 letsreturn:
266 			if (WARN(sp == 0, "RETURN on base chain")) {
267 				/* act like this is EBT_CONTINUE */
268 				goto letscontinue;
269 			}
270 
271 			sp--;
272 			/* put all the local variables right */
273 			i = cs[sp].n;
274 			chaininfo = cs[sp].chaininfo;
275 			nentries = chaininfo->nentries;
276 			point = cs[sp].e;
277 			counter_base = cb_base +
278 			   chaininfo->counter_offset;
279 			continue;
280 		}
281 		if (verdict == EBT_CONTINUE)
282 			goto letscontinue;
283 
284 		if (WARN(verdict < 0, "bogus standard verdict\n")) {
285 			read_unlock_bh(&table->lock);
286 			return NF_DROP;
287 		}
288 
289 		/* jump to a udc */
290 		cs[sp].n = i + 1;
291 		cs[sp].chaininfo = chaininfo;
292 		cs[sp].e = ebt_next_entry(point);
293 		i = 0;
294 		chaininfo = (struct ebt_entries *) (base + verdict);
295 
296 		if (WARN(chaininfo->distinguisher, "jump to non-chain\n")) {
297 			read_unlock_bh(&table->lock);
298 			return NF_DROP;
299 		}
300 
301 		nentries = chaininfo->nentries;
302 		point = (struct ebt_entry *)chaininfo->data;
303 		counter_base = cb_base + chaininfo->counter_offset;
304 		sp++;
305 		continue;
306 letscontinue:
307 		point = ebt_next_entry(point);
308 		i++;
309 	}
310 
311 	/* I actually like this :) */
312 	if (chaininfo->policy == EBT_RETURN)
313 		goto letsreturn;
314 	if (chaininfo->policy == EBT_ACCEPT) {
315 		read_unlock_bh(&table->lock);
316 		return NF_ACCEPT;
317 	}
318 	read_unlock_bh(&table->lock);
319 	return NF_DROP;
320 }
321 
322 /* If it succeeds, returns element and locks mutex */
323 static inline void *
find_inlist_lock_noload(struct net * net,const char * name,int * error,struct mutex * mutex)324 find_inlist_lock_noload(struct net *net, const char *name, int *error,
325 			struct mutex *mutex)
326 {
327 	struct ebt_pernet *ebt_net = net_generic(net, ebt_pernet_id);
328 	struct ebt_template *tmpl;
329 	struct ebt_table *table;
330 
331 	mutex_lock(mutex);
332 	list_for_each_entry(table, &ebt_net->tables, list) {
333 		if (strcmp(table->name, name) == 0)
334 			return table;
335 	}
336 
337 	list_for_each_entry(tmpl, &template_tables, list) {
338 		if (strcmp(name, tmpl->name) == 0) {
339 			struct module *owner = tmpl->owner;
340 
341 			if (!try_module_get(owner))
342 				goto out;
343 
344 			mutex_unlock(mutex);
345 
346 			*error = tmpl->table_init(net);
347 			if (*error) {
348 				module_put(owner);
349 				return NULL;
350 			}
351 
352 			mutex_lock(mutex);
353 			module_put(owner);
354 			break;
355 		}
356 	}
357 
358 	list_for_each_entry(table, &ebt_net->tables, list) {
359 		if (strcmp(table->name, name) == 0)
360 			return table;
361 	}
362 
363 out:
364 	*error = -ENOENT;
365 	mutex_unlock(mutex);
366 	return NULL;
367 }
368 
369 static void *
find_inlist_lock(struct net * net,const char * name,const char * prefix,int * error,struct mutex * mutex)370 find_inlist_lock(struct net *net, const char *name, const char *prefix,
371 		 int *error, struct mutex *mutex)
372 {
373 	return try_then_request_module(
374 			find_inlist_lock_noload(net, name, error, mutex),
375 			"%s%s", prefix, name);
376 }
377 
378 static inline struct ebt_table *
find_table_lock(struct net * net,const char * name,int * error,struct mutex * mutex)379 find_table_lock(struct net *net, const char *name, int *error,
380 		struct mutex *mutex)
381 {
382 	return find_inlist_lock(net, name, "ebtable_", error, mutex);
383 }
384 
ebt_free_table_info(struct ebt_table_info * info)385 static inline void ebt_free_table_info(struct ebt_table_info *info)
386 {
387 	int i;
388 
389 	if (info->chainstack) {
390 		for_each_possible_cpu(i)
391 			vfree(info->chainstack[i]);
392 		vfree(info->chainstack);
393 	}
394 }
395 static inline int
ebt_check_match(struct ebt_entry_match * m,struct xt_mtchk_param * par,unsigned int * cnt)396 ebt_check_match(struct ebt_entry_match *m, struct xt_mtchk_param *par,
397 		unsigned int *cnt)
398 {
399 	const struct ebt_entry *e = par->entryinfo;
400 	struct xt_match *match;
401 	size_t left = ((char *)e + e->watchers_offset) - (char *)m;
402 	int ret;
403 
404 	if (left < sizeof(struct ebt_entry_match) ||
405 	    left - sizeof(struct ebt_entry_match) < m->match_size)
406 		return -EINVAL;
407 
408 	if (strnlen(m->u.name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
409 		return -EINVAL;
410 
411 	match = xt_find_match(NFPROTO_BRIDGE, m->u.name, m->u.revision);
412 	if (IS_ERR(match) || match->family != NFPROTO_BRIDGE) {
413 		if (!IS_ERR(match))
414 			module_put(match->me);
415 		request_module("ebt_%s", m->u.name);
416 		match = xt_find_match(NFPROTO_BRIDGE, m->u.name, m->u.revision);
417 	}
418 	if (IS_ERR(match))
419 		return PTR_ERR(match);
420 	m->u.match = match;
421 
422 	par->match     = match;
423 	par->matchinfo = m->data;
424 	ret = xt_check_match(par, m->match_size,
425 	      ntohs(e->ethproto), e->invflags & EBT_IPROTO);
426 	if (ret < 0) {
427 		module_put(match->me);
428 		return ret;
429 	}
430 
431 	(*cnt)++;
432 	return 0;
433 }
434 
435 static inline int
ebt_check_watcher(struct ebt_entry_watcher * w,struct xt_tgchk_param * par,unsigned int * cnt)436 ebt_check_watcher(struct ebt_entry_watcher *w, struct xt_tgchk_param *par,
437 		  unsigned int *cnt)
438 {
439 	const struct ebt_entry *e = par->entryinfo;
440 	struct xt_target *watcher;
441 	size_t left = ((char *)e + e->target_offset) - (char *)w;
442 	int ret;
443 
444 	if (left < sizeof(struct ebt_entry_watcher) ||
445 	   left - sizeof(struct ebt_entry_watcher) < w->watcher_size)
446 		return -EINVAL;
447 
448 	watcher = xt_request_find_target(NFPROTO_BRIDGE, w->u.name, 0);
449 	if (IS_ERR(watcher))
450 		return PTR_ERR(watcher);
451 
452 	if (watcher->family != NFPROTO_BRIDGE) {
453 		module_put(watcher->me);
454 		return -ENOENT;
455 	}
456 
457 	w->u.watcher = watcher;
458 
459 	par->target   = watcher;
460 	par->targinfo = w->data;
461 	ret = xt_check_target(par, w->watcher_size,
462 	      ntohs(e->ethproto), e->invflags & EBT_IPROTO);
463 	if (ret < 0) {
464 		module_put(watcher->me);
465 		return ret;
466 	}
467 
468 	(*cnt)++;
469 	return 0;
470 }
471 
ebt_verify_pointers(const struct ebt_replace * repl,struct ebt_table_info * newinfo)472 static int ebt_verify_pointers(const struct ebt_replace *repl,
473 			       struct ebt_table_info *newinfo)
474 {
475 	unsigned int limit = repl->entries_size;
476 	unsigned int valid_hooks = repl->valid_hooks;
477 	unsigned int offset = 0;
478 	int i;
479 
480 	for (i = 0; i < NF_BR_NUMHOOKS; i++)
481 		newinfo->hook_entry[i] = NULL;
482 
483 	newinfo->entries_size = repl->entries_size;
484 	newinfo->nentries = repl->nentries;
485 
486 	while (offset < limit) {
487 		size_t left = limit - offset;
488 		struct ebt_entry *e = (void *)newinfo->entries + offset;
489 
490 		if (left < sizeof(unsigned int))
491 			break;
492 
493 		for (i = 0; i < NF_BR_NUMHOOKS; i++) {
494 			if ((valid_hooks & (1 << i)) == 0)
495 				continue;
496 			if ((char __user *)repl->hook_entry[i] ==
497 			     repl->entries + offset)
498 				break;
499 		}
500 
501 		if (i != NF_BR_NUMHOOKS || !(e->bitmask & EBT_ENTRY_OR_ENTRIES)) {
502 			if (e->bitmask != 0) {
503 				/* we make userspace set this right,
504 				 * so there is no misunderstanding
505 				 */
506 				return -EINVAL;
507 			}
508 			if (i != NF_BR_NUMHOOKS)
509 				newinfo->hook_entry[i] = (struct ebt_entries *)e;
510 			if (left < sizeof(struct ebt_entries))
511 				break;
512 			offset += sizeof(struct ebt_entries);
513 		} else {
514 			if (left < sizeof(struct ebt_entry))
515 				break;
516 			if (left < e->next_offset)
517 				break;
518 			if (e->next_offset < sizeof(struct ebt_entry))
519 				return -EINVAL;
520 			offset += e->next_offset;
521 		}
522 	}
523 	if (offset != limit)
524 		return -EINVAL;
525 
526 	/* check if all valid hooks have a chain */
527 	for (i = 0; i < NF_BR_NUMHOOKS; i++) {
528 		if (!newinfo->hook_entry[i] &&
529 		   (valid_hooks & (1 << i)))
530 			return -EINVAL;
531 	}
532 	return 0;
533 }
534 
535 /* this one is very careful, as it is the first function
536  * to parse the userspace data
537  */
538 static inline int
ebt_check_entry_size_and_hooks(const struct ebt_entry * e,const struct ebt_table_info * newinfo,unsigned int * n,unsigned int * cnt,unsigned int * totalcnt,unsigned int * udc_cnt)539 ebt_check_entry_size_and_hooks(const struct ebt_entry *e,
540 			       const struct ebt_table_info *newinfo,
541 			       unsigned int *n, unsigned int *cnt,
542 			       unsigned int *totalcnt, unsigned int *udc_cnt)
543 {
544 	int i;
545 
546 	for (i = 0; i < NF_BR_NUMHOOKS; i++) {
547 		if ((void *)e == (void *)newinfo->hook_entry[i])
548 			break;
549 	}
550 	/* beginning of a new chain
551 	 * if i == NF_BR_NUMHOOKS it must be a user defined chain
552 	 */
553 	if (i != NF_BR_NUMHOOKS || !e->bitmask) {
554 		/* this checks if the previous chain has as many entries
555 		 * as it said it has
556 		 */
557 		if (*n != *cnt)
558 			return -EINVAL;
559 
560 		if (((struct ebt_entries *)e)->policy != EBT_DROP &&
561 		   ((struct ebt_entries *)e)->policy != EBT_ACCEPT) {
562 			/* only RETURN from udc */
563 			if (i != NF_BR_NUMHOOKS ||
564 			   ((struct ebt_entries *)e)->policy != EBT_RETURN)
565 				return -EINVAL;
566 		}
567 		if (i == NF_BR_NUMHOOKS) /* it's a user defined chain */
568 			(*udc_cnt)++;
569 		if (((struct ebt_entries *)e)->counter_offset != *totalcnt)
570 			return -EINVAL;
571 		*n = ((struct ebt_entries *)e)->nentries;
572 		*cnt = 0;
573 		return 0;
574 	}
575 	/* a plain old entry, heh */
576 	if (sizeof(struct ebt_entry) > e->watchers_offset ||
577 	   e->watchers_offset > e->target_offset ||
578 	   e->target_offset >= e->next_offset)
579 		return -EINVAL;
580 
581 	/* this is not checked anywhere else */
582 	if (e->next_offset - e->target_offset < sizeof(struct ebt_entry_target))
583 		return -EINVAL;
584 
585 	(*cnt)++;
586 	(*totalcnt)++;
587 	return 0;
588 }
589 
590 struct ebt_cl_stack {
591 	struct ebt_chainstack cs;
592 	int from;
593 	unsigned int hookmask;
594 };
595 
596 /* We need these positions to check that the jumps to a different part of the
597  * entries is a jump to the beginning of a new chain.
598  */
599 static inline int
ebt_get_udc_positions(struct ebt_entry * e,struct ebt_table_info * newinfo,unsigned int * n,struct ebt_cl_stack * udc)600 ebt_get_udc_positions(struct ebt_entry *e, struct ebt_table_info *newinfo,
601 		      unsigned int *n, struct ebt_cl_stack *udc)
602 {
603 	int i;
604 
605 	/* we're only interested in chain starts */
606 	if (e->bitmask)
607 		return 0;
608 	for (i = 0; i < NF_BR_NUMHOOKS; i++) {
609 		if (newinfo->hook_entry[i] == (struct ebt_entries *)e)
610 			break;
611 	}
612 	/* only care about udc */
613 	if (i != NF_BR_NUMHOOKS)
614 		return 0;
615 
616 	udc[*n].cs.chaininfo = (struct ebt_entries *)e;
617 	/* these initialisations are depended on later in check_chainloops() */
618 	udc[*n].cs.n = 0;
619 	udc[*n].hookmask = 0;
620 
621 	(*n)++;
622 	return 0;
623 }
624 
625 static inline int
ebt_cleanup_match(struct ebt_entry_match * m,struct net * net,unsigned int * i)626 ebt_cleanup_match(struct ebt_entry_match *m, struct net *net, unsigned int *i)
627 {
628 	struct xt_mtdtor_param par;
629 
630 	if (i && (*i)-- == 0)
631 		return 1;
632 
633 	par.net       = net;
634 	par.match     = m->u.match;
635 	par.matchinfo = m->data;
636 	par.family    = NFPROTO_BRIDGE;
637 	if (par.match->destroy != NULL)
638 		par.match->destroy(&par);
639 	module_put(par.match->me);
640 	return 0;
641 }
642 
643 static inline int
ebt_cleanup_watcher(struct ebt_entry_watcher * w,struct net * net,unsigned int * i)644 ebt_cleanup_watcher(struct ebt_entry_watcher *w, struct net *net, unsigned int *i)
645 {
646 	struct xt_tgdtor_param par;
647 
648 	if (i && (*i)-- == 0)
649 		return 1;
650 
651 	par.net      = net;
652 	par.target   = w->u.watcher;
653 	par.targinfo = w->data;
654 	par.family   = NFPROTO_BRIDGE;
655 	if (par.target->destroy != NULL)
656 		par.target->destroy(&par);
657 	module_put(par.target->me);
658 	return 0;
659 }
660 
661 static inline int
ebt_cleanup_entry(struct ebt_entry * e,struct net * net,unsigned int * cnt)662 ebt_cleanup_entry(struct ebt_entry *e, struct net *net, unsigned int *cnt)
663 {
664 	struct xt_tgdtor_param par;
665 	struct ebt_entry_target *t;
666 
667 	if (e->bitmask == 0)
668 		return 0;
669 	/* we're done */
670 	if (cnt && (*cnt)-- == 0)
671 		return 1;
672 	EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, net, NULL);
673 	EBT_MATCH_ITERATE(e, ebt_cleanup_match, net, NULL);
674 	t = ebt_get_target(e);
675 
676 	par.net      = net;
677 	par.target   = t->u.target;
678 	par.targinfo = t->data;
679 	par.family   = NFPROTO_BRIDGE;
680 	if (par.target->destroy != NULL)
681 		par.target->destroy(&par);
682 	module_put(par.target->me);
683 	return 0;
684 }
685 
686 static inline int
ebt_check_entry(struct ebt_entry * e,struct net * net,const struct ebt_table_info * newinfo,const char * name,unsigned int * cnt,struct ebt_cl_stack * cl_s,unsigned int udc_cnt)687 ebt_check_entry(struct ebt_entry *e, struct net *net,
688 		const struct ebt_table_info *newinfo,
689 		const char *name, unsigned int *cnt,
690 		struct ebt_cl_stack *cl_s, unsigned int udc_cnt)
691 {
692 	struct ebt_entry_target *t;
693 	struct xt_target *target;
694 	unsigned int i, j, hook = 0, hookmask = 0;
695 	size_t gap;
696 	int ret;
697 	struct xt_mtchk_param mtpar;
698 	struct xt_tgchk_param tgpar;
699 
700 	/* don't mess with the struct ebt_entries */
701 	if (e->bitmask == 0)
702 		return 0;
703 
704 	if (e->bitmask & ~EBT_F_MASK)
705 		return -EINVAL;
706 
707 	if (e->invflags & ~EBT_INV_MASK)
708 		return -EINVAL;
709 
710 	if ((e->bitmask & EBT_NOPROTO) && (e->bitmask & EBT_802_3))
711 		return -EINVAL;
712 
713 	/* what hook do we belong to? */
714 	for (i = 0; i < NF_BR_NUMHOOKS; i++) {
715 		if (!newinfo->hook_entry[i])
716 			continue;
717 		if ((char *)newinfo->hook_entry[i] < (char *)e)
718 			hook = i;
719 		else
720 			break;
721 	}
722 	/* (1 << NF_BR_NUMHOOKS) tells the check functions the rule is on
723 	 * a base chain
724 	 */
725 	if (i < NF_BR_NUMHOOKS)
726 		hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS);
727 	else {
728 		for (i = 0; i < udc_cnt; i++)
729 			if ((char *)(cl_s[i].cs.chaininfo) > (char *)e)
730 				break;
731 		if (i == 0)
732 			hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS);
733 		else
734 			hookmask = cl_s[i - 1].hookmask;
735 	}
736 	i = 0;
737 
738 	memset(&mtpar, 0, sizeof(mtpar));
739 	memset(&tgpar, 0, sizeof(tgpar));
740 	mtpar.net	= tgpar.net       = net;
741 	mtpar.table     = tgpar.table     = name;
742 	mtpar.entryinfo = tgpar.entryinfo = e;
743 	mtpar.hook_mask = tgpar.hook_mask = hookmask;
744 	mtpar.family    = tgpar.family    = NFPROTO_BRIDGE;
745 	ret = EBT_MATCH_ITERATE(e, ebt_check_match, &mtpar, &i);
746 	if (ret != 0)
747 		goto cleanup_matches;
748 	j = 0;
749 	ret = EBT_WATCHER_ITERATE(e, ebt_check_watcher, &tgpar, &j);
750 	if (ret != 0)
751 		goto cleanup_watchers;
752 	t = ebt_get_target(e);
753 	gap = e->next_offset - e->target_offset;
754 
755 	target = xt_request_find_target(NFPROTO_BRIDGE, t->u.name, 0);
756 	if (IS_ERR(target)) {
757 		ret = PTR_ERR(target);
758 		goto cleanup_watchers;
759 	}
760 
761 	/* Reject UNSPEC, xtables verdicts/return values are incompatible */
762 	if (target->family != NFPROTO_BRIDGE) {
763 		module_put(target->me);
764 		ret = -ENOENT;
765 		goto cleanup_watchers;
766 	}
767 
768 	t->u.target = target;
769 	if (t->u.target == &ebt_standard_target) {
770 		if (gap < sizeof(struct ebt_standard_target)) {
771 			ret = -EFAULT;
772 			goto cleanup_watchers;
773 		}
774 		if (((struct ebt_standard_target *)t)->verdict <
775 		   -NUM_STANDARD_TARGETS) {
776 			ret = -EFAULT;
777 			goto cleanup_watchers;
778 		}
779 	} else if (t->target_size > gap - sizeof(struct ebt_entry_target)) {
780 		module_put(t->u.target->me);
781 		ret = -EFAULT;
782 		goto cleanup_watchers;
783 	}
784 
785 	tgpar.target   = target;
786 	tgpar.targinfo = t->data;
787 	ret = xt_check_target(&tgpar, t->target_size,
788 	      ntohs(e->ethproto), e->invflags & EBT_IPROTO);
789 	if (ret < 0) {
790 		module_put(target->me);
791 		goto cleanup_watchers;
792 	}
793 	(*cnt)++;
794 	return 0;
795 cleanup_watchers:
796 	EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, net, &j);
797 cleanup_matches:
798 	EBT_MATCH_ITERATE(e, ebt_cleanup_match, net, &i);
799 	return ret;
800 }
801 
802 /* checks for loops and sets the hook mask for udc
803  * the hook mask for udc tells us from which base chains the udc can be
804  * accessed. This mask is a parameter to the check() functions of the extensions
805  */
check_chainloops(const struct ebt_entries * chain,struct ebt_cl_stack * cl_s,unsigned int udc_cnt,unsigned int hooknr,char * base)806 static int check_chainloops(const struct ebt_entries *chain, struct ebt_cl_stack *cl_s,
807 			    unsigned int udc_cnt, unsigned int hooknr, char *base)
808 {
809 	int i, chain_nr = -1, pos = 0, nentries = chain->nentries, verdict;
810 	const struct ebt_entry *e = (struct ebt_entry *)chain->data;
811 	const struct ebt_entry_target *t;
812 
813 	while (pos < nentries || chain_nr != -1) {
814 		/* end of udc, go back one 'recursion' step */
815 		if (pos == nentries) {
816 			/* put back values of the time when this chain was called */
817 			e = cl_s[chain_nr].cs.e;
818 			if (cl_s[chain_nr].from != -1)
819 				nentries =
820 				cl_s[cl_s[chain_nr].from].cs.chaininfo->nentries;
821 			else
822 				nentries = chain->nentries;
823 			pos = cl_s[chain_nr].cs.n;
824 			/* make sure we won't see a loop that isn't one */
825 			cl_s[chain_nr].cs.n = 0;
826 			chain_nr = cl_s[chain_nr].from;
827 			if (pos == nentries)
828 				continue;
829 		}
830 		t = ebt_get_target_c(e);
831 		if (strcmp(t->u.name, EBT_STANDARD_TARGET))
832 			goto letscontinue;
833 		if (e->target_offset + sizeof(struct ebt_standard_target) >
834 		   e->next_offset)
835 			return -1;
836 
837 		verdict = ((struct ebt_standard_target *)t)->verdict;
838 		if (verdict >= 0) { /* jump to another chain */
839 			struct ebt_entries *hlp2 =
840 			   (struct ebt_entries *)(base + verdict);
841 			for (i = 0; i < udc_cnt; i++)
842 				if (hlp2 == cl_s[i].cs.chaininfo)
843 					break;
844 			/* bad destination or loop */
845 			if (i == udc_cnt)
846 				return -1;
847 
848 			if (cl_s[i].cs.n)
849 				return -1;
850 
851 			if (cl_s[i].hookmask & (1 << hooknr))
852 				goto letscontinue;
853 			/* this can't be 0, so the loop test is correct */
854 			cl_s[i].cs.n = pos + 1;
855 			pos = 0;
856 			cl_s[i].cs.e = ebt_next_entry(e);
857 			e = (struct ebt_entry *)(hlp2->data);
858 			nentries = hlp2->nentries;
859 			cl_s[i].from = chain_nr;
860 			chain_nr = i;
861 			/* this udc is accessible from the base chain for hooknr */
862 			cl_s[i].hookmask |= (1 << hooknr);
863 			continue;
864 		}
865 letscontinue:
866 		e = ebt_next_entry(e);
867 		pos++;
868 	}
869 	return 0;
870 }
871 
872 /* do the parsing of the table/chains/entries/matches/watchers/targets, heh */
translate_table(struct net * net,const char * name,struct ebt_table_info * newinfo)873 static int translate_table(struct net *net, const char *name,
874 			   struct ebt_table_info *newinfo)
875 {
876 	unsigned int i, j, k, udc_cnt;
877 	int ret;
878 	struct ebt_cl_stack *cl_s = NULL; /* used in the checking for chain loops */
879 
880 	i = 0;
881 	while (i < NF_BR_NUMHOOKS && !newinfo->hook_entry[i])
882 		i++;
883 	if (i == NF_BR_NUMHOOKS)
884 		return -EINVAL;
885 
886 	if (newinfo->hook_entry[i] != (struct ebt_entries *)newinfo->entries)
887 		return -EINVAL;
888 
889 	/* make sure chains are ordered after each other in same order
890 	 * as their corresponding hooks
891 	 */
892 	for (j = i + 1; j < NF_BR_NUMHOOKS; j++) {
893 		if (!newinfo->hook_entry[j])
894 			continue;
895 		if (newinfo->hook_entry[j] <= newinfo->hook_entry[i])
896 			return -EINVAL;
897 
898 		i = j;
899 	}
900 
901 	/* do some early checkings and initialize some things */
902 	i = 0; /* holds the expected nr. of entries for the chain */
903 	j = 0; /* holds the up to now counted entries for the chain */
904 	k = 0; /* holds the total nr. of entries, should equal
905 		* newinfo->nentries afterwards
906 		*/
907 	udc_cnt = 0; /* will hold the nr. of user defined chains (udc) */
908 	ret = EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
909 	   ebt_check_entry_size_and_hooks, newinfo,
910 	   &i, &j, &k, &udc_cnt);
911 
912 	if (ret != 0)
913 		return ret;
914 
915 	if (i != j)
916 		return -EINVAL;
917 
918 	if (k != newinfo->nentries)
919 		return -EINVAL;
920 
921 	/* get the location of the udc, put them in an array
922 	 * while we're at it, allocate the chainstack
923 	 */
924 	if (udc_cnt) {
925 		/* this will get free'd in do_replace()/ebt_register_table()
926 		 * if an error occurs
927 		 */
928 		newinfo->chainstack =
929 			vcalloc(nr_cpu_ids, sizeof(*(newinfo->chainstack)));
930 		if (!newinfo->chainstack)
931 			return -ENOMEM;
932 		for_each_possible_cpu(i) {
933 			newinfo->chainstack[i] =
934 			  vmalloc_node(array_size(udc_cnt,
935 					  sizeof(*(newinfo->chainstack[0]))),
936 				       cpu_to_node(i));
937 			if (!newinfo->chainstack[i]) {
938 				while (i)
939 					vfree(newinfo->chainstack[--i]);
940 				vfree(newinfo->chainstack);
941 				newinfo->chainstack = NULL;
942 				return -ENOMEM;
943 			}
944 		}
945 
946 		cl_s = vmalloc_array(udc_cnt, sizeof(*cl_s));
947 		if (!cl_s)
948 			return -ENOMEM;
949 		i = 0; /* the i'th udc */
950 		EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
951 		   ebt_get_udc_positions, newinfo, &i, cl_s);
952 		/* sanity check */
953 		if (i != udc_cnt) {
954 			vfree(cl_s);
955 			return -EFAULT;
956 		}
957 	}
958 
959 	/* Check for loops */
960 	for (i = 0; i < NF_BR_NUMHOOKS; i++)
961 		if (newinfo->hook_entry[i])
962 			if (check_chainloops(newinfo->hook_entry[i],
963 			   cl_s, udc_cnt, i, newinfo->entries)) {
964 				vfree(cl_s);
965 				return -EINVAL;
966 			}
967 
968 	/* we now know the following (along with E=mc²):
969 	 *  - the nr of entries in each chain is right
970 	 *  - the size of the allocated space is right
971 	 *  - all valid hooks have a corresponding chain
972 	 *  - there are no loops
973 	 *  - wrong data can still be on the level of a single entry
974 	 *  - could be there are jumps to places that are not the
975 	 *    beginning of a chain. This can only occur in chains that
976 	 *    are not accessible from any base chains, so we don't care.
977 	 */
978 
979 	/* used to know what we need to clean up if something goes wrong */
980 	i = 0;
981 	ret = EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
982 	   ebt_check_entry, net, newinfo, name, &i, cl_s, udc_cnt);
983 	if (ret != 0) {
984 		EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
985 				  ebt_cleanup_entry, net, &i);
986 	}
987 	vfree(cl_s);
988 	return ret;
989 }
990 
991 /* called under write_lock */
get_counters(const struct ebt_counter * oldcounters,struct ebt_counter * counters,unsigned int nentries)992 static void get_counters(const struct ebt_counter *oldcounters,
993 			 struct ebt_counter *counters, unsigned int nentries)
994 {
995 	int i, cpu;
996 	struct ebt_counter *counter_base;
997 
998 	/* counters of cpu 0 */
999 	memcpy(counters, oldcounters,
1000 	       sizeof(struct ebt_counter) * nentries);
1001 
1002 	/* add other counters to those of cpu 0 */
1003 	for_each_possible_cpu(cpu) {
1004 		if (cpu == 0)
1005 			continue;
1006 		counter_base = COUNTER_BASE(oldcounters, nentries, cpu);
1007 		for (i = 0; i < nentries; i++)
1008 			ADD_COUNTER(counters[i], counter_base[i].bcnt,
1009 				    counter_base[i].pcnt);
1010 	}
1011 }
1012 
do_replace_finish(struct net * net,struct ebt_replace * repl,struct ebt_table_info * newinfo)1013 static int do_replace_finish(struct net *net, struct ebt_replace *repl,
1014 			      struct ebt_table_info *newinfo)
1015 {
1016 	int ret;
1017 	struct ebt_counter *counterstmp = NULL;
1018 	/* used to be able to unlock earlier */
1019 	struct ebt_table_info *table;
1020 	struct ebt_table *t;
1021 
1022 	/* the user wants counters back
1023 	 * the check on the size is done later, when we have the lock
1024 	 */
1025 	if (repl->num_counters) {
1026 		counterstmp = vmalloc_array(repl->num_counters,
1027 					    sizeof(*counterstmp));
1028 		if (!counterstmp)
1029 			return -ENOMEM;
1030 	}
1031 
1032 	newinfo->chainstack = NULL;
1033 	ret = ebt_verify_pointers(repl, newinfo);
1034 	if (ret != 0)
1035 		goto free_counterstmp;
1036 
1037 	ret = translate_table(net, repl->name, newinfo);
1038 
1039 	if (ret != 0)
1040 		goto free_counterstmp;
1041 
1042 	t = find_table_lock(net, repl->name, &ret, &ebt_mutex);
1043 	if (!t) {
1044 		ret = -ENOENT;
1045 		goto free_iterate;
1046 	}
1047 
1048 	if (repl->valid_hooks != t->valid_hooks) {
1049 		ret = -EINVAL;
1050 		goto free_unlock;
1051 	}
1052 
1053 	if (repl->num_counters && repl->num_counters != t->private->nentries) {
1054 		ret = -EINVAL;
1055 		goto free_unlock;
1056 	}
1057 
1058 	/* we have the mutex lock, so no danger in reading this pointer */
1059 	table = t->private;
1060 	/* make sure the table can only be rmmod'ed if it contains no rules */
1061 	if (!table->nentries && newinfo->nentries && !try_module_get(t->me)) {
1062 		ret = -ENOENT;
1063 		goto free_unlock;
1064 	} else if (table->nentries && !newinfo->nentries)
1065 		module_put(t->me);
1066 	/* we need an atomic snapshot of the counters */
1067 	write_lock_bh(&t->lock);
1068 	if (repl->num_counters)
1069 		get_counters(t->private->counters, counterstmp,
1070 		   t->private->nentries);
1071 
1072 	t->private = newinfo;
1073 	write_unlock_bh(&t->lock);
1074 	mutex_unlock(&ebt_mutex);
1075 	/* so, a user can change the chains while having messed up her counter
1076 	 * allocation. Only reason why this is done is because this way the lock
1077 	 * is held only once, while this doesn't bring the kernel into a
1078 	 * dangerous state.
1079 	 */
1080 	if (repl->num_counters &&
1081 	   copy_to_user(repl->counters, counterstmp,
1082 	   array_size(repl->num_counters, sizeof(struct ebt_counter)))) {
1083 		/* Silent error, can't fail, new table is already in place */
1084 		net_warn_ratelimited("ebtables: counters copy to user failed while replacing table\n");
1085 	}
1086 
1087 	/* decrease module count and free resources */
1088 	EBT_ENTRY_ITERATE(table->entries, table->entries_size,
1089 			  ebt_cleanup_entry, net, NULL);
1090 
1091 	vfree(table->entries);
1092 	ebt_free_table_info(table);
1093 	vfree(table);
1094 	vfree(counterstmp);
1095 
1096 	audit_log_nfcfg(repl->name, AF_BRIDGE, repl->nentries,
1097 			AUDIT_XT_OP_REPLACE, GFP_KERNEL);
1098 	return 0;
1099 
1100 free_unlock:
1101 	mutex_unlock(&ebt_mutex);
1102 free_iterate:
1103 	EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
1104 			  ebt_cleanup_entry, net, NULL);
1105 free_counterstmp:
1106 	vfree(counterstmp);
1107 	/* can be initialized in translate_table() */
1108 	ebt_free_table_info(newinfo);
1109 	return ret;
1110 }
1111 
1112 /* replace the table */
do_replace(struct net * net,sockptr_t arg,unsigned int len)1113 static int do_replace(struct net *net, sockptr_t arg, unsigned int len)
1114 {
1115 	int ret, countersize;
1116 	struct ebt_table_info *newinfo;
1117 	struct ebt_replace tmp;
1118 
1119 	if (len < sizeof(tmp))
1120 		return -EINVAL;
1121 	if (copy_from_sockptr(&tmp, arg, sizeof(tmp)) != 0)
1122 		return -EFAULT;
1123 
1124 	if (len != sizeof(tmp) + tmp.entries_size)
1125 		return -EINVAL;
1126 
1127 	if (tmp.entries_size == 0)
1128 		return -EINVAL;
1129 
1130 	/* overflow check */
1131 	if (tmp.nentries >= MAX_EBT_ENTRIES)
1132 		return -ENOMEM;
1133 	if (tmp.num_counters >= MAX_EBT_ENTRIES)
1134 		return -ENOMEM;
1135 
1136 	tmp.name[sizeof(tmp.name) - 1] = 0;
1137 
1138 	countersize = COUNTER_OFFSET(tmp.nentries) * nr_cpu_ids;
1139 	newinfo = __vmalloc(sizeof(*newinfo) + countersize, GFP_KERNEL_ACCOUNT);
1140 	if (!newinfo)
1141 		return -ENOMEM;
1142 
1143 	if (countersize)
1144 		memset(newinfo->counters, 0, countersize);
1145 
1146 	newinfo->entries = __vmalloc(tmp.entries_size, GFP_KERNEL_ACCOUNT);
1147 	if (!newinfo->entries) {
1148 		ret = -ENOMEM;
1149 		goto free_newinfo;
1150 	}
1151 	if (copy_from_user(
1152 	   newinfo->entries, tmp.entries, tmp.entries_size) != 0) {
1153 		ret = -EFAULT;
1154 		goto free_entries;
1155 	}
1156 
1157 	ret = do_replace_finish(net, &tmp, newinfo);
1158 	if (ret == 0)
1159 		return ret;
1160 free_entries:
1161 	vfree(newinfo->entries);
1162 free_newinfo:
1163 	vfree(newinfo);
1164 	return ret;
1165 }
1166 
__ebt_unregister_table(struct net * net,struct ebt_table * table)1167 static void __ebt_unregister_table(struct net *net, struct ebt_table *table)
1168 {
1169 	EBT_ENTRY_ITERATE(table->private->entries, table->private->entries_size,
1170 			  ebt_cleanup_entry, net, NULL);
1171 	if (table->private->nentries)
1172 		module_put(table->me);
1173 	vfree(table->private->entries);
1174 	ebt_free_table_info(table->private);
1175 	vfree(table->private);
1176 	kfree(table->ops);
1177 	kfree(table);
1178 }
1179 
ebt_register_table(struct net * net,const struct ebt_table * input_table,const struct nf_hook_ops * template_ops)1180 int ebt_register_table(struct net *net, const struct ebt_table *input_table,
1181 		       const struct nf_hook_ops *template_ops)
1182 {
1183 	struct ebt_pernet *ebt_net = net_generic(net, ebt_pernet_id);
1184 	struct ebt_table_info *newinfo;
1185 	struct ebt_table *t, *table;
1186 	struct nf_hook_ops *ops;
1187 	unsigned int num_ops;
1188 	struct ebt_replace_kernel *repl;
1189 	int ret, i, countersize;
1190 	void *p;
1191 
1192 	if (input_table == NULL || (repl = input_table->table) == NULL ||
1193 	    repl->entries == NULL || repl->entries_size == 0 ||
1194 	    repl->counters != NULL || input_table->private != NULL)
1195 		return -EINVAL;
1196 
1197 	/* Don't add one table to multiple lists. */
1198 	table = kmemdup(input_table, sizeof(struct ebt_table), GFP_KERNEL);
1199 	if (!table) {
1200 		ret = -ENOMEM;
1201 		goto out;
1202 	}
1203 
1204 	countersize = COUNTER_OFFSET(repl->nentries) * nr_cpu_ids;
1205 	newinfo = vmalloc(sizeof(*newinfo) + countersize);
1206 	ret = -ENOMEM;
1207 	if (!newinfo)
1208 		goto free_table;
1209 
1210 	p = vmalloc(repl->entries_size);
1211 	if (!p)
1212 		goto free_newinfo;
1213 
1214 	memcpy(p, repl->entries, repl->entries_size);
1215 	newinfo->entries = p;
1216 
1217 	newinfo->entries_size = repl->entries_size;
1218 	newinfo->nentries = repl->nentries;
1219 
1220 	if (countersize)
1221 		memset(newinfo->counters, 0, countersize);
1222 
1223 	/* fill in newinfo and parse the entries */
1224 	newinfo->chainstack = NULL;
1225 	for (i = 0; i < NF_BR_NUMHOOKS; i++) {
1226 		if ((repl->valid_hooks & (1 << i)) == 0)
1227 			newinfo->hook_entry[i] = NULL;
1228 		else
1229 			newinfo->hook_entry[i] = p +
1230 				((char *)repl->hook_entry[i] - repl->entries);
1231 	}
1232 	ret = translate_table(net, repl->name, newinfo);
1233 	if (ret != 0)
1234 		goto free_chainstack;
1235 
1236 	table->private = newinfo;
1237 	rwlock_init(&table->lock);
1238 	mutex_lock(&ebt_mutex);
1239 	list_for_each_entry(t, &ebt_net->tables, list) {
1240 		if (strcmp(t->name, table->name) == 0) {
1241 			ret = -EEXIST;
1242 			goto free_unlock;
1243 		}
1244 	}
1245 
1246 	/* Hold a reference count if the chains aren't empty */
1247 	if (newinfo->nentries && !try_module_get(table->me)) {
1248 		ret = -ENOENT;
1249 		goto free_unlock;
1250 	}
1251 
1252 	num_ops = hweight32(table->valid_hooks);
1253 	if (num_ops == 0) {
1254 		ret = -EINVAL;
1255 		goto free_unlock;
1256 	}
1257 
1258 	ops = kmemdup_array(template_ops, num_ops, sizeof(*ops), GFP_KERNEL);
1259 	if (!ops) {
1260 		ret = -ENOMEM;
1261 		if (newinfo->nentries)
1262 			module_put(table->me);
1263 		goto free_unlock;
1264 	}
1265 
1266 	for (i = 0; i < num_ops; i++)
1267 		ops[i].priv = table;
1268 
1269 	table->ops = ops;
1270 	ret = nf_register_net_hooks(net, ops, num_ops);
1271 	if (ret) {
1272 		synchronize_rcu();
1273 		__ebt_unregister_table(net, table);
1274 	} else {
1275 		list_add(&table->list, &ebt_net->tables);
1276 	}
1277 	mutex_unlock(&ebt_mutex);
1278 
1279 	audit_log_nfcfg(repl->name, AF_BRIDGE, repl->nentries,
1280 			AUDIT_XT_OP_REGISTER, GFP_KERNEL);
1281 	return ret;
1282 free_unlock:
1283 	mutex_unlock(&ebt_mutex);
1284 free_chainstack:
1285 	ebt_free_table_info(newinfo);
1286 	vfree(newinfo->entries);
1287 free_newinfo:
1288 	vfree(newinfo);
1289 free_table:
1290 	kfree(table);
1291 out:
1292 	return ret;
1293 }
1294 
ebt_register_template(const struct ebt_table * t,int (* table_init)(struct net * net))1295 int ebt_register_template(const struct ebt_table *t, int (*table_init)(struct net *net))
1296 {
1297 	struct ebt_template *tmpl;
1298 
1299 	mutex_lock(&ebt_mutex);
1300 	list_for_each_entry(tmpl, &template_tables, list) {
1301 		if (WARN_ON_ONCE(strcmp(t->name, tmpl->name) == 0)) {
1302 			mutex_unlock(&ebt_mutex);
1303 			return -EBUSY;
1304 		}
1305 	}
1306 
1307 	tmpl = kzalloc_obj(*tmpl);
1308 	if (!tmpl) {
1309 		mutex_unlock(&ebt_mutex);
1310 		return -ENOMEM;
1311 	}
1312 
1313 	tmpl->table_init = table_init;
1314 	strscpy(tmpl->name, t->name, sizeof(tmpl->name));
1315 	tmpl->owner = t->me;
1316 	list_add(&tmpl->list, &template_tables);
1317 
1318 	mutex_unlock(&ebt_mutex);
1319 	return 0;
1320 }
1321 EXPORT_SYMBOL(ebt_register_template);
1322 
ebt_unregister_template(const struct ebt_table * t)1323 void ebt_unregister_template(const struct ebt_table *t)
1324 {
1325 	struct ebt_template *tmpl;
1326 
1327 	mutex_lock(&ebt_mutex);
1328 	list_for_each_entry(tmpl, &template_tables, list) {
1329 		if (strcmp(t->name, tmpl->name))
1330 			continue;
1331 
1332 		list_del(&tmpl->list);
1333 		mutex_unlock(&ebt_mutex);
1334 		kfree(tmpl);
1335 		return;
1336 	}
1337 
1338 	mutex_unlock(&ebt_mutex);
1339 	WARN_ON_ONCE(1);
1340 }
1341 EXPORT_SYMBOL(ebt_unregister_template);
1342 
ebt_unregister_table_pre_exit(struct net * net,const char * name)1343 void ebt_unregister_table_pre_exit(struct net *net, const char *name)
1344 {
1345 	struct ebt_pernet *ebt_net = net_generic(net, ebt_pernet_id);
1346 	struct ebt_table *t;
1347 
1348 	mutex_lock(&ebt_mutex);
1349 
1350 	list_for_each_entry(t, &ebt_net->tables, list) {
1351 		if (strcmp(t->name, name) == 0) {
1352 			list_move(&t->list, &ebt_net->dead_tables);
1353 			mutex_unlock(&ebt_mutex);
1354 			nf_unregister_net_hooks(net, t->ops, hweight32(t->valid_hooks));
1355 			return;
1356 		}
1357 	}
1358 
1359 	mutex_unlock(&ebt_mutex);
1360 }
1361 EXPORT_SYMBOL(ebt_unregister_table_pre_exit);
1362 
ebt_unregister_table(struct net * net,const char * name)1363 void ebt_unregister_table(struct net *net, const char *name)
1364 {
1365 	struct ebt_pernet *ebt_net = net_generic(net, ebt_pernet_id);
1366 	struct ebt_table *t;
1367 
1368 	mutex_lock(&ebt_mutex);
1369 
1370 	list_for_each_entry(t, &ebt_net->dead_tables, list) {
1371 		if (strcmp(t->name, name) == 0) {
1372 			list_del(&t->list);
1373 			audit_log_nfcfg(t->name, AF_BRIDGE, t->private->nentries,
1374 					AUDIT_XT_OP_UNREGISTER, GFP_KERNEL);
1375 			__ebt_unregister_table(net, t);
1376 			mutex_unlock(&ebt_mutex);
1377 			return;
1378 		}
1379 	}
1380 
1381 	mutex_unlock(&ebt_mutex);
1382 }
1383 
1384 /* userspace just supplied us with counters */
do_update_counters(struct net * net,const char * name,struct ebt_counter __user * counters,unsigned int num_counters,unsigned int len)1385 static int do_update_counters(struct net *net, const char *name,
1386 			      struct ebt_counter __user *counters,
1387 			      unsigned int num_counters, unsigned int len)
1388 {
1389 	int i, ret;
1390 	struct ebt_counter *tmp;
1391 	struct ebt_table *t;
1392 
1393 	if (num_counters == 0)
1394 		return -EINVAL;
1395 
1396 	tmp = vmalloc_array(num_counters, sizeof(*tmp));
1397 	if (!tmp)
1398 		return -ENOMEM;
1399 
1400 	t = find_table_lock(net, name, &ret, &ebt_mutex);
1401 	if (!t)
1402 		goto free_tmp;
1403 
1404 	if (num_counters != t->private->nentries) {
1405 		ret = -EINVAL;
1406 		goto unlock_mutex;
1407 	}
1408 
1409 	if (copy_from_user(tmp, counters,
1410 			   array_size(num_counters, sizeof(*counters)))) {
1411 		ret = -EFAULT;
1412 		goto unlock_mutex;
1413 	}
1414 
1415 	/* we want an atomic add of the counters */
1416 	write_lock_bh(&t->lock);
1417 
1418 	/* we add to the counters of the first cpu */
1419 	for (i = 0; i < num_counters; i++)
1420 		ADD_COUNTER(t->private->counters[i], tmp[i].bcnt, tmp[i].pcnt);
1421 
1422 	write_unlock_bh(&t->lock);
1423 	ret = 0;
1424 unlock_mutex:
1425 	mutex_unlock(&ebt_mutex);
1426 free_tmp:
1427 	vfree(tmp);
1428 	return ret;
1429 }
1430 
update_counters(struct net * net,sockptr_t arg,unsigned int len)1431 static int update_counters(struct net *net, sockptr_t arg, unsigned int len)
1432 {
1433 	struct ebt_replace hlp;
1434 
1435 	if (len < sizeof(hlp))
1436 		return -EINVAL;
1437 	if (copy_from_sockptr(&hlp, arg, sizeof(hlp)))
1438 		return -EFAULT;
1439 
1440 	hlp.name[sizeof(hlp.name) - 1] = '\0';
1441 
1442 	if (len != sizeof(hlp) + hlp.num_counters * sizeof(struct ebt_counter))
1443 		return -EINVAL;
1444 
1445 	return do_update_counters(net, hlp.name, hlp.counters,
1446 				  hlp.num_counters, len);
1447 }
1448 
ebt_obj_to_user(char __user * um,const char * _name,const char * data,int entrysize,int usersize,int datasize,u8 revision)1449 static inline int ebt_obj_to_user(char __user *um, const char *_name,
1450 				  const char *data, int entrysize,
1451 				  int usersize, int datasize, u8 revision)
1452 {
1453 	char name[EBT_EXTENSION_MAXNAMELEN] = {0};
1454 
1455 	/* ebtables expects 31 bytes long names but xt_match names are 29 bytes
1456 	 * long. Copy 29 bytes and fill remaining bytes with zeroes.
1457 	 */
1458 	strscpy(name, _name, sizeof(name));
1459 	if (copy_to_user(um, name, EBT_EXTENSION_MAXNAMELEN) ||
1460 	    put_user(revision, (u8 __user *)(um + EBT_EXTENSION_MAXNAMELEN)) ||
1461 	    put_user(datasize, (int __user *)(um + EBT_EXTENSION_MAXNAMELEN + 1)) ||
1462 	    xt_data_to_user(um + entrysize, data, usersize, datasize,
1463 			    XT_ALIGN(datasize)))
1464 		return -EFAULT;
1465 
1466 	return 0;
1467 }
1468 
ebt_match_to_user(const struct ebt_entry_match * m,const char * base,char __user * ubase)1469 static inline int ebt_match_to_user(const struct ebt_entry_match *m,
1470 				    const char *base, char __user *ubase)
1471 {
1472 	return ebt_obj_to_user(ubase + ((char *)m - base),
1473 			       m->u.match->name, m->data, sizeof(*m),
1474 			       m->u.match->usersize, m->match_size,
1475 			       m->u.match->revision);
1476 }
1477 
ebt_watcher_to_user(const struct ebt_entry_watcher * w,const char * base,char __user * ubase)1478 static inline int ebt_watcher_to_user(const struct ebt_entry_watcher *w,
1479 				      const char *base, char __user *ubase)
1480 {
1481 	return ebt_obj_to_user(ubase + ((char *)w - base),
1482 			       w->u.watcher->name, w->data, sizeof(*w),
1483 			       w->u.watcher->usersize, w->watcher_size,
1484 			       w->u.watcher->revision);
1485 }
1486 
ebt_entry_to_user(struct ebt_entry * e,const char * base,char __user * ubase)1487 static inline int ebt_entry_to_user(struct ebt_entry *e, const char *base,
1488 				    char __user *ubase)
1489 {
1490 	int ret;
1491 	char __user *hlp;
1492 	const struct ebt_entry_target *t;
1493 
1494 	if (e->bitmask == 0) {
1495 		/* special case !EBT_ENTRY_OR_ENTRIES */
1496 		if (copy_to_user(ubase + ((char *)e - base), e,
1497 				 sizeof(struct ebt_entries)))
1498 			return -EFAULT;
1499 		return 0;
1500 	}
1501 
1502 	if (copy_to_user(ubase + ((char *)e - base), e, sizeof(*e)))
1503 		return -EFAULT;
1504 
1505 	hlp = ubase + (((char *)e + e->target_offset) - base);
1506 	t = ebt_get_target_c(e);
1507 
1508 	ret = EBT_MATCH_ITERATE(e, ebt_match_to_user, base, ubase);
1509 	if (ret != 0)
1510 		return ret;
1511 	ret = EBT_WATCHER_ITERATE(e, ebt_watcher_to_user, base, ubase);
1512 	if (ret != 0)
1513 		return ret;
1514 	ret = ebt_obj_to_user(hlp, t->u.target->name, t->data, sizeof(*t),
1515 			      t->u.target->usersize, t->target_size,
1516 			      t->u.target->revision);
1517 	if (ret != 0)
1518 		return ret;
1519 
1520 	return 0;
1521 }
1522 
copy_counters_to_user(struct ebt_table * t,const struct ebt_counter * oldcounters,void __user * user,unsigned int num_counters,unsigned int nentries)1523 static int copy_counters_to_user(struct ebt_table *t,
1524 				 const struct ebt_counter *oldcounters,
1525 				 void __user *user, unsigned int num_counters,
1526 				 unsigned int nentries)
1527 {
1528 	struct ebt_counter *counterstmp;
1529 	int ret = 0;
1530 
1531 	/* userspace might not need the counters */
1532 	if (num_counters == 0)
1533 		return 0;
1534 
1535 	if (num_counters != nentries)
1536 		return -EINVAL;
1537 
1538 	counterstmp = vmalloc_array(nentries, sizeof(*counterstmp));
1539 	if (!counterstmp)
1540 		return -ENOMEM;
1541 
1542 	write_lock_bh(&t->lock);
1543 	get_counters(oldcounters, counterstmp, nentries);
1544 	write_unlock_bh(&t->lock);
1545 
1546 	if (copy_to_user(user, counterstmp,
1547 	    array_size(nentries, sizeof(struct ebt_counter))))
1548 		ret = -EFAULT;
1549 	vfree(counterstmp);
1550 	return ret;
1551 }
1552 
1553 /* called with ebt_mutex locked */
copy_everything_to_user(struct ebt_table * t,void __user * user,const int * len,int cmd)1554 static int copy_everything_to_user(struct ebt_table *t, void __user *user,
1555 				   const int *len, int cmd)
1556 {
1557 	struct ebt_replace tmp;
1558 	const struct ebt_counter *oldcounters;
1559 	unsigned int entries_size, nentries;
1560 	int ret;
1561 	char *entries;
1562 
1563 	if (cmd == EBT_SO_GET_ENTRIES) {
1564 		entries_size = t->private->entries_size;
1565 		nentries = t->private->nentries;
1566 		entries = t->private->entries;
1567 		oldcounters = t->private->counters;
1568 	} else {
1569 		entries_size = t->table->entries_size;
1570 		nentries = t->table->nentries;
1571 		entries = t->table->entries;
1572 		oldcounters = t->table->counters;
1573 	}
1574 
1575 	if (copy_from_user(&tmp, user, sizeof(tmp)))
1576 		return -EFAULT;
1577 
1578 	if (*len != sizeof(struct ebt_replace) + entries_size +
1579 	   (tmp.num_counters ? nentries * sizeof(struct ebt_counter) : 0))
1580 		return -EINVAL;
1581 
1582 	if (tmp.nentries != nentries)
1583 		return -EINVAL;
1584 
1585 	if (tmp.entries_size != entries_size)
1586 		return -EINVAL;
1587 
1588 	ret = copy_counters_to_user(t, oldcounters, tmp.counters,
1589 					tmp.num_counters, nentries);
1590 	if (ret)
1591 		return ret;
1592 
1593 	/* set the match/watcher/target names right */
1594 	return EBT_ENTRY_ITERATE(entries, entries_size,
1595 	   ebt_entry_to_user, entries, tmp.entries);
1596 }
1597 
1598 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
1599 /* 32 bit-userspace compatibility definitions. */
1600 struct compat_ebt_replace {
1601 	char name[EBT_TABLE_MAXNAMELEN];
1602 	compat_uint_t valid_hooks;
1603 	compat_uint_t nentries;
1604 	compat_uint_t entries_size;
1605 	/* start of the chains */
1606 	compat_uptr_t hook_entry[NF_BR_NUMHOOKS];
1607 	/* nr of counters userspace expects back */
1608 	compat_uint_t num_counters;
1609 	/* where the kernel will put the old counters. */
1610 	compat_uptr_t counters;
1611 	compat_uptr_t entries;
1612 };
1613 
1614 /* struct ebt_entry_match, _target and _watcher have same layout */
1615 struct compat_ebt_entry_mwt {
1616 	union {
1617 		struct {
1618 			char name[EBT_EXTENSION_MAXNAMELEN];
1619 			u8 revision;
1620 		};
1621 		compat_uptr_t ptr;
1622 	} u;
1623 	compat_uint_t match_size;
1624 	compat_uint_t data[] __aligned(__alignof__(struct compat_ebt_replace));
1625 };
1626 
1627 /* account for possible padding between match_size and ->data */
ebt_compat_entry_padsize(void)1628 static int ebt_compat_entry_padsize(void)
1629 {
1630 	BUILD_BUG_ON(sizeof(struct ebt_entry_match) <
1631 			sizeof(struct compat_ebt_entry_mwt));
1632 	return (int) sizeof(struct ebt_entry_match) -
1633 			sizeof(struct compat_ebt_entry_mwt);
1634 }
1635 
ebt_compat_match_offset(const struct xt_match * match,unsigned int userlen)1636 static int ebt_compat_match_offset(const struct xt_match *match,
1637 				   unsigned int userlen)
1638 {
1639 	/* ebt_among needs special handling. The kernel .matchsize is
1640 	 * set to -1 at registration time; at runtime an EBT_ALIGN()ed
1641 	 * value is expected.
1642 	 * Example: userspace sends 4500, ebt_among.c wants 4504.
1643 	 */
1644 	if (unlikely(match->matchsize == -1))
1645 		return XT_ALIGN(userlen) - COMPAT_XT_ALIGN(userlen);
1646 	return xt_compat_match_offset(match);
1647 }
1648 
compat_match_to_user(struct ebt_entry_match * m,void __user ** dstptr,unsigned int * size)1649 static int compat_match_to_user(struct ebt_entry_match *m, void __user **dstptr,
1650 				unsigned int *size)
1651 {
1652 	const struct xt_match *match = m->u.match;
1653 	struct compat_ebt_entry_mwt __user *cm = *dstptr;
1654 	int off = ebt_compat_match_offset(match, m->match_size);
1655 	compat_uint_t msize = m->match_size - off;
1656 
1657 	if (WARN_ON(off >= m->match_size))
1658 		return -EINVAL;
1659 
1660 	if (copy_to_user(cm->u.name, match->name, strlen(match->name) + 1) ||
1661 	    put_user(match->revision, &cm->u.revision) ||
1662 	    put_user(msize, &cm->match_size))
1663 		return -EFAULT;
1664 
1665 	if (match->compat_to_user) {
1666 		if (match->compat_to_user(cm->data, m->data))
1667 			return -EFAULT;
1668 	} else {
1669 		if (xt_data_to_user(cm->data, m->data, match->usersize, msize,
1670 				    COMPAT_XT_ALIGN(msize)))
1671 			return -EFAULT;
1672 	}
1673 
1674 	*size -= ebt_compat_entry_padsize() + off;
1675 	*dstptr = cm->data;
1676 	*dstptr += msize;
1677 	return 0;
1678 }
1679 
compat_target_to_user(struct ebt_entry_target * t,void __user ** dstptr,unsigned int * size)1680 static int compat_target_to_user(struct ebt_entry_target *t,
1681 				 void __user **dstptr,
1682 				 unsigned int *size)
1683 {
1684 	const struct xt_target *target = t->u.target;
1685 	struct compat_ebt_entry_mwt __user *cm = *dstptr;
1686 	int off = xt_compat_target_offset(target);
1687 	compat_uint_t tsize = t->target_size - off;
1688 
1689 	if (WARN_ON(off >= t->target_size))
1690 		return -EINVAL;
1691 
1692 	if (copy_to_user(cm->u.name, target->name, strlen(target->name) + 1) ||
1693 	    put_user(target->revision, &cm->u.revision) ||
1694 	    put_user(tsize, &cm->match_size))
1695 		return -EFAULT;
1696 
1697 	if (target->compat_to_user) {
1698 		if (target->compat_to_user(cm->data, t->data))
1699 			return -EFAULT;
1700 	} else {
1701 		if (xt_data_to_user(cm->data, t->data, target->usersize, tsize,
1702 				    COMPAT_XT_ALIGN(tsize)))
1703 			return -EFAULT;
1704 	}
1705 
1706 	*size -= ebt_compat_entry_padsize() + off;
1707 	*dstptr = cm->data;
1708 	*dstptr += tsize;
1709 	return 0;
1710 }
1711 
compat_watcher_to_user(struct ebt_entry_watcher * w,void __user ** dstptr,unsigned int * size)1712 static int compat_watcher_to_user(struct ebt_entry_watcher *w,
1713 				  void __user **dstptr,
1714 				  unsigned int *size)
1715 {
1716 	return compat_target_to_user((struct ebt_entry_target *)w,
1717 							dstptr, size);
1718 }
1719 
compat_copy_entry_to_user(struct ebt_entry * e,void __user ** dstptr,unsigned int * size)1720 static int compat_copy_entry_to_user(struct ebt_entry *e, void __user **dstptr,
1721 				unsigned int *size)
1722 {
1723 	struct ebt_entry_target *t;
1724 	struct ebt_entry __user *ce;
1725 	u32 watchers_offset, target_offset, next_offset;
1726 	compat_uint_t origsize;
1727 	int ret;
1728 
1729 	if (e->bitmask == 0) {
1730 		if (*size < sizeof(struct ebt_entries))
1731 			return -EINVAL;
1732 		if (copy_to_user(*dstptr, e, sizeof(struct ebt_entries)))
1733 			return -EFAULT;
1734 
1735 		*dstptr += sizeof(struct ebt_entries);
1736 		*size -= sizeof(struct ebt_entries);
1737 		return 0;
1738 	}
1739 
1740 	if (*size < sizeof(*ce))
1741 		return -EINVAL;
1742 
1743 	ce = *dstptr;
1744 	if (copy_to_user(ce, e, sizeof(*ce)))
1745 		return -EFAULT;
1746 
1747 	origsize = *size;
1748 	*dstptr += sizeof(*ce);
1749 
1750 	ret = EBT_MATCH_ITERATE(e, compat_match_to_user, dstptr, size);
1751 	if (ret)
1752 		return ret;
1753 	watchers_offset = e->watchers_offset - (origsize - *size);
1754 
1755 	ret = EBT_WATCHER_ITERATE(e, compat_watcher_to_user, dstptr, size);
1756 	if (ret)
1757 		return ret;
1758 	target_offset = e->target_offset - (origsize - *size);
1759 
1760 	t = ebt_get_target(e);
1761 
1762 	ret = compat_target_to_user(t, dstptr, size);
1763 	if (ret)
1764 		return ret;
1765 	next_offset = e->next_offset - (origsize - *size);
1766 
1767 	if (put_user(watchers_offset, &ce->watchers_offset) ||
1768 	    put_user(target_offset, &ce->target_offset) ||
1769 	    put_user(next_offset, &ce->next_offset))
1770 		return -EFAULT;
1771 
1772 	*size -= sizeof(*ce);
1773 	return 0;
1774 }
1775 
compat_calc_match(struct ebt_entry_match * m,int * off)1776 static int compat_calc_match(struct ebt_entry_match *m, int *off)
1777 {
1778 	*off += ebt_compat_match_offset(m->u.match, m->match_size);
1779 	*off += ebt_compat_entry_padsize();
1780 	return 0;
1781 }
1782 
compat_calc_watcher(struct ebt_entry_watcher * w,int * off)1783 static int compat_calc_watcher(struct ebt_entry_watcher *w, int *off)
1784 {
1785 	*off += xt_compat_target_offset(w->u.watcher);
1786 	*off += ebt_compat_entry_padsize();
1787 	return 0;
1788 }
1789 
compat_calc_entry(const struct ebt_entry * e,const struct ebt_table_info * info,const void * base,struct compat_ebt_replace * newinfo)1790 static int compat_calc_entry(const struct ebt_entry *e,
1791 			     const struct ebt_table_info *info,
1792 			     const void *base,
1793 			     struct compat_ebt_replace *newinfo)
1794 {
1795 	const struct ebt_entry_target *t;
1796 	unsigned int entry_offset;
1797 	int off, ret, i;
1798 
1799 	if (e->bitmask == 0)
1800 		return 0;
1801 
1802 	off = 0;
1803 	entry_offset = (void *)e - base;
1804 
1805 	EBT_MATCH_ITERATE(e, compat_calc_match, &off);
1806 	EBT_WATCHER_ITERATE(e, compat_calc_watcher, &off);
1807 
1808 	t = ebt_get_target_c(e);
1809 
1810 	off += xt_compat_target_offset(t->u.target);
1811 	off += ebt_compat_entry_padsize();
1812 
1813 	newinfo->entries_size -= off;
1814 
1815 	ret = xt_compat_add_offset(NFPROTO_BRIDGE, entry_offset, off);
1816 	if (ret)
1817 		return ret;
1818 
1819 	for (i = 0; i < NF_BR_NUMHOOKS; i++) {
1820 		const void *hookptr = info->hook_entry[i];
1821 		if (info->hook_entry[i] &&
1822 		    (e < (struct ebt_entry *)(base - hookptr))) {
1823 			newinfo->hook_entry[i] -= off;
1824 			pr_debug("0x%08X -> 0x%08X\n",
1825 					newinfo->hook_entry[i] + off,
1826 					newinfo->hook_entry[i]);
1827 		}
1828 	}
1829 
1830 	return 0;
1831 }
1832 
ebt_compat_init_offsets(unsigned int number)1833 static int ebt_compat_init_offsets(unsigned int number)
1834 {
1835 	if (number > INT_MAX)
1836 		return -EINVAL;
1837 
1838 	/* also count the base chain policies */
1839 	number += NF_BR_NUMHOOKS;
1840 
1841 	return xt_compat_init_offsets(NFPROTO_BRIDGE, number);
1842 }
1843 
compat_table_info(const struct ebt_table_info * info,struct compat_ebt_replace * newinfo)1844 static int compat_table_info(const struct ebt_table_info *info,
1845 			     struct compat_ebt_replace *newinfo)
1846 {
1847 	unsigned int size = info->entries_size;
1848 	const void *entries = info->entries;
1849 	int ret;
1850 
1851 	newinfo->entries_size = size;
1852 	ret = ebt_compat_init_offsets(info->nentries);
1853 	if (ret)
1854 		return ret;
1855 
1856 	return EBT_ENTRY_ITERATE(entries, size, compat_calc_entry, info,
1857 							entries, newinfo);
1858 }
1859 
compat_copy_everything_to_user(struct ebt_table * t,void __user * user,int * len,int cmd)1860 static int compat_copy_everything_to_user(struct ebt_table *t,
1861 					  void __user *user, int *len, int cmd)
1862 {
1863 	struct compat_ebt_replace repl, tmp;
1864 	struct ebt_counter *oldcounters;
1865 	struct ebt_table_info tinfo;
1866 	int ret;
1867 	void __user *pos;
1868 
1869 	memset(&tinfo, 0, sizeof(tinfo));
1870 
1871 	if (cmd == EBT_SO_GET_ENTRIES) {
1872 		tinfo.entries_size = t->private->entries_size;
1873 		tinfo.nentries = t->private->nentries;
1874 		tinfo.entries = t->private->entries;
1875 		oldcounters = t->private->counters;
1876 	} else {
1877 		tinfo.entries_size = t->table->entries_size;
1878 		tinfo.nentries = t->table->nentries;
1879 		tinfo.entries = t->table->entries;
1880 		oldcounters = t->table->counters;
1881 	}
1882 
1883 	if (copy_from_user(&tmp, user, sizeof(tmp)))
1884 		return -EFAULT;
1885 
1886 	if (tmp.nentries != tinfo.nentries ||
1887 	   (tmp.num_counters && tmp.num_counters != tinfo.nentries))
1888 		return -EINVAL;
1889 
1890 	memcpy(&repl, &tmp, sizeof(repl));
1891 	if (cmd == EBT_SO_GET_ENTRIES)
1892 		ret = compat_table_info(t->private, &repl);
1893 	else
1894 		ret = compat_table_info(&tinfo, &repl);
1895 	if (ret)
1896 		return ret;
1897 
1898 	if (*len != sizeof(tmp) + repl.entries_size +
1899 	   (tmp.num_counters? tinfo.nentries * sizeof(struct ebt_counter): 0)) {
1900 		pr_err("wrong size: *len %d, entries_size %u, replsz %d\n",
1901 				*len, tinfo.entries_size, repl.entries_size);
1902 		return -EINVAL;
1903 	}
1904 
1905 	/* userspace might not need the counters */
1906 	ret = copy_counters_to_user(t, oldcounters, compat_ptr(tmp.counters),
1907 					tmp.num_counters, tinfo.nentries);
1908 	if (ret)
1909 		return ret;
1910 
1911 	pos = compat_ptr(tmp.entries);
1912 	return EBT_ENTRY_ITERATE(tinfo.entries, tinfo.entries_size,
1913 			compat_copy_entry_to_user, &pos, &tmp.entries_size);
1914 }
1915 
1916 struct ebt_entries_buf_state {
1917 	char *buf_kern_start;	/* kernel buffer to copy (translated) data to */
1918 	u32 buf_kern_len;	/* total size of kernel buffer */
1919 	u32 buf_kern_offset;	/* amount of data copied so far */
1920 	u32 buf_user_offset;	/* read position in userspace buffer */
1921 };
1922 
ebt_buf_count(struct ebt_entries_buf_state * state,unsigned int sz)1923 static int ebt_buf_count(struct ebt_entries_buf_state *state, unsigned int sz)
1924 {
1925 	state->buf_kern_offset += sz;
1926 	return state->buf_kern_offset >= sz ? 0 : -EINVAL;
1927 }
1928 
ebt_buf_add(struct ebt_entries_buf_state * state,const void * data,unsigned int sz)1929 static int ebt_buf_add(struct ebt_entries_buf_state *state,
1930 		       const void *data, unsigned int sz)
1931 {
1932 	if (state->buf_kern_start == NULL)
1933 		goto count_only;
1934 
1935 	if (WARN_ON(state->buf_kern_offset + sz > state->buf_kern_len))
1936 		return -EINVAL;
1937 
1938 	memcpy(state->buf_kern_start + state->buf_kern_offset, data, sz);
1939 
1940  count_only:
1941 	state->buf_user_offset += sz;
1942 	return ebt_buf_count(state, sz);
1943 }
1944 
ebt_buf_add_pad(struct ebt_entries_buf_state * state,unsigned int sz)1945 static int ebt_buf_add_pad(struct ebt_entries_buf_state *state, unsigned int sz)
1946 {
1947 	char *b = state->buf_kern_start;
1948 
1949 	if (WARN_ON(b && state->buf_kern_offset > state->buf_kern_len))
1950 		return -EINVAL;
1951 
1952 	if (b != NULL && sz > 0)
1953 		memset(b + state->buf_kern_offset, 0, sz);
1954 	/* do not adjust ->buf_user_offset here, we added kernel-side padding */
1955 	return ebt_buf_count(state, sz);
1956 }
1957 
1958 enum compat_mwt {
1959 	EBT_COMPAT_MATCH,
1960 	EBT_COMPAT_WATCHER,
1961 	EBT_COMPAT_TARGET,
1962 };
1963 
match_size_ok(const struct xt_match * match,unsigned int match_size)1964 static bool match_size_ok(const struct xt_match *match, unsigned int match_size)
1965 {
1966 	u16 csize;
1967 
1968 	if (match->matchsize == -1) /* cannot validate ebt_among */
1969 		return true;
1970 
1971 	csize = match->compatsize ? : match->matchsize;
1972 
1973 	return match_size >= csize;
1974 }
1975 
tgt_size_ok(const struct xt_target * tgt,unsigned int tgt_size)1976 static bool tgt_size_ok(const struct xt_target *tgt, unsigned int tgt_size)
1977 {
1978 	u16 csize = tgt->compatsize ? : tgt->targetsize;
1979 
1980 	return tgt_size >= csize;
1981 }
1982 
compat_mtw_from_user(const struct compat_ebt_entry_mwt * mwt,enum compat_mwt compat_mwt,struct ebt_entries_buf_state * state,const unsigned char * base)1983 static int compat_mtw_from_user(const struct compat_ebt_entry_mwt *mwt,
1984 				enum compat_mwt compat_mwt,
1985 				struct ebt_entries_buf_state *state,
1986 				const unsigned char *base)
1987 {
1988 	char name[EBT_EXTENSION_MAXNAMELEN];
1989 	struct xt_match *match;
1990 	struct xt_target *wt;
1991 	void *dst = NULL;
1992 	int off, pad = 0;
1993 	unsigned int size_kern, match_size = mwt->match_size;
1994 
1995 	if (strscpy(name, mwt->u.name, sizeof(name)) < 0)
1996 		return -EINVAL;
1997 
1998 	if (state->buf_kern_start)
1999 		dst = state->buf_kern_start + state->buf_kern_offset;
2000 
2001 	switch (compat_mwt) {
2002 	case EBT_COMPAT_MATCH:
2003 		match = xt_request_find_match(NFPROTO_BRIDGE, name,
2004 					      mwt->u.revision);
2005 		if (IS_ERR(match))
2006 			return PTR_ERR(match);
2007 
2008 		if (!match_size_ok(match, match_size)) {
2009 			module_put(match->me);
2010 			return -EINVAL;
2011 		}
2012 
2013 		off = ebt_compat_match_offset(match, match_size);
2014 		if (dst) {
2015 			if (match->compat_from_user)
2016 				match->compat_from_user(dst, mwt->data);
2017 			else
2018 				memcpy(dst, mwt->data, match_size);
2019 		}
2020 
2021 		size_kern = match->matchsize;
2022 		if (unlikely(size_kern == -1))
2023 			size_kern = match_size;
2024 		module_put(match->me);
2025 		break;
2026 	case EBT_COMPAT_WATCHER:
2027 	case EBT_COMPAT_TARGET:
2028 		wt = xt_request_find_target(NFPROTO_BRIDGE, name,
2029 					    mwt->u.revision);
2030 		if (IS_ERR(wt))
2031 			return PTR_ERR(wt);
2032 
2033 		if (!tgt_size_ok(wt, match_size)) {
2034 			module_put(wt->me);
2035 			return -EINVAL;
2036 		}
2037 
2038 		off = xt_compat_target_offset(wt);
2039 
2040 		if (dst) {
2041 			if (wt->compat_from_user)
2042 				wt->compat_from_user(dst, mwt->data);
2043 			else
2044 				memcpy(dst, mwt->data, match_size);
2045 		}
2046 
2047 		size_kern = wt->targetsize;
2048 		module_put(wt->me);
2049 		break;
2050 
2051 	default:
2052 		return -EINVAL;
2053 	}
2054 
2055 	state->buf_kern_offset += match_size + off;
2056 	state->buf_user_offset += match_size;
2057 	pad = XT_ALIGN(size_kern) - size_kern;
2058 
2059 	if (pad > 0 && dst) {
2060 		if (WARN_ON(state->buf_kern_len <= pad))
2061 			return -EINVAL;
2062 		if (WARN_ON(state->buf_kern_offset - (match_size + off) + size_kern > state->buf_kern_len - pad))
2063 			return -EINVAL;
2064 		memset(dst + size_kern, 0, pad);
2065 	}
2066 	return off + match_size;
2067 }
2068 
2069 /* return size of all matches, watchers or target, including necessary
2070  * alignment and padding.
2071  */
ebt_size_mwt(const struct compat_ebt_entry_mwt * match32,unsigned int size_left,enum compat_mwt type,struct ebt_entries_buf_state * state,const void * base)2072 static int ebt_size_mwt(const struct compat_ebt_entry_mwt *match32,
2073 			unsigned int size_left, enum compat_mwt type,
2074 			struct ebt_entries_buf_state *state, const void *base)
2075 {
2076 	const char *buf = (const char *)match32;
2077 	int growth = 0;
2078 
2079 	if (size_left == 0)
2080 		return 0;
2081 
2082 	do {
2083 		struct ebt_entry_match *match_kern;
2084 		int ret;
2085 
2086 		if (size_left < sizeof(*match32))
2087 			return -EINVAL;
2088 
2089 		match_kern = (struct ebt_entry_match *) state->buf_kern_start;
2090 		if (match_kern) {
2091 			char *tmp;
2092 			tmp = state->buf_kern_start + state->buf_kern_offset;
2093 			match_kern = (struct ebt_entry_match *) tmp;
2094 		}
2095 		ret = ebt_buf_add(state, buf, sizeof(*match32));
2096 		if (ret < 0)
2097 			return ret;
2098 		size_left -= sizeof(*match32);
2099 
2100 		/* add padding before match->data (if any) */
2101 		ret = ebt_buf_add_pad(state, ebt_compat_entry_padsize());
2102 		if (ret < 0)
2103 			return ret;
2104 
2105 		if (match32->match_size > size_left)
2106 			return -EINVAL;
2107 
2108 		size_left -= match32->match_size;
2109 
2110 		ret = compat_mtw_from_user(match32, type, state, base);
2111 		if (ret < 0)
2112 			return ret;
2113 
2114 		if (WARN_ON(ret < match32->match_size))
2115 			return -EINVAL;
2116 		growth += ret - match32->match_size;
2117 		growth += ebt_compat_entry_padsize();
2118 
2119 		buf += sizeof(*match32);
2120 		buf += match32->match_size;
2121 
2122 		if (match_kern)
2123 			match_kern->match_size = ret;
2124 
2125 		match32 = (struct compat_ebt_entry_mwt *) buf;
2126 	} while (size_left);
2127 
2128 	return growth;
2129 }
2130 
2131 /* called for all ebt_entry structures. */
size_entry_mwt(const struct ebt_entry * entry,const unsigned char * base,unsigned int * total,struct ebt_entries_buf_state * state)2132 static int size_entry_mwt(const struct ebt_entry *entry, const unsigned char *base,
2133 			  unsigned int *total,
2134 			  struct ebt_entries_buf_state *state)
2135 {
2136 	unsigned int i, j, startoff, next_expected_off, new_offset = 0;
2137 	/* stores match/watchers/targets & offset of next struct ebt_entry: */
2138 	unsigned int offsets[4];
2139 	unsigned int *offsets_update = NULL;
2140 	int ret;
2141 	char *buf_start;
2142 
2143 	if (*total < sizeof(struct ebt_entries))
2144 		return -EINVAL;
2145 
2146 	if (!entry->bitmask) {
2147 		*total -= sizeof(struct ebt_entries);
2148 		return ebt_buf_add(state, entry, sizeof(struct ebt_entries));
2149 	}
2150 	if (*total < sizeof(*entry) || entry->next_offset < sizeof(*entry))
2151 		return -EINVAL;
2152 
2153 	startoff = state->buf_user_offset;
2154 	/* pull in most part of ebt_entry, it does not need to be changed. */
2155 	ret = ebt_buf_add(state, entry,
2156 			offsetof(struct ebt_entry, watchers_offset));
2157 	if (ret < 0)
2158 		return ret;
2159 
2160 	offsets[0] = sizeof(struct ebt_entry); /* matches come first */
2161 	memcpy(&offsets[1], &entry->offsets, sizeof(entry->offsets));
2162 
2163 	if (state->buf_kern_start) {
2164 		buf_start = state->buf_kern_start + state->buf_kern_offset;
2165 		offsets_update = (unsigned int *) buf_start;
2166 	}
2167 	ret = ebt_buf_add(state, &offsets[1],
2168 			sizeof(offsets) - sizeof(offsets[0]));
2169 	if (ret < 0)
2170 		return ret;
2171 	buf_start = (char *) entry;
2172 	/* 0: matches offset, always follows ebt_entry.
2173 	 * 1: watchers offset, from ebt_entry structure
2174 	 * 2: target offset, from ebt_entry structure
2175 	 * 3: next ebt_entry offset, from ebt_entry structure
2176 	 *
2177 	 * offsets are relative to beginning of struct ebt_entry (i.e., 0).
2178 	 */
2179 	for (i = 0; i < 4 ; ++i) {
2180 		if (offsets[i] > *total)
2181 			return -EINVAL;
2182 
2183 		if (i < 3 && offsets[i] == *total)
2184 			return -EINVAL;
2185 
2186 		if (i == 0)
2187 			continue;
2188 		if (offsets[i-1] > offsets[i])
2189 			return -EINVAL;
2190 	}
2191 
2192 	for (i = 0, j = 1 ; j < 4 ; j++, i++) {
2193 		struct compat_ebt_entry_mwt *match32;
2194 		unsigned int size;
2195 		char *buf = buf_start + offsets[i];
2196 
2197 		if (offsets[i] > offsets[j])
2198 			return -EINVAL;
2199 
2200 		match32 = (struct compat_ebt_entry_mwt *) buf;
2201 		size = offsets[j] - offsets[i];
2202 		ret = ebt_size_mwt(match32, size, i, state, base);
2203 		if (ret < 0)
2204 			return ret;
2205 		new_offset += ret;
2206 		if (offsets_update && new_offset) {
2207 			pr_debug("change offset %d to %d\n",
2208 				offsets_update[i], offsets[j] + new_offset);
2209 			offsets_update[i] = offsets[j] + new_offset;
2210 		}
2211 	}
2212 
2213 	if (state->buf_kern_start == NULL) {
2214 		unsigned int offset = buf_start - (char *) base;
2215 
2216 		ret = xt_compat_add_offset(NFPROTO_BRIDGE, offset, new_offset);
2217 		if (ret < 0)
2218 			return ret;
2219 	}
2220 
2221 	next_expected_off = state->buf_user_offset - startoff;
2222 	if (next_expected_off != entry->next_offset)
2223 		return -EINVAL;
2224 
2225 	if (*total < entry->next_offset)
2226 		return -EINVAL;
2227 	*total -= entry->next_offset;
2228 	return 0;
2229 }
2230 
2231 /* repl->entries_size is the size of the ebt_entry blob in userspace.
2232  * It might need more memory when copied to a 64 bit kernel in case
2233  * userspace is 32-bit. So, first task: find out how much memory is needed.
2234  *
2235  * Called before validation is performed.
2236  */
compat_copy_entries(unsigned char * data,unsigned int size_user,struct ebt_entries_buf_state * state)2237 static int compat_copy_entries(unsigned char *data, unsigned int size_user,
2238 				struct ebt_entries_buf_state *state)
2239 {
2240 	unsigned int size_remaining = size_user;
2241 	int ret;
2242 
2243 	ret = EBT_ENTRY_ITERATE(data, size_user, size_entry_mwt, data,
2244 					&size_remaining, state);
2245 	if (ret < 0)
2246 		return ret;
2247 
2248 	if (size_remaining)
2249 		return -EINVAL;
2250 
2251 	return state->buf_kern_offset;
2252 }
2253 
2254 
compat_copy_ebt_replace_from_user(struct ebt_replace * repl,sockptr_t arg,unsigned int len)2255 static int compat_copy_ebt_replace_from_user(struct ebt_replace *repl,
2256 					     sockptr_t arg, unsigned int len)
2257 {
2258 	struct compat_ebt_replace tmp;
2259 	int i;
2260 
2261 	if (len < sizeof(tmp))
2262 		return -EINVAL;
2263 
2264 	if (copy_from_sockptr(&tmp, arg, sizeof(tmp)))
2265 		return -EFAULT;
2266 
2267 	if (len != sizeof(tmp) + tmp.entries_size)
2268 		return -EINVAL;
2269 
2270 	if (tmp.entries_size == 0)
2271 		return -EINVAL;
2272 
2273 	if (tmp.nentries >= MAX_EBT_ENTRIES)
2274 		return -ENOMEM;
2275 	if (tmp.num_counters >= MAX_EBT_ENTRIES)
2276 		return -ENOMEM;
2277 
2278 	memcpy(repl, &tmp, offsetof(struct ebt_replace, hook_entry));
2279 
2280 	repl->name[sizeof(repl->name) - 1] = '\0';
2281 
2282 	/* starting with hook_entry, 32 vs. 64 bit structures are different */
2283 	for (i = 0; i < NF_BR_NUMHOOKS; i++)
2284 		repl->hook_entry[i] = compat_ptr(tmp.hook_entry[i]);
2285 
2286 	repl->num_counters = tmp.num_counters;
2287 	repl->counters = compat_ptr(tmp.counters);
2288 	repl->entries = compat_ptr(tmp.entries);
2289 	return 0;
2290 }
2291 
compat_do_replace(struct net * net,sockptr_t arg,unsigned int len)2292 static int compat_do_replace(struct net *net, sockptr_t arg, unsigned int len)
2293 {
2294 	int ret, i, countersize, size64;
2295 	struct ebt_table_info *newinfo;
2296 	struct ebt_replace tmp;
2297 	struct ebt_entries_buf_state state;
2298 	void *entries_tmp;
2299 
2300 	ret = compat_copy_ebt_replace_from_user(&tmp, arg, len);
2301 	if (ret) {
2302 		/* try real handler in case userland supplied needed padding */
2303 		if (ret == -EINVAL && do_replace(net, arg, len) == 0)
2304 			ret = 0;
2305 		return ret;
2306 	}
2307 
2308 	countersize = COUNTER_OFFSET(tmp.nentries) * nr_cpu_ids;
2309 	newinfo = vmalloc(sizeof(*newinfo) + countersize);
2310 	if (!newinfo)
2311 		return -ENOMEM;
2312 
2313 	if (countersize)
2314 		memset(newinfo->counters, 0, countersize);
2315 
2316 	memset(&state, 0, sizeof(state));
2317 
2318 	newinfo->entries = vmalloc(tmp.entries_size);
2319 	if (!newinfo->entries) {
2320 		ret = -ENOMEM;
2321 		goto free_newinfo;
2322 	}
2323 	if (copy_from_user(
2324 	   newinfo->entries, tmp.entries, tmp.entries_size) != 0) {
2325 		ret = -EFAULT;
2326 		goto free_entries;
2327 	}
2328 
2329 	entries_tmp = newinfo->entries;
2330 
2331 	xt_compat_lock(NFPROTO_BRIDGE);
2332 
2333 	ret = ebt_compat_init_offsets(tmp.nentries);
2334 	if (ret < 0)
2335 		goto out_unlock;
2336 
2337 	ret = compat_copy_entries(entries_tmp, tmp.entries_size, &state);
2338 	if (ret < 0)
2339 		goto out_unlock;
2340 
2341 	pr_debug("tmp.entries_size %d, kern off %d, user off %d delta %d\n",
2342 		tmp.entries_size, state.buf_kern_offset, state.buf_user_offset,
2343 		xt_compat_calc_jump(NFPROTO_BRIDGE, tmp.entries_size));
2344 
2345 	size64 = ret;
2346 	newinfo->entries = vmalloc(size64);
2347 	if (!newinfo->entries) {
2348 		vfree(entries_tmp);
2349 		ret = -ENOMEM;
2350 		goto out_unlock;
2351 	}
2352 
2353 	memset(&state, 0, sizeof(state));
2354 	state.buf_kern_start = newinfo->entries;
2355 	state.buf_kern_len = size64;
2356 
2357 	ret = compat_copy_entries(entries_tmp, tmp.entries_size, &state);
2358 	if (WARN_ON(ret < 0)) {
2359 		vfree(entries_tmp);
2360 		goto out_unlock;
2361 	}
2362 
2363 	vfree(entries_tmp);
2364 	tmp.entries_size = size64;
2365 
2366 	for (i = 0; i < NF_BR_NUMHOOKS; i++) {
2367 		char __user *usrptr;
2368 		if (tmp.hook_entry[i]) {
2369 			unsigned int delta;
2370 			usrptr = (char __user *) tmp.hook_entry[i];
2371 			delta = usrptr - tmp.entries;
2372 			usrptr += xt_compat_calc_jump(NFPROTO_BRIDGE, delta);
2373 			tmp.hook_entry[i] = (struct ebt_entries __user *)usrptr;
2374 		}
2375 	}
2376 
2377 	xt_compat_flush_offsets(NFPROTO_BRIDGE);
2378 	xt_compat_unlock(NFPROTO_BRIDGE);
2379 
2380 	ret = do_replace_finish(net, &tmp, newinfo);
2381 	if (ret == 0)
2382 		return ret;
2383 free_entries:
2384 	vfree(newinfo->entries);
2385 free_newinfo:
2386 	vfree(newinfo);
2387 	return ret;
2388 out_unlock:
2389 	xt_compat_flush_offsets(NFPROTO_BRIDGE);
2390 	xt_compat_unlock(NFPROTO_BRIDGE);
2391 	goto free_entries;
2392 }
2393 
compat_update_counters(struct net * net,sockptr_t arg,unsigned int len)2394 static int compat_update_counters(struct net *net, sockptr_t arg,
2395 				  unsigned int len)
2396 {
2397 	struct compat_ebt_replace hlp;
2398 
2399 	if (len < sizeof(hlp))
2400 		return -EINVAL;
2401 	if (copy_from_sockptr(&hlp, arg, sizeof(hlp)))
2402 		return -EFAULT;
2403 
2404 	hlp.name[sizeof(hlp.name) - 1] = '\0';
2405 
2406 	/* try real handler in case userland supplied needed padding */
2407 	if (len != sizeof(hlp) + hlp.num_counters * sizeof(struct ebt_counter))
2408 		return update_counters(net, arg, len);
2409 
2410 	return do_update_counters(net, hlp.name, compat_ptr(hlp.counters),
2411 				  hlp.num_counters, len);
2412 }
2413 
compat_do_ebt_get_ctl(struct sock * sk,int cmd,void __user * user,int * len)2414 static int compat_do_ebt_get_ctl(struct sock *sk, int cmd,
2415 		void __user *user, int *len)
2416 {
2417 	int ret;
2418 	struct compat_ebt_replace tmp;
2419 	struct ebt_table *t;
2420 	struct net *net = sock_net(sk);
2421 
2422 	if ((cmd == EBT_SO_GET_INFO || cmd == EBT_SO_GET_INIT_INFO) &&
2423 	    *len != sizeof(struct compat_ebt_replace))
2424 		return -EINVAL;
2425 
2426 	if (copy_from_user(&tmp, user, sizeof(tmp)))
2427 		return -EFAULT;
2428 
2429 	tmp.name[sizeof(tmp.name) - 1] = '\0';
2430 
2431 	t = find_table_lock(net, tmp.name, &ret, &ebt_mutex);
2432 	if (!t)
2433 		return ret;
2434 
2435 	xt_compat_lock(NFPROTO_BRIDGE);
2436 	switch (cmd) {
2437 	case EBT_SO_GET_INFO:
2438 		tmp.nentries = t->private->nentries;
2439 		ret = compat_table_info(t->private, &tmp);
2440 		if (ret)
2441 			goto out;
2442 		tmp.valid_hooks = t->valid_hooks;
2443 
2444 		if (copy_to_user(user, &tmp, *len) != 0) {
2445 			ret = -EFAULT;
2446 			break;
2447 		}
2448 		ret = 0;
2449 		break;
2450 	case EBT_SO_GET_INIT_INFO:
2451 		tmp.nentries = t->table->nentries;
2452 		tmp.entries_size = t->table->entries_size;
2453 		tmp.valid_hooks = t->table->valid_hooks;
2454 
2455 		if (copy_to_user(user, &tmp, *len) != 0) {
2456 			ret = -EFAULT;
2457 			break;
2458 		}
2459 		ret = 0;
2460 		break;
2461 	case EBT_SO_GET_ENTRIES:
2462 	case EBT_SO_GET_INIT_ENTRIES:
2463 		/* try real handler first in case of userland-side padding.
2464 		 * in case we are dealing with an 'ordinary' 32 bit binary
2465 		 * without 64bit compatibility padding, this will fail right
2466 		 * after copy_from_user when the *len argument is validated.
2467 		 *
2468 		 * the compat_ variant needs to do one pass over the kernel
2469 		 * data set to adjust for size differences before it the check.
2470 		 */
2471 		if (copy_everything_to_user(t, user, len, cmd) == 0)
2472 			ret = 0;
2473 		else
2474 			ret = compat_copy_everything_to_user(t, user, len, cmd);
2475 		break;
2476 	default:
2477 		ret = -EINVAL;
2478 	}
2479  out:
2480 	xt_compat_flush_offsets(NFPROTO_BRIDGE);
2481 	xt_compat_unlock(NFPROTO_BRIDGE);
2482 	mutex_unlock(&ebt_mutex);
2483 	return ret;
2484 }
2485 #endif
2486 
do_ebt_get_ctl(struct sock * sk,int cmd,void __user * user,int * len)2487 static int do_ebt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
2488 {
2489 	struct net *net = sock_net(sk);
2490 	struct ebt_replace tmp;
2491 	struct ebt_table *t;
2492 	int ret;
2493 
2494 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2495 		return -EPERM;
2496 	if (!xt_compat_check())
2497 		return -EPERM;
2498 
2499 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
2500 	/* try real handler in case userland supplied needed padding */
2501 	if (in_compat_syscall() &&
2502 	    ((cmd != EBT_SO_GET_INFO && cmd != EBT_SO_GET_INIT_INFO) ||
2503 	     *len != sizeof(tmp)))
2504 		return compat_do_ebt_get_ctl(sk, cmd, user, len);
2505 #endif
2506 
2507 	if (copy_from_user(&tmp, user, sizeof(tmp)))
2508 		return -EFAULT;
2509 
2510 	tmp.name[sizeof(tmp.name) - 1] = '\0';
2511 
2512 	t = find_table_lock(net, tmp.name, &ret, &ebt_mutex);
2513 	if (!t)
2514 		return ret;
2515 
2516 	switch (cmd) {
2517 	case EBT_SO_GET_INFO:
2518 	case EBT_SO_GET_INIT_INFO:
2519 		if (*len != sizeof(struct ebt_replace)) {
2520 			ret = -EINVAL;
2521 			mutex_unlock(&ebt_mutex);
2522 			break;
2523 		}
2524 		if (cmd == EBT_SO_GET_INFO) {
2525 			tmp.nentries = t->private->nentries;
2526 			tmp.entries_size = t->private->entries_size;
2527 			tmp.valid_hooks = t->valid_hooks;
2528 		} else {
2529 			tmp.nentries = t->table->nentries;
2530 			tmp.entries_size = t->table->entries_size;
2531 			tmp.valid_hooks = t->table->valid_hooks;
2532 		}
2533 		mutex_unlock(&ebt_mutex);
2534 		if (copy_to_user(user, &tmp, *len) != 0) {
2535 			ret = -EFAULT;
2536 			break;
2537 		}
2538 		ret = 0;
2539 		break;
2540 
2541 	case EBT_SO_GET_ENTRIES:
2542 	case EBT_SO_GET_INIT_ENTRIES:
2543 		ret = copy_everything_to_user(t, user, len, cmd);
2544 		mutex_unlock(&ebt_mutex);
2545 		break;
2546 
2547 	default:
2548 		mutex_unlock(&ebt_mutex);
2549 		ret = -EINVAL;
2550 	}
2551 
2552 	return ret;
2553 }
2554 
do_ebt_set_ctl(struct sock * sk,int cmd,sockptr_t arg,unsigned int len)2555 static int do_ebt_set_ctl(struct sock *sk, int cmd, sockptr_t arg,
2556 		unsigned int len)
2557 {
2558 	struct net *net = sock_net(sk);
2559 	int ret;
2560 
2561 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2562 		return -EPERM;
2563 	if (!xt_compat_check())
2564 		return -EPERM;
2565 
2566 	switch (cmd) {
2567 	case EBT_SO_SET_ENTRIES:
2568 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
2569 		if (in_compat_syscall())
2570 			ret = compat_do_replace(net, arg, len);
2571 		else
2572 #endif
2573 			ret = do_replace(net, arg, len);
2574 		break;
2575 	case EBT_SO_SET_COUNTERS:
2576 #ifdef CONFIG_NETFILTER_XTABLES_COMPAT
2577 		if (in_compat_syscall())
2578 			ret = compat_update_counters(net, arg, len);
2579 		else
2580 #endif
2581 			ret = update_counters(net, arg, len);
2582 		break;
2583 	default:
2584 		ret = -EINVAL;
2585 	}
2586 	return ret;
2587 }
2588 
2589 static struct nf_sockopt_ops ebt_sockopts = {
2590 	.pf		= PF_INET,
2591 	.set_optmin	= EBT_BASE_CTL,
2592 	.set_optmax	= EBT_SO_SET_MAX + 1,
2593 	.set		= do_ebt_set_ctl,
2594 	.get_optmin	= EBT_BASE_CTL,
2595 	.get_optmax	= EBT_SO_GET_MAX + 1,
2596 	.get		= do_ebt_get_ctl,
2597 	.owner		= THIS_MODULE,
2598 };
2599 
ebt_pernet_init(struct net * net)2600 static int __net_init ebt_pernet_init(struct net *net)
2601 {
2602 	struct ebt_pernet *ebt_net = net_generic(net, ebt_pernet_id);
2603 
2604 	INIT_LIST_HEAD(&ebt_net->tables);
2605 	INIT_LIST_HEAD(&ebt_net->dead_tables);
2606 	return 0;
2607 }
2608 
ebt_pernet_exit(struct net * net)2609 static void __net_exit ebt_pernet_exit(struct net *net)
2610 {
2611 	struct ebt_pernet *ebt_net = net_generic(net, ebt_pernet_id);
2612 
2613 	WARN_ON_ONCE(!list_empty(&ebt_net->tables));
2614 	WARN_ON_ONCE(!list_empty(&ebt_net->dead_tables));
2615 }
2616 
2617 static struct pernet_operations ebt_net_ops = {
2618 	.init = ebt_pernet_init,
2619 	.exit = ebt_pernet_exit,
2620 	.id   = &ebt_pernet_id,
2621 	.size = sizeof(struct ebt_pernet),
2622 };
2623 
ebtables_init(void)2624 static int __init ebtables_init(void)
2625 {
2626 	int ret;
2627 
2628 	ret = register_pernet_subsys(&ebt_net_ops);
2629 	if (ret < 0)
2630 		return ret;
2631 
2632 	ret = xt_register_target(&ebt_standard_target);
2633 	if (ret < 0) {
2634 		unregister_pernet_subsys(&ebt_net_ops);
2635 		return ret;
2636 	}
2637 
2638 	ret = nf_register_sockopt(&ebt_sockopts);
2639 	if (ret < 0) {
2640 		xt_unregister_target(&ebt_standard_target);
2641 		unregister_pernet_subsys(&ebt_net_ops);
2642 		return ret;
2643 	}
2644 
2645 	return 0;
2646 }
2647 
ebtables_fini(void)2648 static void ebtables_fini(void)
2649 {
2650 	nf_unregister_sockopt(&ebt_sockopts);
2651 	xt_unregister_target(&ebt_standard_target);
2652 	unregister_pernet_subsys(&ebt_net_ops);
2653 }
2654 
2655 EXPORT_SYMBOL(ebt_register_table);
2656 EXPORT_SYMBOL(ebt_unregister_table);
2657 EXPORT_SYMBOL(ebt_do_table);
2658 module_init(ebtables_init);
2659 module_exit(ebtables_fini);
2660 MODULE_LICENSE("GPL");
2661 MODULE_DESCRIPTION("ebtables legacy core");
2662