Skip to contentSkip to conversation

net: ieee802154: fix net_device reference release too early

PATCH In review linzhang 2 participants 3 messages May 18, 2017

Subsystems (3)
lore
Share & tools
lore

linzhang

author
#0 lore raw

PATCH 1/1 net: ieee802154: fix net_device reference release too early

This patch fixes the kernel oops when release net_device reference in advance. In function raw_sendmsg(i think the dgram_sendmsg has the same problem), there is a race condition between dev_put and dev_queue_xmit when the device is gong that maybe lead to dev_queue_ximt to see an illegal net_device pointer.

So i think that dev_put should be behind of the dev_queue_xmit.

Also, explicit set skb->sk is needless, sock_alloc_send_skb is already set it.

---
 net/ieee802154/socket.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
net/ieee802154/socket.c +4 -6
index eedba76..a60658c 100644
@@ -301,15 +301,14 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
 		goto out_skb;
 
 	skb->dev = dev;
-	skb->sk  = sk;
 	skb->protocol = htons(ETH_P_IEEE802154);
 
-	dev_put(dev);
-
 	err = dev_queue_xmit(skb);
 	if (err > 0)
 		err = net_xmit_errno(err);
 
+	dev_put(dev);
+
 	return err ?: size;
 
 out_skb:
@@ -690,15 +689,14 @@ static int dgram_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
 		goto out_skb;
 
 	skb->dev = dev;
-	skb->sk  = sk;
 	skb->protocol = htons(ETH_P_IEEE802154);
 
-	dev_put(dev);
-
 	err = dev_queue_xmit(skb);
 	if (err > 0)
 		err = net_xmit_errno(err);
 
+	dev_put(dev);
+
 	return err ?: size;
 
 out_skb:
Signature · 1 line
1.8.3.1
#1 lore raw

Hello.

Quoted 6 lines · linzhang

This patch fixes the kernel oops when release net_device reference in advance. In function raw_sendmsg(i think the dgram_sendmsg has the same problem), there is a race condition between dev_put and dev_queue_xmit when the device is gong that maybe lead to dev_queue_ximt to see an illegal net_device pointer.

You have a test case to reproduce this oops? I fear I have not seen one.

Quoted 4 lines

So i think that dev_put should be behind of the dev_queue_xmit.

Also, explicit set skb->sk is needless, sock_alloc_send_skb is already set it.

You could have put this fixup in a different patch.

Quoted 1 line

This looks more like a username instead of a real name. If you have Lin Zhang as you English real name that would be better here. :)

Quoted 32 lines
---
 net/ieee802154/socket.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
net/ieee802154/socket.c +2 -4
index eedba76..a60658c 100644
@@ -301,15 +301,14 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
 		goto out_skb;
 
 	skb->dev = dev;
-	skb->sk  = sk;
 	skb->protocol = htons(ETH_P_IEEE802154);
 
-	dev_put(dev);
-
 	err = dev_queue_xmit(skb);
 	if (err > 0)
 		err = net_xmit_errno(err);
 
+	dev_put(dev);
+
 	return err ?: size;
 
 out_skb:
@@ -690,15 +689,14 @@ static int dgram_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
 		goto out_skb;
 
 	skb->dev = dev;
-	skb->sk  = sk;
 	skb->protocol = htons(ETH_P_IEEE802154);

net/ieee802154/socket.c static int dgram_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) · line 700

Quoted 10 lines
net/ieee802154/socket.c +2 -2
 
-	dev_put(dev);
-
 	err = dev_queue_xmit(skb);
 	if (err > 0)
 		err = net_xmit_errno(err);
 
+	dev_put(dev);
+
 	return err ?: size;

Going to give this a test ride here now.

regards
Stefan Schmidt

#2 lore raw

Hello.

Quoted 10 lines · Stefan Schmidt

Hello.

Quoted 6 lines · linzhang

This patch fixes the kernel oops when release net_device reference in advance. In function raw_sendmsg(i think the dgram_sendmsg has the same problem), there is a race condition between dev_put and dev_queue_xmit when the device is gong that maybe lead to dev_queue_ximt to see an illegal net_device pointer.

Quoted 2 lines

You have a test case to reproduce this oops? I fear I have not seen one.

If you have a test case handy adding it to the commit would be handy. If you do not have one around we can do without.

Quoted 6 lines
Quoted 4 lines

So i think that dev_put should be behind of the dev_queue_xmit.

Also, explicit set skb->sk is needless, sock_alloc_send_skb is already set it.

You could have put this fixup in a different patch.

I actually would request you to split this into two patches. One for the removal of the sk setting and one for the race condition fix.

Quoted 4 lines
Quoted 1 line

This looks more like a username instead of a real name. If you have Lin Zhang as you English real name that would be better here. :)

This would be also appreciated.

Quoted 39 lines
Quoted 39 lines
---
 net/ieee802154/socket.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
net/ieee802154/socket.c +2 -6
index eedba76..a60658c 100644
@@ -301,15 +301,14 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
 		goto out_skb;
 
 	skb->dev = dev;
-	skb->sk  = sk;
 	skb->protocol = htons(ETH_P_IEEE802154);
 
-	dev_put(dev);
-
 	err = dev_queue_xmit(skb);
 	if (err > 0)
 		err = net_xmit_errno(err);
 
+	dev_put(dev);
+
 	return err ?: size;
 
 out_skb:
@@ -690,15 +689,14 @@ static int dgram_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
 		goto out_skb;
 
 	skb->dev = dev;
-	skb->sk  = sk;
 	skb->protocol = htons(ETH_P_IEEE802154);
 
-	dev_put(dev);
-
 	err = dev_queue_xmit(skb);
 	if (err > 0)
 		err = net_xmit_errno(err);
 
Quoted 5 lines

net/ieee802154/socket.c static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) · line 312

Quoted 3 lines
net/ieee802154/socket.c +2 -0
+	dev_put(dev);
+
 	return err ?: size;

Going to give this a test ride here now.

I gave it a ride in my testbed and I encountered no problems. While I have never seen the race and oops myself doing the dev_put before the xmit can surely lead to such a race and the fix is valid.

Once you have done the changes requested above and re-submit your two patches you can add my

to both of them.

regards
Stefan Schmidt