Skip to content

Commit 6d772ac

Browse files
edumazetdavem330
authored andcommitted
netlink: use kfree_rcu() in netlink_release()
On some suspend/resume operations involving wimax device, we have noticed some intermittent memory corruptions in netlink code. Stéphane Marchesin tracked this corruption in netlink_update_listeners() and suggested a patch. It appears netlink_release() should use kfree_rcu() instead of kfree() for the listeners structure as it may be used by other cpus using RCU protection. netlink_release() must set to NULL the listeners pointer when it is about to be freed. Also have to protect netlink_update_listeners() and netlink_has_listeners() if listeners is NULL. Add a nl_deref_protected() lockdep helper to properly document which locks protects us. Reported-by: Jonathan Kliegman <kliegs@google.com> Signed-off-by: Eric Dumazet <edumazet@google.com> Cc: Stéphane Marchesin <marcheu@google.com> Cc: Sam Leffler <sleffler@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 13d82bf commit 6d772ac

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

net/netlink/af_netlink.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ static int netlink_dump(struct sock *sk);
138138
static DEFINE_RWLOCK(nl_table_lock);
139139
static atomic_t nl_table_users = ATOMIC_INIT(0);
140140

141+
#define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
142+
141143
static ATOMIC_NOTIFIER_HEAD(netlink_chain);
142144

143145
static inline u32 netlink_group_mask(u32 group)
@@ -345,14 +347,19 @@ netlink_update_listeners(struct sock *sk)
345347
struct hlist_node *node;
346348
unsigned long mask;
347349
unsigned int i;
350+
struct listeners *listeners;
351+
352+
listeners = nl_deref_protected(tbl->listeners);
353+
if (!listeners)
354+
return;
348355

349356
for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
350357
mask = 0;
351358
sk_for_each_bound(sk, node, &tbl->mc_list) {
352359
if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
353360
mask |= nlk_sk(sk)->groups[i];
354361
}
355-
tbl->listeners->masks[i] = mask;
362+
listeners->masks[i] = mask;
356363
}
357364
/* this function is only called with the netlink table "grabbed", which
358365
* makes sure updates are visible before bind or setsockopt return. */
@@ -536,7 +543,11 @@ static int netlink_release(struct socket *sock)
536543
if (netlink_is_kernel(sk)) {
537544
BUG_ON(nl_table[sk->sk_protocol].registered == 0);
538545
if (--nl_table[sk->sk_protocol].registered == 0) {
539-
kfree(nl_table[sk->sk_protocol].listeners);
546+
struct listeners *old;
547+
548+
old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
549+
RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
550+
kfree_rcu(old, rcu);
540551
nl_table[sk->sk_protocol].module = NULL;
541552
nl_table[sk->sk_protocol].bind = NULL;
542553
nl_table[sk->sk_protocol].flags = 0;
@@ -982,7 +993,7 @@ int netlink_has_listeners(struct sock *sk, unsigned int group)
982993
rcu_read_lock();
983994
listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
984995

985-
if (group - 1 < nl_table[sk->sk_protocol].groups)
996+
if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
986997
res = test_bit(group - 1, listeners->masks);
987998

988999
rcu_read_unlock();
@@ -1625,7 +1636,7 @@ int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
16251636
new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
16261637
if (!new)
16271638
return -ENOMEM;
1628-
old = rcu_dereference_protected(tbl->listeners, 1);
1639+
old = nl_deref_protected(tbl->listeners);
16291640
memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
16301641
rcu_assign_pointer(tbl->listeners, new);
16311642

0 commit comments

Comments
 (0)