• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * x_tables core - Backend for {ip,ip6,arp}_tables
3  *
4  * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org>
5  * Copyright (C) 2006-2012 Patrick McHardy <kaber@trash.net>
6  *
7  * Based on existing ip_tables code which is
8  *   Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
9  *   Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  *
15  */
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/socket.h>
20 #include <linux/net.h>
21 #include <linux/proc_fs.h>
22 #include <linux/seq_file.h>
23 #include <linux/string.h>
24 #include <linux/vmalloc.h>
25 #include <linux/mutex.h>
26 #include <linux/mm.h>
27 #include <linux/slab.h>
28 #include <linux/audit.h>
29 #include <linux/user_namespace.h>
30 #include <net/net_namespace.h>
31 
32 #include <linux/netfilter/x_tables.h>
33 #include <linux/netfilter_arp.h>
34 #include <linux/netfilter_ipv4/ip_tables.h>
35 #include <linux/netfilter_ipv6/ip6_tables.h>
36 #include <linux/netfilter_arp/arp_tables.h>
37 
38 MODULE_LICENSE("GPL");
39 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
40 MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
41 
42 #define XT_PCPU_BLOCK_SIZE 4096
43 #define XT_MAX_TABLE_SIZE	(512 * 1024 * 1024)
44 
45 struct compat_delta {
46 	unsigned int offset; /* offset in kernel */
47 	int delta; /* delta in 32bit user land */
48 };
49 
50 struct xt_af {
51 	struct mutex mutex;
52 	struct list_head match;
53 	struct list_head target;
54 #ifdef CONFIG_COMPAT
55 	struct mutex compat_mutex;
56 	struct compat_delta *compat_tab;
57 	unsigned int number; /* number of slots in compat_tab[] */
58 	unsigned int cur; /* number of used slots in compat_tab[] */
59 #endif
60 };
61 
62 static struct xt_af *xt;
63 
64 static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
65 	[NFPROTO_UNSPEC] = "x",
66 	[NFPROTO_IPV4]   = "ip",
67 	[NFPROTO_ARP]    = "arp",
68 	[NFPROTO_BRIDGE] = "eb",
69 	[NFPROTO_IPV6]   = "ip6",
70 };
71 
72 /* Registration hooks for targets. */
xt_register_target(struct xt_target * target)73 int xt_register_target(struct xt_target *target)
74 {
75 	u_int8_t af = target->family;
76 
77 	mutex_lock(&xt[af].mutex);
78 	list_add(&target->list, &xt[af].target);
79 	mutex_unlock(&xt[af].mutex);
80 	return 0;
81 }
82 EXPORT_SYMBOL(xt_register_target);
83 
84 void
xt_unregister_target(struct xt_target * target)85 xt_unregister_target(struct xt_target *target)
86 {
87 	u_int8_t af = target->family;
88 
89 	mutex_lock(&xt[af].mutex);
90 	list_del(&target->list);
91 	mutex_unlock(&xt[af].mutex);
92 }
93 EXPORT_SYMBOL(xt_unregister_target);
94 
95 int
xt_register_targets(struct xt_target * target,unsigned int n)96 xt_register_targets(struct xt_target *target, unsigned int n)
97 {
98 	unsigned int i;
99 	int err = 0;
100 
101 	for (i = 0; i < n; i++) {
102 		err = xt_register_target(&target[i]);
103 		if (err)
104 			goto err;
105 	}
106 	return err;
107 
108 err:
109 	if (i > 0)
110 		xt_unregister_targets(target, i);
111 	return err;
112 }
113 EXPORT_SYMBOL(xt_register_targets);
114 
115 void
xt_unregister_targets(struct xt_target * target,unsigned int n)116 xt_unregister_targets(struct xt_target *target, unsigned int n)
117 {
118 	while (n-- > 0)
119 		xt_unregister_target(&target[n]);
120 }
121 EXPORT_SYMBOL(xt_unregister_targets);
122 
xt_register_match(struct xt_match * match)123 int xt_register_match(struct xt_match *match)
124 {
125 	u_int8_t af = match->family;
126 
127 	mutex_lock(&xt[af].mutex);
128 	list_add(&match->list, &xt[af].match);
129 	mutex_unlock(&xt[af].mutex);
130 	return 0;
131 }
132 EXPORT_SYMBOL(xt_register_match);
133 
134 void
xt_unregister_match(struct xt_match * match)135 xt_unregister_match(struct xt_match *match)
136 {
137 	u_int8_t af = match->family;
138 
139 	mutex_lock(&xt[af].mutex);
140 	list_del(&match->list);
141 	mutex_unlock(&xt[af].mutex);
142 }
143 EXPORT_SYMBOL(xt_unregister_match);
144 
145 int
xt_register_matches(struct xt_match * match,unsigned int n)146 xt_register_matches(struct xt_match *match, unsigned int n)
147 {
148 	unsigned int i;
149 	int err = 0;
150 
151 	for (i = 0; i < n; i++) {
152 		err = xt_register_match(&match[i]);
153 		if (err)
154 			goto err;
155 	}
156 	return err;
157 
158 err:
159 	if (i > 0)
160 		xt_unregister_matches(match, i);
161 	return err;
162 }
163 EXPORT_SYMBOL(xt_register_matches);
164 
165 void
xt_unregister_matches(struct xt_match * match,unsigned int n)166 xt_unregister_matches(struct xt_match *match, unsigned int n)
167 {
168 	while (n-- > 0)
169 		xt_unregister_match(&match[n]);
170 }
171 EXPORT_SYMBOL(xt_unregister_matches);
172 
173 
174 /*
175  * These are weird, but module loading must not be done with mutex
176  * held (since they will register), and we have to have a single
177  * function to use.
178  */
179 
180 /* Find match, grabs ref.  Returns ERR_PTR() on error. */
xt_find_match(u8 af,const char * name,u8 revision)181 struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
182 {
183 	struct xt_match *m;
184 	int err = -ENOENT;
185 
186 	mutex_lock(&xt[af].mutex);
187 	list_for_each_entry(m, &xt[af].match, list) {
188 		if (strcmp(m->name, name) == 0) {
189 			if (m->revision == revision) {
190 				if (try_module_get(m->me)) {
191 					mutex_unlock(&xt[af].mutex);
192 					return m;
193 				}
194 			} else
195 				err = -EPROTOTYPE; /* Found something. */
196 		}
197 	}
198 	mutex_unlock(&xt[af].mutex);
199 
200 	if (af != NFPROTO_UNSPEC)
201 		/* Try searching again in the family-independent list */
202 		return xt_find_match(NFPROTO_UNSPEC, name, revision);
203 
204 	return ERR_PTR(err);
205 }
206 EXPORT_SYMBOL(xt_find_match);
207 
208 struct xt_match *
xt_request_find_match(uint8_t nfproto,const char * name,uint8_t revision)209 xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
210 {
211 	struct xt_match *match;
212 
213 	if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
214 		return ERR_PTR(-EINVAL);
215 
216 	match = xt_find_match(nfproto, name, revision);
217 	if (IS_ERR(match)) {
218 		request_module("%st_%s", xt_prefix[nfproto], name);
219 		match = xt_find_match(nfproto, name, revision);
220 	}
221 
222 	return match;
223 }
224 EXPORT_SYMBOL_GPL(xt_request_find_match);
225 
226 /* Find target, grabs ref.  Returns ERR_PTR() on error. */
xt_find_target(u8 af,const char * name,u8 revision)227 struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
228 {
229 	struct xt_target *t;
230 	int err = -ENOENT;
231 
232 	mutex_lock(&xt[af].mutex);
233 	list_for_each_entry(t, &xt[af].target, list) {
234 		if (strcmp(t->name, name) == 0) {
235 			if (t->revision == revision) {
236 				if (try_module_get(t->me)) {
237 					mutex_unlock(&xt[af].mutex);
238 					return t;
239 				}
240 			} else
241 				err = -EPROTOTYPE; /* Found something. */
242 		}
243 	}
244 	mutex_unlock(&xt[af].mutex);
245 
246 	if (af != NFPROTO_UNSPEC)
247 		/* Try searching again in the family-independent list */
248 		return xt_find_target(NFPROTO_UNSPEC, name, revision);
249 
250 	return ERR_PTR(err);
251 }
252 EXPORT_SYMBOL(xt_find_target);
253 
xt_request_find_target(u8 af,const char * name,u8 revision)254 struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
255 {
256 	struct xt_target *target;
257 
258 	if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
259 		return ERR_PTR(-EINVAL);
260 
261 	target = xt_find_target(af, name, revision);
262 	if (IS_ERR(target)) {
263 		request_module("%st_%s", xt_prefix[af], name);
264 		target = xt_find_target(af, name, revision);
265 	}
266 
267 	return target;
268 }
269 EXPORT_SYMBOL_GPL(xt_request_find_target);
270 
271 
xt_obj_to_user(u16 __user * psize,u16 size,void __user * pname,const char * name,u8 __user * prev,u8 rev)272 static int xt_obj_to_user(u16 __user *psize, u16 size,
273 			  void __user *pname, const char *name,
274 			  u8 __user *prev, u8 rev)
275 {
276 	if (put_user(size, psize))
277 		return -EFAULT;
278 	if (copy_to_user(pname, name, strlen(name) + 1))
279 		return -EFAULT;
280 	if (put_user(rev, prev))
281 		return -EFAULT;
282 
283 	return 0;
284 }
285 
286 #define XT_OBJ_TO_USER(U, K, TYPE, C_SIZE)				\
287 	xt_obj_to_user(&U->u.TYPE##_size, C_SIZE ? : K->u.TYPE##_size,	\
288 		       U->u.user.name, K->u.kernel.TYPE->name,		\
289 		       &U->u.user.revision, K->u.kernel.TYPE->revision)
290 
xt_data_to_user(void __user * dst,const void * src,int usersize,int size,int aligned_size)291 int xt_data_to_user(void __user *dst, const void *src,
292 		    int usersize, int size, int aligned_size)
293 {
294 	usersize = usersize ? : size;
295 	if (copy_to_user(dst, src, usersize))
296 		return -EFAULT;
297 	if (usersize != aligned_size &&
298 	    clear_user(dst + usersize, aligned_size - usersize))
299 		return -EFAULT;
300 
301 	return 0;
302 }
303 EXPORT_SYMBOL_GPL(xt_data_to_user);
304 
305 #define XT_DATA_TO_USER(U, K, TYPE)					\
306 	xt_data_to_user(U->data, K->data,				\
307 			K->u.kernel.TYPE->usersize,			\
308 			K->u.kernel.TYPE->TYPE##size,			\
309 			XT_ALIGN(K->u.kernel.TYPE->TYPE##size))
310 
xt_match_to_user(const struct xt_entry_match * m,struct xt_entry_match __user * u)311 int xt_match_to_user(const struct xt_entry_match *m,
312 		     struct xt_entry_match __user *u)
313 {
314 	return XT_OBJ_TO_USER(u, m, match, 0) ||
315 	       XT_DATA_TO_USER(u, m, match);
316 }
317 EXPORT_SYMBOL_GPL(xt_match_to_user);
318 
xt_target_to_user(const struct xt_entry_target * t,struct xt_entry_target __user * u)319 int xt_target_to_user(const struct xt_entry_target *t,
320 		      struct xt_entry_target __user *u)
321 {
322 	return XT_OBJ_TO_USER(u, t, target, 0) ||
323 	       XT_DATA_TO_USER(u, t, target);
324 }
325 EXPORT_SYMBOL_GPL(xt_target_to_user);
326 
match_revfn(u8 af,const char * name,u8 revision,int * bestp)327 static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
328 {
329 	const struct xt_match *m;
330 	int have_rev = 0;
331 
332 	list_for_each_entry(m, &xt[af].match, list) {
333 		if (strcmp(m->name, name) == 0) {
334 			if (m->revision > *bestp)
335 				*bestp = m->revision;
336 			if (m->revision == revision)
337 				have_rev = 1;
338 		}
339 	}
340 
341 	if (af != NFPROTO_UNSPEC && !have_rev)
342 		return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
343 
344 	return have_rev;
345 }
346 
target_revfn(u8 af,const char * name,u8 revision,int * bestp)347 static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
348 {
349 	const struct xt_target *t;
350 	int have_rev = 0;
351 
352 	list_for_each_entry(t, &xt[af].target, list) {
353 		if (strcmp(t->name, name) == 0) {
354 			if (t->revision > *bestp)
355 				*bestp = t->revision;
356 			if (t->revision == revision)
357 				have_rev = 1;
358 		}
359 	}
360 
361 	if (af != NFPROTO_UNSPEC && !have_rev)
362 		return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
363 
364 	return have_rev;
365 }
366 
367 /* Returns true or false (if no such extension at all) */
xt_find_revision(u8 af,const char * name,u8 revision,int target,int * err)368 int xt_find_revision(u8 af, const char *name, u8 revision, int target,
369 		     int *err)
370 {
371 	int have_rev, best = -1;
372 
373 	mutex_lock(&xt[af].mutex);
374 	if (target == 1)
375 		have_rev = target_revfn(af, name, revision, &best);
376 	else
377 		have_rev = match_revfn(af, name, revision, &best);
378 	mutex_unlock(&xt[af].mutex);
379 
380 	/* Nothing at all?  Return 0 to try loading module. */
381 	if (best == -1) {
382 		*err = -ENOENT;
383 		return 0;
384 	}
385 
386 	*err = best;
387 	if (!have_rev)
388 		*err = -EPROTONOSUPPORT;
389 	return 1;
390 }
391 EXPORT_SYMBOL_GPL(xt_find_revision);
392 
393 static char *
textify_hooks(char * buf,size_t size,unsigned int mask,uint8_t nfproto)394 textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
395 {
396 	static const char *const inetbr_names[] = {
397 		"PREROUTING", "INPUT", "FORWARD",
398 		"OUTPUT", "POSTROUTING", "BROUTING",
399 	};
400 	static const char *const arp_names[] = {
401 		"INPUT", "FORWARD", "OUTPUT",
402 	};
403 	const char *const *names;
404 	unsigned int i, max;
405 	char *p = buf;
406 	bool np = false;
407 	int res;
408 
409 	names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
410 	max   = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
411 	                                   ARRAY_SIZE(inetbr_names);
412 	*p = '\0';
413 	for (i = 0; i < max; ++i) {
414 		if (!(mask & (1 << i)))
415 			continue;
416 		res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
417 		if (res > 0) {
418 			size -= res;
419 			p += res;
420 		}
421 		np = true;
422 	}
423 
424 	return buf;
425 }
426 
427 /**
428  * xt_check_proc_name - check that name is suitable for /proc file creation
429  *
430  * @name: file name candidate
431  * @size: length of buffer
432  *
433  * some x_tables modules wish to create a file in /proc.
434  * This function makes sure that the name is suitable for this
435  * purpose, it checks that name is NUL terminated and isn't a 'special'
436  * name, like "..".
437  *
438  * returns negative number on error or 0 if name is useable.
439  */
xt_check_proc_name(const char * name,unsigned int size)440 int xt_check_proc_name(const char *name, unsigned int size)
441 {
442 	if (name[0] == '\0')
443 		return -EINVAL;
444 
445 	if (strnlen(name, size) == size)
446 		return -ENAMETOOLONG;
447 
448 	if (strcmp(name, ".") == 0 ||
449 	    strcmp(name, "..") == 0 ||
450 	    strchr(name, '/'))
451 		return -EINVAL;
452 
453 	return 0;
454 }
455 EXPORT_SYMBOL(xt_check_proc_name);
456 
xt_check_match(struct xt_mtchk_param * par,unsigned int size,u_int8_t proto,bool inv_proto)457 int xt_check_match(struct xt_mtchk_param *par,
458 		   unsigned int size, u_int8_t proto, bool inv_proto)
459 {
460 	int ret;
461 
462 	if (XT_ALIGN(par->match->matchsize) != size &&
463 	    par->match->matchsize != -1) {
464 		/*
465 		 * ebt_among is exempt from centralized matchsize checking
466 		 * because it uses a dynamic-size data set.
467 		 */
468 		pr_err("%s_tables: %s.%u match: invalid size "
469 		       "%u (kernel) != (user) %u\n",
470 		       xt_prefix[par->family], par->match->name,
471 		       par->match->revision,
472 		       XT_ALIGN(par->match->matchsize), size);
473 		return -EINVAL;
474 	}
475 	if (par->match->table != NULL &&
476 	    strcmp(par->match->table, par->table) != 0) {
477 		pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
478 		       xt_prefix[par->family], par->match->name,
479 		       par->match->table, par->table);
480 		return -EINVAL;
481 	}
482 	if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
483 		char used[64], allow[64];
484 
485 		pr_err("%s_tables: %s match: used from hooks %s, but only "
486 		       "valid from %s\n",
487 		       xt_prefix[par->family], par->match->name,
488 		       textify_hooks(used, sizeof(used), par->hook_mask,
489 		                     par->family),
490 		       textify_hooks(allow, sizeof(allow), par->match->hooks,
491 		                     par->family));
492 		return -EINVAL;
493 	}
494 	if (par->match->proto && (par->match->proto != proto || inv_proto)) {
495 		pr_err("%s_tables: %s match: only valid for protocol %u\n",
496 		       xt_prefix[par->family], par->match->name,
497 		       par->match->proto);
498 		return -EINVAL;
499 	}
500 	if (par->match->checkentry != NULL) {
501 		ret = par->match->checkentry(par);
502 		if (ret < 0)
503 			return ret;
504 		else if (ret > 0)
505 			/* Flag up potential errors. */
506 			return -EIO;
507 	}
508 	return 0;
509 }
510 EXPORT_SYMBOL_GPL(xt_check_match);
511 
512 /** xt_check_entry_match - check that matches end before start of target
513  *
514  * @match: beginning of xt_entry_match
515  * @target: beginning of this rules target (alleged end of matches)
516  * @alignment: alignment requirement of match structures
517  *
518  * Validates that all matches add up to the beginning of the target,
519  * and that each match covers at least the base structure size.
520  *
521  * Return: 0 on success, negative errno on failure.
522  */
xt_check_entry_match(const char * match,const char * target,const size_t alignment)523 static int xt_check_entry_match(const char *match, const char *target,
524 				const size_t alignment)
525 {
526 	const struct xt_entry_match *pos;
527 	int length = target - match;
528 
529 	if (length == 0) /* no matches */
530 		return 0;
531 
532 	pos = (struct xt_entry_match *)match;
533 	do {
534 		if ((unsigned long)pos % alignment)
535 			return -EINVAL;
536 
537 		if (length < (int)sizeof(struct xt_entry_match))
538 			return -EINVAL;
539 
540 		if (pos->u.match_size < sizeof(struct xt_entry_match))
541 			return -EINVAL;
542 
543 		if (pos->u.match_size > length)
544 			return -EINVAL;
545 
546 		length -= pos->u.match_size;
547 		pos = ((void *)((char *)(pos) + (pos)->u.match_size));
548 	} while (length > 0);
549 
550 	return 0;
551 }
552 
553 #ifdef CONFIG_COMPAT
xt_compat_add_offset(u_int8_t af,unsigned int offset,int delta)554 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
555 {
556 	struct xt_af *xp = &xt[af];
557 
558 	if (WARN_ON(!xp->compat_tab))
559 		return -ENOMEM;
560 
561 	if (xp->cur >= xp->number)
562 		return -EINVAL;
563 
564 	if (xp->cur)
565 		delta += xp->compat_tab[xp->cur - 1].delta;
566 	xp->compat_tab[xp->cur].offset = offset;
567 	xp->compat_tab[xp->cur].delta = delta;
568 	xp->cur++;
569 	return 0;
570 }
571 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
572 
xt_compat_flush_offsets(u_int8_t af)573 void xt_compat_flush_offsets(u_int8_t af)
574 {
575 	if (xt[af].compat_tab) {
576 		vfree(xt[af].compat_tab);
577 		xt[af].compat_tab = NULL;
578 		xt[af].number = 0;
579 		xt[af].cur = 0;
580 	}
581 }
582 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
583 
xt_compat_calc_jump(u_int8_t af,unsigned int offset)584 int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
585 {
586 	struct compat_delta *tmp = xt[af].compat_tab;
587 	int mid, left = 0, right = xt[af].cur - 1;
588 
589 	while (left <= right) {
590 		mid = (left + right) >> 1;
591 		if (offset > tmp[mid].offset)
592 			left = mid + 1;
593 		else if (offset < tmp[mid].offset)
594 			right = mid - 1;
595 		else
596 			return mid ? tmp[mid - 1].delta : 0;
597 	}
598 	return left ? tmp[left - 1].delta : 0;
599 }
600 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
601 
xt_compat_init_offsets(u8 af,unsigned int number)602 int xt_compat_init_offsets(u8 af, unsigned int number)
603 {
604 	size_t mem;
605 
606 	if (!number || number > (INT_MAX / sizeof(struct compat_delta)))
607 		return -EINVAL;
608 
609 	if (WARN_ON(xt[af].compat_tab))
610 		return -EINVAL;
611 
612 	mem = sizeof(struct compat_delta) * number;
613 	if (mem > XT_MAX_TABLE_SIZE)
614 		return -ENOMEM;
615 
616 	xt[af].compat_tab = vmalloc(mem);
617 	if (!xt[af].compat_tab)
618 		return -ENOMEM;
619 
620 	xt[af].number = number;
621 	xt[af].cur = 0;
622 
623 	return 0;
624 }
625 EXPORT_SYMBOL(xt_compat_init_offsets);
626 
xt_compat_match_offset(const struct xt_match * match)627 int xt_compat_match_offset(const struct xt_match *match)
628 {
629 	u_int16_t csize = match->compatsize ? : match->matchsize;
630 	return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
631 }
632 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
633 
xt_compat_match_from_user(struct xt_entry_match * m,void ** dstptr,unsigned int * size)634 void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
635 			       unsigned int *size)
636 {
637 	const struct xt_match *match = m->u.kernel.match;
638 	struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
639 	int pad, off = xt_compat_match_offset(match);
640 	u_int16_t msize = cm->u.user.match_size;
641 	char name[sizeof(m->u.user.name)];
642 
643 	m = *dstptr;
644 	memcpy(m, cm, sizeof(*cm));
645 	if (match->compat_from_user)
646 		match->compat_from_user(m->data, cm->data);
647 	else
648 		memcpy(m->data, cm->data, msize - sizeof(*cm));
649 	pad = XT_ALIGN(match->matchsize) - match->matchsize;
650 	if (pad > 0)
651 		memset(m->data + match->matchsize, 0, pad);
652 
653 	msize += off;
654 	m->u.user.match_size = msize;
655 	strlcpy(name, match->name, sizeof(name));
656 	module_put(match->me);
657 	strncpy(m->u.user.name, name, sizeof(m->u.user.name));
658 
659 	*size += off;
660 	*dstptr += msize;
661 }
662 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
663 
664 #define COMPAT_XT_DATA_TO_USER(U, K, TYPE, C_SIZE)			\
665 	xt_data_to_user(U->data, K->data,				\
666 			K->u.kernel.TYPE->usersize,			\
667 			C_SIZE,						\
668 			COMPAT_XT_ALIGN(C_SIZE))
669 
xt_compat_match_to_user(const struct xt_entry_match * m,void __user ** dstptr,unsigned int * size)670 int xt_compat_match_to_user(const struct xt_entry_match *m,
671 			    void __user **dstptr, unsigned int *size)
672 {
673 	const struct xt_match *match = m->u.kernel.match;
674 	struct compat_xt_entry_match __user *cm = *dstptr;
675 	int off = xt_compat_match_offset(match);
676 	u_int16_t msize = m->u.user.match_size - off;
677 
678 	if (XT_OBJ_TO_USER(cm, m, match, msize))
679 		return -EFAULT;
680 
681 	if (match->compat_to_user) {
682 		if (match->compat_to_user((void __user *)cm->data, m->data))
683 			return -EFAULT;
684 	} else {
685 		if (COMPAT_XT_DATA_TO_USER(cm, m, match, msize - sizeof(*cm)))
686 			return -EFAULT;
687 	}
688 
689 	*size -= off;
690 	*dstptr += msize;
691 	return 0;
692 }
693 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
694 
695 /* non-compat version may have padding after verdict */
696 struct compat_xt_standard_target {
697 	struct compat_xt_entry_target t;
698 	compat_uint_t verdict;
699 };
700 
xt_compat_check_entry_offsets(const void * base,const char * elems,unsigned int target_offset,unsigned int next_offset)701 int xt_compat_check_entry_offsets(const void *base, const char *elems,
702 				  unsigned int target_offset,
703 				  unsigned int next_offset)
704 {
705 	long size_of_base_struct = elems - (const char *)base;
706 	const struct compat_xt_entry_target *t;
707 	const char *e = base;
708 
709 	if (target_offset < size_of_base_struct)
710 		return -EINVAL;
711 
712 	if (target_offset + sizeof(*t) > next_offset)
713 		return -EINVAL;
714 
715 	t = (void *)(e + target_offset);
716 	if (t->u.target_size < sizeof(*t))
717 		return -EINVAL;
718 
719 	if (target_offset + t->u.target_size > next_offset)
720 		return -EINVAL;
721 
722 	if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
723 	    COMPAT_XT_ALIGN(target_offset + sizeof(struct compat_xt_standard_target)) != next_offset)
724 		return -EINVAL;
725 
726 	/* compat_xt_entry match has less strict alignment requirements,
727 	 * otherwise they are identical.  In case of padding differences
728 	 * we need to add compat version of xt_check_entry_match.
729 	 */
730 	BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
731 
732 	return xt_check_entry_match(elems, base + target_offset,
733 				    __alignof__(struct compat_xt_entry_match));
734 }
735 EXPORT_SYMBOL(xt_compat_check_entry_offsets);
736 #endif /* CONFIG_COMPAT */
737 
738 /**
739  * xt_check_entry_offsets - validate arp/ip/ip6t_entry
740  *
741  * @base: pointer to arp/ip/ip6t_entry
742  * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
743  * @target_offset: the arp/ip/ip6_t->target_offset
744  * @next_offset: the arp/ip/ip6_t->next_offset
745  *
746  * validates that target_offset and next_offset are sane and that all
747  * match sizes (if any) align with the target offset.
748  *
749  * This function does not validate the targets or matches themselves, it
750  * only tests that all the offsets and sizes are correct, that all
751  * match structures are aligned, and that the last structure ends where
752  * the target structure begins.
753  *
754  * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
755  *
756  * The arp/ip/ip6t_entry structure @base must have passed following tests:
757  * - it must point to a valid memory location
758  * - base to base + next_offset must be accessible, i.e. not exceed allocated
759  *   length.
760  *
761  * A well-formed entry looks like this:
762  *
763  * ip(6)t_entry   match [mtdata]  match [mtdata] target [tgdata] ip(6)t_entry
764  * e->elems[]-----'                              |               |
765  *                matchsize                      |               |
766  *                                matchsize      |               |
767  *                                               |               |
768  * target_offset---------------------------------'               |
769  * next_offset---------------------------------------------------'
770  *
771  * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
772  *          This is where matches (if any) and the target reside.
773  * target_offset: beginning of target.
774  * next_offset: start of the next rule; also: size of this rule.
775  * Since targets have a minimum size, target_offset + minlen <= next_offset.
776  *
777  * Every match stores its size, sum of sizes must not exceed target_offset.
778  *
779  * Return: 0 on success, negative errno on failure.
780  */
xt_check_entry_offsets(const void * base,const char * elems,unsigned int target_offset,unsigned int next_offset)781 int xt_check_entry_offsets(const void *base,
782 			   const char *elems,
783 			   unsigned int target_offset,
784 			   unsigned int next_offset)
785 {
786 	long size_of_base_struct = elems - (const char *)base;
787 	const struct xt_entry_target *t;
788 	const char *e = base;
789 
790 	/* target start is within the ip/ip6/arpt_entry struct */
791 	if (target_offset < size_of_base_struct)
792 		return -EINVAL;
793 
794 	if (target_offset + sizeof(*t) > next_offset)
795 		return -EINVAL;
796 
797 	t = (void *)(e + target_offset);
798 	if (t->u.target_size < sizeof(*t))
799 		return -EINVAL;
800 
801 	if (target_offset + t->u.target_size > next_offset)
802 		return -EINVAL;
803 
804 	if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
805 	    XT_ALIGN(target_offset + sizeof(struct xt_standard_target)) != next_offset)
806 		return -EINVAL;
807 
808 	return xt_check_entry_match(elems, base + target_offset,
809 				    __alignof__(struct xt_entry_match));
810 }
811 EXPORT_SYMBOL(xt_check_entry_offsets);
812 
813 /**
814  * xt_alloc_entry_offsets - allocate array to store rule head offsets
815  *
816  * @size: number of entries
817  *
818  * Return: NULL or kmalloc'd or vmalloc'd array
819  */
xt_alloc_entry_offsets(unsigned int size)820 unsigned int *xt_alloc_entry_offsets(unsigned int size)
821 {
822 	if (size > XT_MAX_TABLE_SIZE / sizeof(unsigned int))
823 		return NULL;
824 
825 	return kvmalloc_array(size, sizeof(unsigned int), GFP_KERNEL | __GFP_ZERO);
826 
827 }
828 EXPORT_SYMBOL(xt_alloc_entry_offsets);
829 
830 /**
831  * xt_find_jump_offset - check if target is a valid jump offset
832  *
833  * @offsets: array containing all valid rule start offsets of a rule blob
834  * @target: the jump target to search for
835  * @size: entries in @offset
836  */
xt_find_jump_offset(const unsigned int * offsets,unsigned int target,unsigned int size)837 bool xt_find_jump_offset(const unsigned int *offsets,
838 			 unsigned int target, unsigned int size)
839 {
840 	int m, low = 0, hi = size;
841 
842 	while (hi > low) {
843 		m = (low + hi) / 2u;
844 
845 		if (offsets[m] > target)
846 			hi = m;
847 		else if (offsets[m] < target)
848 			low = m + 1;
849 		else
850 			return true;
851 	}
852 
853 	return false;
854 }
855 EXPORT_SYMBOL(xt_find_jump_offset);
856 
xt_check_target(struct xt_tgchk_param * par,unsigned int size,u_int8_t proto,bool inv_proto)857 int xt_check_target(struct xt_tgchk_param *par,
858 		    unsigned int size, u_int8_t proto, bool inv_proto)
859 {
860 	int ret;
861 
862 	if (XT_ALIGN(par->target->targetsize) != size) {
863 		pr_err("%s_tables: %s.%u target: invalid size "
864 		       "%u (kernel) != (user) %u\n",
865 		       xt_prefix[par->family], par->target->name,
866 		       par->target->revision,
867 		       XT_ALIGN(par->target->targetsize), size);
868 		return -EINVAL;
869 	}
870 	if (par->target->table != NULL &&
871 	    strcmp(par->target->table, par->table) != 0) {
872 		pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
873 		       xt_prefix[par->family], par->target->name,
874 		       par->target->table, par->table);
875 		return -EINVAL;
876 	}
877 	if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
878 		char used[64], allow[64];
879 
880 		pr_err("%s_tables: %s target: used from hooks %s, but only "
881 		       "usable from %s\n",
882 		       xt_prefix[par->family], par->target->name,
883 		       textify_hooks(used, sizeof(used), par->hook_mask,
884 		                     par->family),
885 		       textify_hooks(allow, sizeof(allow), par->target->hooks,
886 		                     par->family));
887 		return -EINVAL;
888 	}
889 	if (par->target->proto && (par->target->proto != proto || inv_proto)) {
890 		pr_err("%s_tables: %s target: only valid for protocol %u\n",
891 		       xt_prefix[par->family], par->target->name,
892 		       par->target->proto);
893 		return -EINVAL;
894 	}
895 	if (par->target->checkentry != NULL) {
896 		ret = par->target->checkentry(par);
897 		if (ret < 0)
898 			return ret;
899 		else if (ret > 0)
900 			/* Flag up potential errors. */
901 			return -EIO;
902 	}
903 	return 0;
904 }
905 EXPORT_SYMBOL_GPL(xt_check_target);
906 
907 /**
908  * xt_copy_counters_from_user - copy counters and metadata from userspace
909  *
910  * @user: src pointer to userspace memory
911  * @len: alleged size of userspace memory
912  * @info: where to store the xt_counters_info metadata
913  * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel
914  *
915  * Copies counter meta data from @user and stores it in @info.
916  *
917  * vmallocs memory to hold the counters, then copies the counter data
918  * from @user to the new memory and returns a pointer to it.
919  *
920  * If @compat is true, @info gets converted automatically to the 64bit
921  * representation.
922  *
923  * The metadata associated with the counters is stored in @info.
924  *
925  * Return: returns pointer that caller has to test via IS_ERR().
926  * If IS_ERR is false, caller has to vfree the pointer.
927  */
xt_copy_counters_from_user(const void __user * user,unsigned int len,struct xt_counters_info * info,bool compat)928 void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
929 				 struct xt_counters_info *info, bool compat)
930 {
931 	void *mem;
932 	u64 size;
933 
934 #ifdef CONFIG_COMPAT
935 	if (compat) {
936 		/* structures only differ in size due to alignment */
937 		struct compat_xt_counters_info compat_tmp;
938 
939 		if (len <= sizeof(compat_tmp))
940 			return ERR_PTR(-EINVAL);
941 
942 		len -= sizeof(compat_tmp);
943 		if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0)
944 			return ERR_PTR(-EFAULT);
945 
946 		memcpy(info->name, compat_tmp.name, sizeof(info->name) - 1);
947 		info->num_counters = compat_tmp.num_counters;
948 		user += sizeof(compat_tmp);
949 	} else
950 #endif
951 	{
952 		if (len <= sizeof(*info))
953 			return ERR_PTR(-EINVAL);
954 
955 		len -= sizeof(*info);
956 		if (copy_from_user(info, user, sizeof(*info)) != 0)
957 			return ERR_PTR(-EFAULT);
958 
959 		user += sizeof(*info);
960 	}
961 	info->name[sizeof(info->name) - 1] = '\0';
962 
963 	size = sizeof(struct xt_counters);
964 	size *= info->num_counters;
965 
966 	if (size != (u64)len)
967 		return ERR_PTR(-EINVAL);
968 
969 	mem = vmalloc(len);
970 	if (!mem)
971 		return ERR_PTR(-ENOMEM);
972 
973 	if (copy_from_user(mem, user, len) == 0)
974 		return mem;
975 
976 	vfree(mem);
977 	return ERR_PTR(-EFAULT);
978 }
979 EXPORT_SYMBOL_GPL(xt_copy_counters_from_user);
980 
981 #ifdef CONFIG_COMPAT
xt_compat_target_offset(const struct xt_target * target)982 int xt_compat_target_offset(const struct xt_target *target)
983 {
984 	u_int16_t csize = target->compatsize ? : target->targetsize;
985 	return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
986 }
987 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
988 
xt_compat_target_from_user(struct xt_entry_target * t,void ** dstptr,unsigned int * size)989 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
990 				unsigned int *size)
991 {
992 	const struct xt_target *target = t->u.kernel.target;
993 	struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
994 	int pad, off = xt_compat_target_offset(target);
995 	u_int16_t tsize = ct->u.user.target_size;
996 	char name[sizeof(t->u.user.name)];
997 
998 	t = *dstptr;
999 	memcpy(t, ct, sizeof(*ct));
1000 	if (target->compat_from_user)
1001 		target->compat_from_user(t->data, ct->data);
1002 	else
1003 		memcpy(t->data, ct->data, tsize - sizeof(*ct));
1004 	pad = XT_ALIGN(target->targetsize) - target->targetsize;
1005 	if (pad > 0)
1006 		memset(t->data + target->targetsize, 0, pad);
1007 
1008 	tsize += off;
1009 	t->u.user.target_size = tsize;
1010 	strlcpy(name, target->name, sizeof(name));
1011 	module_put(target->me);
1012 	strncpy(t->u.user.name, name, sizeof(t->u.user.name));
1013 
1014 	*size += off;
1015 	*dstptr += tsize;
1016 }
1017 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
1018 
xt_compat_target_to_user(const struct xt_entry_target * t,void __user ** dstptr,unsigned int * size)1019 int xt_compat_target_to_user(const struct xt_entry_target *t,
1020 			     void __user **dstptr, unsigned int *size)
1021 {
1022 	const struct xt_target *target = t->u.kernel.target;
1023 	struct compat_xt_entry_target __user *ct = *dstptr;
1024 	int off = xt_compat_target_offset(target);
1025 	u_int16_t tsize = t->u.user.target_size - off;
1026 
1027 	if (XT_OBJ_TO_USER(ct, t, target, tsize))
1028 		return -EFAULT;
1029 
1030 	if (target->compat_to_user) {
1031 		if (target->compat_to_user((void __user *)ct->data, t->data))
1032 			return -EFAULT;
1033 	} else {
1034 		if (COMPAT_XT_DATA_TO_USER(ct, t, target, tsize - sizeof(*ct)))
1035 			return -EFAULT;
1036 	}
1037 
1038 	*size -= off;
1039 	*dstptr += tsize;
1040 	return 0;
1041 }
1042 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
1043 #endif
1044 
xt_alloc_table_info(unsigned int size)1045 struct xt_table_info *xt_alloc_table_info(unsigned int size)
1046 {
1047 	struct xt_table_info *info = NULL;
1048 	size_t sz = sizeof(*info) + size;
1049 
1050 	if (sz < sizeof(*info) || sz >= XT_MAX_TABLE_SIZE)
1051 		return NULL;
1052 
1053 	/* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
1054 	if ((size >> PAGE_SHIFT) + 2 > totalram_pages)
1055 		return NULL;
1056 
1057 	/* __GFP_NORETRY is not fully supported by kvmalloc but it should
1058 	 * work reasonably well if sz is too large and bail out rather
1059 	 * than shoot all processes down before realizing there is nothing
1060 	 * more to reclaim.
1061 	 */
1062 	info = kvmalloc(sz, GFP_KERNEL | __GFP_NORETRY);
1063 	if (!info)
1064 		return NULL;
1065 
1066 	memset(info, 0, sizeof(*info));
1067 	info->size = size;
1068 	return info;
1069 }
1070 EXPORT_SYMBOL(xt_alloc_table_info);
1071 
xt_free_table_info(struct xt_table_info * info)1072 void xt_free_table_info(struct xt_table_info *info)
1073 {
1074 	int cpu;
1075 
1076 	if (info->jumpstack != NULL) {
1077 		for_each_possible_cpu(cpu)
1078 			kvfree(info->jumpstack[cpu]);
1079 		kvfree(info->jumpstack);
1080 	}
1081 
1082 	kvfree(info);
1083 }
1084 EXPORT_SYMBOL(xt_free_table_info);
1085 
1086 /* Find table by name, grabs mutex & ref.  Returns NULL on error. */
xt_find_table_lock(struct net * net,u_int8_t af,const char * name)1087 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
1088 				    const char *name)
1089 {
1090 	struct xt_table *t, *found = NULL;
1091 
1092 	mutex_lock(&xt[af].mutex);
1093 	list_for_each_entry(t, &net->xt.tables[af], list)
1094 		if (strcmp(t->name, name) == 0 && try_module_get(t->me))
1095 			return t;
1096 
1097 	if (net == &init_net)
1098 		goto out;
1099 
1100 	/* Table doesn't exist in this netns, re-try init */
1101 	list_for_each_entry(t, &init_net.xt.tables[af], list) {
1102 		if (strcmp(t->name, name))
1103 			continue;
1104 		if (!try_module_get(t->me)) {
1105 			mutex_unlock(&xt[af].mutex);
1106 			return NULL;
1107 		}
1108 
1109 		mutex_unlock(&xt[af].mutex);
1110 		if (t->table_init(net) != 0) {
1111 			module_put(t->me);
1112 			return NULL;
1113 		}
1114 
1115 		found = t;
1116 
1117 		mutex_lock(&xt[af].mutex);
1118 		break;
1119 	}
1120 
1121 	if (!found)
1122 		goto out;
1123 
1124 	/* and once again: */
1125 	list_for_each_entry(t, &net->xt.tables[af], list)
1126 		if (strcmp(t->name, name) == 0)
1127 			return t;
1128 
1129 	module_put(found->me);
1130  out:
1131 	mutex_unlock(&xt[af].mutex);
1132 	return NULL;
1133 }
1134 EXPORT_SYMBOL_GPL(xt_find_table_lock);
1135 
xt_table_unlock(struct xt_table * table)1136 void xt_table_unlock(struct xt_table *table)
1137 {
1138 	mutex_unlock(&xt[table->af].mutex);
1139 }
1140 EXPORT_SYMBOL_GPL(xt_table_unlock);
1141 
1142 #ifdef CONFIG_COMPAT
xt_compat_lock(u_int8_t af)1143 void xt_compat_lock(u_int8_t af)
1144 {
1145 	mutex_lock(&xt[af].compat_mutex);
1146 }
1147 EXPORT_SYMBOL_GPL(xt_compat_lock);
1148 
xt_compat_unlock(u_int8_t af)1149 void xt_compat_unlock(u_int8_t af)
1150 {
1151 	mutex_unlock(&xt[af].compat_mutex);
1152 }
1153 EXPORT_SYMBOL_GPL(xt_compat_unlock);
1154 #endif
1155 
1156 DEFINE_PER_CPU(seqcount_t, xt_recseq);
1157 EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
1158 
1159 struct static_key xt_tee_enabled __read_mostly;
1160 EXPORT_SYMBOL_GPL(xt_tee_enabled);
1161 
xt_jumpstack_alloc(struct xt_table_info * i)1162 static int xt_jumpstack_alloc(struct xt_table_info *i)
1163 {
1164 	unsigned int size;
1165 	int cpu;
1166 
1167 	size = sizeof(void **) * nr_cpu_ids;
1168 	if (size > PAGE_SIZE)
1169 		i->jumpstack = kvzalloc(size, GFP_KERNEL);
1170 	else
1171 		i->jumpstack = kzalloc(size, GFP_KERNEL);
1172 	if (i->jumpstack == NULL)
1173 		return -ENOMEM;
1174 
1175 	/* ruleset without jumps -- no stack needed */
1176 	if (i->stacksize == 0)
1177 		return 0;
1178 
1179 	/* Jumpstack needs to be able to record two full callchains, one
1180 	 * from the first rule set traversal, plus one table reentrancy
1181 	 * via -j TEE without clobbering the callchain that brought us to
1182 	 * TEE target.
1183 	 *
1184 	 * This is done by allocating two jumpstacks per cpu, on reentry
1185 	 * the upper half of the stack is used.
1186 	 *
1187 	 * see the jumpstack setup in ipt_do_table() for more details.
1188 	 */
1189 	size = sizeof(void *) * i->stacksize * 2u;
1190 	for_each_possible_cpu(cpu) {
1191 		i->jumpstack[cpu] = kvmalloc_node(size, GFP_KERNEL,
1192 			cpu_to_node(cpu));
1193 		if (i->jumpstack[cpu] == NULL)
1194 			/*
1195 			 * Freeing will be done later on by the callers. The
1196 			 * chain is: xt_replace_table -> __do_replace ->
1197 			 * do_replace -> xt_free_table_info.
1198 			 */
1199 			return -ENOMEM;
1200 	}
1201 
1202 	return 0;
1203 }
1204 
xt_counters_alloc(unsigned int counters)1205 struct xt_counters *xt_counters_alloc(unsigned int counters)
1206 {
1207 	struct xt_counters *mem;
1208 
1209 	if (counters == 0 || counters > INT_MAX / sizeof(*mem))
1210 		return NULL;
1211 
1212 	counters *= sizeof(*mem);
1213 	if (counters > XT_MAX_TABLE_SIZE)
1214 		return NULL;
1215 
1216 	return vzalloc(counters);
1217 }
1218 EXPORT_SYMBOL(xt_counters_alloc);
1219 
1220 struct xt_table_info *
xt_replace_table(struct xt_table * table,unsigned int num_counters,struct xt_table_info * newinfo,int * error)1221 xt_replace_table(struct xt_table *table,
1222 	      unsigned int num_counters,
1223 	      struct xt_table_info *newinfo,
1224 	      int *error)
1225 {
1226 	struct xt_table_info *private;
1227 	int ret;
1228 
1229 	ret = xt_jumpstack_alloc(newinfo);
1230 	if (ret < 0) {
1231 		*error = ret;
1232 		return NULL;
1233 	}
1234 
1235 	/* Do the substitution. */
1236 	local_bh_disable();
1237 	private = table->private;
1238 
1239 	/* Check inside lock: is the old number correct? */
1240 	if (num_counters != private->number) {
1241 		pr_debug("num_counters != table->private->number (%u/%u)\n",
1242 			 num_counters, private->number);
1243 		local_bh_enable();
1244 		*error = -EAGAIN;
1245 		return NULL;
1246 	}
1247 
1248 	newinfo->initial_entries = private->initial_entries;
1249 	/*
1250 	 * Ensure contents of newinfo are visible before assigning to
1251 	 * private.
1252 	 */
1253 	smp_wmb();
1254 	table->private = newinfo;
1255 
1256 	/*
1257 	 * Even though table entries have now been swapped, other CPU's
1258 	 * may still be using the old entries. This is okay, because
1259 	 * resynchronization happens because of the locking done
1260 	 * during the get_counters() routine.
1261 	 */
1262 	local_bh_enable();
1263 
1264 #ifdef CONFIG_AUDIT
1265 	if (audit_enabled) {
1266 		audit_log(current->audit_context, GFP_KERNEL,
1267 			  AUDIT_NETFILTER_CFG,
1268 			  "table=%s family=%u entries=%u",
1269 			  table->name, table->af, private->number);
1270 	}
1271 #endif
1272 
1273 	return private;
1274 }
1275 EXPORT_SYMBOL_GPL(xt_replace_table);
1276 
xt_register_table(struct net * net,const struct xt_table * input_table,struct xt_table_info * bootstrap,struct xt_table_info * newinfo)1277 struct xt_table *xt_register_table(struct net *net,
1278 				   const struct xt_table *input_table,
1279 				   struct xt_table_info *bootstrap,
1280 				   struct xt_table_info *newinfo)
1281 {
1282 	int ret;
1283 	struct xt_table_info *private;
1284 	struct xt_table *t, *table;
1285 
1286 	/* Don't add one object to multiple lists. */
1287 	table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
1288 	if (!table) {
1289 		ret = -ENOMEM;
1290 		goto out;
1291 	}
1292 
1293 	mutex_lock(&xt[table->af].mutex);
1294 	/* Don't autoload: we'd eat our tail... */
1295 	list_for_each_entry(t, &net->xt.tables[table->af], list) {
1296 		if (strcmp(t->name, table->name) == 0) {
1297 			ret = -EEXIST;
1298 			goto unlock;
1299 		}
1300 	}
1301 
1302 	/* Simplifies replace_table code. */
1303 	table->private = bootstrap;
1304 
1305 	if (!xt_replace_table(table, 0, newinfo, &ret))
1306 		goto unlock;
1307 
1308 	private = table->private;
1309 	pr_debug("table->private->number = %u\n", private->number);
1310 
1311 	/* save number of initial entries */
1312 	private->initial_entries = private->number;
1313 
1314 	list_add(&table->list, &net->xt.tables[table->af]);
1315 	mutex_unlock(&xt[table->af].mutex);
1316 	return table;
1317 
1318 unlock:
1319 	mutex_unlock(&xt[table->af].mutex);
1320 	kfree(table);
1321 out:
1322 	return ERR_PTR(ret);
1323 }
1324 EXPORT_SYMBOL_GPL(xt_register_table);
1325 
xt_unregister_table(struct xt_table * table)1326 void *xt_unregister_table(struct xt_table *table)
1327 {
1328 	struct xt_table_info *private;
1329 
1330 	mutex_lock(&xt[table->af].mutex);
1331 	private = table->private;
1332 	list_del(&table->list);
1333 	mutex_unlock(&xt[table->af].mutex);
1334 	kfree(table);
1335 
1336 	return private;
1337 }
1338 EXPORT_SYMBOL_GPL(xt_unregister_table);
1339 
1340 #ifdef CONFIG_PROC_FS
1341 struct xt_names_priv {
1342 	struct seq_net_private p;
1343 	u_int8_t af;
1344 };
xt_table_seq_start(struct seq_file * seq,loff_t * pos)1345 static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
1346 {
1347 	struct xt_names_priv *priv = seq->private;
1348 	struct net *net = seq_file_net(seq);
1349 	u_int8_t af = priv->af;
1350 
1351 	mutex_lock(&xt[af].mutex);
1352 	return seq_list_start(&net->xt.tables[af], *pos);
1353 }
1354 
xt_table_seq_next(struct seq_file * seq,void * v,loff_t * pos)1355 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1356 {
1357 	struct xt_names_priv *priv = seq->private;
1358 	struct net *net = seq_file_net(seq);
1359 	u_int8_t af = priv->af;
1360 
1361 	return seq_list_next(v, &net->xt.tables[af], pos);
1362 }
1363 
xt_table_seq_stop(struct seq_file * seq,void * v)1364 static void xt_table_seq_stop(struct seq_file *seq, void *v)
1365 {
1366 	struct xt_names_priv *priv = seq->private;
1367 	u_int8_t af = priv->af;
1368 
1369 	mutex_unlock(&xt[af].mutex);
1370 }
1371 
xt_table_seq_show(struct seq_file * seq,void * v)1372 static int xt_table_seq_show(struct seq_file *seq, void *v)
1373 {
1374 	struct xt_table *table = list_entry(v, struct xt_table, list);
1375 
1376 	if (*table->name)
1377 		seq_printf(seq, "%s\n", table->name);
1378 	return 0;
1379 }
1380 
1381 static const struct seq_operations xt_table_seq_ops = {
1382 	.start	= xt_table_seq_start,
1383 	.next	= xt_table_seq_next,
1384 	.stop	= xt_table_seq_stop,
1385 	.show	= xt_table_seq_show,
1386 };
1387 
xt_table_open(struct inode * inode,struct file * file)1388 static int xt_table_open(struct inode *inode, struct file *file)
1389 {
1390 	int ret;
1391 	struct xt_names_priv *priv;
1392 
1393 	ret = seq_open_net(inode, file, &xt_table_seq_ops,
1394 			   sizeof(struct xt_names_priv));
1395 	if (!ret) {
1396 		priv = ((struct seq_file *)file->private_data)->private;
1397 		priv->af = (unsigned long)PDE_DATA(inode);
1398 	}
1399 	return ret;
1400 }
1401 
1402 static const struct file_operations xt_table_ops = {
1403 	.owner	 = THIS_MODULE,
1404 	.open	 = xt_table_open,
1405 	.read	 = seq_read,
1406 	.llseek	 = seq_lseek,
1407 	.release = seq_release_net,
1408 };
1409 
1410 /*
1411  * Traverse state for ip{,6}_{tables,matches} for helping crossing
1412  * the multi-AF mutexes.
1413  */
1414 struct nf_mttg_trav {
1415 	struct list_head *head, *curr;
1416 	uint8_t class, nfproto;
1417 };
1418 
1419 enum {
1420 	MTTG_TRAV_INIT,
1421 	MTTG_TRAV_NFP_UNSPEC,
1422 	MTTG_TRAV_NFP_SPEC,
1423 	MTTG_TRAV_DONE,
1424 };
1425 
xt_mttg_seq_next(struct seq_file * seq,void * v,loff_t * ppos,bool is_target)1426 static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1427     bool is_target)
1428 {
1429 	static const uint8_t next_class[] = {
1430 		[MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1431 		[MTTG_TRAV_NFP_SPEC]   = MTTG_TRAV_DONE,
1432 	};
1433 	struct nf_mttg_trav *trav = seq->private;
1434 
1435 	switch (trav->class) {
1436 	case MTTG_TRAV_INIT:
1437 		trav->class = MTTG_TRAV_NFP_UNSPEC;
1438 		mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1439 		trav->head = trav->curr = is_target ?
1440 			&xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1441  		break;
1442 	case MTTG_TRAV_NFP_UNSPEC:
1443 		trav->curr = trav->curr->next;
1444 		if (trav->curr != trav->head)
1445 			break;
1446 		mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1447 		mutex_lock(&xt[trav->nfproto].mutex);
1448 		trav->head = trav->curr = is_target ?
1449 			&xt[trav->nfproto].target : &xt[trav->nfproto].match;
1450 		trav->class = next_class[trav->class];
1451 		break;
1452 	case MTTG_TRAV_NFP_SPEC:
1453 		trav->curr = trav->curr->next;
1454 		if (trav->curr != trav->head)
1455 			break;
1456 		/* fallthru, _stop will unlock */
1457 	default:
1458 		return NULL;
1459 	}
1460 
1461 	if (ppos != NULL)
1462 		++*ppos;
1463 	return trav;
1464 }
1465 
xt_mttg_seq_start(struct seq_file * seq,loff_t * pos,bool is_target)1466 static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1467     bool is_target)
1468 {
1469 	struct nf_mttg_trav *trav = seq->private;
1470 	unsigned int j;
1471 
1472 	trav->class = MTTG_TRAV_INIT;
1473 	for (j = 0; j < *pos; ++j)
1474 		if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1475 			return NULL;
1476 	return trav;
1477 }
1478 
xt_mttg_seq_stop(struct seq_file * seq,void * v)1479 static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1480 {
1481 	struct nf_mttg_trav *trav = seq->private;
1482 
1483 	switch (trav->class) {
1484 	case MTTG_TRAV_NFP_UNSPEC:
1485 		mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1486 		break;
1487 	case MTTG_TRAV_NFP_SPEC:
1488 		mutex_unlock(&xt[trav->nfproto].mutex);
1489 		break;
1490 	}
1491 }
1492 
xt_match_seq_start(struct seq_file * seq,loff_t * pos)1493 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1494 {
1495 	return xt_mttg_seq_start(seq, pos, false);
1496 }
1497 
xt_match_seq_next(struct seq_file * seq,void * v,loff_t * ppos)1498 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1499 {
1500 	return xt_mttg_seq_next(seq, v, ppos, false);
1501 }
1502 
xt_match_seq_show(struct seq_file * seq,void * v)1503 static int xt_match_seq_show(struct seq_file *seq, void *v)
1504 {
1505 	const struct nf_mttg_trav *trav = seq->private;
1506 	const struct xt_match *match;
1507 
1508 	switch (trav->class) {
1509 	case MTTG_TRAV_NFP_UNSPEC:
1510 	case MTTG_TRAV_NFP_SPEC:
1511 		if (trav->curr == trav->head)
1512 			return 0;
1513 		match = list_entry(trav->curr, struct xt_match, list);
1514 		if (*match->name)
1515 			seq_printf(seq, "%s\n", match->name);
1516 	}
1517 	return 0;
1518 }
1519 
1520 static const struct seq_operations xt_match_seq_ops = {
1521 	.start	= xt_match_seq_start,
1522 	.next	= xt_match_seq_next,
1523 	.stop	= xt_mttg_seq_stop,
1524 	.show	= xt_match_seq_show,
1525 };
1526 
xt_match_open(struct inode * inode,struct file * file)1527 static int xt_match_open(struct inode *inode, struct file *file)
1528 {
1529 	struct nf_mttg_trav *trav;
1530 	trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
1531 	if (!trav)
1532 		return -ENOMEM;
1533 
1534 	trav->nfproto = (unsigned long)PDE_DATA(inode);
1535 	return 0;
1536 }
1537 
1538 static const struct file_operations xt_match_ops = {
1539 	.owner	 = THIS_MODULE,
1540 	.open	 = xt_match_open,
1541 	.read	 = seq_read,
1542 	.llseek	 = seq_lseek,
1543 	.release = seq_release_private,
1544 };
1545 
xt_target_seq_start(struct seq_file * seq,loff_t * pos)1546 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1547 {
1548 	return xt_mttg_seq_start(seq, pos, true);
1549 }
1550 
xt_target_seq_next(struct seq_file * seq,void * v,loff_t * ppos)1551 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
1552 {
1553 	return xt_mttg_seq_next(seq, v, ppos, true);
1554 }
1555 
xt_target_seq_show(struct seq_file * seq,void * v)1556 static int xt_target_seq_show(struct seq_file *seq, void *v)
1557 {
1558 	const struct nf_mttg_trav *trav = seq->private;
1559 	const struct xt_target *target;
1560 
1561 	switch (trav->class) {
1562 	case MTTG_TRAV_NFP_UNSPEC:
1563 	case MTTG_TRAV_NFP_SPEC:
1564 		if (trav->curr == trav->head)
1565 			return 0;
1566 		target = list_entry(trav->curr, struct xt_target, list);
1567 		if (*target->name)
1568 			seq_printf(seq, "%s\n", target->name);
1569 	}
1570 	return 0;
1571 }
1572 
1573 static const struct seq_operations xt_target_seq_ops = {
1574 	.start	= xt_target_seq_start,
1575 	.next	= xt_target_seq_next,
1576 	.stop	= xt_mttg_seq_stop,
1577 	.show	= xt_target_seq_show,
1578 };
1579 
xt_target_open(struct inode * inode,struct file * file)1580 static int xt_target_open(struct inode *inode, struct file *file)
1581 {
1582 	struct nf_mttg_trav *trav;
1583 	trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1584 	if (!trav)
1585 		return -ENOMEM;
1586 
1587 	trav->nfproto = (unsigned long)PDE_DATA(inode);
1588 	return 0;
1589 }
1590 
1591 static const struct file_operations xt_target_ops = {
1592 	.owner	 = THIS_MODULE,
1593 	.open	 = xt_target_open,
1594 	.read	 = seq_read,
1595 	.llseek	 = seq_lseek,
1596 	.release = seq_release_private,
1597 };
1598 
1599 #define FORMAT_TABLES	"_tables_names"
1600 #define	FORMAT_MATCHES	"_tables_matches"
1601 #define FORMAT_TARGETS 	"_tables_targets"
1602 
1603 #endif /* CONFIG_PROC_FS */
1604 
1605 /**
1606  * xt_hook_ops_alloc - set up hooks for a new table
1607  * @table:	table with metadata needed to set up hooks
1608  * @fn:		Hook function
1609  *
1610  * This function will create the nf_hook_ops that the x_table needs
1611  * to hand to xt_hook_link_net().
1612  */
1613 struct nf_hook_ops *
xt_hook_ops_alloc(const struct xt_table * table,nf_hookfn * fn)1614 xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
1615 {
1616 	unsigned int hook_mask = table->valid_hooks;
1617 	uint8_t i, num_hooks = hweight32(hook_mask);
1618 	uint8_t hooknum;
1619 	struct nf_hook_ops *ops;
1620 
1621 	if (!num_hooks)
1622 		return ERR_PTR(-EINVAL);
1623 
1624 	ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
1625 	if (ops == NULL)
1626 		return ERR_PTR(-ENOMEM);
1627 
1628 	for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1629 	     hook_mask >>= 1, ++hooknum) {
1630 		if (!(hook_mask & 1))
1631 			continue;
1632 		ops[i].hook     = fn;
1633 		ops[i].pf       = table->af;
1634 		ops[i].hooknum  = hooknum;
1635 		ops[i].priority = table->priority;
1636 		++i;
1637 	}
1638 
1639 	return ops;
1640 }
1641 EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
1642 
xt_proto_init(struct net * net,u_int8_t af)1643 int xt_proto_init(struct net *net, u_int8_t af)
1644 {
1645 #ifdef CONFIG_PROC_FS
1646 	char buf[XT_FUNCTION_MAXNAMELEN];
1647 	struct proc_dir_entry *proc;
1648 	kuid_t root_uid;
1649 	kgid_t root_gid;
1650 #endif
1651 
1652 	if (af >= ARRAY_SIZE(xt_prefix))
1653 		return -EINVAL;
1654 
1655 
1656 #ifdef CONFIG_PROC_FS
1657 	root_uid = make_kuid(net->user_ns, 0);
1658 	root_gid = make_kgid(net->user_ns, 0);
1659 
1660 	strlcpy(buf, xt_prefix[af], sizeof(buf));
1661 	strlcat(buf, FORMAT_TABLES, sizeof(buf));
1662 	proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1663 				(void *)(unsigned long)af);
1664 	if (!proc)
1665 		goto out;
1666 	if (uid_valid(root_uid) && gid_valid(root_gid))
1667 		proc_set_user(proc, root_uid, root_gid);
1668 
1669 	strlcpy(buf, xt_prefix[af], sizeof(buf));
1670 	strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1671 	proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1672 				(void *)(unsigned long)af);
1673 	if (!proc)
1674 		goto out_remove_tables;
1675 	if (uid_valid(root_uid) && gid_valid(root_gid))
1676 		proc_set_user(proc, root_uid, root_gid);
1677 
1678 	strlcpy(buf, xt_prefix[af], sizeof(buf));
1679 	strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1680 	proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1681 				(void *)(unsigned long)af);
1682 	if (!proc)
1683 		goto out_remove_matches;
1684 	if (uid_valid(root_uid) && gid_valid(root_gid))
1685 		proc_set_user(proc, root_uid, root_gid);
1686 #endif
1687 
1688 	return 0;
1689 
1690 #ifdef CONFIG_PROC_FS
1691 out_remove_matches:
1692 	strlcpy(buf, xt_prefix[af], sizeof(buf));
1693 	strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1694 	remove_proc_entry(buf, net->proc_net);
1695 
1696 out_remove_tables:
1697 	strlcpy(buf, xt_prefix[af], sizeof(buf));
1698 	strlcat(buf, FORMAT_TABLES, sizeof(buf));
1699 	remove_proc_entry(buf, net->proc_net);
1700 out:
1701 	return -1;
1702 #endif
1703 }
1704 EXPORT_SYMBOL_GPL(xt_proto_init);
1705 
xt_proto_fini(struct net * net,u_int8_t af)1706 void xt_proto_fini(struct net *net, u_int8_t af)
1707 {
1708 #ifdef CONFIG_PROC_FS
1709 	char buf[XT_FUNCTION_MAXNAMELEN];
1710 
1711 	strlcpy(buf, xt_prefix[af], sizeof(buf));
1712 	strlcat(buf, FORMAT_TABLES, sizeof(buf));
1713 	remove_proc_entry(buf, net->proc_net);
1714 
1715 	strlcpy(buf, xt_prefix[af], sizeof(buf));
1716 	strlcat(buf, FORMAT_TARGETS, sizeof(buf));
1717 	remove_proc_entry(buf, net->proc_net);
1718 
1719 	strlcpy(buf, xt_prefix[af], sizeof(buf));
1720 	strlcat(buf, FORMAT_MATCHES, sizeof(buf));
1721 	remove_proc_entry(buf, net->proc_net);
1722 #endif /*CONFIG_PROC_FS*/
1723 }
1724 EXPORT_SYMBOL_GPL(xt_proto_fini);
1725 
1726 /**
1727  * xt_percpu_counter_alloc - allocate x_tables rule counter
1728  *
1729  * @state: pointer to xt_percpu allocation state
1730  * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1731  *
1732  * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1733  * contain the address of the real (percpu) counter.
1734  *
1735  * Rule evaluation needs to use xt_get_this_cpu_counter() helper
1736  * to fetch the real percpu counter.
1737  *
1738  * To speed up allocation and improve data locality, a 4kb block is
1739  * allocated.
1740  *
1741  * xt_percpu_counter_alloc_state contains the base address of the
1742  * allocated page and the current sub-offset.
1743  *
1744  * returns false on error.
1745  */
xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state * state,struct xt_counters * counter)1746 bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
1747 			     struct xt_counters *counter)
1748 {
1749 	BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2));
1750 
1751 	if (nr_cpu_ids <= 1)
1752 		return true;
1753 
1754 	if (!state->mem) {
1755 		state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE,
1756 					    XT_PCPU_BLOCK_SIZE);
1757 		if (!state->mem)
1758 			return false;
1759 	}
1760 	counter->pcnt = (__force unsigned long)(state->mem + state->off);
1761 	state->off += sizeof(*counter);
1762 	if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) {
1763 		state->mem = NULL;
1764 		state->off = 0;
1765 	}
1766 	return true;
1767 }
1768 EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
1769 
xt_percpu_counter_free(struct xt_counters * counters)1770 void xt_percpu_counter_free(struct xt_counters *counters)
1771 {
1772 	unsigned long pcnt = counters->pcnt;
1773 
1774 	if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0)
1775 		free_percpu((void __percpu *)pcnt);
1776 }
1777 EXPORT_SYMBOL_GPL(xt_percpu_counter_free);
1778 
xt_net_init(struct net * net)1779 static int __net_init xt_net_init(struct net *net)
1780 {
1781 	int i;
1782 
1783 	for (i = 0; i < NFPROTO_NUMPROTO; i++)
1784 		INIT_LIST_HEAD(&net->xt.tables[i]);
1785 	return 0;
1786 }
1787 
1788 static struct pernet_operations xt_net_ops = {
1789 	.init = xt_net_init,
1790 };
1791 
xt_init(void)1792 static int __init xt_init(void)
1793 {
1794 	unsigned int i;
1795 	int rv;
1796 
1797 	for_each_possible_cpu(i) {
1798 		seqcount_init(&per_cpu(xt_recseq, i));
1799 	}
1800 
1801 	xt = kcalloc(NFPROTO_NUMPROTO, sizeof(struct xt_af), GFP_KERNEL);
1802 	if (!xt)
1803 		return -ENOMEM;
1804 
1805 	for (i = 0; i < NFPROTO_NUMPROTO; i++) {
1806 		mutex_init(&xt[i].mutex);
1807 #ifdef CONFIG_COMPAT
1808 		mutex_init(&xt[i].compat_mutex);
1809 		xt[i].compat_tab = NULL;
1810 #endif
1811 		INIT_LIST_HEAD(&xt[i].target);
1812 		INIT_LIST_HEAD(&xt[i].match);
1813 	}
1814 	rv = register_pernet_subsys(&xt_net_ops);
1815 	if (rv < 0)
1816 		kfree(xt);
1817 	return rv;
1818 }
1819 
xt_fini(void)1820 static void __exit xt_fini(void)
1821 {
1822 	unregister_pernet_subsys(&xt_net_ops);
1823 	kfree(xt);
1824 }
1825 
1826 module_init(xt_init);
1827 module_exit(xt_fini);
1828 
1829