Skip to content

Commit 1f770c0

Browse files
herbertxdavem330
authored andcommitted
netlink: Fix autobind race condition that leads to zero port ID
The commit c0bb07d ("netlink: Reset portid after netlink_insert failure") introduced a race condition where if two threads try to autobind the same socket one of them may end up with a zero port ID. This led to kernel deadlocks that were observed by multiple people. This patch reverts that commit and instead fixes it by introducing a separte rhash_portid variable so that the real portid is only set after the socket has been successfully hashed. Fixes: c0bb07d ("netlink: Reset portid after netlink_insert failure") Reported-by: Tejun Heo <[email protected]> Reported-by: Linus Torvalds <[email protected]> Signed-off-by: Herbert Xu <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 3ea7924 commit 1f770c0

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

net/netlink/af_netlink.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ static inline int netlink_compare(struct rhashtable_compare_arg *arg,
10311031
const struct netlink_compare_arg *x = arg->key;
10321032
const struct netlink_sock *nlk = ptr;
10331033

1034-
return nlk->portid != x->portid ||
1034+
return nlk->rhash_portid != x->portid ||
10351035
!net_eq(sock_net(&nlk->sk), read_pnet(&x->pnet));
10361036
}
10371037

@@ -1057,7 +1057,7 @@ static int __netlink_insert(struct netlink_table *table, struct sock *sk)
10571057
{
10581058
struct netlink_compare_arg arg;
10591059

1060-
netlink_compare_arg_init(&arg, sock_net(sk), nlk_sk(sk)->portid);
1060+
netlink_compare_arg_init(&arg, sock_net(sk), nlk_sk(sk)->rhash_portid);
10611061
return rhashtable_lookup_insert_key(&table->hash, &arg,
10621062
&nlk_sk(sk)->node,
10631063
netlink_rhashtable_params);
@@ -1119,7 +1119,7 @@ static int netlink_insert(struct sock *sk, u32 portid)
11191119
unlikely(atomic_read(&table->hash.nelems) >= UINT_MAX))
11201120
goto err;
11211121

1122-
nlk_sk(sk)->portid = portid;
1122+
nlk_sk(sk)->rhash_portid = portid;
11231123
sock_hold(sk);
11241124

11251125
err = __netlink_insert(table, sk);
@@ -1131,10 +1131,12 @@ static int netlink_insert(struct sock *sk, u32 portid)
11311131
err = -EOVERFLOW;
11321132
if (err == -EEXIST)
11331133
err = -EADDRINUSE;
1134-
nlk_sk(sk)->portid = 0;
11351134
sock_put(sk);
1135+
goto err;
11361136
}
11371137

1138+
nlk_sk(sk)->portid = portid;
1139+
11381140
err:
11391141
release_sock(sk);
11401142
return err;
@@ -3271,7 +3273,7 @@ static inline u32 netlink_hash(const void *data, u32 len, u32 seed)
32713273
const struct netlink_sock *nlk = data;
32723274
struct netlink_compare_arg arg;
32733275

3274-
netlink_compare_arg_init(&arg, sock_net(&nlk->sk), nlk->portid);
3276+
netlink_compare_arg_init(&arg, sock_net(&nlk->sk), nlk->rhash_portid);
32753277
return jhash2((u32 *)&arg, netlink_compare_arg_len / sizeof(u32), seed);
32763278
}
32773279

net/netlink/af_netlink.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct netlink_ring {
2525
struct netlink_sock {
2626
/* struct sock has to be the first member of netlink_sock */
2727
struct sock sk;
28+
u32 rhash_portid;
2829
u32 portid;
2930
u32 dst_portid;
3031
u32 dst_group;

0 commit comments

Comments
 (0)