Skip to contentSkip to conversation

kobject: fix use-after-free in tagged uevent broadcast

PATCH Superseded Ömer Mete Kaya 2 participants 3 messages Nov 23

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 kobject: fix use-after-free in tagged uevent broadcast

kobject_uevent_net_broadcast() accesses net->uevent_sock->sk without protection in the tagged (namespace-aware) path. During network namespace teardown, uevent_net_exit() frees the uevent_sock while a concurrent kobject_uevent_env() call can still reference it, causing a use-after-free.

Extend uevent_sock_mutex to cover the tagged broadcast path and the netlink receive path, serializing them with teardown: in uevent_net_exit(), set net->uevent_sock to NULL under the mutex before releasing the socket. In the tagged broadcast path and in uevent_net_rcv_skb(), check for NULL under the same mutex before accessing the socket.

Fixes 5c0941c55e5f ("kobject: reduce uevent_sock_mutex scope")

---
v2: Add Closes: tag.

 lib/kobject_uevent.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
lib/kobject_uevent.c +22 -8
index ddbc4d7482d2..fd61307c045e 100644
@@ -411,9 +411,15 @@ static int kobject_uevent_net_broadcast(struct kobject *kobj,
 						    devpath);
 	else {
 		const struct net *net = container_of(ns, struct net, ns);
+		struct uevent_sock *ue_sk;
 
-		ret = uevent_net_broadcast_tagged(net->uevent_sock->sk, env,
-						  action_string, devpath);
+		mutex_lock(&uevent_sock_mutex);
+		ue_sk = net->uevent_sock;
+		if (ue_sk)
+			ret = uevent_net_broadcast_tagged(ue_sk->sk, env,
+							     action_string,
+							     devpath);
+		mutex_unlock(&uevent_sock_mutex);
 	}
 #endif
 
@@ -743,6 +749,7 @@ static int uevent_net_broadcast(struct sock *usk, struct sk_buff *skb,
 static int uevent_net_rcv_skb(struct sk_buff *skb, struct nlmsghdr *nlh,
 			      struct netlink_ext_ack *extack)
 {
+	struct uevent_sock *ue_sk;
 	struct net *net;
 	int ret;
 
@@ -760,7 +767,13 @@ static int uevent_net_rcv_skb(struct sk_buff *skb, struct nlmsghdr *nlh,
 		return -EPERM;
 	}
 
-	ret = uevent_net_broadcast(net->uevent_sock->sk, skb, extack);
+	mutex_lock(&uevent_sock_mutex);
+	ue_sk = net->uevent_sock;
+	if (ue_sk)
+		ret = uevent_net_broadcast(ue_sk->sk, skb, extack);
+	else
+		ret = -ENODEV;
+	mutex_unlock(&uevent_sock_mutex);
 
 	return ret;
 }
@@ -804,13 +817,14 @@ static int uevent_net_init(struct net *net)
 
 static void uevent_net_exit(struct net *net)
 {
-	struct uevent_sock *ue_sk = net->uevent_sock;
+	struct uevent_sock *ue_sk;
 
-	if (sock_net(ue_sk->sk)->user_ns == &init_user_ns) {
-		mutex_lock(&uevent_sock_mutex);
+	mutex_lock(&uevent_sock_mutex);
+	ue_sk = net->uevent_sock;
+	net->uevent_sock = NULL;
+	if (sock_net(ue_sk->sk)->user_ns == &init_user_ns)
 		list_del(&ue_sk->list);
-		mutex_unlock(&uevent_sock_mutex);
-	}
+	mutex_unlock(&uevent_sock_mutex);
 
 	netlink_kernel_release(ue_sk->sk);
 	kfree(ue_sk);
Signature · 1 line
2.56.0
maintainer
#1 lore raw
Quoted 91 lines · Ömer Mete Kaya

kobject_uevent_net_broadcast() accesses net->uevent_sock->sk without protection in the tagged (namespace-aware) path. During network namespace teardown, uevent_net_exit() frees the uevent_sock while a concurrent kobject_uevent_env() call can still reference it, causing a use-after-free.

Extend uevent_sock_mutex to cover the tagged broadcast path and the netlink receive path, serializing them with teardown: in uevent_net_exit(), set net->uevent_sock to NULL under the mutex before releasing the socket. In the tagged broadcast path and in uevent_net_rcv_skb(), check for NULL under the same mutex before accessing the socket.

Fixes 5c0941c55e5f ("kobject: reduce uevent_sock_mutex scope")

---
v2: Add Closes: tag.

 lib/kobject_uevent.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
lib/kobject_uevent.c +22 -8
index ddbc4d7482d2..fd61307c045e 100644
@@ -411,9 +411,15 @@ static int kobject_uevent_net_broadcast(struct kobject *kobj,
 						    devpath);
 	else {
 		const struct net *net = container_of(ns, struct net, ns);
+		struct uevent_sock *ue_sk;
 
-		ret = uevent_net_broadcast_tagged(net->uevent_sock->sk, env,
-						  action_string, devpath);
+		mutex_lock(&uevent_sock_mutex);
+		ue_sk = net->uevent_sock;
+		if (ue_sk)
+			ret = uevent_net_broadcast_tagged(ue_sk->sk, env,
+							     action_string,
+							     devpath);
+		mutex_unlock(&uevent_sock_mutex);
 	}
 #endif
 
@@ -743,6 +749,7 @@ static int uevent_net_broadcast(struct sock *usk, struct sk_buff *skb,
 static int uevent_net_rcv_skb(struct sk_buff *skb, struct nlmsghdr *nlh,
 			      struct netlink_ext_ack *extack)
 {
+	struct uevent_sock *ue_sk;
 	struct net *net;
 	int ret;
 
@@ -760,7 +767,13 @@ static int uevent_net_rcv_skb(struct sk_buff *skb, struct nlmsghdr *nlh,
 		return -EPERM;
 	}
 
-	ret = uevent_net_broadcast(net->uevent_sock->sk, skb, extack);
+	mutex_lock(&uevent_sock_mutex);
+	ue_sk = net->uevent_sock;
+	if (ue_sk)
+		ret = uevent_net_broadcast(ue_sk->sk, skb, extack);
+	else
+		ret = -ENODEV;
+	mutex_unlock(&uevent_sock_mutex);
 
 	return ret;
 }
@@ -804,13 +817,14 @@ static int uevent_net_init(struct net *net)
 
 static void uevent_net_exit(struct net *net)
 {
-	struct uevent_sock *ue_sk = net->uevent_sock;
+	struct uevent_sock *ue_sk;
 
-	if (sock_net(ue_sk->sk)->user_ns == &init_user_ns) {
-		mutex_lock(&uevent_sock_mutex);
+	mutex_lock(&uevent_sock_mutex);
+	ue_sk = net->uevent_sock;
+	net->uevent_sock = NULL;
+	if (sock_net(ue_sk->sk)->user_ns == &init_user_ns)
 		list_del(&ue_sk->list);
-		mutex_unlock(&uevent_sock_mutex);
-	}
+	mutex_unlock(&uevent_sock_mutex);
 
 	netlink_kernel_release(ue_sk->sk);
 	kfree(ue_sk);
Signature · 1 line
2.56.0

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him a patch that has triggered this response. He used to manually respond to these common problems, but in order to save his sanity (he kept writing the same thing over and over, yet to different people), I was created. Hopefully you will not take offence and will fix the problem in your patch and resubmit it so that it can be accepted into the Linux kernel tree.

You are receiving this message because of the following common error(s) as indicated below:

- You have marked a patch with a "Fixes:" tag for a commit that is in an older released kernel, yet you do not have a cc: stable line in the signed-off-by area at all, which means that the patch will not be applied to any older kernel releases. To properly fix this, please follow the documented rules in the Documentation/process/stable-kernel-rules.rst file for how to resolve this.

If you wish to discuss this problem further, or you have questions about how to resolve this issue, please feel free to respond to this email and Greg will reply once he has dug out from the pending patches received from other developers.

thanks,

greg k-h's patch email bot

maintainer
#2 lore raw
In reply to Ömer Mete Kaya
Quoted 27 lines · Ömer Mete Kaya

kobject_uevent_net_broadcast() accesses net->uevent_sock->sk without protection in the tagged (namespace-aware) path. During network namespace teardown, uevent_net_exit() frees the uevent_sock while a concurrent kobject_uevent_env() call can still reference it, causing a use-after-free.

Extend uevent_sock_mutex to cover the tagged broadcast path and the netlink receive path, serializing them with teardown: in uevent_net_exit(), set net->uevent_sock to NULL under the mutex before releasing the socket. In the tagged broadcast path and in uevent_net_rcv_skb(), check for NULL under the same mutex before accessing the socket.

Fixes 5c0941c55e5f ("kobject: reduce uevent_sock_mutex scope")

---
v2: Add Closes: tag.

 lib/kobject_uevent.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
lib/kobject_uevent.c +0 -0
index ddbc4d7482d2..fd61307c045e 100644

lib/kobject_uevent.c static int kobject_uevent_net_broadcast(struct kobject *kobj, · line 416

Quoted 8 lines
lib/kobject_uevent.c +2 -2
@@ -411,9 +411,15 @@ static int kobject_uevent_net_broadcast(struct kobject *kobj,
 						    devpath);
 	else {
 		const struct net *net = container_of(ns, struct net, ns);
+		struct uevent_sock *ue_sk;
 
-		ret = uevent_net_broadcast_tagged(net->uevent_sock->sk, env,
-						  action_string, devpath);
+		mutex_lock(&uevent_sock_mutex);

guard()?