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