2019-05-27 00:55:01 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-16 16:20:36 -06:00
|
|
|
/*
|
|
|
|
* ebtables
|
|
|
|
*
|
|
|
|
* Author:
|
|
|
|
* Bart De Schuymer <bdschuym@pandora.be>
|
|
|
|
*
|
|
|
|
* ebtables.c,v 2.0, July, 2002
|
|
|
|
*
|
2015-03-03 03:58:14 -07:00
|
|
|
* This code is strongly inspired by the iptables code which is
|
2005-04-16 16:20:36 -06:00
|
|
|
* Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
|
|
|
|
*/
|
2010-03-19 14:08:16 -06:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <linux/kmod.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/vmalloc.h>
|
2008-10-08 03:35:13 -06:00
|
|
|
#include <linux/netfilter/x_tables.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <linux/netfilter_bridge/ebtables.h>
|
|
|
|
#include <linux/spinlock.h>
|
2006-09-20 12:57:53 -06:00
|
|
|
#include <linux/mutex.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 02:04:11 -06:00
|
|
|
#include <linux/slab.h>
|
2016-12-24 12:46:01 -07:00
|
|
|
#include <linux/uaccess.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <linux/smp.h>
|
2005-10-13 15:41:23 -06:00
|
|
|
#include <linux/cpumask.h>
|
2014-09-08 06:11:45 -06:00
|
|
|
#include <linux/audit.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <net/sock.h>
|
|
|
|
/* needed for logical [in,out]-dev filtering */
|
|
|
|
#include "../br_private.h"
|
|
|
|
|
2015-10-26 03:10:41 -06:00
|
|
|
/* Each cpu has its own set of counters, so there is no need for write_lock in
|
2005-04-16 16:20:36 -06:00
|
|
|
* the softirq
|
|
|
|
* For reading or updating the counters, the user context needs to
|
|
|
|
* get a write_lock
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* The size of each set of counters is altered to get cache alignment */
|
|
|
|
#define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
|
|
|
|
#define COUNTER_OFFSET(n) (SMP_ALIGN(n * sizeof(struct ebt_counter)))
|
|
|
|
#define COUNTER_BASE(c, n, cpu) ((struct ebt_counter *)(((char *)c) + \
|
2015-10-26 03:10:40 -06:00
|
|
|
COUNTER_OFFSET(n) * cpu))
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
2006-03-20 23:35:41 -07:00
|
|
|
static DEFINE_MUTEX(ebt_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2010-01-05 08:09:46 -07:00
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
static void ebt_standard_compat_from_user(void *dst, const void *src)
|
|
|
|
{
|
|
|
|
int v = *(compat_int_t *)src;
|
|
|
|
|
|
|
|
if (v >= 0)
|
|
|
|
v += xt_compat_calc_jump(NFPROTO_BRIDGE, v);
|
|
|
|
memcpy(dst, &v, sizeof(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ebt_standard_compat_to_user(void __user *dst, const void *src)
|
|
|
|
{
|
|
|
|
compat_int_t cv = *(int *)src;
|
|
|
|
|
|
|
|
if (cv >= 0)
|
|
|
|
cv -= xt_compat_calc_jump(NFPROTO_BRIDGE, cv);
|
|
|
|
return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-10-08 03:35:15 -06:00
|
|
|
static struct xt_target ebt_standard_target = {
|
2008-10-08 03:35:14 -06:00
|
|
|
.name = "standard",
|
|
|
|
.revision = 0,
|
|
|
|
.family = NFPROTO_BRIDGE,
|
2008-10-08 03:35:15 -06:00
|
|
|
.targetsize = sizeof(int),
|
2010-01-05 08:09:46 -07:00
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
.compatsize = sizeof(compat_int_t),
|
|
|
|
.compat_from_user = ebt_standard_compat_from_user,
|
|
|
|
.compat_to_user = ebt_standard_compat_to_user,
|
|
|
|
#endif
|
2008-10-08 03:35:13 -06:00
|
|
|
};
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2008-10-08 03:35:19 -06:00
|
|
|
static inline int
|
|
|
|
ebt_do_watcher(const struct ebt_entry_watcher *w, struct sk_buff *skb,
|
2009-07-05 10:26:37 -06:00
|
|
|
struct xt_action_param *par)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-10-08 03:35:19 -06:00
|
|
|
par->target = w->u.watcher;
|
|
|
|
par->targinfo = w->data;
|
|
|
|
w->u.watcher->target(skb, par);
|
2005-04-16 16:20:36 -06:00
|
|
|
/* watchers don't give a verdict */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-05 10:26:37 -06:00
|
|
|
static inline int
|
|
|
|
ebt_do_match(struct ebt_entry_match *m, const struct sk_buff *skb,
|
|
|
|
struct xt_action_param *par)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-10-08 03:35:18 -06:00
|
|
|
par->match = m->u.match;
|
|
|
|
par->matchinfo = m->data;
|
2018-04-08 09:00:57 -06:00
|
|
|
return !m->u.match->match(skb, par);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2009-06-25 23:51:59 -06:00
|
|
|
static inline int
|
|
|
|
ebt_dev_check(const char *entry, const struct net_device *device)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
int i = 0;
|
2009-01-09 03:22:22 -07:00
|
|
|
const char *devname;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
if (*entry == '\0')
|
|
|
|
return 0;
|
|
|
|
if (!device)
|
|
|
|
return 1;
|
2009-01-09 03:22:22 -07:00
|
|
|
devname = device->name;
|
2005-04-16 16:20:36 -06:00
|
|
|
/* 1 is the wildcard token */
|
|
|
|
while (entry[i] != '\0' && entry[i] != 1 && entry[i] == devname[i])
|
|
|
|
i++;
|
2013-12-18 22:28:11 -07:00
|
|
|
return devname[i] != entry[i] && entry[i] != 1;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* process standard matches */
|
2009-06-25 23:51:59 -06:00
|
|
|
static inline int
|
2010-10-20 07:56:01 -06:00
|
|
|
ebt_basic_match(const struct ebt_entry *e, const struct sk_buff *skb,
|
2015-10-26 03:10:40 -06:00
|
|
|
const struct net_device *in, const struct net_device *out)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2010-10-20 07:56:01 -06:00
|
|
|
const struct ethhdr *h = eth_hdr(skb);
|
2010-11-14 23:38:13 -07:00
|
|
|
const struct net_bridge_port *p;
|
2010-10-20 07:56:01 -06:00
|
|
|
__be16 ethproto;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2015-01-13 09:13:44 -07:00
|
|
|
if (skb_vlan_tag_present(skb))
|
2010-10-20 07:56:01 -06:00
|
|
|
ethproto = htons(ETH_P_8021Q);
|
|
|
|
else
|
|
|
|
ethproto = h->h_proto;
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
if (e->bitmask & EBT_802_3) {
|
2016-06-24 14:25:22 -06:00
|
|
|
if (NF_INVF(e, EBT_IPROTO, eth_proto_is_802_3(ethproto)))
|
2005-04-16 16:20:36 -06:00
|
|
|
return 1;
|
|
|
|
} else if (!(e->bitmask & EBT_NOPROTO) &&
|
2016-06-24 14:25:22 -06:00
|
|
|
NF_INVF(e, EBT_IPROTO, e->ethproto != ethproto))
|
2005-04-16 16:20:36 -06:00
|
|
|
return 1;
|
|
|
|
|
2016-06-24 14:25:22 -06:00
|
|
|
if (NF_INVF(e, EBT_IIN, ebt_dev_check(e->in, in)))
|
2005-04-16 16:20:36 -06:00
|
|
|
return 1;
|
2016-06-24 14:25:22 -06:00
|
|
|
if (NF_INVF(e, EBT_IOUT, ebt_dev_check(e->out, out)))
|
2005-04-16 16:20:36 -06:00
|
|
|
return 1;
|
2016-09-21 09:35:04 -06:00
|
|
|
/* rcu_read_lock()ed by nf_hook_thresh */
|
2010-11-14 23:38:13 -07:00
|
|
|
if (in && (p = br_port_get_rcu(in)) != NULL &&
|
2016-06-24 14:25:22 -06:00
|
|
|
NF_INVF(e, EBT_ILOGICALIN,
|
|
|
|
ebt_dev_check(e->logical_in, p->br->dev)))
|
2005-04-16 16:20:36 -06:00
|
|
|
return 1;
|
2010-11-14 23:38:13 -07:00
|
|
|
if (out && (p = br_port_get_rcu(out)) != NULL &&
|
2016-06-24 14:25:22 -06:00
|
|
|
NF_INVF(e, EBT_ILOGICALOUT,
|
|
|
|
ebt_dev_check(e->logical_out, p->br->dev)))
|
2005-04-16 16:20:36 -06:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (e->bitmask & EBT_SOURCEMAC) {
|
2016-06-24 14:25:22 -06:00
|
|
|
if (NF_INVF(e, EBT_ISOURCE,
|
|
|
|
!ether_addr_equal_masked(h->h_source, e->sourcemac,
|
|
|
|
e->sourcemsk)))
|
2005-04-16 16:20:36 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (e->bitmask & EBT_DESTMAC) {
|
2016-06-24 14:25:22 -06:00
|
|
|
if (NF_INVF(e, EBT_IDEST,
|
|
|
|
!ether_addr_equal_masked(h->h_dest, e->destmac,
|
|
|
|
e->destmsk)))
|
2005-04-16 16:20:36 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-27 16:16:21 -06:00
|
|
|
static inline
|
2009-04-15 13:06:05 -06:00
|
|
|
struct ebt_entry *ebt_next_entry(const struct ebt_entry *entry)
|
|
|
|
{
|
|
|
|
return (void *)entry + entry->next_offset;
|
|
|
|
}
|
|
|
|
|
2018-04-13 08:09:58 -06:00
|
|
|
static inline const struct ebt_entry_target *
|
|
|
|
ebt_get_target_c(const struct ebt_entry *e)
|
|
|
|
{
|
|
|
|
return ebt_get_target((struct ebt_entry *)e);
|
|
|
|
}
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
/* Do some firewalling */
|
2015-09-18 13:32:54 -06:00
|
|
|
unsigned int ebt_do_table(struct sk_buff *skb,
|
|
|
|
const struct nf_hook_state *state,
|
|
|
|
struct ebt_table *table)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2015-09-18 13:32:54 -06:00
|
|
|
unsigned int hook = state->hook;
|
2005-04-16 16:20:36 -06:00
|
|
|
int i, nentries;
|
|
|
|
struct ebt_entry *point;
|
|
|
|
struct ebt_counter *counter_base, *cb_base;
|
2009-06-25 23:51:59 -06:00
|
|
|
const struct ebt_entry_target *t;
|
2005-04-16 16:20:36 -06:00
|
|
|
int verdict, sp = 0;
|
|
|
|
struct ebt_chainstack *cs;
|
|
|
|
struct ebt_entries *chaininfo;
|
2009-06-25 23:51:59 -06:00
|
|
|
const char *base;
|
|
|
|
const struct ebt_table_info *private;
|
2009-07-05 10:26:37 -06:00
|
|
|
struct xt_action_param acpar;
|
2008-10-08 03:35:18 -06:00
|
|
|
|
2016-11-03 03:56:21 -06:00
|
|
|
acpar.state = state;
|
2009-07-07 12:54:30 -06:00
|
|
|
acpar.hotdrop = false;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
read_lock_bh(&table->lock);
|
|
|
|
private = table->private;
|
|
|
|
cb_base = COUNTER_BASE(private->counters, private->nentries,
|
|
|
|
smp_processor_id());
|
|
|
|
if (private->chainstack)
|
|
|
|
cs = private->chainstack[smp_processor_id()];
|
|
|
|
else
|
|
|
|
cs = NULL;
|
|
|
|
chaininfo = private->hook_entry[hook];
|
|
|
|
nentries = private->hook_entry[hook]->nentries;
|
|
|
|
point = (struct ebt_entry *)(private->hook_entry[hook]->data);
|
|
|
|
counter_base = cb_base + private->hook_entry[hook]->counter_offset;
|
|
|
|
/* base for chain jumps */
|
|
|
|
base = private->entries;
|
|
|
|
i = 0;
|
|
|
|
while (i < nentries) {
|
2015-09-18 13:32:54 -06:00
|
|
|
if (ebt_basic_match(point, skb, state->in, state->out))
|
2005-04-16 16:20:36 -06:00
|
|
|
goto letscontinue;
|
|
|
|
|
2009-07-05 10:26:37 -06:00
|
|
|
if (EBT_MATCH_ITERATE(point, ebt_do_match, skb, &acpar) != 0)
|
2005-04-16 16:20:36 -06:00
|
|
|
goto letscontinue;
|
2009-07-07 12:54:30 -06:00
|
|
|
if (acpar.hotdrop) {
|
2008-10-08 03:35:16 -06:00
|
|
|
read_unlock_bh(&table->lock);
|
|
|
|
return NF_DROP;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2019-08-14 08:54:16 -06:00
|
|
|
ADD_COUNTER(*(counter_base + i), skb->len, 1);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/* these should only watch: not modify, nor tell us
|
2015-10-26 03:10:41 -06:00
|
|
|
* what to do with the packet
|
|
|
|
*/
|
2009-07-05 10:26:37 -06:00
|
|
|
EBT_WATCHER_ITERATE(point, ebt_do_watcher, skb, &acpar);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2018-04-13 08:09:58 -06:00
|
|
|
t = ebt_get_target_c(point);
|
2005-04-16 16:20:36 -06:00
|
|
|
/* standard target */
|
|
|
|
if (!t->u.target->target)
|
|
|
|
verdict = ((struct ebt_standard_target *)t)->verdict;
|
2008-10-08 03:35:19 -06:00
|
|
|
else {
|
2009-07-05 10:26:37 -06:00
|
|
|
acpar.target = t->u.target;
|
|
|
|
acpar.targinfo = t->data;
|
|
|
|
verdict = t->u.target->target(skb, &acpar);
|
2008-10-08 03:35:19 -06:00
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
if (verdict == EBT_ACCEPT) {
|
|
|
|
read_unlock_bh(&table->lock);
|
|
|
|
return NF_ACCEPT;
|
|
|
|
}
|
|
|
|
if (verdict == EBT_DROP) {
|
|
|
|
read_unlock_bh(&table->lock);
|
|
|
|
return NF_DROP;
|
|
|
|
}
|
|
|
|
if (verdict == EBT_RETURN) {
|
|
|
|
letsreturn:
|
2017-08-30 02:07:12 -06:00
|
|
|
if (WARN(sp == 0, "RETURN on base chain")) {
|
2005-04-16 16:20:36 -06:00
|
|
|
/* act like this is EBT_CONTINUE */
|
|
|
|
goto letscontinue;
|
|
|
|
}
|
2017-08-30 02:07:12 -06:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
sp--;
|
|
|
|
/* put all the local variables right */
|
|
|
|
i = cs[sp].n;
|
|
|
|
chaininfo = cs[sp].chaininfo;
|
|
|
|
nentries = chaininfo->nentries;
|
|
|
|
point = cs[sp].e;
|
|
|
|
counter_base = cb_base +
|
|
|
|
chaininfo->counter_offset;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (verdict == EBT_CONTINUE)
|
|
|
|
goto letscontinue;
|
2017-08-30 02:07:12 -06:00
|
|
|
|
|
|
|
if (WARN(verdict < 0, "bogus standard verdict\n")) {
|
2005-04-16 16:20:36 -06:00
|
|
|
read_unlock_bh(&table->lock);
|
|
|
|
return NF_DROP;
|
|
|
|
}
|
2017-08-30 02:07:12 -06:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
/* jump to a udc */
|
|
|
|
cs[sp].n = i + 1;
|
|
|
|
cs[sp].chaininfo = chaininfo;
|
2009-04-15 13:06:05 -06:00
|
|
|
cs[sp].e = ebt_next_entry(point);
|
2005-04-16 16:20:36 -06:00
|
|
|
i = 0;
|
|
|
|
chaininfo = (struct ebt_entries *) (base + verdict);
|
2017-08-30 02:07:12 -06:00
|
|
|
|
|
|
|
if (WARN(chaininfo->distinguisher, "jump to non-chain\n")) {
|
2005-04-16 16:20:36 -06:00
|
|
|
read_unlock_bh(&table->lock);
|
|
|
|
return NF_DROP;
|
|
|
|
}
|
2017-08-30 02:07:12 -06:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
nentries = chaininfo->nentries;
|
|
|
|
point = (struct ebt_entry *)chaininfo->data;
|
|
|
|
counter_base = cb_base + chaininfo->counter_offset;
|
|
|
|
sp++;
|
|
|
|
continue;
|
|
|
|
letscontinue:
|
2009-04-15 13:06:05 -06:00
|
|
|
point = ebt_next_entry(point);
|
2005-04-16 16:20:36 -06:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* I actually like this :) */
|
|
|
|
if (chaininfo->policy == EBT_RETURN)
|
|
|
|
goto letsreturn;
|
|
|
|
if (chaininfo->policy == EBT_ACCEPT) {
|
|
|
|
read_unlock_bh(&table->lock);
|
|
|
|
return NF_ACCEPT;
|
|
|
|
}
|
|
|
|
read_unlock_bh(&table->lock);
|
|
|
|
return NF_DROP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If it succeeds, returns element and locks mutex */
|
|
|
|
static inline void *
|
|
|
|
find_inlist_lock_noload(struct list_head *head, const char *name, int *error,
|
2015-10-26 03:10:40 -06:00
|
|
|
struct mutex *mutex)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2006-09-20 12:57:53 -06:00
|
|
|
struct {
|
|
|
|
struct list_head list;
|
|
|
|
char name[EBT_FUNCTION_MAXNAMELEN];
|
|
|
|
} *e;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2014-07-31 12:38:46 -06:00
|
|
|
mutex_lock(mutex);
|
2006-09-20 12:57:53 -06:00
|
|
|
list_for_each_entry(e, head, list) {
|
|
|
|
if (strcmp(e->name, name) == 0)
|
|
|
|
return e;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2006-09-20 12:57:53 -06:00
|
|
|
*error = -ENOENT;
|
|
|
|
mutex_unlock(mutex);
|
|
|
|
return NULL;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
find_inlist_lock(struct list_head *head, const char *name, const char *prefix,
|
2015-10-26 03:10:40 -06:00
|
|
|
int *error, struct mutex *mutex)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-10-16 16:24:51 -06:00
|
|
|
return try_then_request_module(
|
|
|
|
find_inlist_lock_noload(head, name, error, mutex),
|
|
|
|
"%s%s", prefix, name);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct ebt_table *
|
2008-11-04 06:22:55 -07:00
|
|
|
find_table_lock(struct net *net, const char *name, int *error,
|
|
|
|
struct mutex *mutex)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-11-04 06:22:55 -07:00
|
|
|
return find_inlist_lock(&net->xt.tables[NFPROTO_BRIDGE], name,
|
|
|
|
"ebtable_", error, mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2018-04-08 09:00:21 -06:00
|
|
|
static inline void ebt_free_table_info(struct ebt_table_info *info)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (info->chainstack) {
|
|
|
|
for_each_possible_cpu(i)
|
|
|
|
vfree(info->chainstack[i]);
|
|
|
|
vfree(info->chainstack);
|
|
|
|
}
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
static inline int
|
2008-10-08 03:35:18 -06:00
|
|
|
ebt_check_match(struct ebt_entry_match *m, struct xt_mtchk_param *par,
|
|
|
|
unsigned int *cnt)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-10-08 03:35:18 -06:00
|
|
|
const struct ebt_entry *e = par->entryinfo;
|
2008-10-08 03:35:15 -06:00
|
|
|
struct xt_match *match;
|
2006-11-30 20:25:21 -07:00
|
|
|
size_t left = ((char *)e + e->watchers_offset) - (char *)m;
|
2005-04-16 16:20:36 -06:00
|
|
|
int ret;
|
|
|
|
|
2006-11-30 20:25:21 -07:00
|
|
|
if (left < sizeof(struct ebt_entry_match) ||
|
|
|
|
left - sizeof(struct ebt_entry_match) < m->match_size)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
2008-10-08 03:35:15 -06:00
|
|
|
|
2018-03-20 20:42:15 -06:00
|
|
|
match = xt_find_match(NFPROTO_BRIDGE, m->u.name, m->u.revision);
|
2016-04-11 17:31:14 -06:00
|
|
|
if (IS_ERR(match) || match->family != NFPROTO_BRIDGE) {
|
2016-08-23 02:20:31 -06:00
|
|
|
if (!IS_ERR(match))
|
|
|
|
module_put(match->me);
|
2016-04-11 17:31:14 -06:00
|
|
|
request_module("ebt_%s", m->u.name);
|
2018-03-20 20:42:15 -06:00
|
|
|
match = xt_find_match(NFPROTO_BRIDGE, m->u.name, m->u.revision);
|
2016-04-11 17:31:14 -06:00
|
|
|
}
|
2008-10-08 03:35:15 -06:00
|
|
|
if (IS_ERR(match))
|
|
|
|
return PTR_ERR(match);
|
|
|
|
m->u.match = match;
|
|
|
|
|
2008-10-08 03:35:18 -06:00
|
|
|
par->match = match;
|
|
|
|
par->matchinfo = m->data;
|
2008-10-08 03:35:20 -06:00
|
|
|
ret = xt_check_match(par, m->match_size,
|
2019-02-22 06:45:52 -07:00
|
|
|
ntohs(e->ethproto), e->invflags & EBT_IPROTO);
|
2008-10-08 03:35:15 -06:00
|
|
|
if (ret < 0) {
|
|
|
|
module_put(match->me);
|
|
|
|
return ret;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2008-10-08 03:35:15 -06:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
(*cnt)++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
2008-10-08 03:35:19 -06:00
|
|
|
ebt_check_watcher(struct ebt_entry_watcher *w, struct xt_tgchk_param *par,
|
|
|
|
unsigned int *cnt)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-10-08 03:35:19 -06:00
|
|
|
const struct ebt_entry *e = par->entryinfo;
|
2008-10-08 03:35:15 -06:00
|
|
|
struct xt_target *watcher;
|
2006-11-30 20:25:21 -07:00
|
|
|
size_t left = ((char *)e + e->target_offset) - (char *)w;
|
2005-04-16 16:20:36 -06:00
|
|
|
int ret;
|
|
|
|
|
2006-11-30 20:25:21 -07:00
|
|
|
if (left < sizeof(struct ebt_entry_watcher) ||
|
|
|
|
left - sizeof(struct ebt_entry_watcher) < w->watcher_size)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
2008-10-08 03:35:15 -06:00
|
|
|
|
2009-07-10 10:55:11 -06:00
|
|
|
watcher = xt_request_find_target(NFPROTO_BRIDGE, w->u.name, 0);
|
2008-10-08 03:35:15 -06:00
|
|
|
if (IS_ERR(watcher))
|
|
|
|
return PTR_ERR(watcher);
|
2018-06-06 04:14:56 -06:00
|
|
|
|
|
|
|
if (watcher->family != NFPROTO_BRIDGE) {
|
|
|
|
module_put(watcher->me);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2008-10-08 03:35:15 -06:00
|
|
|
w->u.watcher = watcher;
|
|
|
|
|
2008-10-08 03:35:19 -06:00
|
|
|
par->target = watcher;
|
|
|
|
par->targinfo = w->data;
|
2008-10-08 03:35:20 -06:00
|
|
|
ret = xt_check_target(par, w->watcher_size,
|
2019-02-22 06:45:52 -07:00
|
|
|
ntohs(e->ethproto), e->invflags & EBT_IPROTO);
|
2008-10-08 03:35:15 -06:00
|
|
|
if (ret < 0) {
|
|
|
|
module_put(watcher->me);
|
|
|
|
return ret;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2008-10-08 03:35:15 -06:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
(*cnt)++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-25 23:51:59 -06:00
|
|
|
static int ebt_verify_pointers(const struct ebt_replace *repl,
|
2006-11-30 20:26:14 -07:00
|
|
|
struct ebt_table_info *newinfo)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2006-11-30 20:26:14 -07:00
|
|
|
unsigned int limit = repl->entries_size;
|
|
|
|
unsigned int valid_hooks = repl->valid_hooks;
|
|
|
|
unsigned int offset = 0;
|
2005-04-16 16:20:36 -06:00
|
|
|
int i;
|
|
|
|
|
2006-11-30 20:26:35 -07:00
|
|
|
for (i = 0; i < NF_BR_NUMHOOKS; i++)
|
|
|
|
newinfo->hook_entry[i] = NULL;
|
|
|
|
|
|
|
|
newinfo->entries_size = repl->entries_size;
|
|
|
|
newinfo->nentries = repl->nentries;
|
|
|
|
|
2006-11-30 20:26:14 -07:00
|
|
|
while (offset < limit) {
|
|
|
|
size_t left = limit - offset;
|
|
|
|
struct ebt_entry *e = (void *)newinfo->entries + offset;
|
2006-11-30 20:22:42 -07:00
|
|
|
|
2006-11-30 20:26:14 -07:00
|
|
|
if (left < sizeof(unsigned int))
|
2005-04-16 16:20:36 -06:00
|
|
|
break;
|
2006-11-30 20:26:14 -07:00
|
|
|
|
|
|
|
for (i = 0; i < NF_BR_NUMHOOKS; i++) {
|
|
|
|
if ((valid_hooks & (1 << i)) == 0)
|
|
|
|
continue;
|
2006-11-30 20:28:48 -07:00
|
|
|
if ((char __user *)repl->hook_entry[i] ==
|
|
|
|
repl->entries + offset)
|
2006-11-30 20:26:14 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != NF_BR_NUMHOOKS || !(e->bitmask & EBT_ENTRY_OR_ENTRIES)) {
|
|
|
|
if (e->bitmask != 0) {
|
|
|
|
/* we make userspace set this right,
|
2015-10-26 03:10:41 -06:00
|
|
|
* so there is no misunderstanding
|
|
|
|
*/
|
2006-11-30 20:26:14 -07:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (i != NF_BR_NUMHOOKS)
|
|
|
|
newinfo->hook_entry[i] = (struct ebt_entries *)e;
|
|
|
|
if (left < sizeof(struct ebt_entries))
|
|
|
|
break;
|
|
|
|
offset += sizeof(struct ebt_entries);
|
|
|
|
} else {
|
|
|
|
if (left < sizeof(struct ebt_entry))
|
|
|
|
break;
|
|
|
|
if (left < e->next_offset)
|
|
|
|
break;
|
2010-02-15 10:15:55 -07:00
|
|
|
if (e->next_offset < sizeof(struct ebt_entry))
|
|
|
|
return -EINVAL;
|
2006-11-30 20:26:14 -07:00
|
|
|
offset += e->next_offset;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2006-11-30 20:25:51 -07:00
|
|
|
}
|
2019-02-18 16:37:21 -07:00
|
|
|
if (offset != limit)
|
2006-11-30 20:26:14 -07:00
|
|
|
return -EINVAL;
|
2006-11-30 20:26:35 -07:00
|
|
|
|
|
|
|
/* check if all valid hooks have a chain */
|
|
|
|
for (i = 0; i < NF_BR_NUMHOOKS; i++) {
|
|
|
|
if (!newinfo->hook_entry[i] &&
|
2019-02-18 16:37:21 -07:00
|
|
|
(valid_hooks & (1 << i)))
|
2006-11-30 20:26:35 -07:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2006-11-30 20:25:51 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-26 03:10:41 -06:00
|
|
|
/* this one is very careful, as it is the first function
|
2006-11-30 20:25:51 -07:00
|
|
|
* to parse the userspace data
|
|
|
|
*/
|
|
|
|
static inline int
|
2009-06-25 23:51:59 -06:00
|
|
|
ebt_check_entry_size_and_hooks(const struct ebt_entry *e,
|
2015-10-26 03:10:40 -06:00
|
|
|
const struct ebt_table_info *newinfo,
|
|
|
|
unsigned int *n, unsigned int *cnt,
|
|
|
|
unsigned int *totalcnt, unsigned int *udc_cnt)
|
2006-11-30 20:25:51 -07:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NF_BR_NUMHOOKS; i++) {
|
2006-11-30 20:27:13 -07:00
|
|
|
if ((void *)e == (void *)newinfo->hook_entry[i])
|
2006-11-30 20:25:51 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* beginning of a new chain
|
2015-10-26 03:10:41 -06:00
|
|
|
* if i == NF_BR_NUMHOOKS it must be a user defined chain
|
|
|
|
*/
|
2006-11-30 20:25:51 -07:00
|
|
|
if (i != NF_BR_NUMHOOKS || !e->bitmask) {
|
2005-04-16 16:20:36 -06:00
|
|
|
/* this checks if the previous chain has as many entries
|
2015-10-26 03:10:41 -06:00
|
|
|
* as it said it has
|
|
|
|
*/
|
2019-02-18 16:37:21 -07:00
|
|
|
if (*n != *cnt)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
2019-02-18 16:37:21 -07:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
if (((struct ebt_entries *)e)->policy != EBT_DROP &&
|
|
|
|
((struct ebt_entries *)e)->policy != EBT_ACCEPT) {
|
|
|
|
/* only RETURN from udc */
|
|
|
|
if (i != NF_BR_NUMHOOKS ||
|
2019-02-18 16:37:21 -07:00
|
|
|
((struct ebt_entries *)e)->policy != EBT_RETURN)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (i == NF_BR_NUMHOOKS) /* it's a user defined chain */
|
|
|
|
(*udc_cnt)++;
|
2019-02-18 16:37:21 -07:00
|
|
|
if (((struct ebt_entries *)e)->counter_offset != *totalcnt)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
|
|
|
*n = ((struct ebt_entries *)e)->nentries;
|
|
|
|
*cnt = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* a plain old entry, heh */
|
|
|
|
if (sizeof(struct ebt_entry) > e->watchers_offset ||
|
|
|
|
e->watchers_offset > e->target_offset ||
|
2019-02-18 16:37:21 -07:00
|
|
|
e->target_offset >= e->next_offset)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
2019-02-18 16:37:21 -07:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
/* this is not checked anywhere else */
|
2019-02-18 16:37:21 -07:00
|
|
|
if (e->next_offset - e->target_offset < sizeof(struct ebt_entry_target))
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
2019-02-18 16:37:21 -07:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
(*cnt)++;
|
|
|
|
(*totalcnt)++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-26 03:10:42 -06:00
|
|
|
struct ebt_cl_stack {
|
2005-04-16 16:20:36 -06:00
|
|
|
struct ebt_chainstack cs;
|
|
|
|
int from;
|
|
|
|
unsigned int hookmask;
|
|
|
|
};
|
|
|
|
|
2015-10-26 03:10:41 -06:00
|
|
|
/* We need these positions to check that the jumps to a different part of the
|
2005-04-16 16:20:36 -06:00
|
|
|
* entries is a jump to the beginning of a new chain.
|
|
|
|
*/
|
|
|
|
static inline int
|
|
|
|
ebt_get_udc_positions(struct ebt_entry *e, struct ebt_table_info *newinfo,
|
2015-10-26 03:10:40 -06:00
|
|
|
unsigned int *n, struct ebt_cl_stack *udc)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* we're only interested in chain starts */
|
2006-11-30 20:24:12 -07:00
|
|
|
if (e->bitmask)
|
2005-04-16 16:20:36 -06:00
|
|
|
return 0;
|
|
|
|
for (i = 0; i < NF_BR_NUMHOOKS; i++) {
|
|
|
|
if (newinfo->hook_entry[i] == (struct ebt_entries *)e)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* only care about udc */
|
|
|
|
if (i != NF_BR_NUMHOOKS)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
udc[*n].cs.chaininfo = (struct ebt_entries *)e;
|
|
|
|
/* these initialisations are depended on later in check_chainloops() */
|
|
|
|
udc[*n].cs.n = 0;
|
|
|
|
udc[*n].hookmask = 0;
|
|
|
|
|
|
|
|
(*n)++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
2010-01-18 00:25:47 -07:00
|
|
|
ebt_cleanup_match(struct ebt_entry_match *m, struct net *net, unsigned int *i)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-10-08 03:35:19 -06:00
|
|
|
struct xt_mtdtor_param par;
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
if (i && (*i)-- == 0)
|
|
|
|
return 1;
|
|
|
|
|
2010-01-18 00:25:47 -07:00
|
|
|
par.net = net;
|
2008-10-08 03:35:19 -06:00
|
|
|
par.match = m->u.match;
|
|
|
|
par.matchinfo = m->data;
|
2008-10-08 03:35:20 -06:00
|
|
|
par.family = NFPROTO_BRIDGE;
|
2008-10-08 03:35:19 -06:00
|
|
|
if (par.match->destroy != NULL)
|
|
|
|
par.match->destroy(&par);
|
|
|
|
module_put(par.match->me);
|
2005-04-16 16:20:36 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
2010-02-03 05:45:12 -07:00
|
|
|
ebt_cleanup_watcher(struct ebt_entry_watcher *w, struct net *net, unsigned int *i)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-10-08 03:35:19 -06:00
|
|
|
struct xt_tgdtor_param par;
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
if (i && (*i)-- == 0)
|
|
|
|
return 1;
|
|
|
|
|
2010-02-03 05:45:12 -07:00
|
|
|
par.net = net;
|
2008-10-08 03:35:19 -06:00
|
|
|
par.target = w->u.watcher;
|
|
|
|
par.targinfo = w->data;
|
2008-10-08 03:35:20 -06:00
|
|
|
par.family = NFPROTO_BRIDGE;
|
2008-10-08 03:35:19 -06:00
|
|
|
if (par.target->destroy != NULL)
|
|
|
|
par.target->destroy(&par);
|
|
|
|
module_put(par.target->me);
|
2005-04-16 16:20:36 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
2010-01-18 00:25:47 -07:00
|
|
|
ebt_cleanup_entry(struct ebt_entry *e, struct net *net, unsigned int *cnt)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2008-10-08 03:35:19 -06:00
|
|
|
struct xt_tgdtor_param par;
|
2005-04-16 16:20:36 -06:00
|
|
|
struct ebt_entry_target *t;
|
|
|
|
|
2006-11-30 20:24:12 -07:00
|
|
|
if (e->bitmask == 0)
|
2005-04-16 16:20:36 -06:00
|
|
|
return 0;
|
|
|
|
/* we're done */
|
|
|
|
if (cnt && (*cnt)-- == 0)
|
|
|
|
return 1;
|
2010-02-03 05:45:12 -07:00
|
|
|
EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, net, NULL);
|
2010-01-18 00:25:47 -07:00
|
|
|
EBT_MATCH_ITERATE(e, ebt_cleanup_match, net, NULL);
|
2018-04-13 08:09:58 -06:00
|
|
|
t = ebt_get_target(e);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2010-02-03 05:45:12 -07:00
|
|
|
par.net = net;
|
2008-10-08 03:35:19 -06:00
|
|
|
par.target = t->u.target;
|
|
|
|
par.targinfo = t->data;
|
2008-10-08 03:35:20 -06:00
|
|
|
par.family = NFPROTO_BRIDGE;
|
2008-10-08 03:35:19 -06:00
|
|
|
if (par.target->destroy != NULL)
|
|
|
|
par.target->destroy(&par);
|
|
|
|
module_put(par.target->me);
|
2005-04-16 16:20:36 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
2009-06-25 23:51:59 -06:00
|
|
|
ebt_check_entry(struct ebt_entry *e, struct net *net,
|
2015-10-26 03:10:40 -06:00
|
|
|
const struct ebt_table_info *newinfo,
|
|
|
|
const char *name, unsigned int *cnt,
|
|
|
|
struct ebt_cl_stack *cl_s, unsigned int udc_cnt)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
struct ebt_entry_target *t;
|
2008-10-08 03:35:15 -06:00
|
|
|
struct xt_target *target;
|
2005-04-16 16:20:36 -06:00
|
|
|
unsigned int i, j, hook = 0, hookmask = 0;
|
2007-01-04 13:17:44 -07:00
|
|
|
size_t gap;
|
2005-04-16 16:20:36 -06:00
|
|
|
int ret;
|
2008-10-08 03:35:19 -06:00
|
|
|
struct xt_mtchk_param mtpar;
|
2008-10-08 03:35:19 -06:00
|
|
|
struct xt_tgchk_param tgpar;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/* don't mess with the struct ebt_entries */
|
2006-11-30 20:24:12 -07:00
|
|
|
if (e->bitmask == 0)
|
2005-04-16 16:20:36 -06:00
|
|
|
return 0;
|
|
|
|
|
2019-02-18 16:37:21 -07:00
|
|
|
if (e->bitmask & ~EBT_F_MASK)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
2019-02-18 16:37:21 -07:00
|
|
|
|
|
|
|
if (e->invflags & ~EBT_INV_MASK)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
2019-02-18 16:37:21 -07:00
|
|
|
|
|
|
|
if ((e->bitmask & EBT_NOPROTO) && (e->bitmask & EBT_802_3))
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
2019-02-18 16:37:21 -07:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
/* what hook do we belong to? */
|
|
|
|
for (i = 0; i < NF_BR_NUMHOOKS; i++) {
|
2006-11-30 20:27:48 -07:00
|
|
|
if (!newinfo->hook_entry[i])
|
2005-04-16 16:20:36 -06:00
|
|
|
continue;
|
|
|
|
if ((char *)newinfo->hook_entry[i] < (char *)e)
|
|
|
|
hook = i;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* (1 << NF_BR_NUMHOOKS) tells the check functions the rule is on
|
2015-10-26 03:10:41 -06:00
|
|
|
* a base chain
|
|
|
|
*/
|
2005-04-16 16:20:36 -06:00
|
|
|
if (i < NF_BR_NUMHOOKS)
|
|
|
|
hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS);
|
|
|
|
else {
|
|
|
|
for (i = 0; i < udc_cnt; i++)
|
|
|
|
if ((char *)(cl_s[i].cs.chaininfo) > (char *)e)
|
|
|
|
break;
|
|
|
|
if (i == 0)
|
|
|
|
hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS);
|
|
|
|
else
|
|
|
|
hookmask = cl_s[i - 1].hookmask;
|
|
|
|
}
|
|
|
|
i = 0;
|
2008-10-08 03:35:18 -06:00
|
|
|
|
2018-06-07 13:34:43 -06:00
|
|
|
memset(&mtpar, 0, sizeof(mtpar));
|
|
|
|
memset(&tgpar, 0, sizeof(tgpar));
|
2010-02-03 05:45:12 -07:00
|
|
|
mtpar.net = tgpar.net = net;
|
2008-10-08 03:35:19 -06:00
|
|
|
mtpar.table = tgpar.table = name;
|
|
|
|
mtpar.entryinfo = tgpar.entryinfo = e;
|
|
|
|
mtpar.hook_mask = tgpar.hook_mask = hookmask;
|
2008-10-08 03:35:20 -06:00
|
|
|
mtpar.family = tgpar.family = NFPROTO_BRIDGE;
|
2008-10-08 03:35:19 -06:00
|
|
|
ret = EBT_MATCH_ITERATE(e, ebt_check_match, &mtpar, &i);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (ret != 0)
|
|
|
|
goto cleanup_matches;
|
|
|
|
j = 0;
|
2008-10-08 03:35:19 -06:00
|
|
|
ret = EBT_WATCHER_ITERATE(e, ebt_check_watcher, &tgpar, &j);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (ret != 0)
|
|
|
|
goto cleanup_watchers;
|
2018-04-13 08:09:58 -06:00
|
|
|
t = ebt_get_target(e);
|
2007-01-04 13:17:44 -07:00
|
|
|
gap = e->next_offset - e->target_offset;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2009-07-10 10:55:11 -06:00
|
|
|
target = xt_request_find_target(NFPROTO_BRIDGE, t->u.name, 0);
|
2008-10-08 03:35:15 -06:00
|
|
|
if (IS_ERR(target)) {
|
|
|
|
ret = PTR_ERR(target);
|
2008-10-08 03:35:14 -06:00
|
|
|
goto cleanup_watchers;
|
|
|
|
}
|
|
|
|
|
2018-06-06 04:14:56 -06:00
|
|
|
/* Reject UNSPEC, xtables verdicts/return values are incompatible */
|
|
|
|
if (target->family != NFPROTO_BRIDGE) {
|
|
|
|
module_put(target->me);
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto cleanup_watchers;
|
|
|
|
}
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
t->u.target = target;
|
|
|
|
if (t->u.target == &ebt_standard_target) {
|
2006-11-30 20:25:21 -07:00
|
|
|
if (gap < sizeof(struct ebt_standard_target)) {
|
2005-04-16 16:20:36 -06:00
|
|
|
ret = -EFAULT;
|
|
|
|
goto cleanup_watchers;
|
|
|
|
}
|
|
|
|
if (((struct ebt_standard_target *)t)->verdict <
|
|
|
|
-NUM_STANDARD_TARGETS) {
|
|
|
|
ret = -EFAULT;
|
|
|
|
goto cleanup_watchers;
|
|
|
|
}
|
2008-10-08 03:35:13 -06:00
|
|
|
} else if (t->target_size > gap - sizeof(struct ebt_entry_target)) {
|
|
|
|
module_put(t->u.target->me);
|
|
|
|
ret = -EFAULT;
|
|
|
|
goto cleanup_watchers;
|
2008-10-08 03:35:15 -06:00
|
|
|
}
|
|
|
|
|
2008-10-08 03:35:19 -06:00
|
|
|
tgpar.target = target;
|
|
|
|
tgpar.targinfo = t->data;
|
2008-10-08 03:35:20 -06:00
|
|
|
ret = xt_check_target(&tgpar, t->target_size,
|
2019-02-22 06:45:52 -07:00
|
|
|
ntohs(e->ethproto), e->invflags & EBT_IPROTO);
|
2008-10-08 03:35:15 -06:00
|
|
|
if (ret < 0) {
|
|
|
|
module_put(target->me);
|
2008-10-08 03:35:13 -06:00
|
|
|
goto cleanup_watchers;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
(*cnt)++;
|
|
|
|
return 0;
|
|
|
|
cleanup_watchers:
|
2010-02-03 05:45:12 -07:00
|
|
|
EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, net, &j);
|
2005-04-16 16:20:36 -06:00
|
|
|
cleanup_matches:
|
2010-01-18 00:25:47 -07:00
|
|
|
EBT_MATCH_ITERATE(e, ebt_cleanup_match, net, &i);
|
2005-04-16 16:20:36 -06:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-26 03:10:41 -06:00
|
|
|
/* checks for loops and sets the hook mask for udc
|
2005-04-16 16:20:36 -06:00
|
|
|
* the hook mask for udc tells us from which base chains the udc can be
|
|
|
|
* accessed. This mask is a parameter to the check() functions of the extensions
|
|
|
|
*/
|
2009-06-25 23:51:59 -06:00
|
|
|
static int check_chainloops(const struct ebt_entries *chain, struct ebt_cl_stack *cl_s,
|
2015-10-26 03:10:40 -06:00
|
|
|
unsigned int udc_cnt, unsigned int hooknr, char *base)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
int i, chain_nr = -1, pos = 0, nentries = chain->nentries, verdict;
|
2009-06-25 23:51:59 -06:00
|
|
|
const struct ebt_entry *e = (struct ebt_entry *)chain->data;
|
|
|
|
const struct ebt_entry_target *t;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
while (pos < nentries || chain_nr != -1) {
|
|
|
|
/* end of udc, go back one 'recursion' step */
|
|
|
|
if (pos == nentries) {
|
|
|
|
/* put back values of the time when this chain was called */
|
|
|
|
e = cl_s[chain_nr].cs.e;
|
|
|
|
if (cl_s[chain_nr].from != -1)
|
|
|
|
nentries =
|
|
|
|
cl_s[cl_s[chain_nr].from].cs.chaininfo->nentries;
|
|
|
|
else
|
|
|
|
nentries = chain->nentries;
|
|
|
|
pos = cl_s[chain_nr].cs.n;
|
|
|
|
/* make sure we won't see a loop that isn't one */
|
|
|
|
cl_s[chain_nr].cs.n = 0;
|
|
|
|
chain_nr = cl_s[chain_nr].from;
|
|
|
|
if (pos == nentries)
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-13 08:09:58 -06:00
|
|
|
t = ebt_get_target_c(e);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (strcmp(t->u.name, EBT_STANDARD_TARGET))
|
|
|
|
goto letscontinue;
|
|
|
|
if (e->target_offset + sizeof(struct ebt_standard_target) >
|
2019-02-18 16:37:21 -07:00
|
|
|
e->next_offset)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -1;
|
2019-02-18 16:37:21 -07:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
verdict = ((struct ebt_standard_target *)t)->verdict;
|
|
|
|
if (verdict >= 0) { /* jump to another chain */
|
|
|
|
struct ebt_entries *hlp2 =
|
|
|
|
(struct ebt_entries *)(base + verdict);
|
|
|
|
for (i = 0; i < udc_cnt; i++)
|
|
|
|
if (hlp2 == cl_s[i].cs.chaininfo)
|
|
|
|
break;
|
|
|
|
/* bad destination or loop */
|
2019-02-18 16:37:21 -07:00
|
|
|
if (i == udc_cnt)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -1;
|
2019-02-18 16:37:21 -07:00
|
|
|
|
|
|
|
if (cl_s[i].cs.n)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -1;
|
2019-02-18 16:37:21 -07:00
|
|
|
|
2006-11-30 20:24:49 -07:00
|
|
|
if (cl_s[i].hookmask & (1 << hooknr))
|
|
|
|
goto letscontinue;
|
|
|
|
/* this can't be 0, so the loop test is correct */
|
2005-04-16 16:20:36 -06:00
|
|
|
cl_s[i].cs.n = pos + 1;
|
|
|
|
pos = 0;
|
2009-04-15 13:06:05 -06:00
|
|
|
cl_s[i].cs.e = ebt_next_entry(e);
|
2005-04-16 16:20:36 -06:00
|
|
|
e = (struct ebt_entry *)(hlp2->data);
|
|
|
|
nentries = hlp2->nentries;
|
|
|
|
cl_s[i].from = chain_nr;
|
|
|
|
chain_nr = i;
|
|
|
|
/* this udc is accessible from the base chain for hooknr */
|
|
|
|
cl_s[i].hookmask |= (1 << hooknr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
letscontinue:
|
2009-04-15 13:06:05 -06:00
|
|
|
e = ebt_next_entry(e);
|
2005-04-16 16:20:36 -06:00
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do the parsing of the table/chains/entries/matches/watchers/targets, heh */
|
2009-06-25 23:51:59 -06:00
|
|
|
static int translate_table(struct net *net, const char *name,
|
2010-01-18 00:21:13 -07:00
|
|
|
struct ebt_table_info *newinfo)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
unsigned int i, j, k, udc_cnt;
|
|
|
|
int ret;
|
|
|
|
struct ebt_cl_stack *cl_s = NULL; /* used in the checking for chain loops */
|
|
|
|
|
|
|
|
i = 0;
|
2006-11-30 20:26:53 -07:00
|
|
|
while (i < NF_BR_NUMHOOKS && !newinfo->hook_entry[i])
|
2005-04-16 16:20:36 -06:00
|
|
|
i++;
|
2019-02-18 16:37:21 -07:00
|
|
|
if (i == NF_BR_NUMHOOKS)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
2019-02-18 16:37:21 -07:00
|
|
|
|
|
|
|
if (newinfo->hook_entry[i] != (struct ebt_entries *)newinfo->entries)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
2019-02-18 16:37:21 -07:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
/* make sure chains are ordered after each other in same order
|
2015-10-26 03:10:41 -06:00
|
|
|
* as their corresponding hooks
|
|
|
|
*/
|
2005-04-16 16:20:36 -06:00
|
|
|
for (j = i + 1; j < NF_BR_NUMHOOKS; j++) {
|
2006-11-30 20:26:53 -07:00
|
|
|
if (!newinfo->hook_entry[j])
|
2005-04-16 16:20:36 -06:00
|
|
|
continue;
|
2019-02-18 16:37:21 -07:00
|
|
|
if (newinfo->hook_entry[j] <= newinfo->hook_entry[i])
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
2019-02-18 16:37:21 -07:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
i = j;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do some early checkings and initialize some things */
|
|
|
|
i = 0; /* holds the expected nr. of entries for the chain */
|
|
|
|
j = 0; /* holds the up to now counted entries for the chain */
|
|
|
|
k = 0; /* holds the total nr. of entries, should equal
|
2015-10-26 03:10:41 -06:00
|
|
|
* newinfo->nentries afterwards
|
|
|
|
*/
|
2005-04-16 16:20:36 -06:00
|
|
|
udc_cnt = 0; /* will hold the nr. of user defined chains (udc) */
|
|
|
|
ret = EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
|
2006-11-30 20:27:13 -07:00
|
|
|
ebt_check_entry_size_and_hooks, newinfo,
|
|
|
|
&i, &j, &k, &udc_cnt);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
|
2019-02-18 16:37:21 -07:00
|
|
|
if (i != j)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
2019-02-18 16:37:21 -07:00
|
|
|
|
|
|
|
if (k != newinfo->nentries)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* get the location of the udc, put them in an array
|
2015-10-26 03:10:41 -06:00
|
|
|
* while we're at it, allocate the chainstack
|
|
|
|
*/
|
2005-04-16 16:20:36 -06:00
|
|
|
if (udc_cnt) {
|
|
|
|
/* this will get free'd in do_replace()/ebt_register_table()
|
2015-10-26 03:10:41 -06:00
|
|
|
* if an error occurs
|
|
|
|
*/
|
2006-04-11 18:25:38 -06:00
|
|
|
newinfo->chainstack =
|
treewide: Use array_size() in vmalloc()
The vmalloc() function has no 2-factor argument form, so multiplication
factors need to be wrapped in array_size(). This patch replaces cases of:
vmalloc(a * b)
with:
vmalloc(array_size(a, b))
as well as handling cases of:
vmalloc(a * b * c)
with:
vmalloc(array3_size(a, b, c))
This does, however, attempt to ignore constant size factors like:
vmalloc(4 * 1024)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
vmalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
vmalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
vmalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
vmalloc(
- sizeof(TYPE) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT_ID
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT_ID
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
vmalloc(
- SIZE * COUNT
+ array_size(COUNT, SIZE)
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
vmalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
vmalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vmalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
vmalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
vmalloc(C1 * C2 * C3, ...)
|
vmalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants.
@@
expression E1, E2;
constant C1, C2;
@@
(
vmalloc(C1 * C2, ...)
|
vmalloc(
- E1 * E2
+ array_size(E1, E2)
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 15:27:11 -06:00
|
|
|
vmalloc(array_size(nr_cpu_ids,
|
|
|
|
sizeof(*(newinfo->chainstack))));
|
2005-04-16 16:20:36 -06:00
|
|
|
if (!newinfo->chainstack)
|
|
|
|
return -ENOMEM;
|
2006-04-10 23:52:50 -06:00
|
|
|
for_each_possible_cpu(i) {
|
2005-04-16 16:20:36 -06:00
|
|
|
newinfo->chainstack[i] =
|
treewide: Use array_size() in vmalloc()
The vmalloc() function has no 2-factor argument form, so multiplication
factors need to be wrapped in array_size(). This patch replaces cases of:
vmalloc(a * b)
with:
vmalloc(array_size(a, b))
as well as handling cases of:
vmalloc(a * b * c)
with:
vmalloc(array3_size(a, b, c))
This does, however, attempt to ignore constant size factors like:
vmalloc(4 * 1024)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
vmalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
vmalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
vmalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
vmalloc(
- sizeof(TYPE) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT_ID
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT_ID
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
vmalloc(
- SIZE * COUNT
+ array_size(COUNT, SIZE)
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
vmalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
vmalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vmalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
vmalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
vmalloc(C1 * C2 * C3, ...)
|
vmalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants.
@@
expression E1, E2;
constant C1, C2;
@@
(
vmalloc(C1 * C2, ...)
|
vmalloc(
- E1 * E2
+ array_size(E1, E2)
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 15:27:11 -06:00
|
|
|
vmalloc(array_size(udc_cnt, sizeof(*(newinfo->chainstack[0]))));
|
2005-04-16 16:20:36 -06:00
|
|
|
if (!newinfo->chainstack[i]) {
|
|
|
|
while (i)
|
|
|
|
vfree(newinfo->chainstack[--i]);
|
|
|
|
vfree(newinfo->chainstack);
|
|
|
|
newinfo->chainstack = NULL;
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
treewide: Use array_size() in vmalloc()
The vmalloc() function has no 2-factor argument form, so multiplication
factors need to be wrapped in array_size(). This patch replaces cases of:
vmalloc(a * b)
with:
vmalloc(array_size(a, b))
as well as handling cases of:
vmalloc(a * b * c)
with:
vmalloc(array3_size(a, b, c))
This does, however, attempt to ignore constant size factors like:
vmalloc(4 * 1024)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
vmalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
vmalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
vmalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
vmalloc(
- sizeof(TYPE) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT_ID
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT_ID
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
vmalloc(
- SIZE * COUNT
+ array_size(COUNT, SIZE)
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
vmalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
vmalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vmalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
vmalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
vmalloc(C1 * C2 * C3, ...)
|
vmalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants.
@@
expression E1, E2;
constant C1, C2;
@@
(
vmalloc(C1 * C2, ...)
|
vmalloc(
- E1 * E2
+ array_size(E1, E2)
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 15:27:11 -06:00
|
|
|
cl_s = vmalloc(array_size(udc_cnt, sizeof(*cl_s)));
|
2005-04-16 16:20:36 -06:00
|
|
|
if (!cl_s)
|
|
|
|
return -ENOMEM;
|
|
|
|
i = 0; /* the i'th udc */
|
|
|
|
EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
|
2006-11-30 20:27:32 -07:00
|
|
|
ebt_get_udc_positions, newinfo, &i, cl_s);
|
2005-04-16 16:20:36 -06:00
|
|
|
/* sanity check */
|
|
|
|
if (i != udc_cnt) {
|
|
|
|
vfree(cl_s);
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for loops */
|
|
|
|
for (i = 0; i < NF_BR_NUMHOOKS; i++)
|
2006-11-30 20:26:53 -07:00
|
|
|
if (newinfo->hook_entry[i])
|
2005-04-16 16:20:36 -06:00
|
|
|
if (check_chainloops(newinfo->hook_entry[i],
|
|
|
|
cl_s, udc_cnt, i, newinfo->entries)) {
|
2005-06-22 23:12:57 -06:00
|
|
|
vfree(cl_s);
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2007-10-19 15:21:04 -06:00
|
|
|
/* we now know the following (along with E=mc²):
|
2015-10-26 03:10:41 -06:00
|
|
|
* - the nr of entries in each chain is right
|
|
|
|
* - the size of the allocated space is right
|
|
|
|
* - all valid hooks have a corresponding chain
|
|
|
|
* - there are no loops
|
|
|
|
* - wrong data can still be on the level of a single entry
|
|
|
|
* - could be there are jumps to places that are not the
|
|
|
|
* beginning of a chain. This can only occur in chains that
|
|
|
|
* are not accessible from any base chains, so we don't care.
|
|
|
|
*/
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/* used to know what we need to clean up if something goes wrong */
|
|
|
|
i = 0;
|
|
|
|
ret = EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
|
2010-01-18 00:21:13 -07:00
|
|
|
ebt_check_entry, net, newinfo, name, &i, cl_s, udc_cnt);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (ret != 0) {
|
|
|
|
EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
|
2010-01-18 00:25:47 -07:00
|
|
|
ebt_cleanup_entry, net, &i);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2005-06-22 23:12:57 -06:00
|
|
|
vfree(cl_s);
|
2005-04-16 16:20:36 -06:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* called under write_lock */
|
2009-06-25 23:51:59 -06:00
|
|
|
static void get_counters(const struct ebt_counter *oldcounters,
|
2015-10-26 03:10:40 -06:00
|
|
|
struct ebt_counter *counters, unsigned int nentries)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
int i, cpu;
|
|
|
|
struct ebt_counter *counter_base;
|
|
|
|
|
|
|
|
/* counters of cpu 0 */
|
|
|
|
memcpy(counters, oldcounters,
|
2005-10-13 15:41:23 -06:00
|
|
|
sizeof(struct ebt_counter) * nentries);
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
/* add other counters to those of cpu 0 */
|
2006-04-10 23:52:50 -06:00
|
|
|
for_each_possible_cpu(cpu) {
|
2005-10-13 15:41:23 -06:00
|
|
|
if (cpu == 0)
|
|
|
|
continue;
|
2005-04-16 16:20:36 -06:00
|
|
|
counter_base = COUNTER_BASE(oldcounters, nentries, cpu);
|
2018-03-14 08:36:53 -06:00
|
|
|
for (i = 0; i < nentries; i++)
|
2019-08-14 08:54:16 -06:00
|
|
|
ADD_COUNTER(counters[i], counter_base[i].bcnt,
|
|
|
|
counter_base[i].pcnt);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-04 10:38:53 -07:00
|
|
|
static int do_replace_finish(struct net *net, struct ebt_replace *repl,
|
|
|
|
struct ebt_table_info *newinfo)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2018-04-08 09:00:21 -06:00
|
|
|
int ret;
|
2005-04-16 16:20:36 -06:00
|
|
|
struct ebt_counter *counterstmp = NULL;
|
|
|
|
/* used to be able to unlock earlier */
|
|
|
|
struct ebt_table_info *table;
|
2010-02-04 10:38:53 -07:00
|
|
|
struct ebt_table *t;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/* the user wants counters back
|
2015-10-26 03:10:41 -06:00
|
|
|
* the check on the size is done later, when we have the lock
|
|
|
|
*/
|
2010-02-04 10:38:53 -07:00
|
|
|
if (repl->num_counters) {
|
|
|
|
unsigned long size = repl->num_counters * sizeof(*counterstmp);
|
|
|
|
counterstmp = vmalloc(size);
|
|
|
|
if (!counterstmp)
|
|
|
|
return -ENOMEM;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
newinfo->chainstack = NULL;
|
2010-02-04 10:38:53 -07:00
|
|
|
ret = ebt_verify_pointers(repl, newinfo);
|
2006-11-30 20:28:08 -07:00
|
|
|
if (ret != 0)
|
|
|
|
goto free_counterstmp;
|
|
|
|
|
2010-02-04 10:38:53 -07:00
|
|
|
ret = translate_table(net, repl->name, newinfo);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
goto free_counterstmp;
|
|
|
|
|
2010-02-04 10:38:53 -07:00
|
|
|
t = find_table_lock(net, repl->name, &ret, &ebt_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (!t) {
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto free_iterate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the table doesn't like it */
|
2010-02-04 10:38:53 -07:00
|
|
|
if (t->check && (ret = t->check(newinfo, repl->valid_hooks)))
|
2005-04-16 16:20:36 -06:00
|
|
|
goto free_unlock;
|
|
|
|
|
2010-02-04 10:38:53 -07:00
|
|
|
if (repl->num_counters && repl->num_counters != t->private->nentries) {
|
2005-04-16 16:20:36 -06:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto free_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we have the mutex lock, so no danger in reading this pointer */
|
|
|
|
table = t->private;
|
|
|
|
/* make sure the table can only be rmmod'ed if it contains no rules */
|
|
|
|
if (!table->nentries && newinfo->nentries && !try_module_get(t->me)) {
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto free_unlock;
|
|
|
|
} else if (table->nentries && !newinfo->nentries)
|
|
|
|
module_put(t->me);
|
|
|
|
/* we need an atomic snapshot of the counters */
|
|
|
|
write_lock_bh(&t->lock);
|
2010-02-04 10:38:53 -07:00
|
|
|
if (repl->num_counters)
|
2005-04-16 16:20:36 -06:00
|
|
|
get_counters(t->private->counters, counterstmp,
|
|
|
|
t->private->nentries);
|
|
|
|
|
|
|
|
t->private = newinfo;
|
|
|
|
write_unlock_bh(&t->lock);
|
2006-03-20 23:35:41 -07:00
|
|
|
mutex_unlock(&ebt_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
/* so, a user can change the chains while having messed up her counter
|
2015-10-26 03:10:41 -06:00
|
|
|
* allocation. Only reason why this is done is because this way the lock
|
|
|
|
* is held only once, while this doesn't bring the kernel into a
|
|
|
|
* dangerous state.
|
|
|
|
*/
|
2010-02-04 10:38:53 -07:00
|
|
|
if (repl->num_counters &&
|
|
|
|
copy_to_user(repl->counters, counterstmp,
|
|
|
|
repl->num_counters * sizeof(struct ebt_counter))) {
|
2014-04-04 09:57:45 -06:00
|
|
|
/* Silent error, can't fail, new table is already in place */
|
|
|
|
net_warn_ratelimited("ebtables: counters copy to user failed while replacing table\n");
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* decrease module count and free resources */
|
|
|
|
EBT_ENTRY_ITERATE(table->entries, table->entries_size,
|
2010-01-18 00:25:47 -07:00
|
|
|
ebt_cleanup_entry, net, NULL);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
vfree(table->entries);
|
2018-04-08 09:00:21 -06:00
|
|
|
ebt_free_table_info(table);
|
2005-04-16 16:20:36 -06:00
|
|
|
vfree(table);
|
2005-06-22 23:12:57 -06:00
|
|
|
vfree(counterstmp);
|
2014-09-08 06:11:45 -06:00
|
|
|
|
2020-04-22 15:39:28 -06:00
|
|
|
audit_log_nfcfg(repl->name, AF_BRIDGE, repl->nentries,
|
2020-06-27 21:24:19 -06:00
|
|
|
AUDIT_XT_OP_REPLACE, GFP_KERNEL);
|
2005-04-16 16:20:36 -06:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
free_unlock:
|
2006-03-20 23:35:41 -07:00
|
|
|
mutex_unlock(&ebt_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
free_iterate:
|
|
|
|
EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
|
2010-01-18 00:25:47 -07:00
|
|
|
ebt_cleanup_entry, net, NULL);
|
2005-04-16 16:20:36 -06:00
|
|
|
free_counterstmp:
|
2005-06-22 23:12:57 -06:00
|
|
|
vfree(counterstmp);
|
2005-04-16 16:20:36 -06:00
|
|
|
/* can be initialized in translate_table() */
|
2018-04-08 09:00:21 -06:00
|
|
|
ebt_free_table_info(newinfo);
|
2010-02-04 10:38:53 -07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* replace the table */
|
2020-07-23 00:08:54 -06:00
|
|
|
static int do_replace(struct net *net, sockptr_t arg, unsigned int len)
|
2010-02-04 10:38:53 -07:00
|
|
|
{
|
|
|
|
int ret, countersize;
|
|
|
|
struct ebt_table_info *newinfo;
|
|
|
|
struct ebt_replace tmp;
|
|
|
|
|
2020-07-23 00:08:54 -06:00
|
|
|
if (copy_from_sockptr(&tmp, arg, sizeof(tmp)) != 0)
|
2010-02-04 10:38:53 -07:00
|
|
|
return -EFAULT;
|
|
|
|
|
2019-02-18 16:37:21 -07:00
|
|
|
if (len != sizeof(tmp) + tmp.entries_size)
|
2010-02-04 10:38:53 -07:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-02-18 16:37:21 -07:00
|
|
|
if (tmp.entries_size == 0)
|
2010-02-04 10:38:53 -07:00
|
|
|
return -EINVAL;
|
2019-02-18 16:37:21 -07:00
|
|
|
|
2010-02-04 10:38:53 -07:00
|
|
|
/* overflow check */
|
|
|
|
if (tmp.nentries >= ((INT_MAX - sizeof(struct ebt_table_info)) /
|
|
|
|
NR_CPUS - SMP_CACHE_BYTES) / sizeof(struct ebt_counter))
|
|
|
|
return -ENOMEM;
|
|
|
|
if (tmp.num_counters >= INT_MAX / sizeof(struct ebt_counter))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2011-02-14 08:49:23 -07:00
|
|
|
tmp.name[sizeof(tmp.name) - 1] = 0;
|
|
|
|
|
2010-02-04 10:38:53 -07:00
|
|
|
countersize = COUNTER_OFFSET(tmp.nentries) * nr_cpu_ids;
|
2020-06-01 22:51:40 -06:00
|
|
|
newinfo = __vmalloc(sizeof(*newinfo) + countersize, GFP_KERNEL_ACCOUNT);
|
2010-02-04 10:38:53 -07:00
|
|
|
if (!newinfo)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
if (countersize)
|
|
|
|
memset(newinfo->counters, 0, countersize);
|
|
|
|
|
2020-06-01 22:51:40 -06:00
|
|
|
newinfo->entries = __vmalloc(tmp.entries_size, GFP_KERNEL_ACCOUNT);
|
2010-02-04 10:38:53 -07:00
|
|
|
if (!newinfo->entries) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto free_newinfo;
|
|
|
|
}
|
|
|
|
if (copy_from_user(
|
|
|
|
newinfo->entries, tmp.entries, tmp.entries_size) != 0) {
|
|
|
|
ret = -EFAULT;
|
|
|
|
goto free_entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = do_replace_finish(net, &tmp, newinfo);
|
|
|
|
if (ret == 0)
|
|
|
|
return ret;
|
2005-04-16 16:20:36 -06:00
|
|
|
free_entries:
|
2005-06-22 23:12:57 -06:00
|
|
|
vfree(newinfo->entries);
|
2005-04-16 16:20:36 -06:00
|
|
|
free_newinfo:
|
2005-06-22 23:12:57 -06:00
|
|
|
vfree(newinfo);
|
2005-04-16 16:20:36 -06:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-19 16:45:48 -06:00
|
|
|
static void __ebt_unregister_table(struct net *net, struct ebt_table *table)
|
|
|
|
{
|
|
|
|
mutex_lock(&ebt_mutex);
|
|
|
|
list_del(&table->list);
|
|
|
|
mutex_unlock(&ebt_mutex);
|
2020-04-22 15:39:29 -06:00
|
|
|
audit_log_nfcfg(table->name, AF_BRIDGE, table->private->nentries,
|
2020-06-27 21:24:19 -06:00
|
|
|
AUDIT_XT_OP_UNREGISTER, GFP_KERNEL);
|
2017-04-19 16:45:48 -06:00
|
|
|
EBT_ENTRY_ITERATE(table->private->entries, table->private->entries_size,
|
|
|
|
ebt_cleanup_entry, net, NULL);
|
|
|
|
if (table->private->nentries)
|
|
|
|
module_put(table->me);
|
|
|
|
vfree(table->private->entries);
|
2018-04-08 09:00:21 -06:00
|
|
|
ebt_free_table_info(table->private);
|
2017-04-19 16:45:48 -06:00
|
|
|
vfree(table->private);
|
|
|
|
kfree(table);
|
|
|
|
}
|
|
|
|
|
2017-09-26 10:35:45 -06:00
|
|
|
int ebt_register_table(struct net *net, const struct ebt_table *input_table,
|
|
|
|
const struct nf_hook_ops *ops, struct ebt_table **res)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
struct ebt_table_info *newinfo;
|
2009-08-24 06:56:30 -06:00
|
|
|
struct ebt_table *t, *table;
|
2006-11-30 20:28:48 -07:00
|
|
|
struct ebt_replace_kernel *repl;
|
2005-04-16 16:20:36 -06:00
|
|
|
int ret, i, countersize;
|
2006-11-30 20:28:25 -07:00
|
|
|
void *p;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2009-08-24 06:56:30 -06:00
|
|
|
if (input_table == NULL || (repl = input_table->table) == NULL ||
|
2011-01-03 04:08:58 -07:00
|
|
|
repl->entries == NULL || repl->entries_size == 0 ||
|
2019-02-18 16:37:21 -07:00
|
|
|
repl->counters != NULL || input_table->private != NULL)
|
2017-09-26 10:35:45 -06:00
|
|
|
return -EINVAL;
|
2008-11-04 06:27:15 -07:00
|
|
|
|
|
|
|
/* Don't add one table to multiple lists. */
|
2009-08-24 06:56:30 -06:00
|
|
|
table = kmemdup(input_table, sizeof(struct ebt_table), GFP_KERNEL);
|
2008-11-04 06:27:15 -07:00
|
|
|
if (!table) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2007-02-20 14:57:51 -07:00
|
|
|
countersize = COUNTER_OFFSET(repl->nentries) * nr_cpu_ids;
|
2006-04-20 01:14:49 -06:00
|
|
|
newinfo = vmalloc(sizeof(*newinfo) + countersize);
|
2005-04-16 16:20:36 -06:00
|
|
|
ret = -ENOMEM;
|
|
|
|
if (!newinfo)
|
2008-11-04 06:27:15 -07:00
|
|
|
goto free_table;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-11-30 20:28:25 -07:00
|
|
|
p = vmalloc(repl->entries_size);
|
|
|
|
if (!p)
|
2005-04-16 16:20:36 -06:00
|
|
|
goto free_newinfo;
|
|
|
|
|
2006-11-30 20:28:25 -07:00
|
|
|
memcpy(p, repl->entries, repl->entries_size);
|
|
|
|
newinfo->entries = p;
|
|
|
|
|
|
|
|
newinfo->entries_size = repl->entries_size;
|
|
|
|
newinfo->nentries = repl->nentries;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
if (countersize)
|
|
|
|
memset(newinfo->counters, 0, countersize);
|
|
|
|
|
|
|
|
/* fill in newinfo and parse the entries */
|
|
|
|
newinfo->chainstack = NULL;
|
2006-11-30 20:28:25 -07:00
|
|
|
for (i = 0; i < NF_BR_NUMHOOKS; i++) {
|
|
|
|
if ((repl->valid_hooks & (1 << i)) == 0)
|
|
|
|
newinfo->hook_entry[i] = NULL;
|
|
|
|
else
|
|
|
|
newinfo->hook_entry[i] = p +
|
|
|
|
((char *)repl->hook_entry[i] - repl->entries);
|
|
|
|
}
|
2010-01-18 00:21:13 -07:00
|
|
|
ret = translate_table(net, repl->name, newinfo);
|
2019-02-18 16:37:21 -07:00
|
|
|
if (ret != 0)
|
2005-04-16 16:20:36 -06:00
|
|
|
goto free_chainstack;
|
|
|
|
|
|
|
|
if (table->check && table->check(newinfo, table->valid_hooks)) {
|
2011-08-10 19:59:38 -06:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto free_chainstack;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
table->private = newinfo;
|
|
|
|
rwlock_init(&table->lock);
|
2014-07-31 12:38:46 -06:00
|
|
|
mutex_lock(&ebt_mutex);
|
2008-11-04 06:22:55 -07:00
|
|
|
list_for_each_entry(t, &net->xt.tables[NFPROTO_BRIDGE], list) {
|
2006-09-20 12:57:53 -06:00
|
|
|
if (strcmp(t->name, table->name) == 0) {
|
|
|
|
ret = -EEXIST;
|
|
|
|
goto free_unlock;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Hold a reference count if the chains aren't empty */
|
|
|
|
if (newinfo->nentries && !try_module_get(table->me)) {
|
|
|
|
ret = -ENOENT;
|
|
|
|
goto free_unlock;
|
|
|
|
}
|
2008-11-04 06:22:55 -07:00
|
|
|
list_add(&table->list, &net->xt.tables[NFPROTO_BRIDGE]);
|
2006-03-20 23:35:41 -07:00
|
|
|
mutex_unlock(&ebt_mutex);
|
2017-04-19 16:45:48 -06:00
|
|
|
|
2017-09-26 10:35:45 -06:00
|
|
|
WRITE_ONCE(*res, table);
|
2017-04-19 16:45:48 -06:00
|
|
|
ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
|
|
|
|
if (ret) {
|
|
|
|
__ebt_unregister_table(net, table);
|
2017-09-26 10:35:45 -06:00
|
|
|
*res = NULL;
|
2017-04-19 16:45:48 -06:00
|
|
|
}
|
|
|
|
|
2020-04-22 15:39:28 -06:00
|
|
|
audit_log_nfcfg(repl->name, AF_BRIDGE, repl->nentries,
|
2020-06-27 21:24:19 -06:00
|
|
|
AUDIT_XT_OP_REGISTER, GFP_KERNEL);
|
2017-09-26 10:35:45 -06:00
|
|
|
return ret;
|
2005-04-16 16:20:36 -06:00
|
|
|
free_unlock:
|
2006-03-20 23:35:41 -07:00
|
|
|
mutex_unlock(&ebt_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
free_chainstack:
|
2018-04-08 09:00:21 -06:00
|
|
|
ebt_free_table_info(newinfo);
|
2005-04-16 16:20:36 -06:00
|
|
|
vfree(newinfo->entries);
|
|
|
|
free_newinfo:
|
|
|
|
vfree(newinfo);
|
2008-11-04 06:27:15 -07:00
|
|
|
free_table:
|
|
|
|
kfree(table);
|
|
|
|
out:
|
2017-09-26 10:35:45 -06:00
|
|
|
return ret;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2017-04-19 16:45:48 -06:00
|
|
|
void ebt_unregister_table(struct net *net, struct ebt_table *table,
|
|
|
|
const struct nf_hook_ops *ops)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2019-04-11 08:36:42 -06:00
|
|
|
nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
|
2017-04-19 16:45:48 -06:00
|
|
|
__ebt_unregister_table(net, table);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* userspace just supplied us with counters */
|
2010-02-06 18:48:47 -07:00
|
|
|
static int do_update_counters(struct net *net, const char *name,
|
2020-07-23 00:08:52 -06:00
|
|
|
struct ebt_counter __user *counters,
|
|
|
|
unsigned int num_counters, unsigned int len)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
struct ebt_counter *tmp;
|
|
|
|
struct ebt_table *t;
|
|
|
|
|
2010-02-06 18:48:47 -07:00
|
|
|
if (num_counters == 0)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
|
|
|
|
treewide: Use array_size() in vmalloc()
The vmalloc() function has no 2-factor argument form, so multiplication
factors need to be wrapped in array_size(). This patch replaces cases of:
vmalloc(a * b)
with:
vmalloc(array_size(a, b))
as well as handling cases of:
vmalloc(a * b * c)
with:
vmalloc(array3_size(a, b, c))
This does, however, attempt to ignore constant size factors like:
vmalloc(4 * 1024)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
vmalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
vmalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
vmalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
vmalloc(
- sizeof(TYPE) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT_ID
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT_ID
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
vmalloc(
- SIZE * COUNT
+ array_size(COUNT, SIZE)
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
vmalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
vmalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vmalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
vmalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
vmalloc(C1 * C2 * C3, ...)
|
vmalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants.
@@
expression E1, E2;
constant C1, C2;
@@
(
vmalloc(C1 * C2, ...)
|
vmalloc(
- E1 * E2
+ array_size(E1, E2)
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 15:27:11 -06:00
|
|
|
tmp = vmalloc(array_size(num_counters, sizeof(*tmp)));
|
2010-02-06 18:48:47 -07:00
|
|
|
if (!tmp)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2010-02-06 18:48:47 -07:00
|
|
|
t = find_table_lock(net, name, &ret, &ebt_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (!t)
|
|
|
|
goto free_tmp;
|
|
|
|
|
2010-02-06 18:48:47 -07:00
|
|
|
if (num_counters != t->private->nentries) {
|
2005-04-16 16:20:36 -06:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto unlock_mutex;
|
|
|
|
}
|
|
|
|
|
2010-02-06 18:48:47 -07:00
|
|
|
if (copy_from_user(tmp, counters, num_counters * sizeof(*counters))) {
|
2005-04-16 16:20:36 -06:00
|
|
|
ret = -EFAULT;
|
|
|
|
goto unlock_mutex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we want an atomic add of the counters */
|
|
|
|
write_lock_bh(&t->lock);
|
|
|
|
|
|
|
|
/* we add to the counters of the first cpu */
|
2018-03-14 08:36:53 -06:00
|
|
|
for (i = 0; i < num_counters; i++)
|
2019-08-14 08:54:16 -06:00
|
|
|
ADD_COUNTER(t->private->counters[i], tmp[i].bcnt, tmp[i].pcnt);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
write_unlock_bh(&t->lock);
|
|
|
|
ret = 0;
|
|
|
|
unlock_mutex:
|
2006-03-20 23:35:41 -07:00
|
|
|
mutex_unlock(&ebt_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
free_tmp:
|
|
|
|
vfree(tmp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-07-23 00:08:54 -06:00
|
|
|
static int update_counters(struct net *net, sockptr_t arg, unsigned int len)
|
2010-02-06 18:48:47 -07:00
|
|
|
{
|
|
|
|
struct ebt_replace hlp;
|
|
|
|
|
2020-07-23 00:08:54 -06:00
|
|
|
if (copy_from_sockptr(&hlp, arg, sizeof(hlp)))
|
2010-02-06 18:48:47 -07:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (len != sizeof(hlp) + hlp.num_counters * sizeof(struct ebt_counter))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return do_update_counters(net, hlp.name, hlp.counters,
|
2020-07-23 00:08:52 -06:00
|
|
|
hlp.num_counters, len);
|
2010-02-06 18:48:47 -07:00
|
|
|
}
|
|
|
|
|
2017-01-02 15:19:44 -07:00
|
|
|
static inline int ebt_obj_to_user(char __user *um, const char *_name,
|
|
|
|
const char *data, int entrysize,
|
2018-03-20 20:42:15 -06:00
|
|
|
int usersize, int datasize, u8 revision)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2018-03-20 20:42:15 -06:00
|
|
|
char name[EBT_EXTENSION_MAXNAMELEN] = {0};
|
2012-03-05 18:22:50 -07:00
|
|
|
|
2018-03-20 20:42:15 -06:00
|
|
|
/* ebtables expects 31 bytes long names but xt_match names are 29 bytes
|
2015-10-26 03:10:41 -06:00
|
|
|
* long. Copy 29 bytes and fill remaining bytes with zeroes.
|
|
|
|
*/
|
2017-01-02 15:19:44 -07:00
|
|
|
strlcpy(name, _name, sizeof(name));
|
2018-03-20 20:42:15 -06:00
|
|
|
if (copy_to_user(um, name, EBT_EXTENSION_MAXNAMELEN) ||
|
|
|
|
put_user(revision, (u8 __user *)(um + EBT_EXTENSION_MAXNAMELEN)) ||
|
|
|
|
put_user(datasize, (int __user *)(um + EBT_EXTENSION_MAXNAMELEN + 1)) ||
|
2017-05-09 14:17:37 -06:00
|
|
|
xt_data_to_user(um + entrysize, data, usersize, datasize,
|
|
|
|
XT_ALIGN(datasize)))
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EFAULT;
|
2017-01-02 15:19:44 -07:00
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-02 15:19:44 -07:00
|
|
|
static inline int ebt_match_to_user(const struct ebt_entry_match *m,
|
|
|
|
const char *base, char __user *ubase)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2017-01-02 15:19:44 -07:00
|
|
|
return ebt_obj_to_user(ubase + ((char *)m - base),
|
|
|
|
m->u.match->name, m->data, sizeof(*m),
|
2018-03-20 20:42:15 -06:00
|
|
|
m->u.match->usersize, m->match_size,
|
|
|
|
m->u.match->revision);
|
2017-01-02 15:19:44 -07:00
|
|
|
}
|
2012-03-05 18:22:50 -07:00
|
|
|
|
2017-01-02 15:19:44 -07:00
|
|
|
static inline int ebt_watcher_to_user(const struct ebt_entry_watcher *w,
|
|
|
|
const char *base, char __user *ubase)
|
|
|
|
{
|
|
|
|
return ebt_obj_to_user(ubase + ((char *)w - base),
|
|
|
|
w->u.watcher->name, w->data, sizeof(*w),
|
2018-03-20 20:42:15 -06:00
|
|
|
w->u.watcher->usersize, w->watcher_size,
|
|
|
|
w->u.watcher->revision);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2017-01-02 15:19:44 -07:00
|
|
|
static inline int ebt_entry_to_user(struct ebt_entry *e, const char *base,
|
|
|
|
char __user *ubase)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
int ret;
|
2006-11-30 20:28:48 -07:00
|
|
|
char __user *hlp;
|
2009-06-25 23:51:59 -06:00
|
|
|
const struct ebt_entry_target *t;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2017-01-02 15:19:44 -07:00
|
|
|
if (e->bitmask == 0) {
|
|
|
|
/* special case !EBT_ENTRY_OR_ENTRIES */
|
|
|
|
if (copy_to_user(ubase + ((char *)e - base), e,
|
|
|
|
sizeof(struct ebt_entries)))
|
|
|
|
return -EFAULT;
|
2005-04-16 16:20:36 -06:00
|
|
|
return 0;
|
2017-01-02 15:19:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (copy_to_user(ubase + ((char *)e - base), e, sizeof(*e)))
|
|
|
|
return -EFAULT;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2006-11-30 20:28:48 -07:00
|
|
|
hlp = ubase + (((char *)e + e->target_offset) - base);
|
2018-04-13 08:09:58 -06:00
|
|
|
t = ebt_get_target_c(e);
|
2007-02-09 07:24:35 -07:00
|
|
|
|
2017-01-02 15:19:44 -07:00
|
|
|
ret = EBT_MATCH_ITERATE(e, ebt_match_to_user, base, ubase);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
2017-01-02 15:19:44 -07:00
|
|
|
ret = EBT_WATCHER_ITERATE(e, ebt_watcher_to_user, base, ubase);
|
2005-04-16 16:20:36 -06:00
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
2017-01-02 15:19:44 -07:00
|
|
|
ret = ebt_obj_to_user(hlp, t->u.target->name, t->data, sizeof(*t),
|
2018-03-20 20:42:15 -06:00
|
|
|
t->u.target->usersize, t->target_size,
|
|
|
|
t->u.target->revision);
|
2017-01-02 15:19:44 -07:00
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
|
2005-04-16 16:20:36 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-06 18:11:34 -07:00
|
|
|
static int copy_counters_to_user(struct ebt_table *t,
|
2015-10-26 03:10:40 -06:00
|
|
|
const struct ebt_counter *oldcounters,
|
|
|
|
void __user *user, unsigned int num_counters,
|
|
|
|
unsigned int nentries)
|
2010-02-06 18:11:34 -07:00
|
|
|
{
|
|
|
|
struct ebt_counter *counterstmp;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* userspace might not need the counters */
|
|
|
|
if (num_counters == 0)
|
|
|
|
return 0;
|
|
|
|
|
2019-02-18 16:37:21 -07:00
|
|
|
if (num_counters != nentries)
|
2010-02-06 18:11:34 -07:00
|
|
|
return -EINVAL;
|
|
|
|
|
treewide: Use array_size() in vmalloc()
The vmalloc() function has no 2-factor argument form, so multiplication
factors need to be wrapped in array_size(). This patch replaces cases of:
vmalloc(a * b)
with:
vmalloc(array_size(a, b))
as well as handling cases of:
vmalloc(a * b * c)
with:
vmalloc(array3_size(a, b, c))
This does, however, attempt to ignore constant size factors like:
vmalloc(4 * 1024)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
vmalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
vmalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
vmalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
vmalloc(
- sizeof(TYPE) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT_ID
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT_ID
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
vmalloc(
- SIZE * COUNT
+ array_size(COUNT, SIZE)
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
vmalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
vmalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vmalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
vmalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
vmalloc(C1 * C2 * C3, ...)
|
vmalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants.
@@
expression E1, E2;
constant C1, C2;
@@
(
vmalloc(C1 * C2, ...)
|
vmalloc(
- E1 * E2
+ array_size(E1, E2)
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 15:27:11 -06:00
|
|
|
counterstmp = vmalloc(array_size(nentries, sizeof(*counterstmp)));
|
2010-02-06 18:11:34 -07:00
|
|
|
if (!counterstmp)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
write_lock_bh(&t->lock);
|
|
|
|
get_counters(oldcounters, counterstmp, nentries);
|
|
|
|
write_unlock_bh(&t->lock);
|
|
|
|
|
|
|
|
if (copy_to_user(user, counterstmp,
|
|
|
|
nentries * sizeof(struct ebt_counter)))
|
|
|
|
ret = -EFAULT;
|
|
|
|
vfree(counterstmp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-03-20 23:35:41 -07:00
|
|
|
/* called with ebt_mutex locked */
|
2005-04-16 16:20:36 -06:00
|
|
|
static int copy_everything_to_user(struct ebt_table *t, void __user *user,
|
2015-10-26 03:10:40 -06:00
|
|
|
const int *len, int cmd)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
struct ebt_replace tmp;
|
2009-06-25 23:51:59 -06:00
|
|
|
const struct ebt_counter *oldcounters;
|
2005-04-16 16:20:36 -06:00
|
|
|
unsigned int entries_size, nentries;
|
2010-02-06 18:11:34 -07:00
|
|
|
int ret;
|
2005-04-16 16:20:36 -06:00
|
|
|
char *entries;
|
|
|
|
|
|
|
|
if (cmd == EBT_SO_GET_ENTRIES) {
|
|
|
|
entries_size = t->private->entries_size;
|
|
|
|
nentries = t->private->nentries;
|
|
|
|
entries = t->private->entries;
|
|
|
|
oldcounters = t->private->counters;
|
|
|
|
} else {
|
|
|
|
entries_size = t->table->entries_size;
|
|
|
|
nentries = t->table->nentries;
|
|
|
|
entries = t->table->entries;
|
|
|
|
oldcounters = t->table->counters;
|
|
|
|
}
|
|
|
|
|
2010-02-06 19:19:12 -07:00
|
|
|
if (copy_from_user(&tmp, user, sizeof(tmp)))
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (*len != sizeof(struct ebt_replace) + entries_size +
|
2013-12-18 22:28:12 -07:00
|
|
|
(tmp.num_counters ? nentries * sizeof(struct ebt_counter) : 0))
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-02-18 16:37:21 -07:00
|
|
|
if (tmp.nentries != nentries)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-02-18 16:37:21 -07:00
|
|
|
if (tmp.entries_size != entries_size)
|
2005-04-16 16:20:36 -06:00
|
|
|
return -EINVAL;
|
|
|
|
|
2010-02-06 18:11:34 -07:00
|
|
|
ret = copy_counters_to_user(t, oldcounters, tmp.counters,
|
|
|
|
tmp.num_counters, nentries);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/* set the match/watcher/target names right */
|
|
|
|
return EBT_ENTRY_ITERATE(entries, entries_size,
|
2017-01-02 15:19:44 -07:00
|
|
|
ebt_entry_to_user, entries, tmp.entries);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2010-01-05 08:09:46 -07:00
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
/* 32 bit-userspace compatibility definitions. */
|
|
|
|
struct compat_ebt_replace {
|
|
|
|
char name[EBT_TABLE_MAXNAMELEN];
|
|
|
|
compat_uint_t valid_hooks;
|
|
|
|
compat_uint_t nentries;
|
|
|
|
compat_uint_t entries_size;
|
|
|
|
/* start of the chains */
|
|
|
|
compat_uptr_t hook_entry[NF_BR_NUMHOOKS];
|
|
|
|
/* nr of counters userspace expects back */
|
|
|
|
compat_uint_t num_counters;
|
|
|
|
/* where the kernel will put the old counters. */
|
|
|
|
compat_uptr_t counters;
|
|
|
|
compat_uptr_t entries;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* struct ebt_entry_match, _target and _watcher have same layout */
|
|
|
|
struct compat_ebt_entry_mwt {
|
|
|
|
union {
|
2018-03-20 20:42:15 -06:00
|
|
|
struct {
|
|
|
|
char name[EBT_EXTENSION_MAXNAMELEN];
|
|
|
|
u8 revision;
|
|
|
|
};
|
2010-01-05 08:09:46 -07:00
|
|
|
compat_uptr_t ptr;
|
|
|
|
} u;
|
|
|
|
compat_uint_t match_size;
|
2020-02-20 06:59:14 -07:00
|
|
|
compat_uint_t data[] __aligned(__alignof__(struct compat_ebt_replace));
|
2010-01-05 08:09:46 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/* account for possible padding between match_size and ->data */
|
|
|
|
static int ebt_compat_entry_padsize(void)
|
|
|
|
{
|
2018-05-30 07:19:36 -06:00
|
|
|
BUILD_BUG_ON(sizeof(struct ebt_entry_match) <
|
|
|
|
sizeof(struct compat_ebt_entry_mwt));
|
|
|
|
return (int) sizeof(struct ebt_entry_match) -
|
|
|
|
sizeof(struct compat_ebt_entry_mwt);
|
2010-01-05 08:09:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ebt_compat_match_offset(const struct xt_match *match,
|
|
|
|
unsigned int userlen)
|
|
|
|
{
|
2015-10-26 03:10:41 -06:00
|
|
|
/* ebt_among needs special handling. The kernel .matchsize is
|
2010-01-05 08:09:46 -07:00
|
|
|
* set to -1 at registration time; at runtime an EBT_ALIGN()ed
|
|
|
|
* value is expected.
|
|
|
|
* Example: userspace sends 4500, ebt_among.c wants 4504.
|
|
|
|
*/
|
|
|
|
if (unlikely(match->matchsize == -1))
|
|
|
|
return XT_ALIGN(userlen) - COMPAT_XT_ALIGN(userlen);
|
|
|
|
return xt_compat_match_offset(match);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compat_match_to_user(struct ebt_entry_match *m, void __user **dstptr,
|
|
|
|
unsigned int *size)
|
|
|
|
{
|
|
|
|
const struct xt_match *match = m->u.match;
|
|
|
|
struct compat_ebt_entry_mwt __user *cm = *dstptr;
|
|
|
|
int off = ebt_compat_match_offset(match, m->match_size);
|
|
|
|
compat_uint_t msize = m->match_size - off;
|
|
|
|
|
2018-02-18 17:24:53 -07:00
|
|
|
if (WARN_ON(off >= m->match_size))
|
|
|
|
return -EINVAL;
|
2010-01-05 08:09:46 -07:00
|
|
|
|
2018-03-20 20:42:15 -06:00
|
|
|
if (copy_to_user(cm->u.name, match->name, strlen(match->name) + 1) ||
|
|
|
|
put_user(match->revision, &cm->u.revision) ||
|
|
|
|
put_user(msize, &cm->match_size))
|
2010-01-05 08:09:46 -07:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (match->compat_to_user) {
|
|
|
|
if (match->compat_to_user(cm->data, m->data))
|
|
|
|
return -EFAULT;
|
2017-01-02 15:19:44 -07:00
|
|
|
} else {
|
2017-05-09 14:17:37 -06:00
|
|
|
if (xt_data_to_user(cm->data, m->data, match->usersize, msize,
|
|
|
|
COMPAT_XT_ALIGN(msize)))
|
2010-01-05 08:09:46 -07:00
|
|
|
return -EFAULT;
|
2017-01-02 15:19:44 -07:00
|
|
|
}
|
2010-01-05 08:09:46 -07:00
|
|
|
|
|
|
|
*size -= ebt_compat_entry_padsize() + off;
|
|
|
|
*dstptr = cm->data;
|
|
|
|
*dstptr += msize;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compat_target_to_user(struct ebt_entry_target *t,
|
|
|
|
void __user **dstptr,
|
|
|
|
unsigned int *size)
|
|
|
|
{
|
|
|
|
const struct xt_target *target = t->u.target;
|
|
|
|
struct compat_ebt_entry_mwt __user *cm = *dstptr;
|
|
|
|
int off = xt_compat_target_offset(target);
|
|
|
|
compat_uint_t tsize = t->target_size - off;
|
|
|
|
|
2018-02-18 17:24:53 -07:00
|
|
|
if (WARN_ON(off >= t->target_size))
|
|
|
|
return -EINVAL;
|
2010-01-05 08:09:46 -07:00
|
|
|
|
2018-03-20 20:42:15 -06:00
|
|
|
if (copy_to_user(cm->u.name, target->name, strlen(target->name) + 1) ||
|
|
|
|
put_user(target->revision, &cm->u.revision) ||
|
|
|
|
put_user(tsize, &cm->match_size))
|
2010-01-05 08:09:46 -07:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (target->compat_to_user) {
|
|
|
|
if (target->compat_to_user(cm->data, t->data))
|
|
|
|
return -EFAULT;
|
2017-01-02 15:19:44 -07:00
|
|
|
} else {
|
2017-05-09 14:17:37 -06:00
|
|
|
if (xt_data_to_user(cm->data, t->data, target->usersize, tsize,
|
|
|
|
COMPAT_XT_ALIGN(tsize)))
|
2017-01-02 15:19:44 -07:00
|
|
|
return -EFAULT;
|
|
|
|
}
|
2010-01-05 08:09:46 -07:00
|
|
|
|
|
|
|
*size -= ebt_compat_entry_padsize() + off;
|
|
|
|
*dstptr = cm->data;
|
|
|
|
*dstptr += tsize;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compat_watcher_to_user(struct ebt_entry_watcher *w,
|
|
|
|
void __user **dstptr,
|
|
|
|
unsigned int *size)
|
|
|
|
{
|
|
|
|
return compat_target_to_user((struct ebt_entry_target *)w,
|
|
|
|
dstptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compat_copy_entry_to_user(struct ebt_entry *e, void __user **dstptr,
|
|
|
|
unsigned int *size)
|
|
|
|
{
|
|
|
|
struct ebt_entry_target *t;
|
|
|
|
struct ebt_entry __user *ce;
|
|
|
|
u32 watchers_offset, target_offset, next_offset;
|
|
|
|
compat_uint_t origsize;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (e->bitmask == 0) {
|
|
|
|
if (*size < sizeof(struct ebt_entries))
|
|
|
|
return -EINVAL;
|
|
|
|
if (copy_to_user(*dstptr, e, sizeof(struct ebt_entries)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
*dstptr += sizeof(struct ebt_entries);
|
|
|
|
*size -= sizeof(struct ebt_entries);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*size < sizeof(*ce))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2017-03-28 13:05:16 -06:00
|
|
|
ce = *dstptr;
|
2010-01-05 08:09:46 -07:00
|
|
|
if (copy_to_user(ce, e, sizeof(*ce)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
origsize = *size;
|
|
|
|
*dstptr += sizeof(*ce);
|
|
|
|
|
|
|
|
ret = EBT_MATCH_ITERATE(e, compat_match_to_user, dstptr, size);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
watchers_offset = e->watchers_offset - (origsize - *size);
|
|
|
|
|
|
|
|
ret = EBT_WATCHER_ITERATE(e, compat_watcher_to_user, dstptr, size);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
target_offset = e->target_offset - (origsize - *size);
|
|
|
|
|
2018-04-13 08:09:58 -06:00
|
|
|
t = ebt_get_target(e);
|
2010-01-05 08:09:46 -07:00
|
|
|
|
|
|
|
ret = compat_target_to_user(t, dstptr, size);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
next_offset = e->next_offset - (origsize - *size);
|
|
|
|
|
|
|
|
if (put_user(watchers_offset, &ce->watchers_offset) ||
|
|
|
|
put_user(target_offset, &ce->target_offset) ||
|
|
|
|
put_user(next_offset, &ce->next_offset))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
*size -= sizeof(*ce);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compat_calc_match(struct ebt_entry_match *m, int *off)
|
|
|
|
{
|
|
|
|
*off += ebt_compat_match_offset(m->u.match, m->match_size);
|
|
|
|
*off += ebt_compat_entry_padsize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compat_calc_watcher(struct ebt_entry_watcher *w, int *off)
|
|
|
|
{
|
|
|
|
*off += xt_compat_target_offset(w->u.watcher);
|
|
|
|
*off += ebt_compat_entry_padsize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compat_calc_entry(const struct ebt_entry *e,
|
|
|
|
const struct ebt_table_info *info,
|
|
|
|
const void *base,
|
|
|
|
struct compat_ebt_replace *newinfo)
|
|
|
|
{
|
|
|
|
const struct ebt_entry_target *t;
|
|
|
|
unsigned int entry_offset;
|
|
|
|
int off, ret, i;
|
|
|
|
|
|
|
|
if (e->bitmask == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
off = 0;
|
|
|
|
entry_offset = (void *)e - base;
|
|
|
|
|
|
|
|
EBT_MATCH_ITERATE(e, compat_calc_match, &off);
|
|
|
|
EBT_WATCHER_ITERATE(e, compat_calc_watcher, &off);
|
|
|
|
|
2018-04-13 08:09:58 -06:00
|
|
|
t = ebt_get_target_c(e);
|
2010-01-05 08:09:46 -07:00
|
|
|
|
|
|
|
off += xt_compat_target_offset(t->u.target);
|
|
|
|
off += ebt_compat_entry_padsize();
|
|
|
|
|
|
|
|
newinfo->entries_size -= off;
|
|
|
|
|
|
|
|
ret = xt_compat_add_offset(NFPROTO_BRIDGE, entry_offset, off);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
for (i = 0; i < NF_BR_NUMHOOKS; i++) {
|
|
|
|
const void *hookptr = info->hook_entry[i];
|
|
|
|
if (info->hook_entry[i] &&
|
|
|
|
(e < (struct ebt_entry *)(base - hookptr))) {
|
|
|
|
newinfo->hook_entry[i] -= off;
|
|
|
|
pr_debug("0x%08X -> 0x%08X\n",
|
|
|
|
newinfo->hook_entry[i] + off,
|
|
|
|
newinfo->hook_entry[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-29 09:58:10 -06:00
|
|
|
static int ebt_compat_init_offsets(unsigned int number)
|
|
|
|
{
|
|
|
|
if (number > INT_MAX)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* also count the base chain policies */
|
|
|
|
number += NF_BR_NUMHOOKS;
|
|
|
|
|
|
|
|
return xt_compat_init_offsets(NFPROTO_BRIDGE, number);
|
|
|
|
}
|
2010-01-05 08:09:46 -07:00
|
|
|
|
|
|
|
static int compat_table_info(const struct ebt_table_info *info,
|
|
|
|
struct compat_ebt_replace *newinfo)
|
|
|
|
{
|
|
|
|
unsigned int size = info->entries_size;
|
|
|
|
const void *entries = info->entries;
|
2019-07-29 09:58:10 -06:00
|
|
|
int ret;
|
2010-01-05 08:09:46 -07:00
|
|
|
|
|
|
|
newinfo->entries_size = size;
|
2019-07-29 09:58:10 -06:00
|
|
|
ret = ebt_compat_init_offsets(info->nentries);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2018-02-27 11:42:34 -07:00
|
|
|
|
2010-01-05 08:09:46 -07:00
|
|
|
return EBT_ENTRY_ITERATE(entries, size, compat_calc_entry, info,
|
|
|
|
entries, newinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compat_copy_everything_to_user(struct ebt_table *t,
|
|
|
|
void __user *user, int *len, int cmd)
|
|
|
|
{
|
|
|
|
struct compat_ebt_replace repl, tmp;
|
|
|
|
struct ebt_counter *oldcounters;
|
|
|
|
struct ebt_table_info tinfo;
|
|
|
|
int ret;
|
|
|
|
void __user *pos;
|
|
|
|
|
|
|
|
memset(&tinfo, 0, sizeof(tinfo));
|
|
|
|
|
|
|
|
if (cmd == EBT_SO_GET_ENTRIES) {
|
|
|
|
tinfo.entries_size = t->private->entries_size;
|
|
|
|
tinfo.nentries = t->private->nentries;
|
|
|
|
tinfo.entries = t->private->entries;
|
|
|
|
oldcounters = t->private->counters;
|
|
|
|
} else {
|
|
|
|
tinfo.entries_size = t->table->entries_size;
|
|
|
|
tinfo.nentries = t->table->nentries;
|
|
|
|
tinfo.entries = t->table->entries;
|
|
|
|
oldcounters = t->table->counters;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (copy_from_user(&tmp, user, sizeof(tmp)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (tmp.nentries != tinfo.nentries ||
|
|
|
|
(tmp.num_counters && tmp.num_counters != tinfo.nentries))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
memcpy(&repl, &tmp, sizeof(repl));
|
|
|
|
if (cmd == EBT_SO_GET_ENTRIES)
|
|
|
|
ret = compat_table_info(t->private, &repl);
|
|
|
|
else
|
|
|
|
ret = compat_table_info(&tinfo, &repl);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (*len != sizeof(tmp) + repl.entries_size +
|
|
|
|
(tmp.num_counters? tinfo.nentries * sizeof(struct ebt_counter): 0)) {
|
|
|
|
pr_err("wrong size: *len %d, entries_size %u, replsz %d\n",
|
|
|
|
*len, tinfo.entries_size, repl.entries_size);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* userspace might not need the counters */
|
|
|
|
ret = copy_counters_to_user(t, oldcounters, compat_ptr(tmp.counters),
|
|
|
|
tmp.num_counters, tinfo.nentries);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
pos = compat_ptr(tmp.entries);
|
|
|
|
return EBT_ENTRY_ITERATE(tinfo.entries, tinfo.entries_size,
|
|
|
|
compat_copy_entry_to_user, &pos, &tmp.entries_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ebt_entries_buf_state {
|
|
|
|
char *buf_kern_start; /* kernel buffer to copy (translated) data to */
|
|
|
|
u32 buf_kern_len; /* total size of kernel buffer */
|
|
|
|
u32 buf_kern_offset; /* amount of data copied so far */
|
|
|
|
u32 buf_user_offset; /* read position in userspace buffer */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int ebt_buf_count(struct ebt_entries_buf_state *state, unsigned int sz)
|
|
|
|
{
|
|
|
|
state->buf_kern_offset += sz;
|
|
|
|
return state->buf_kern_offset >= sz ? 0 : -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ebt_buf_add(struct ebt_entries_buf_state *state,
|
2019-12-14 19:49:25 -07:00
|
|
|
const void *data, unsigned int sz)
|
2010-01-05 08:09:46 -07:00
|
|
|
{
|
|
|
|
if (state->buf_kern_start == NULL)
|
|
|
|
goto count_only;
|
|
|
|
|
2018-02-18 17:24:53 -07:00
|
|
|
if (WARN_ON(state->buf_kern_offset + sz > state->buf_kern_len))
|
|
|
|
return -EINVAL;
|
2010-01-05 08:09:46 -07:00
|
|
|
|
|
|
|
memcpy(state->buf_kern_start + state->buf_kern_offset, data, sz);
|
|
|
|
|
|
|
|
count_only:
|
|
|
|
state->buf_user_offset += sz;
|
|
|
|
return ebt_buf_count(state, sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ebt_buf_add_pad(struct ebt_entries_buf_state *state, unsigned int sz)
|
|
|
|
{
|
|
|
|
char *b = state->buf_kern_start;
|
|
|
|
|
2018-02-18 17:24:53 -07:00
|
|
|
if (WARN_ON(b && state->buf_kern_offset > state->buf_kern_len))
|
|
|
|
return -EINVAL;
|
2010-01-05 08:09:46 -07:00
|
|
|
|
|
|
|
if (b != NULL && sz > 0)
|
|
|
|
memset(b + state->buf_kern_offset, 0, sz);
|
|
|
|
/* do not adjust ->buf_user_offset here, we added kernel-side padding */
|
|
|
|
return ebt_buf_count(state, sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum compat_mwt {
|
|
|
|
EBT_COMPAT_MATCH,
|
|
|
|
EBT_COMPAT_WATCHER,
|
|
|
|
EBT_COMPAT_TARGET,
|
|
|
|
};
|
|
|
|
|
2019-12-14 19:49:25 -07:00
|
|
|
static int compat_mtw_from_user(const struct compat_ebt_entry_mwt *mwt,
|
2010-01-05 08:09:46 -07:00
|
|
|
enum compat_mwt compat_mwt,
|
|
|
|
struct ebt_entries_buf_state *state,
|
|
|
|
const unsigned char *base)
|
|
|
|
{
|
2018-03-20 20:42:15 -06:00
|
|
|
char name[EBT_EXTENSION_MAXNAMELEN];
|
2010-01-05 08:09:46 -07:00
|
|
|
struct xt_match *match;
|
|
|
|
struct xt_target *wt;
|
|
|
|
void *dst = NULL;
|
2011-04-21 02:58:25 -06:00
|
|
|
int off, pad = 0;
|
2011-05-19 16:14:39 -06:00
|
|
|
unsigned int size_kern, match_size = mwt->match_size;
|
2010-01-05 08:09:46 -07:00
|
|
|
|
2018-04-27 02:45:31 -06:00
|
|
|
if (strscpy(name, mwt->u.name, sizeof(name)) < 0)
|
|
|
|
return -EINVAL;
|
2010-01-05 08:09:46 -07:00
|
|
|
|
|
|
|
if (state->buf_kern_start)
|
|
|
|
dst = state->buf_kern_start + state->buf_kern_offset;
|
|
|
|
|
|
|
|
switch (compat_mwt) {
|
|
|
|
case EBT_COMPAT_MATCH:
|
2018-03-20 20:42:15 -06:00
|
|
|
match = xt_request_find_match(NFPROTO_BRIDGE, name,
|
|
|
|
mwt->u.revision);
|
2010-01-05 08:09:46 -07:00
|
|
|
if (IS_ERR(match))
|
|
|
|
return PTR_ERR(match);
|
|
|
|
|
|
|
|
off = ebt_compat_match_offset(match, match_size);
|
|
|
|
if (dst) {
|
|
|
|
if (match->compat_from_user)
|
|
|
|
match->compat_from_user(dst, mwt->data);
|
|
|
|
else
|
|
|
|
memcpy(dst, mwt->data, match_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_kern = match->matchsize;
|
|
|
|
if (unlikely(size_kern == -1))
|
|
|
|
size_kern = match_size;
|
|
|
|
module_put(match->me);
|
|
|
|
break;
|
2020-07-08 14:09:39 -06:00
|
|
|
case EBT_COMPAT_WATCHER:
|
2010-01-05 08:09:46 -07:00
|
|
|
case EBT_COMPAT_TARGET:
|
2018-03-20 20:42:15 -06:00
|
|
|
wt = xt_request_find_target(NFPROTO_BRIDGE, name,
|
|
|
|
mwt->u.revision);
|
2010-01-05 08:09:46 -07:00
|
|
|
if (IS_ERR(wt))
|
|
|
|
return PTR_ERR(wt);
|
|
|
|
off = xt_compat_target_offset(wt);
|
|
|
|
|
|
|
|
if (dst) {
|
|
|
|
if (wt->compat_from_user)
|
|
|
|
wt->compat_from_user(dst, mwt->data);
|
|
|
|
else
|
|
|
|
memcpy(dst, mwt->data, match_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_kern = wt->targetsize;
|
|
|
|
module_put(wt->me);
|
|
|
|
break;
|
2011-05-19 16:14:39 -06:00
|
|
|
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
2010-01-05 08:09:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
state->buf_kern_offset += match_size + off;
|
|
|
|
state->buf_user_offset += match_size;
|
|
|
|
pad = XT_ALIGN(size_kern) - size_kern;
|
|
|
|
|
|
|
|
if (pad > 0 && dst) {
|
2018-02-18 17:24:53 -07:00
|
|
|
if (WARN_ON(state->buf_kern_len <= pad))
|
|
|
|
return -EINVAL;
|
|
|
|
if (WARN_ON(state->buf_kern_offset - (match_size + off) + size_kern > state->buf_kern_len - pad))
|
|
|
|
return -EINVAL;
|
2010-01-05 08:09:46 -07:00
|
|
|
memset(dst + size_kern, 0, pad);
|
|
|
|
}
|
|
|
|
return off + match_size;
|
|
|
|
}
|
|
|
|
|
2015-10-26 03:10:41 -06:00
|
|
|
/* return size of all matches, watchers or target, including necessary
|
2010-01-05 08:09:46 -07:00
|
|
|
* alignment and padding.
|
|
|
|
*/
|
2019-12-14 19:49:25 -07:00
|
|
|
static int ebt_size_mwt(const struct compat_ebt_entry_mwt *match32,
|
2010-01-05 08:09:46 -07:00
|
|
|
unsigned int size_left, enum compat_mwt type,
|
|
|
|
struct ebt_entries_buf_state *state, const void *base)
|
|
|
|
{
|
2019-12-14 19:49:25 -07:00
|
|
|
const char *buf = (const char *)match32;
|
2010-01-05 08:09:46 -07:00
|
|
|
int growth = 0;
|
|
|
|
|
|
|
|
if (size_left == 0)
|
|
|
|
return 0;
|
|
|
|
|
2019-12-14 19:49:25 -07:00
|
|
|
do {
|
2010-01-05 08:09:46 -07:00
|
|
|
struct ebt_entry_match *match_kern;
|
|
|
|
int ret;
|
|
|
|
|
2019-12-14 19:49:25 -07:00
|
|
|
if (size_left < sizeof(*match32))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-01-05 08:09:46 -07:00
|
|
|
match_kern = (struct ebt_entry_match *) state->buf_kern_start;
|
|
|
|
if (match_kern) {
|
|
|
|
char *tmp;
|
|
|
|
tmp = state->buf_kern_start + state->buf_kern_offset;
|
|
|
|
match_kern = (struct ebt_entry_match *) tmp;
|
|
|
|
}
|
|
|
|
ret = ebt_buf_add(state, buf, sizeof(*match32));
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
size_left -= sizeof(*match32);
|
|
|
|
|
|
|
|
/* add padding before match->data (if any) */
|
|
|
|
ret = ebt_buf_add_pad(state, ebt_compat_entry_padsize());
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (match32->match_size > size_left)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
size_left -= match32->match_size;
|
|
|
|
|
|
|
|
ret = compat_mtw_from_user(match32, type, state, base);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2018-02-18 17:24:53 -07:00
|
|
|
if (WARN_ON(ret < match32->match_size))
|
|
|
|
return -EINVAL;
|
2010-01-05 08:09:46 -07:00
|
|
|
growth += ret - match32->match_size;
|
|
|
|
growth += ebt_compat_entry_padsize();
|
|
|
|
|
|
|
|
buf += sizeof(*match32);
|
|
|
|
buf += match32->match_size;
|
|
|
|
|
|
|
|
if (match_kern)
|
|
|
|
match_kern->match_size = ret;
|
|
|
|
|
|
|
|
match32 = (struct compat_ebt_entry_mwt *) buf;
|
2019-12-14 19:49:25 -07:00
|
|
|
} while (size_left);
|
2010-01-05 08:09:46 -07:00
|
|
|
|
|
|
|
return growth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* called for all ebt_entry structures. */
|
2019-12-14 19:49:25 -07:00
|
|
|
static int size_entry_mwt(const struct ebt_entry *entry, const unsigned char *base,
|
2010-01-05 08:09:46 -07:00
|
|
|
unsigned int *total,
|
|
|
|
struct ebt_entries_buf_state *state)
|
|
|
|
{
|
2019-12-14 19:49:25 -07:00
|
|
|
unsigned int i, j, startoff, next_expected_off, new_offset = 0;
|
2010-01-05 08:09:46 -07:00
|
|
|
/* stores match/watchers/targets & offset of next struct ebt_entry: */
|
|
|
|
unsigned int offsets[4];
|
|
|
|
unsigned int *offsets_update = NULL;
|
|
|
|
int ret;
|
|
|
|
char *buf_start;
|
|
|
|
|
|
|
|
if (*total < sizeof(struct ebt_entries))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!entry->bitmask) {
|
|
|
|
*total -= sizeof(struct ebt_entries);
|
|
|
|
return ebt_buf_add(state, entry, sizeof(struct ebt_entries));
|
|
|
|
}
|
|
|
|
if (*total < sizeof(*entry) || entry->next_offset < sizeof(*entry))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
startoff = state->buf_user_offset;
|
|
|
|
/* pull in most part of ebt_entry, it does not need to be changed. */
|
|
|
|
ret = ebt_buf_add(state, entry,
|
|
|
|
offsetof(struct ebt_entry, watchers_offset));
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
offsets[0] = sizeof(struct ebt_entry); /* matches come first */
|
|
|
|
memcpy(&offsets[1], &entry->watchers_offset,
|
|
|
|
sizeof(offsets) - sizeof(offsets[0]));
|
|
|
|
|
|
|
|
if (state->buf_kern_start) {
|
|
|
|
buf_start = state->buf_kern_start + state->buf_kern_offset;
|
|
|
|
offsets_update = (unsigned int *) buf_start;
|
|
|
|
}
|
|
|
|
ret = ebt_buf_add(state, &offsets[1],
|
|
|
|
sizeof(offsets) - sizeof(offsets[0]));
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
buf_start = (char *) entry;
|
2015-10-26 03:10:41 -06:00
|
|
|
/* 0: matches offset, always follows ebt_entry.
|
2010-01-05 08:09:46 -07:00
|
|
|
* 1: watchers offset, from ebt_entry structure
|
|
|
|
* 2: target offset, from ebt_entry structure
|
|
|
|
* 3: next ebt_entry offset, from ebt_entry structure
|
|
|
|
*
|
|
|
|
* offsets are relative to beginning of struct ebt_entry (i.e., 0).
|
|
|
|
*/
|
2018-02-18 17:24:15 -07:00
|
|
|
for (i = 0; i < 4 ; ++i) {
|
2018-03-08 04:54:19 -07:00
|
|
|
if (offsets[i] > *total)
|
2018-02-18 17:24:15 -07:00
|
|
|
return -EINVAL;
|
2018-03-08 04:54:19 -07:00
|
|
|
|
|
|
|
if (i < 3 && offsets[i] == *total)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2018-02-18 17:24:15 -07:00
|
|
|
if (i == 0)
|
|
|
|
continue;
|
|
|
|
if (offsets[i-1] > offsets[i])
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2010-01-05 08:09:46 -07:00
|
|
|
for (i = 0, j = 1 ; j < 4 ; j++, i++) {
|
|
|
|
struct compat_ebt_entry_mwt *match32;
|
|
|
|
unsigned int size;
|
2017-10-16 04:24:02 -06:00
|
|
|
char *buf = buf_start + offsets[i];
|
2010-01-05 08:09:46 -07:00
|
|
|
|
|
|
|
if (offsets[i] > offsets[j])
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
match32 = (struct compat_ebt_entry_mwt *) buf;
|
|
|
|
size = offsets[j] - offsets[i];
|
|
|
|
ret = ebt_size_mwt(match32, size, i, state, base);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
new_offset += ret;
|
|
|
|
if (offsets_update && new_offset) {
|
2010-03-19 14:08:16 -06:00
|
|
|
pr_debug("change offset %d to %d\n",
|
2010-01-05 08:09:46 -07:00
|
|
|
offsets_update[i], offsets[j] + new_offset);
|
|
|
|
offsets_update[i] = offsets[j] + new_offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-21 02:58:25 -06:00
|
|
|
if (state->buf_kern_start == NULL) {
|
|
|
|
unsigned int offset = buf_start - (char *) base;
|
|
|
|
|
|
|
|
ret = xt_compat_add_offset(NFPROTO_BRIDGE, offset, new_offset);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-12-14 19:49:25 -07:00
|
|
|
next_expected_off = state->buf_user_offset - startoff;
|
|
|
|
if (next_expected_off != entry->next_offset)
|
|
|
|
return -EINVAL;
|
2010-01-05 08:09:46 -07:00
|
|
|
|
2019-12-14 19:49:25 -07:00
|
|
|
if (*total < entry->next_offset)
|
2018-02-18 17:24:53 -07:00
|
|
|
return -EINVAL;
|
2019-12-14 19:49:25 -07:00
|
|
|
*total -= entry->next_offset;
|
2010-01-05 08:09:46 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-26 03:10:41 -06:00
|
|
|
/* repl->entries_size is the size of the ebt_entry blob in userspace.
|
2010-01-05 08:09:46 -07:00
|
|
|
* It might need more memory when copied to a 64 bit kernel in case
|
|
|
|
* userspace is 32-bit. So, first task: find out how much memory is needed.
|
|
|
|
*
|
|
|
|
* Called before validation is performed.
|
|
|
|
*/
|
|
|
|
static int compat_copy_entries(unsigned char *data, unsigned int size_user,
|
|
|
|
struct ebt_entries_buf_state *state)
|
|
|
|
{
|
|
|
|
unsigned int size_remaining = size_user;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = EBT_ENTRY_ITERATE(data, size_user, size_entry_mwt, data,
|
|
|
|
&size_remaining, state);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2019-05-05 10:47:33 -06:00
|
|
|
if (size_remaining)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-01-05 08:09:46 -07:00
|
|
|
return state->buf_kern_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int compat_copy_ebt_replace_from_user(struct ebt_replace *repl,
|
2020-07-23 00:08:54 -06:00
|
|
|
sockptr_t arg, unsigned int len)
|
2010-01-05 08:09:46 -07:00
|
|
|
{
|
|
|
|
struct compat_ebt_replace tmp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (len < sizeof(tmp))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-07-23 00:08:54 -06:00
|
|
|
if (copy_from_sockptr(&tmp, arg, sizeof(tmp)))
|
2010-01-05 08:09:46 -07:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (len != sizeof(tmp) + tmp.entries_size)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (tmp.entries_size == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (tmp.nentries >= ((INT_MAX - sizeof(struct ebt_table_info)) /
|
|
|
|
NR_CPUS - SMP_CACHE_BYTES) / sizeof(struct ebt_counter))
|
|
|
|
return -ENOMEM;
|
|
|
|
if (tmp.num_counters >= INT_MAX / sizeof(struct ebt_counter))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
memcpy(repl, &tmp, offsetof(struct ebt_replace, hook_entry));
|
|
|
|
|
|
|
|
/* starting with hook_entry, 32 vs. 64 bit structures are different */
|
|
|
|
for (i = 0; i < NF_BR_NUMHOOKS; i++)
|
|
|
|
repl->hook_entry[i] = compat_ptr(tmp.hook_entry[i]);
|
|
|
|
|
|
|
|
repl->num_counters = tmp.num_counters;
|
|
|
|
repl->counters = compat_ptr(tmp.counters);
|
|
|
|
repl->entries = compat_ptr(tmp.entries);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-23 00:08:54 -06:00
|
|
|
static int compat_do_replace(struct net *net, sockptr_t arg, unsigned int len)
|
2010-01-05 08:09:46 -07:00
|
|
|
{
|
|
|
|
int ret, i, countersize, size64;
|
|
|
|
struct ebt_table_info *newinfo;
|
|
|
|
struct ebt_replace tmp;
|
|
|
|
struct ebt_entries_buf_state state;
|
|
|
|
void *entries_tmp;
|
|
|
|
|
2020-07-23 00:08:54 -06:00
|
|
|
ret = compat_copy_ebt_replace_from_user(&tmp, arg, len);
|
2010-02-06 19:19:12 -07:00
|
|
|
if (ret) {
|
|
|
|
/* try real handler in case userland supplied needed padding */
|
2020-07-23 00:08:54 -06:00
|
|
|
if (ret == -EINVAL && do_replace(net, arg, len) == 0)
|
2010-02-06 19:19:12 -07:00
|
|
|
ret = 0;
|
2010-01-05 08:09:46 -07:00
|
|
|
return ret;
|
2010-02-06 19:19:12 -07:00
|
|
|
}
|
2010-01-05 08:09:46 -07:00
|
|
|
|
|
|
|
countersize = COUNTER_OFFSET(tmp.nentries) * nr_cpu_ids;
|
|
|
|
newinfo = vmalloc(sizeof(*newinfo) + countersize);
|
|
|
|
if (!newinfo)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
if (countersize)
|
|
|
|
memset(newinfo->counters, 0, countersize);
|
|
|
|
|
|
|
|
memset(&state, 0, sizeof(state));
|
|
|
|
|
|
|
|
newinfo->entries = vmalloc(tmp.entries_size);
|
|
|
|
if (!newinfo->entries) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto free_newinfo;
|
|
|
|
}
|
|
|
|
if (copy_from_user(
|
|
|
|
newinfo->entries, tmp.entries, tmp.entries_size) != 0) {
|
|
|
|
ret = -EFAULT;
|
|
|
|
goto free_entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
entries_tmp = newinfo->entries;
|
|
|
|
|
|
|
|
xt_compat_lock(NFPROTO_BRIDGE);
|
|
|
|
|
2019-07-29 09:58:10 -06:00
|
|
|
ret = ebt_compat_init_offsets(tmp.nentries);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out_unlock;
|
2019-01-21 13:54:36 -07:00
|
|
|
|
2010-01-05 08:09:46 -07:00
|
|
|
ret = compat_copy_entries(entries_tmp, tmp.entries_size, &state);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out_unlock;
|
|
|
|
|
|
|
|
pr_debug("tmp.entries_size %d, kern off %d, user off %d delta %d\n",
|
|
|
|
tmp.entries_size, state.buf_kern_offset, state.buf_user_offset,
|
|
|
|
xt_compat_calc_jump(NFPROTO_BRIDGE, tmp.entries_size));
|
|
|
|
|
|
|
|
size64 = ret;
|
|
|
|
newinfo->entries = vmalloc(size64);
|
|
|
|
if (!newinfo->entries) {
|
|
|
|
vfree(entries_tmp);
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&state, 0, sizeof(state));
|
|
|
|
state.buf_kern_start = newinfo->entries;
|
|
|
|
state.buf_kern_len = size64;
|
|
|
|
|
|
|
|
ret = compat_copy_entries(entries_tmp, tmp.entries_size, &state);
|
2019-07-20 06:22:45 -06:00
|
|
|
if (WARN_ON(ret < 0)) {
|
|
|
|
vfree(entries_tmp);
|
2018-02-18 17:24:53 -07:00
|
|
|
goto out_unlock;
|
2019-07-20 06:22:45 -06:00
|
|
|
}
|
2010-01-05 08:09:46 -07:00
|
|
|
|
|
|
|
vfree(entries_tmp);
|
|
|
|
tmp.entries_size = size64;
|
|
|
|
|
|
|
|
for (i = 0; i < NF_BR_NUMHOOKS; i++) {
|
|
|
|
char __user *usrptr;
|
|
|
|
if (tmp.hook_entry[i]) {
|
|
|
|
unsigned int delta;
|
|
|
|
usrptr = (char __user *) tmp.hook_entry[i];
|
|
|
|
delta = usrptr - tmp.entries;
|
|
|
|
usrptr += xt_compat_calc_jump(NFPROTO_BRIDGE, delta);
|
|
|
|
tmp.hook_entry[i] = (struct ebt_entries __user *)usrptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xt_compat_flush_offsets(NFPROTO_BRIDGE);
|
|
|
|
xt_compat_unlock(NFPROTO_BRIDGE);
|
|
|
|
|
|
|
|
ret = do_replace_finish(net, &tmp, newinfo);
|
|
|
|
if (ret == 0)
|
|
|
|
return ret;
|
|
|
|
free_entries:
|
|
|
|
vfree(newinfo->entries);
|
|
|
|
free_newinfo:
|
|
|
|
vfree(newinfo);
|
|
|
|
return ret;
|
|
|
|
out_unlock:
|
|
|
|
xt_compat_flush_offsets(NFPROTO_BRIDGE);
|
|
|
|
xt_compat_unlock(NFPROTO_BRIDGE);
|
|
|
|
goto free_entries;
|
|
|
|
}
|
|
|
|
|
2020-07-23 00:08:54 -06:00
|
|
|
static int compat_update_counters(struct net *net, sockptr_t arg,
|
2010-01-05 08:09:46 -07:00
|
|
|
unsigned int len)
|
|
|
|
{
|
|
|
|
struct compat_ebt_replace hlp;
|
|
|
|
|
2020-07-23 00:08:54 -06:00
|
|
|
if (copy_from_sockptr(&hlp, arg, sizeof(hlp)))
|
2010-01-05 08:09:46 -07:00
|
|
|
return -EFAULT;
|
|
|
|
|
2010-02-06 19:19:12 -07:00
|
|
|
/* try real handler in case userland supplied needed padding */
|
2010-01-05 08:09:46 -07:00
|
|
|
if (len != sizeof(hlp) + hlp.num_counters * sizeof(struct ebt_counter))
|
2020-07-23 00:08:54 -06:00
|
|
|
return update_counters(net, arg, len);
|
2010-01-05 08:09:46 -07:00
|
|
|
|
|
|
|
return do_update_counters(net, hlp.name, compat_ptr(hlp.counters),
|
2020-07-23 00:08:52 -06:00
|
|
|
hlp.num_counters, len);
|
2010-01-05 08:09:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int compat_do_ebt_get_ctl(struct sock *sk, int cmd,
|
|
|
|
void __user *user, int *len)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct compat_ebt_replace tmp;
|
|
|
|
struct ebt_table *t;
|
2013-01-31 09:30:58 -07:00
|
|
|
struct net *net = sock_net(sk);
|
2010-01-05 08:09:46 -07:00
|
|
|
|
2020-08-13 01:46:11 -06:00
|
|
|
if ((cmd == EBT_SO_GET_INFO || cmd == EBT_SO_GET_INIT_INFO) &&
|
|
|
|
*len != sizeof(struct compat_ebt_replace))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-01-05 08:09:46 -07:00
|
|
|
if (copy_from_user(&tmp, user, sizeof(tmp)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
2016-03-24 14:29:53 -06:00
|
|
|
tmp.name[sizeof(tmp.name) - 1] = '\0';
|
|
|
|
|
2013-01-31 09:30:58 -07:00
|
|
|
t = find_table_lock(net, tmp.name, &ret, &ebt_mutex);
|
2010-01-05 08:09:46 -07:00
|
|
|
if (!t)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
xt_compat_lock(NFPROTO_BRIDGE);
|
|
|
|
switch (cmd) {
|
|
|
|
case EBT_SO_GET_INFO:
|
|
|
|
tmp.nentries = t->private->nentries;
|
|
|
|
ret = compat_table_info(t->private, &tmp);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
tmp.valid_hooks = t->valid_hooks;
|
|
|
|
|
|
|
|
if (copy_to_user(user, &tmp, *len) != 0) {
|
|
|
|
ret = -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case EBT_SO_GET_INIT_INFO:
|
|
|
|
tmp.nentries = t->table->nentries;
|
|
|
|
tmp.entries_size = t->table->entries_size;
|
|
|
|
tmp.valid_hooks = t->table->valid_hooks;
|
|
|
|
|
|
|
|
if (copy_to_user(user, &tmp, *len) != 0) {
|
|
|
|
ret = -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case EBT_SO_GET_ENTRIES:
|
|
|
|
case EBT_SO_GET_INIT_ENTRIES:
|
2015-10-26 03:10:41 -06:00
|
|
|
/* try real handler first in case of userland-side padding.
|
2010-02-06 19:19:12 -07:00
|
|
|
* in case we are dealing with an 'ordinary' 32 bit binary
|
|
|
|
* without 64bit compatibility padding, this will fail right
|
|
|
|
* after copy_from_user when the *len argument is validated.
|
|
|
|
*
|
|
|
|
* the compat_ variant needs to do one pass over the kernel
|
|
|
|
* data set to adjust for size differences before it the check.
|
|
|
|
*/
|
|
|
|
if (copy_everything_to_user(t, user, len, cmd) == 0)
|
|
|
|
ret = 0;
|
|
|
|
else
|
|
|
|
ret = compat_copy_everything_to_user(t, user, len, cmd);
|
2010-01-05 08:09:46 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
xt_compat_flush_offsets(NFPROTO_BRIDGE);
|
|
|
|
xt_compat_unlock(NFPROTO_BRIDGE);
|
|
|
|
mutex_unlock(&ebt_mutex);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-07-17 00:23:19 -06:00
|
|
|
static int do_ebt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
|
|
|
|
{
|
|
|
|
struct net *net = sock_net(sk);
|
|
|
|
struct ebt_replace tmp;
|
|
|
|
struct ebt_table *t;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
/* try real handler in case userland supplied needed padding */
|
|
|
|
if (in_compat_syscall() &&
|
|
|
|
((cmd != EBT_SO_GET_INFO && cmd != EBT_SO_GET_INIT_INFO) ||
|
|
|
|
*len != sizeof(tmp)))
|
|
|
|
return compat_do_ebt_get_ctl(sk, cmd, user, len);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (copy_from_user(&tmp, user, sizeof(tmp)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
tmp.name[sizeof(tmp.name) - 1] = '\0';
|
|
|
|
|
|
|
|
t = find_table_lock(net, tmp.name, &ret, &ebt_mutex);
|
|
|
|
if (!t)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case EBT_SO_GET_INFO:
|
|
|
|
case EBT_SO_GET_INIT_INFO:
|
|
|
|
if (*len != sizeof(struct ebt_replace)) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
mutex_unlock(&ebt_mutex);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cmd == EBT_SO_GET_INFO) {
|
|
|
|
tmp.nentries = t->private->nentries;
|
|
|
|
tmp.entries_size = t->private->entries_size;
|
|
|
|
tmp.valid_hooks = t->valid_hooks;
|
|
|
|
} else {
|
|
|
|
tmp.nentries = t->table->nentries;
|
|
|
|
tmp.entries_size = t->table->entries_size;
|
|
|
|
tmp.valid_hooks = t->table->valid_hooks;
|
|
|
|
}
|
|
|
|
mutex_unlock(&ebt_mutex);
|
|
|
|
if (copy_to_user(user, &tmp, *len) != 0) {
|
|
|
|
ret = -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EBT_SO_GET_ENTRIES:
|
|
|
|
case EBT_SO_GET_INIT_ENTRIES:
|
|
|
|
ret = copy_everything_to_user(t, user, len, cmd);
|
|
|
|
mutex_unlock(&ebt_mutex);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
mutex_unlock(&ebt_mutex);
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-07-23 00:08:54 -06:00
|
|
|
static int do_ebt_set_ctl(struct sock *sk, int cmd, sockptr_t arg,
|
2020-07-17 00:23:19 -06:00
|
|
|
unsigned int len)
|
|
|
|
{
|
|
|
|
struct net *net = sock_net(sk);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case EBT_SO_SET_ENTRIES:
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
if (in_compat_syscall())
|
2020-07-23 00:08:54 -06:00
|
|
|
ret = compat_do_replace(net, arg, len);
|
2020-07-17 00:23:19 -06:00
|
|
|
else
|
|
|
|
#endif
|
2020-07-23 00:08:54 -06:00
|
|
|
ret = do_replace(net, arg, len);
|
2020-07-17 00:23:19 -06:00
|
|
|
break;
|
|
|
|
case EBT_SO_SET_COUNTERS:
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
if (in_compat_syscall())
|
2020-07-23 00:08:54 -06:00
|
|
|
ret = compat_update_counters(net, arg, len);
|
2020-07-17 00:23:19 -06:00
|
|
|
else
|
|
|
|
#endif
|
2020-07-23 00:08:54 -06:00
|
|
|
ret = update_counters(net, arg, len);
|
2020-07-17 00:23:19 -06:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-18 22:28:14 -07:00
|
|
|
static struct nf_sockopt_ops ebt_sockopts = {
|
2006-03-20 23:55:02 -07:00
|
|
|
.pf = PF_INET,
|
|
|
|
.set_optmin = EBT_BASE_CTL,
|
|
|
|
.set_optmax = EBT_SO_SET_MAX + 1,
|
|
|
|
.set = do_ebt_set_ctl,
|
|
|
|
.get_optmin = EBT_BASE_CTL,
|
|
|
|
.get_optmax = EBT_SO_GET_MAX + 1,
|
|
|
|
.get = do_ebt_get_ctl,
|
[NETFILTER]: Fix/improve deadlock condition on module removal netfilter
So I've had a deadlock reported to me. I've found that the sequence of
events goes like this:
1) process A (modprobe) runs to remove ip_tables.ko
2) process B (iptables-restore) runs and calls setsockopt on a netfilter socket,
increasing the ip_tables socket_ops use count
3) process A acquires a file lock on the file ip_tables.ko, calls remove_module
in the kernel, which in turn executes the ip_tables module cleanup routine,
which calls nf_unregister_sockopt
4) nf_unregister_sockopt, seeing that the use count is non-zero, puts the
calling process into uninterruptible sleep, expecting the process using the
socket option code to wake it up when it exits the kernel
4) the user of the socket option code (process B) in do_ipt_get_ctl, calls
ipt_find_table_lock, which in this case calls request_module to load
ip_tables_nat.ko
5) request_module forks a copy of modprobe (process C) to load the module and
blocks until modprobe exits.
6) Process C. forked by request_module process the dependencies of
ip_tables_nat.ko, of which ip_tables.ko is one.
7) Process C attempts to lock the request module and all its dependencies, it
blocks when it attempts to lock ip_tables.ko (which was previously locked in
step 3)
Theres not really any great permanent solution to this that I can see, but I've
developed a two part solution that corrects the problem
Part 1) Modifies the nf_sockopt registration code so that, instead of using a
use counter internal to the nf_sockopt_ops structure, we instead use a pointer
to the registering modules owner to do module reference counting when nf_sockopt
calls a modules set/get routine. This prevents the deadlock by preventing set 4
from happening.
Part 2) Enhances the modprobe utilty so that by default it preforms non-blocking
remove operations (the same way rmmod does), and add an option to explicity
request blocking operation. So if you select blocking operation in modprobe you
can still cause the above deadlock, but only if you explicity try (and since
root can do any old stupid thing it would like.... :) ).
Signed-off-by: Neil Horman <nhorman@tuxdriver.com>
Signed-off-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
2007-09-11 03:28:26 -06:00
|
|
|
.owner = THIS_MODULE,
|
2005-04-16 16:20:36 -06:00
|
|
|
};
|
|
|
|
|
2006-03-28 17:37:06 -07:00
|
|
|
static int __init ebtables_init(void)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2008-10-08 03:35:15 -06:00
|
|
|
ret = xt_register_target(&ebt_standard_target);
|
|
|
|
if (ret < 0)
|
2005-04-16 16:20:36 -06:00
|
|
|
return ret;
|
2008-10-08 03:35:15 -06:00
|
|
|
ret = nf_register_sockopt(&ebt_sockopts);
|
|
|
|
if (ret < 0) {
|
|
|
|
xt_unregister_target(&ebt_standard_target);
|
|
|
|
return ret;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-28 17:37:06 -07:00
|
|
|
static void __exit ebtables_fini(void)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
nf_unregister_sockopt(&ebt_sockopts);
|
2008-10-08 03:35:15 -06:00
|
|
|
xt_unregister_target(&ebt_standard_target);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(ebt_register_table);
|
|
|
|
EXPORT_SYMBOL(ebt_unregister_table);
|
|
|
|
EXPORT_SYMBOL(ebt_do_table);
|
2006-03-28 17:37:06 -07:00
|
|
|
module_init(ebtables_init);
|
|
|
|
module_exit(ebtables_fini);
|
2005-04-16 16:20:36 -06:00
|
|
|
MODULE_LICENSE("GPL");
|