Commit b7e4ba9a authored by David S. Miller's avatar David S. Miller

Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf

Pablo Neira Ayuso says:

====================
Netfilter fixes for net

The following patchset contains Netfilter fixes for net:

1) Switch to RCU in x_tables to fix possible NULL pointer dereference,
   from Subash Abhinov Kasiviswanathan.

2) Fix netlink dump of dynset timeouts later than 23 days.

3) Add comment for the indirect serialization of the nft commit mutex
   with rtnl_mutex.

4) Remove bogus check for confirmed conntrack when matching on the
   conntrack ID, from Brett Mastbergen.
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 88287773 2d94b20b
...@@ -227,7 +227,7 @@ struct xt_table { ...@@ -227,7 +227,7 @@ struct xt_table {
unsigned int valid_hooks; unsigned int valid_hooks;
/* Man behind the curtain... */ /* Man behind the curtain... */
struct xt_table_info *private; struct xt_table_info __rcu *private;
/* Set this to THIS_MODULE if you are a module, otherwise NULL */ /* Set this to THIS_MODULE if you are a module, otherwise NULL */
struct module *me; struct module *me;
...@@ -448,6 +448,9 @@ xt_get_per_cpu_counter(struct xt_counters *cnt, unsigned int cpu) ...@@ -448,6 +448,9 @@ xt_get_per_cpu_counter(struct xt_counters *cnt, unsigned int cpu)
struct nf_hook_ops *xt_hook_ops_alloc(const struct xt_table *, nf_hookfn *); struct nf_hook_ops *xt_hook_ops_alloc(const struct xt_table *, nf_hookfn *);
struct xt_table_info
*xt_table_get_private_protected(const struct xt_table *table);
#ifdef CONFIG_COMPAT #ifdef CONFIG_COMPAT
#include <net/compat.h> #include <net/compat.h>
......
...@@ -1524,4 +1524,8 @@ void __init nft_chain_route_init(void); ...@@ -1524,4 +1524,8 @@ void __init nft_chain_route_init(void);
void nft_chain_route_fini(void); void nft_chain_route_fini(void);
void nf_tables_trans_destroy_flush_work(void); void nf_tables_trans_destroy_flush_work(void);
int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result);
__be64 nf_jiffies64_to_msecs(u64 input);
#endif /* _NET_NF_TABLES_H */ #endif /* _NET_NF_TABLES_H */
...@@ -203,7 +203,7 @@ unsigned int arpt_do_table(struct sk_buff *skb, ...@@ -203,7 +203,7 @@ unsigned int arpt_do_table(struct sk_buff *skb,
local_bh_disable(); local_bh_disable();
addend = xt_write_recseq_begin(); addend = xt_write_recseq_begin();
private = READ_ONCE(table->private); /* Address dependency. */ private = rcu_access_pointer(table->private);
cpu = smp_processor_id(); cpu = smp_processor_id();
table_base = private->entries; table_base = private->entries;
jumpstack = (struct arpt_entry **)private->jumpstack[cpu]; jumpstack = (struct arpt_entry **)private->jumpstack[cpu];
...@@ -649,7 +649,7 @@ static struct xt_counters *alloc_counters(const struct xt_table *table) ...@@ -649,7 +649,7 @@ static struct xt_counters *alloc_counters(const struct xt_table *table)
{ {
unsigned int countersize; unsigned int countersize;
struct xt_counters *counters; struct xt_counters *counters;
const struct xt_table_info *private = table->private; const struct xt_table_info *private = xt_table_get_private_protected(table);
/* We need atomic snapshot of counters: rest doesn't change /* We need atomic snapshot of counters: rest doesn't change
* (other than comefrom, which userspace doesn't care * (other than comefrom, which userspace doesn't care
...@@ -673,7 +673,7 @@ static int copy_entries_to_user(unsigned int total_size, ...@@ -673,7 +673,7 @@ static int copy_entries_to_user(unsigned int total_size,
unsigned int off, num; unsigned int off, num;
const struct arpt_entry *e; const struct arpt_entry *e;
struct xt_counters *counters; struct xt_counters *counters;
struct xt_table_info *private = table->private; struct xt_table_info *private = xt_table_get_private_protected(table);
int ret = 0; int ret = 0;
void *loc_cpu_entry; void *loc_cpu_entry;
...@@ -807,7 +807,7 @@ static int get_info(struct net *net, void __user *user, const int *len) ...@@ -807,7 +807,7 @@ static int get_info(struct net *net, void __user *user, const int *len)
t = xt_request_find_table_lock(net, NFPROTO_ARP, name); t = xt_request_find_table_lock(net, NFPROTO_ARP, name);
if (!IS_ERR(t)) { if (!IS_ERR(t)) {
struct arpt_getinfo info; struct arpt_getinfo info;
const struct xt_table_info *private = t->private; const struct xt_table_info *private = xt_table_get_private_protected(t);
#ifdef CONFIG_COMPAT #ifdef CONFIG_COMPAT
struct xt_table_info tmp; struct xt_table_info tmp;
...@@ -860,7 +860,7 @@ static int get_entries(struct net *net, struct arpt_get_entries __user *uptr, ...@@ -860,7 +860,7 @@ static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
t = xt_find_table_lock(net, NFPROTO_ARP, get.name); t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
if (!IS_ERR(t)) { if (!IS_ERR(t)) {
const struct xt_table_info *private = t->private; const struct xt_table_info *private = xt_table_get_private_protected(t);
if (get.size == private->size) if (get.size == private->size)
ret = copy_entries_to_user(private->size, ret = copy_entries_to_user(private->size,
...@@ -1017,7 +1017,7 @@ static int do_add_counters(struct net *net, sockptr_t arg, unsigned int len) ...@@ -1017,7 +1017,7 @@ static int do_add_counters(struct net *net, sockptr_t arg, unsigned int len)
} }
local_bh_disable(); local_bh_disable();
private = t->private; private = xt_table_get_private_protected(t);
if (private->number != tmp.num_counters) { if (private->number != tmp.num_counters) {
ret = -EINVAL; ret = -EINVAL;
goto unlock_up_free; goto unlock_up_free;
...@@ -1330,7 +1330,7 @@ static int compat_copy_entries_to_user(unsigned int total_size, ...@@ -1330,7 +1330,7 @@ static int compat_copy_entries_to_user(unsigned int total_size,
void __user *userptr) void __user *userptr)
{ {
struct xt_counters *counters; struct xt_counters *counters;
const struct xt_table_info *private = table->private; const struct xt_table_info *private = xt_table_get_private_protected(table);
void __user *pos; void __user *pos;
unsigned int size; unsigned int size;
int ret = 0; int ret = 0;
......
...@@ -258,7 +258,7 @@ ipt_do_table(struct sk_buff *skb, ...@@ -258,7 +258,7 @@ ipt_do_table(struct sk_buff *skb,
WARN_ON(!(table->valid_hooks & (1 << hook))); WARN_ON(!(table->valid_hooks & (1 << hook)));
local_bh_disable(); local_bh_disable();
addend = xt_write_recseq_begin(); addend = xt_write_recseq_begin();
private = READ_ONCE(table->private); /* Address dependency. */ private = rcu_access_pointer(table->private);
cpu = smp_processor_id(); cpu = smp_processor_id();
table_base = private->entries; table_base = private->entries;
jumpstack = (struct ipt_entry **)private->jumpstack[cpu]; jumpstack = (struct ipt_entry **)private->jumpstack[cpu];
...@@ -791,7 +791,7 @@ static struct xt_counters *alloc_counters(const struct xt_table *table) ...@@ -791,7 +791,7 @@ static struct xt_counters *alloc_counters(const struct xt_table *table)
{ {
unsigned int countersize; unsigned int countersize;
struct xt_counters *counters; struct xt_counters *counters;
const struct xt_table_info *private = table->private; const struct xt_table_info *private = xt_table_get_private_protected(table);
/* We need atomic snapshot of counters: rest doesn't change /* We need atomic snapshot of counters: rest doesn't change
(other than comefrom, which userspace doesn't care (other than comefrom, which userspace doesn't care
...@@ -815,7 +815,7 @@ copy_entries_to_user(unsigned int total_size, ...@@ -815,7 +815,7 @@ copy_entries_to_user(unsigned int total_size,
unsigned int off, num; unsigned int off, num;
const struct ipt_entry *e; const struct ipt_entry *e;
struct xt_counters *counters; struct xt_counters *counters;
const struct xt_table_info *private = table->private; const struct xt_table_info *private = xt_table_get_private_protected(table);
int ret = 0; int ret = 0;
const void *loc_cpu_entry; const void *loc_cpu_entry;
...@@ -964,7 +964,7 @@ static int get_info(struct net *net, void __user *user, const int *len) ...@@ -964,7 +964,7 @@ static int get_info(struct net *net, void __user *user, const int *len)
t = xt_request_find_table_lock(net, AF_INET, name); t = xt_request_find_table_lock(net, AF_INET, name);
if (!IS_ERR(t)) { if (!IS_ERR(t)) {
struct ipt_getinfo info; struct ipt_getinfo info;
const struct xt_table_info *private = t->private; const struct xt_table_info *private = xt_table_get_private_protected(t);
#ifdef CONFIG_COMPAT #ifdef CONFIG_COMPAT
struct xt_table_info tmp; struct xt_table_info tmp;
...@@ -1018,7 +1018,7 @@ get_entries(struct net *net, struct ipt_get_entries __user *uptr, ...@@ -1018,7 +1018,7 @@ get_entries(struct net *net, struct ipt_get_entries __user *uptr,
t = xt_find_table_lock(net, AF_INET, get.name); t = xt_find_table_lock(net, AF_INET, get.name);
if (!IS_ERR(t)) { if (!IS_ERR(t)) {
const struct xt_table_info *private = t->private; const struct xt_table_info *private = xt_table_get_private_protected(t);
if (get.size == private->size) if (get.size == private->size)
ret = copy_entries_to_user(private->size, ret = copy_entries_to_user(private->size,
t, uptr->entrytable); t, uptr->entrytable);
...@@ -1173,7 +1173,7 @@ do_add_counters(struct net *net, sockptr_t arg, unsigned int len) ...@@ -1173,7 +1173,7 @@ do_add_counters(struct net *net, sockptr_t arg, unsigned int len)
} }
local_bh_disable(); local_bh_disable();
private = t->private; private = xt_table_get_private_protected(t);
if (private->number != tmp.num_counters) { if (private->number != tmp.num_counters) {
ret = -EINVAL; ret = -EINVAL;
goto unlock_up_free; goto unlock_up_free;
...@@ -1543,7 +1543,7 @@ compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table, ...@@ -1543,7 +1543,7 @@ compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
void __user *userptr) void __user *userptr)
{ {
struct xt_counters *counters; struct xt_counters *counters;
const struct xt_table_info *private = table->private; const struct xt_table_info *private = xt_table_get_private_protected(table);
void __user *pos; void __user *pos;
unsigned int size; unsigned int size;
int ret = 0; int ret = 0;
......
...@@ -280,7 +280,7 @@ ip6t_do_table(struct sk_buff *skb, ...@@ -280,7 +280,7 @@ ip6t_do_table(struct sk_buff *skb,
local_bh_disable(); local_bh_disable();
addend = xt_write_recseq_begin(); addend = xt_write_recseq_begin();
private = READ_ONCE(table->private); /* Address dependency. */ private = rcu_access_pointer(table->private);
cpu = smp_processor_id(); cpu = smp_processor_id();
table_base = private->entries; table_base = private->entries;
jumpstack = (struct ip6t_entry **)private->jumpstack[cpu]; jumpstack = (struct ip6t_entry **)private->jumpstack[cpu];
...@@ -807,7 +807,7 @@ static struct xt_counters *alloc_counters(const struct xt_table *table) ...@@ -807,7 +807,7 @@ static struct xt_counters *alloc_counters(const struct xt_table *table)
{ {
unsigned int countersize; unsigned int countersize;
struct xt_counters *counters; struct xt_counters *counters;
const struct xt_table_info *private = table->private; const struct xt_table_info *private = xt_table_get_private_protected(table);
/* We need atomic snapshot of counters: rest doesn't change /* We need atomic snapshot of counters: rest doesn't change
(other than comefrom, which userspace doesn't care (other than comefrom, which userspace doesn't care
...@@ -831,7 +831,7 @@ copy_entries_to_user(unsigned int total_size, ...@@ -831,7 +831,7 @@ copy_entries_to_user(unsigned int total_size,
unsigned int off, num; unsigned int off, num;
const struct ip6t_entry *e; const struct ip6t_entry *e;
struct xt_counters *counters; struct xt_counters *counters;
const struct xt_table_info *private = table->private; const struct xt_table_info *private = xt_table_get_private_protected(table);
int ret = 0; int ret = 0;
const void *loc_cpu_entry; const void *loc_cpu_entry;
...@@ -980,7 +980,7 @@ static int get_info(struct net *net, void __user *user, const int *len) ...@@ -980,7 +980,7 @@ static int get_info(struct net *net, void __user *user, const int *len)
t = xt_request_find_table_lock(net, AF_INET6, name); t = xt_request_find_table_lock(net, AF_INET6, name);
if (!IS_ERR(t)) { if (!IS_ERR(t)) {
struct ip6t_getinfo info; struct ip6t_getinfo info;
const struct xt_table_info *private = t->private; const struct xt_table_info *private = xt_table_get_private_protected(t);
#ifdef CONFIG_COMPAT #ifdef CONFIG_COMPAT
struct xt_table_info tmp; struct xt_table_info tmp;
...@@ -1035,7 +1035,7 @@ get_entries(struct net *net, struct ip6t_get_entries __user *uptr, ...@@ -1035,7 +1035,7 @@ get_entries(struct net *net, struct ip6t_get_entries __user *uptr,
t = xt_find_table_lock(net, AF_INET6, get.name); t = xt_find_table_lock(net, AF_INET6, get.name);
if (!IS_ERR(t)) { if (!IS_ERR(t)) {
struct xt_table_info *private = t->private; struct xt_table_info *private = xt_table_get_private_protected(t);
if (get.size == private->size) if (get.size == private->size)
ret = copy_entries_to_user(private->size, ret = copy_entries_to_user(private->size,
t, uptr->entrytable); t, uptr->entrytable);
...@@ -1189,7 +1189,7 @@ do_add_counters(struct net *net, sockptr_t arg, unsigned int len) ...@@ -1189,7 +1189,7 @@ do_add_counters(struct net *net, sockptr_t arg, unsigned int len)
} }
local_bh_disable(); local_bh_disable();
private = t->private; private = xt_table_get_private_protected(t);
if (private->number != tmp.num_counters) { if (private->number != tmp.num_counters) {
ret = -EINVAL; ret = -EINVAL;
goto unlock_up_free; goto unlock_up_free;
...@@ -1552,7 +1552,7 @@ compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table, ...@@ -1552,7 +1552,7 @@ compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
void __user *userptr) void __user *userptr)
{ {
struct xt_counters *counters; struct xt_counters *counters;
const struct xt_table_info *private = table->private; const struct xt_table_info *private = xt_table_get_private_protected(table);
void __user *pos; void __user *pos;
unsigned int size; unsigned int size;
int ret = 0; int ret = 0;
......
...@@ -1723,6 +1723,10 @@ static struct nft_hook *nft_netdev_hook_alloc(struct net *net, ...@@ -1723,6 +1723,10 @@ static struct nft_hook *nft_netdev_hook_alloc(struct net *net,
} }
nla_strlcpy(ifname, attr, IFNAMSIZ); nla_strlcpy(ifname, attr, IFNAMSIZ);
/* nf_tables_netdev_event() is called under rtnl_mutex, this is
* indirectly serializing all the other holders of the commit_mutex with
* the rtnl_mutex.
*/
dev = __dev_get_by_name(net, ifname); dev = __dev_get_by_name(net, ifname);
if (!dev) { if (!dev) {
err = -ENOENT; err = -ENOENT;
...@@ -3719,7 +3723,7 @@ static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, ...@@ -3719,7 +3723,7 @@ static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
return 0; return 0;
} }
static int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result) int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result)
{ {
u64 ms = be64_to_cpu(nla_get_be64(nla)); u64 ms = be64_to_cpu(nla_get_be64(nla));
u64 max = (u64)(~((u64)0)); u64 max = (u64)(~((u64)0));
...@@ -3733,7 +3737,7 @@ static int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result) ...@@ -3733,7 +3737,7 @@ static int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result)
return 0; return 0;
} }
static __be64 nf_jiffies64_to_msecs(u64 input) __be64 nf_jiffies64_to_msecs(u64 input)
{ {
return cpu_to_be64(jiffies64_to_msecs(input)); return cpu_to_be64(jiffies64_to_msecs(input));
} }
......
...@@ -177,8 +177,6 @@ static void nft_ct_get_eval(const struct nft_expr *expr, ...@@ -177,8 +177,6 @@ static void nft_ct_get_eval(const struct nft_expr *expr,
} }
#endif #endif
case NFT_CT_ID: case NFT_CT_ID:
if (!nf_ct_is_confirmed(ct))
goto err;
*dest = nf_ct_get_id(ct); *dest = nf_ct_get_id(ct);
return; return;
default: default:
......
...@@ -157,8 +157,10 @@ static int nft_dynset_init(const struct nft_ctx *ctx, ...@@ -157,8 +157,10 @@ static int nft_dynset_init(const struct nft_ctx *ctx,
if (tb[NFTA_DYNSET_TIMEOUT] != NULL) { if (tb[NFTA_DYNSET_TIMEOUT] != NULL) {
if (!(set->flags & NFT_SET_TIMEOUT)) if (!(set->flags & NFT_SET_TIMEOUT))
return -EINVAL; return -EINVAL;
timeout = msecs_to_jiffies(be64_to_cpu(nla_get_be64(
tb[NFTA_DYNSET_TIMEOUT]))); err = nf_msecs_to_jiffies64(tb[NFTA_DYNSET_TIMEOUT], &timeout);
if (err)
return err;
} }
priv->sreg_key = nft_parse_register(tb[NFTA_DYNSET_SREG_KEY]); priv->sreg_key = nft_parse_register(tb[NFTA_DYNSET_SREG_KEY]);
...@@ -267,7 +269,7 @@ static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr) ...@@ -267,7 +269,7 @@ static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr)
if (nla_put_string(skb, NFTA_DYNSET_SET_NAME, priv->set->name)) if (nla_put_string(skb, NFTA_DYNSET_SET_NAME, priv->set->name))
goto nla_put_failure; goto nla_put_failure;
if (nla_put_be64(skb, NFTA_DYNSET_TIMEOUT, if (nla_put_be64(skb, NFTA_DYNSET_TIMEOUT,
cpu_to_be64(jiffies_to_msecs(priv->timeout)), nf_jiffies64_to_msecs(priv->timeout),
NFTA_DYNSET_PAD)) NFTA_DYNSET_PAD))
goto nla_put_failure; goto nla_put_failure;
if (priv->expr && nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr)) if (priv->expr && nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr))
......
...@@ -1349,6 +1349,14 @@ struct xt_counters *xt_counters_alloc(unsigned int counters) ...@@ -1349,6 +1349,14 @@ struct xt_counters *xt_counters_alloc(unsigned int counters)
} }
EXPORT_SYMBOL(xt_counters_alloc); EXPORT_SYMBOL(xt_counters_alloc);
struct xt_table_info
*xt_table_get_private_protected(const struct xt_table *table)
{
return rcu_dereference_protected(table->private,
mutex_is_locked(&xt[table->af].mutex));
}
EXPORT_SYMBOL(xt_table_get_private_protected);
struct xt_table_info * struct xt_table_info *
xt_replace_table(struct xt_table *table, xt_replace_table(struct xt_table *table,
unsigned int num_counters, unsigned int num_counters,
...@@ -1356,7 +1364,6 @@ xt_replace_table(struct xt_table *table, ...@@ -1356,7 +1364,6 @@ xt_replace_table(struct xt_table *table,
int *error) int *error)
{ {
struct xt_table_info *private; struct xt_table_info *private;
unsigned int cpu;
int ret; int ret;
ret = xt_jumpstack_alloc(newinfo); ret = xt_jumpstack_alloc(newinfo);
...@@ -1366,47 +1373,20 @@ xt_replace_table(struct xt_table *table, ...@@ -1366,47 +1373,20 @@ xt_replace_table(struct xt_table *table,
} }
/* Do the substitution. */ /* Do the substitution. */
local_bh_disable(); private = xt_table_get_private_protected(table);
private = table->private;
/* Check inside lock: is the old number correct? */ /* Check inside lock: is the old number correct? */
if (num_counters != private->number) { if (num_counters != private->number) {
pr_debug("num_counters != table->private->number (%u/%u)\n", pr_debug("num_counters != table->private->number (%u/%u)\n",
num_counters, private->number); num_counters, private->number);
local_bh_enable();
*error = -EAGAIN; *error = -EAGAIN;
return NULL; return NULL;
} }
newinfo->initial_entries = private->initial_entries; newinfo->initial_entries = private->initial_entries;
/*
* Ensure contents of newinfo are visible before assigning to
* private.
*/
smp_wmb();
table->private = newinfo;
/* make sure all cpus see new ->private value */
smp_wmb();
/*
* Even though table entries have now been swapped, other CPU's
* may still be using the old entries...
*/
local_bh_enable();
/* ... so wait for even xt_recseq on all cpus */
for_each_possible_cpu(cpu) {
seqcount_t *s = &per_cpu(xt_recseq, cpu);
u32 seq = raw_read_seqcount(s);
if (seq & 1) { rcu_assign_pointer(table->private, newinfo);
do { synchronize_rcu();
cond_resched();
cpu_relax();
} while (seq == raw_read_seqcount(s));
}
}
audit_log_nfcfg(table->name, table->af, private->number, audit_log_nfcfg(table->name, table->af, private->number,
!private->number ? AUDIT_XT_OP_REGISTER : !private->number ? AUDIT_XT_OP_REGISTER :
...@@ -1442,12 +1422,12 @@ struct xt_table *xt_register_table(struct net *net, ...@@ -1442,12 +1422,12 @@ struct xt_table *xt_register_table(struct net *net,
} }
/* Simplifies replace_table code. */ /* Simplifies replace_table code. */
table->private = bootstrap; rcu_assign_pointer(table->private, bootstrap);
if (!xt_replace_table(table, 0, newinfo, &ret)) if (!xt_replace_table(table, 0, newinfo, &ret))
goto unlock; goto unlock;
private = table->private; private = xt_table_get_private_protected(table);
pr_debug("table->private->number = %u\n", private->number); pr_debug("table->private->number = %u\n", private->number);
/* save number of initial entries */ /* save number of initial entries */
...@@ -1470,7 +1450,8 @@ void *xt_unregister_table(struct xt_table *table) ...@@ -1470,7 +1450,8 @@ void *xt_unregister_table(struct xt_table *table)
struct xt_table_info *private; struct xt_table_info *private;
mutex_lock(&xt[table->af].mutex); mutex_lock(&xt[table->af].mutex);
private = table->private; private = xt_table_get_private_protected(table);
RCU_INIT_POINTER(table->private, NULL);
list_del(&table->list); list_del(&table->list);
mutex_unlock(&xt[table->af].mutex); mutex_unlock(&xt[table->af].mutex);
audit_log_nfcfg(table->name, table->af, private->number, audit_log_nfcfg(table->name, table->af, private->number,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment