PageRenderTime 62ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/net/bridge/netfilter/ebtables.c

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