Skip to content

Commit b73a656

Browse files
committed
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) Missing netns context in arp_tables, from Florian Westphal. 2) Underflow in flowtable reference counter, from wenxu. 3) Fix incorrect ethernet destination address in flowtable offload, from wenxu. 4) Check for status of neighbour entry, from wenxu. 5) Fix NAT port mangling, from wenxu. 6) Unbind callbacks from destroy path to cleanup hardware properly on flowtable removal. 7) Fix missing casting statistics timestamp, add nf_flowtable_time_stamp and use it. 8) NULL pointer exception when timeout argument is null in conntrack dccp and sctp protocol helpers, from Florian Westphal. 9) Possible nul-dereference in ipset with IPSET_ATTR_LINENO, also from Florian. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 30780d0 + 22dad71 commit b73a656

10 files changed

Lines changed: 75 additions & 39 deletions

File tree

‎include/net/netfilter/nf_flow_table.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ struct flow_offload {
106106
};
107107

108108
#define NF_FLOW_TIMEOUT (30 * HZ)
109+
#define nf_flowtable_time_stamp (u32)jiffies
110+
111+
static inline __s32 nf_flow_timeout_delta(unsigned int timeout)
112+
{
113+
return (__s32)(timeout - nf_flowtable_time_stamp);
114+
}
109115

110116
struct nf_flow_route {
111117
struct {

‎net/ipv4/netfilter/arp_tables.c‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,11 @@ next: ;
384384
return 1;
385385
}
386386

387-
static inline int check_target(struct arpt_entry *e, const char *name)
387+
static int check_target(struct arpt_entry *e, struct net *net, const char *name)
388388
{
389389
struct xt_entry_target *t = arpt_get_target(e);
390390
struct xt_tgchk_param par = {
391+
.net = net,
391392
.table = name,
392393
.entryinfo = e,
393394
.target = t->u.kernel.target,
@@ -399,8 +400,9 @@ static inline int check_target(struct arpt_entry *e, const char *name)
399400
return xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
400401
}
401402

402-
static inline int
403-
find_check_entry(struct arpt_entry *e, const char *name, unsigned int size,
403+
static int
404+
find_check_entry(struct arpt_entry *e, struct net *net, const char *name,
405+
unsigned int size,
404406
struct xt_percpu_counter_alloc_state *alloc_state)
405407
{
406408
struct xt_entry_target *t;
@@ -419,7 +421,7 @@ find_check_entry(struct arpt_entry *e, const char *name, unsigned int size,
419421
}
420422
t->u.kernel.target = target;
421423

422-
ret = check_target(e, name);
424+
ret = check_target(e, net, name);
423425
if (ret)
424426
goto err;
425427
return 0;
@@ -512,7 +514,9 @@ static inline void cleanup_entry(struct arpt_entry *e)
512514
/* Checks and translates the user-supplied table segment (held in
513515
* newinfo).
514516
*/
515-
static int translate_table(struct xt_table_info *newinfo, void *entry0,
517+
static int translate_table(struct net *net,
518+
struct xt_table_info *newinfo,
519+
void *entry0,
516520
const struct arpt_replace *repl)
517521
{
518522
struct xt_percpu_counter_alloc_state alloc_state = { 0 };
@@ -569,7 +573,7 @@ static int translate_table(struct xt_table_info *newinfo, void *entry0,
569573
/* Finally, each sanity check must pass */
570574
i = 0;
571575
xt_entry_foreach(iter, entry0, newinfo->size) {
572-
ret = find_check_entry(iter, repl->name, repl->size,
576+
ret = find_check_entry(iter, net, repl->name, repl->size,
573577
&alloc_state);
574578
if (ret != 0)
575579
break;
@@ -974,7 +978,7 @@ static int do_replace(struct net *net, const void __user *user,
974978
goto free_newinfo;
975979
}
976980

977-
ret = translate_table(newinfo, loc_cpu_entry, &tmp);
981+
ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
978982
if (ret != 0)
979983
goto free_newinfo;
980984

@@ -1149,7 +1153,8 @@ compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
11491153
}
11501154
}
11511155

1152-
static int translate_compat_table(struct xt_table_info **pinfo,
1156+
static int translate_compat_table(struct net *net,
1157+
struct xt_table_info **pinfo,
11531158
void **pentry0,
11541159
const struct compat_arpt_replace *compatr)
11551160
{
@@ -1217,7 +1222,7 @@ static int translate_compat_table(struct xt_table_info **pinfo,
12171222
repl.num_counters = 0;
12181223
repl.counters = NULL;
12191224
repl.size = newinfo->size;
1220-
ret = translate_table(newinfo, entry1, &repl);
1225+
ret = translate_table(net, newinfo, entry1, &repl);
12211226
if (ret)
12221227
goto free_newinfo;
12231228

@@ -1270,7 +1275,7 @@ static int compat_do_replace(struct net *net, void __user *user,
12701275
goto free_newinfo;
12711276
}
12721277

1273-
ret = translate_compat_table(&newinfo, &loc_cpu_entry, &tmp);
1278+
ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
12741279
if (ret != 0)
12751280
goto free_newinfo;
12761281

@@ -1546,7 +1551,7 @@ int arpt_register_table(struct net *net,
15461551
loc_cpu_entry = newinfo->entries;
15471552
memcpy(loc_cpu_entry, repl->entries, repl->size);
15481553

1549-
ret = translate_table(newinfo, loc_cpu_entry, repl);
1554+
ret = translate_table(net, newinfo, loc_cpu_entry, repl);
15501555
if (ret != 0)
15511556
goto out_free;
15521557

‎net/netfilter/ipset/ip_set_core.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,7 @@ static int ip_set_utest(struct net *net, struct sock *ctnl, struct sk_buff *skb,
18481848
struct ip_set *set;
18491849
struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
18501850
int ret = 0;
1851+
u32 lineno;
18511852

18521853
if (unlikely(protocol_min_failed(attr) ||
18531854
!attr[IPSET_ATTR_SETNAME] ||
@@ -1864,7 +1865,7 @@ static int ip_set_utest(struct net *net, struct sock *ctnl, struct sk_buff *skb,
18641865
return -IPSET_ERR_PROTOCOL;
18651866

18661867
rcu_read_lock_bh();
1867-
ret = set->variant->uadt(set, tb, IPSET_TEST, NULL, 0, 0);
1868+
ret = set->variant->uadt(set, tb, IPSET_TEST, &lineno, 0, 0);
18681869
rcu_read_unlock_bh();
18691870
/* Userspace can't trigger element to be re-added */
18701871
if (ret == -EAGAIN)

‎net/netfilter/nf_conntrack_proto_dccp.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,9 @@ static int dccp_timeout_nlattr_to_obj(struct nlattr *tb[],
677677
unsigned int *timeouts = data;
678678
int i;
679679

680+
if (!timeouts)
681+
timeouts = dn->dccp_timeout;
682+
680683
/* set default DCCP timeouts. */
681684
for (i=0; i<CT_DCCP_MAX; i++)
682685
timeouts[i] = dn->dccp_timeout[i];

‎net/netfilter/nf_conntrack_proto_sctp.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,9 @@ static int sctp_timeout_nlattr_to_obj(struct nlattr *tb[],
594594
struct nf_sctp_net *sn = nf_sctp_pernet(net);
595595
int i;
596596

597+
if (!timeouts)
598+
timeouts = sn->timeouts;
599+
597600
/* set default SCTP timeouts. */
598601
for (i=0; i<SCTP_CONNTRACK_MAX; i++)
599602
timeouts[i] = sn->timeouts[i];

‎net/netfilter/nf_flow_table_core.c‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,6 @@ static void flow_offload_fixup_tcp(struct ip_ct_tcp *tcp)
134134
#define NF_FLOWTABLE_TCP_PICKUP_TIMEOUT (120 * HZ)
135135
#define NF_FLOWTABLE_UDP_PICKUP_TIMEOUT (30 * HZ)
136136

137-
static inline __s32 nf_flow_timeout_delta(unsigned int timeout)
138-
{
139-
return (__s32)(timeout - (u32)jiffies);
140-
}
141-
142137
static void flow_offload_fixup_ct_timeout(struct nf_conn *ct)
143138
{
144139
const struct nf_conntrack_l4proto *l4proto;
@@ -232,7 +227,7 @@ int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)
232227
{
233228
int err;
234229

235-
flow->timeout = (u32)jiffies + NF_FLOW_TIMEOUT;
230+
flow->timeout = nf_flowtable_time_stamp + NF_FLOW_TIMEOUT;
236231

237232
err = rhashtable_insert_fast(&flow_table->rhashtable,
238233
&flow->tuplehash[0].node,

‎net/netfilter/nf_flow_table_ip.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
280280
if (nf_flow_nat_ip(flow, skb, thoff, dir) < 0)
281281
return NF_DROP;
282282

283-
flow->timeout = (u32)jiffies + NF_FLOW_TIMEOUT;
283+
flow->timeout = nf_flowtable_time_stamp + NF_FLOW_TIMEOUT;
284284
iph = ip_hdr(skb);
285285
ip_decrease_ttl(iph);
286286
skb->tstamp = 0;
@@ -509,7 +509,7 @@ nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
509509
if (nf_flow_nat_ipv6(flow, skb, dir) < 0)
510510
return NF_DROP;
511511

512-
flow->timeout = (u32)jiffies + NF_FLOW_TIMEOUT;
512+
flow->timeout = nf_flowtable_time_stamp + NF_FLOW_TIMEOUT;
513513
ip6h = ipv6_hdr(skb);
514514
ip6h->hop_limit--;
515515
skb->tstamp = 0;

‎net/netfilter/nf_flow_table_offload.c‎

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,24 +166,38 @@ static int flow_offload_eth_dst(struct net *net,
166166
enum flow_offload_tuple_dir dir,
167167
struct nf_flow_rule *flow_rule)
168168
{
169-
const struct flow_offload_tuple *tuple = &flow->tuplehash[dir].tuple;
170169
struct flow_action_entry *entry0 = flow_action_entry_next(flow_rule);
171170
struct flow_action_entry *entry1 = flow_action_entry_next(flow_rule);
171+
const void *daddr = &flow->tuplehash[!dir].tuple.src_v4;
172+
const struct dst_entry *dst_cache;
173+
unsigned char ha[ETH_ALEN];
172174
struct neighbour *n;
173175
u32 mask, val;
176+
u8 nud_state;
174177
u16 val16;
175178

176-
n = dst_neigh_lookup(tuple->dst_cache, &tuple->dst_v4);
179+
dst_cache = flow->tuplehash[dir].tuple.dst_cache;
180+
n = dst_neigh_lookup(dst_cache, daddr);
177181
if (!n)
178182
return -ENOENT;
179183

184+
read_lock_bh(&n->lock);
185+
nud_state = n->nud_state;
186+
ether_addr_copy(ha, n->ha);
187+
read_unlock_bh(&n->lock);
188+
189+
if (!(nud_state & NUD_VALID)) {
190+
neigh_release(n);
191+
return -ENOENT;
192+
}
193+
180194
mask = ~0xffffffff;
181-
memcpy(&val, n->ha, 4);
195+
memcpy(&val, ha, 4);
182196
flow_offload_mangle(entry0, FLOW_ACT_MANGLE_HDR_TYPE_ETH, 0,
183197
&val, &mask);
184198

185199
mask = ~0x0000ffff;
186-
memcpy(&val16, n->ha + 4, 2);
200+
memcpy(&val16, ha + 4, 2);
187201
val = val16;
188202
flow_offload_mangle(entry1, FLOW_ACT_MANGLE_HDR_TYPE_ETH, 4,
189203
&val, &mask);
@@ -335,22 +349,26 @@ static void flow_offload_port_snat(struct net *net,
335349
struct nf_flow_rule *flow_rule)
336350
{
337351
struct flow_action_entry *entry = flow_action_entry_next(flow_rule);
338-
u32 mask = ~htonl(0xffff0000), port;
352+
u32 mask, port;
339353
u32 offset;
340354

341355
switch (dir) {
342356
case FLOW_OFFLOAD_DIR_ORIGINAL:
343357
port = ntohs(flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_port);
344358
offset = 0; /* offsetof(struct tcphdr, source); */
359+
port = htonl(port << 16);
360+
mask = ~htonl(0xffff0000);
345361
break;
346362
case FLOW_OFFLOAD_DIR_REPLY:
347363
port = ntohs(flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_port);
348364
offset = 0; /* offsetof(struct tcphdr, dest); */
365+
port = htonl(port);
366+
mask = ~htonl(0xffff);
349367
break;
350368
default:
351369
return;
352370
}
353-
port = htonl(port << 16);
371+
354372
flow_offload_mangle(entry, flow_offload_l4proto(flow), offset,
355373
&port, &mask);
356374
}
@@ -361,22 +379,26 @@ static void flow_offload_port_dnat(struct net *net,
361379
struct nf_flow_rule *flow_rule)
362380
{
363381
struct flow_action_entry *entry = flow_action_entry_next(flow_rule);
364-
u32 mask = ~htonl(0xffff), port;
382+
u32 mask, port;
365383
u32 offset;
366384

367385
switch (dir) {
368386
case FLOW_OFFLOAD_DIR_ORIGINAL:
369-
port = ntohs(flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_port);
370-
offset = 0; /* offsetof(struct tcphdr, source); */
387+
port = ntohs(flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_port);
388+
offset = 0; /* offsetof(struct tcphdr, dest); */
389+
port = htonl(port);
390+
mask = ~htonl(0xffff);
371391
break;
372392
case FLOW_OFFLOAD_DIR_REPLY:
373-
port = ntohs(flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_port);
374-
offset = 0; /* offsetof(struct tcphdr, dest); */
393+
port = ntohs(flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_port);
394+
offset = 0; /* offsetof(struct tcphdr, source); */
395+
port = htonl(port << 16);
396+
mask = ~htonl(0xffff0000);
375397
break;
376398
default:
377399
return;
378400
}
379-
port = htonl(port);
401+
380402
flow_offload_mangle(entry, flow_offload_l4proto(flow), offset,
381403
&port, &mask);
382404
}
@@ -759,9 +781,9 @@ void nf_flow_offload_stats(struct nf_flowtable *flowtable,
759781
struct flow_offload *flow)
760782
{
761783
struct flow_offload_work *offload;
762-
s64 delta;
784+
__s32 delta;
763785

764-
delta = flow->timeout - jiffies;
786+
delta = nf_flow_timeout_delta(flow->timeout);
765787
if ((delta >= (9 * NF_FLOW_TIMEOUT) / 10) ||
766788
flow->flags & FLOW_OFFLOAD_HW_DYING)
767789
return;

‎net/netfilter/nf_tables_api.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5984,6 +5984,7 @@ nft_flowtable_type_get(struct net *net, u8 family)
59845984
return ERR_PTR(-ENOENT);
59855985
}
59865986

5987+
/* Only called from error and netdev event paths. */
59875988
static void nft_unregister_flowtable_hook(struct net *net,
59885989
struct nft_flowtable *flowtable,
59895990
struct nft_hook *hook)
@@ -5999,7 +6000,7 @@ static void nft_unregister_flowtable_net_hooks(struct net *net,
59996000
struct nft_hook *hook;
60006001

60016002
list_for_each_entry(hook, &flowtable->hook_list, list)
6002-
nft_unregister_flowtable_hook(net, flowtable, hook);
6003+
nf_unregister_net_hook(net, &hook->ops);
60036004
}
60046005

60056006
static int nft_register_flowtable_net_hooks(struct net *net,
@@ -6448,12 +6449,14 @@ static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable)
64486449
{
64496450
struct nft_hook *hook, *next;
64506451

6452+
flowtable->data.type->free(&flowtable->data);
64516453
list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) {
6454+
flowtable->data.type->setup(&flowtable->data, hook->ops.dev,
6455+
FLOW_BLOCK_UNBIND);
64526456
list_del_rcu(&hook->list);
64536457
kfree(hook);
64546458
}
64556459
kfree(flowtable->name);
6456-
flowtable->data.type->free(&flowtable->data);
64576460
module_put(flowtable->data.type->owner);
64586461
kfree(flowtable);
64596462
}
@@ -6497,6 +6500,7 @@ static void nft_flowtable_event(unsigned long event, struct net_device *dev,
64976500
if (hook->ops.dev != dev)
64986501
continue;
64996502

6503+
/* flow_offload_netdev_event() cleans up entries for us. */
65006504
nft_unregister_flowtable_hook(dev_net(dev), flowtable, hook);
65016505
list_del_rcu(&hook->list);
65026506
kfree_rcu(hook, rcu);

‎net/netfilter/nft_flow_offload.c‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,6 @@ static void nft_flow_offload_activate(const struct nft_ctx *ctx,
200200
static void nft_flow_offload_destroy(const struct nft_ctx *ctx,
201201
const struct nft_expr *expr)
202202
{
203-
struct nft_flow_offload *priv = nft_expr_priv(expr);
204-
205-
priv->flowtable->use--;
206203
nf_ct_netns_put(ctx->net, ctx->family);
207204
}
208205

0 commit comments

Comments
 (0)