Skip to content

Commit 596cf3f

Browse files
vpai-akamaiummakynes
authored andcommitted
netfilter: ipset: fix race condition in ipset save, swap and delete
This fix adds a new reference counter (ref_netlink) for the struct ip_set. The other reference counter (ref) can be swapped out by ip_set_swap and we need a separate counter to keep track of references for netlink events like dump. Using the same ref counter for dump causes a race condition which can be demonstrated by the following script: ipset create hash_ip1 hash:ip family inet hashsize 1024 maxelem 500000 \ counters ipset create hash_ip2 hash:ip family inet hashsize 300000 maxelem 500000 \ counters ipset create hash_ip3 hash:ip family inet hashsize 1024 maxelem 500000 \ counters ipset save & ipset swap hash_ip3 hash_ip2 ipset destroy hash_ip3 /* will crash the machine */ Swap will exchange the values of ref so destroy will see ref = 0 instead of ref = 1. With this fix in place swap will not succeed because ipset save still has ref_netlink on the set (ip_set_swap doesn't swap ref_netlink). Both delete and swap will error out if ref_netlink != 0 on the set. Note: The changes to *_head functions is because previously we would increment ref whenever we called these functions, we don't do that anymore. Reviewed-by: Joshua Hunt <johunt@akamai.com> Signed-off-by: Vishwanath Pai <vpai@akamai.com> Signed-off-by: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent d7be81a commit 596cf3f

5 files changed

Lines changed: 35 additions & 8 deletions

File tree

‎include/linux/netfilter/ipset/ip_set.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ struct ip_set {
234234
spinlock_t lock;
235235
/* References to the set */
236236
u32 ref;
237+
/* References to the set for netlink events like dump,
238+
* ref can be swapped out by ip_set_swap
239+
*/
240+
u32 ref_netlink;
237241
/* The core set type */
238242
struct ip_set_type *type;
239243
/* The type variant doing the real job */

‎net/netfilter/ipset/ip_set_bitmap_gen.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ mtype_head(struct ip_set *set, struct sk_buff *skb)
9595
if (!nested)
9696
goto nla_put_failure;
9797
if (mtype_do_head(skb, map) ||
98-
nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1)) ||
98+
nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref)) ||
9999
nla_put_net32(skb, IPSET_ATTR_MEMSIZE, htonl(memsize)))
100100
goto nla_put_failure;
101101
if (unlikely(ip_set_put_flags(skb, set)))

‎net/netfilter/ipset/ip_set_core.c‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,26 @@ __ip_set_put(struct ip_set *set)
497497
write_unlock_bh(&ip_set_ref_lock);
498498
}
499499

500+
/* set->ref can be swapped out by ip_set_swap, netlink events (like dump) need
501+
* a separate reference counter
502+
*/
503+
static inline void
504+
__ip_set_get_netlink(struct ip_set *set)
505+
{
506+
write_lock_bh(&ip_set_ref_lock);
507+
set->ref_netlink++;
508+
write_unlock_bh(&ip_set_ref_lock);
509+
}
510+
511+
static inline void
512+
__ip_set_put_netlink(struct ip_set *set)
513+
{
514+
write_lock_bh(&ip_set_ref_lock);
515+
BUG_ON(set->ref_netlink == 0);
516+
set->ref_netlink--;
517+
write_unlock_bh(&ip_set_ref_lock);
518+
}
519+
500520
/* Add, del and test set entries from kernel.
501521
*
502522
* The set behind the index must exist and must be referenced
@@ -1002,7 +1022,7 @@ static int ip_set_destroy(struct net *net, struct sock *ctnl,
10021022
if (!attr[IPSET_ATTR_SETNAME]) {
10031023
for (i = 0; i < inst->ip_set_max; i++) {
10041024
s = ip_set(inst, i);
1005-
if (s && s->ref) {
1025+
if (s && (s->ref || s->ref_netlink)) {
10061026
ret = -IPSET_ERR_BUSY;
10071027
goto out;
10081028
}
@@ -1024,7 +1044,7 @@ static int ip_set_destroy(struct net *net, struct sock *ctnl,
10241044
if (!s) {
10251045
ret = -ENOENT;
10261046
goto out;
1027-
} else if (s->ref) {
1047+
} else if (s->ref || s->ref_netlink) {
10281048
ret = -IPSET_ERR_BUSY;
10291049
goto out;
10301050
}
@@ -1171,6 +1191,9 @@ static int ip_set_swap(struct net *net, struct sock *ctnl, struct sk_buff *skb,
11711191
from->family == to->family))
11721192
return -IPSET_ERR_TYPE_MISMATCH;
11731193

1194+
if (from->ref_netlink || to->ref_netlink)
1195+
return -EBUSY;
1196+
11741197
strncpy(from_name, from->name, IPSET_MAXNAMELEN);
11751198
strncpy(from->name, to->name, IPSET_MAXNAMELEN);
11761199
strncpy(to->name, from_name, IPSET_MAXNAMELEN);
@@ -1206,7 +1229,7 @@ ip_set_dump_done(struct netlink_callback *cb)
12061229
if (set->variant->uref)
12071230
set->variant->uref(set, cb, false);
12081231
pr_debug("release set %s\n", set->name);
1209-
__ip_set_put_byindex(inst, index);
1232+
__ip_set_put_netlink(set);
12101233
}
12111234
return 0;
12121235
}
@@ -1328,7 +1351,7 @@ ip_set_dump_start(struct sk_buff *skb, struct netlink_callback *cb)
13281351
if (!cb->args[IPSET_CB_ARG0]) {
13291352
/* Start listing: make sure set won't be destroyed */
13301353
pr_debug("reference set\n");
1331-
set->ref++;
1354+
set->ref_netlink++;
13321355
}
13331356
write_unlock_bh(&ip_set_ref_lock);
13341357
nlh = start_msg(skb, NETLINK_CB(cb->skb).portid,
@@ -1396,7 +1419,7 @@ ip_set_dump_start(struct sk_buff *skb, struct netlink_callback *cb)
13961419
if (set->variant->uref)
13971420
set->variant->uref(set, cb, false);
13981421
pr_debug("release set %s\n", set->name);
1399-
__ip_set_put_byindex(inst, index);
1422+
__ip_set_put_netlink(set);
14001423
cb->args[IPSET_CB_ARG0] = 0;
14011424
}
14021425
out:

‎net/netfilter/ipset/ip_set_hash_gen.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ mtype_head(struct ip_set *set, struct sk_buff *skb)
10821082
if (nla_put_u32(skb, IPSET_ATTR_MARKMASK, h->markmask))
10831083
goto nla_put_failure;
10841084
#endif
1085-
if (nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1)) ||
1085+
if (nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref)) ||
10861086
nla_put_net32(skb, IPSET_ATTR_MEMSIZE, htonl(memsize)))
10871087
goto nla_put_failure;
10881088
if (unlikely(ip_set_put_flags(skb, set)))

‎net/netfilter/ipset/ip_set_list_set.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ list_set_head(struct ip_set *set, struct sk_buff *skb)
458458
if (!nested)
459459
goto nla_put_failure;
460460
if (nla_put_net32(skb, IPSET_ATTR_SIZE, htonl(map->size)) ||
461-
nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1)) ||
461+
nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref)) ||
462462
nla_put_net32(skb, IPSET_ATTR_MEMSIZE,
463463
htonl(sizeof(*map) + n * set->dsize)))
464464
goto nla_put_failure;

0 commit comments

Comments
 (0)