Skip to content

Commit 592fcb9

Browse files
Eric Dumazetdavem330
authored andcommitted
ip: ip_ra_control() rcu fix
commit 6601850 (ip: Router Alert RCU conversion) introduced RCU lookups to ip_call_ra_chain(). It missed proper deinit phase : When ip_ra_control() deletes an ip_ra_chain, it should make sure ip_call_ra_chain() users can not start to use socket during the rcu grace period. It should also delay the sock_put() after the grace period, or we risk a premature socket freeing and corruptions, as raw sockets are not rcu protected yet. This delay avoids using expensive atomic_inc_not_zero() in ip_call_ra_chain(). Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 96b52e6 commit 592fcb9

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

‎include/net/ip.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ struct ipcm_cookie {
6161
struct ip_ra_chain {
6262
struct ip_ra_chain *next;
6363
struct sock *sk;
64-
void (*destructor)(struct sock *);
64+
union {
65+
void (*destructor)(struct sock *);
66+
struct sock *saved_sk;
67+
};
6568
struct rcu_head rcu;
6669
};
6770

‎net/ipv4/ip_sockglue.c‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,13 @@ int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
241241
struct ip_ra_chain *ip_ra_chain;
242242
static DEFINE_SPINLOCK(ip_ra_lock);
243243

244-
static void ip_ra_free_rcu(struct rcu_head *head)
244+
245+
static void ip_ra_destroy_rcu(struct rcu_head *head)
245246
{
246-
kfree(container_of(head, struct ip_ra_chain, rcu));
247+
struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
248+
249+
sock_put(ra->saved_sk);
250+
kfree(ra);
247251
}
248252

249253
int ip_ra_control(struct sock *sk, unsigned char on,
@@ -264,13 +268,20 @@ int ip_ra_control(struct sock *sk, unsigned char on,
264268
kfree(new_ra);
265269
return -EADDRINUSE;
266270
}
271+
/* dont let ip_call_ra_chain() use sk again */
272+
ra->sk = NULL;
267273
rcu_assign_pointer(*rap, ra->next);
268274
spin_unlock_bh(&ip_ra_lock);
269275

270276
if (ra->destructor)
271277
ra->destructor(sk);
272-
sock_put(sk);
273-
call_rcu(&ra->rcu, ip_ra_free_rcu);
278+
/*
279+
* Delay sock_put(sk) and kfree(ra) after one rcu grace
280+
* period. This guarantee ip_call_ra_chain() dont need
281+
* to mess with socket refcounts.
282+
*/
283+
ra->saved_sk = sk;
284+
call_rcu(&ra->rcu, ip_ra_destroy_rcu);
274285
return 0;
275286
}
276287
}

0 commit comments

Comments
 (0)