Skip to contentSkip to conversation

rds: fix two RCU related problems

PATCH In review Cong Wang 3 participants 3 messages Sep 11, 2018 – Sep 12, 2018

Subsystems (3)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH v2 1/1 rds: fix two RCU related problems

When a rds sock is bound, it is inserted into the bind_hash_table which is protected by RCU. But when releasing rds sock, after it is removed from this hash table, it is freed immediately without respecting RCU grace period. This could cause some use-after-free as reported by syzbot.

Mark the rds sock with SOCK_RCU_FREE before inserting it into the bind_hash_table, so that it would be always freed after a RCU grace period.

The other problem is in rds_find_bound(), the rds sock could be freed in between rhashtable_lookup_fast() and rds_sock_addref(), so we need to extend RCU read lock protection in rds_find_bound() to close this race condition.

Reported-and-tested-by [email protected]
Reported-by [email protected]
Cc Sowmini Varadhan <[email protected]>
Cc Santosh Shilimkar <[email protected]>
---
 net/rds/bind.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/rds/bind.c +4 -1
index 3ab55784b637..762d2c6788a3 100644
@@ -76,11 +76,13 @@ struct rds_sock *rds_find_bound(const struct in6_addr *addr, __be16 port,
 	struct rds_sock *rs;
 
 	__rds_create_bind_key(key, addr, port, scope_id);
-	rs = rhashtable_lookup_fast(&bind_hash_table, key, ht_parms);
+	rcu_read_lock();
+	rs = rhashtable_lookup(&bind_hash_table, key, ht_parms);
 	if (rs && !sock_flag(rds_rs_to_sk(rs), SOCK_DEAD))
 		rds_sock_addref(rs);
 	else
 		rs = NULL;
+	rcu_read_unlock();
 
 	rdsdebug("returning rs %p for %pI6c:%u\n", rs, addr,
 		 ntohs(port));
@@ -235,6 +237,7 @@ int rds_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
 		goto out;
 	}
 
+	sock_set_flag(sk, SOCK_RCU_FREE);
 	ret = rds_add_bound(rs, binding_addr, &port, scope_id);
 	if (ret)
 		goto out;
Signature · 1 line
2.14.4
Acked-by on v2 1/1
#1 lore raw
Quoted 15 lines · Cong Wang

When a rds sock is bound, it is inserted into the bind_hash_table which is protected by RCU. But when releasing rds sock, after it is removed from this hash table, it is freed immediately without respecting RCU grace period. This could cause some use-after-free as reported by syzbot.

Mark the rds sock with SOCK_RCU_FREE before inserting it into the bind_hash_table, so that it would be always freed after a RCU grace period.

The other problem is in rds_find_bound(), the rds sock could be freed in between rhashtable_lookup_fast() and rds_sock_addref(), so we need to extend RCU read lock protection in rds_find_bound() to close this race condition.

Quoted 7 lines
Reported-and-tested-by [email protected]
Reported-by [email protected]
Cc Sowmini Varadhan <[email protected]>
Cc Santosh Shilimkar <[email protected]>

---

Thank you !!

maintainer
#2 lore raw
In reply to Cong Wang

From: Cong Wang <[email protected]>
Date: Mon, 10 Sep 2018 18:27:26 -0700

Quoted 15 lines

When a rds sock is bound, it is inserted into the bind_hash_table which is protected by RCU. But when releasing rds sock, after it is removed from this hash table, it is freed immediately without respecting RCU grace period. This could cause some use-after-free as reported by syzbot.

Mark the rds sock with SOCK_RCU_FREE before inserting it into the bind_hash_table, so that it would be always freed after a RCU grace period.

The other problem is in rds_find_bound(), the rds sock could be freed in between rhashtable_lookup_fast() and rds_sock_addref(), so we need to extend RCU read lock protection in rds_find_bound() to close this race condition.

Quoted 6 lines
Reported-and-tested-by [email protected]
Reported-by [email protected]
Cc Sowmini Varadhan <[email protected]>
Cc Santosh Shilimkar <[email protected]>

Applied and queued up for -stable, thanks.