Skip to contentSkip to conversation

qrtr: orphan skb before queuing in xmit

PATCH In review Cong Wang 2 participants 5 messages Jul 24, 2020

Subsystems (3)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 qrtr: orphan skb before queuing in xmit

Similar to tun_net_xmit(), we have to orphan the skb before queuing it, otherwise we may use the socket when purging the queue after it is freed by user-space.

Reported-and-tested-by [email protected]
Fixes 28fb4e59a47d ("net: qrtr: Expose tunneling endpoint to user space")
Cc Bjorn Andersson <[email protected]>
---
 net/qrtr/tun.c | 1 +
 1 file changed, 1 insertion(+)
net/qrtr/tun.c +1 -0
index 15ce9b642b25..54a565dcfef3 100644
@@ -20,6 +20,7 @@ static int qrtr_tun_send(struct qrtr_endpoint *ep, struct sk_buff *skb)
 {
 	struct qrtr_tun *tun = container_of(ep, struct qrtr_tun, ep);
 
+	skb_orphan(skb);
 	skb_queue_tail(&tun->queue, skb);
 
 	/* wake up any blocking processes, waiting for new data */
Signature · 1 line
2.27.0
#1 lore raw
Quoted 3 lines · Cong Wang

Similar to tun_net_xmit(), we have to orphan the skb before queuing it, otherwise we may use the socket when purging the queue after it is freed by user-space.

Which socket ?

By not calling skb_orphan(skb), this skb should own a reference on skb->sk preventing skb->sk to disappear.

It seems that instead of skb_orphan() here, we could avoid calling skb_set_owner_w() in the first place, because this is confusing.

Also, there seems to be no limit on number of skbs that can be queued, this looks rather dangerous.

net/qrtr/qrtr.c +0 -2
index 0cb4adfc6641da0b1add7a8b40957fd23df1e075..9b180768dbfe9ea100ad008d92522e340f73101e 100644
@@ -661,7 +661,6 @@ static void qrtr_port_remove(struct qrtr_sock *ipc)
                pkt->client.node = cpu_to_le32(ipc->us.sq_node);
                pkt->client.port = cpu_to_le32(ipc->us.sq_port);
 
-               skb_set_owner_w(skb, &ipc->sk);
                qrtr_bcast_enqueue(NULL, skb, QRTR_TYPE_DEL_CLIENT, &ipc->us,
                                   &to);
        }
@@ -855,7 +854,6 @@ static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,
                skbn = skb_clone(skb, GFP_KERNEL);
                if (!skbn)
                        break;
-               skb_set_owner_w(skbn, skb->sk);
                qrtr_node_enqueue(node, skbn, type, from, to);
        }
        mutex_unlock(&qrtr_node_lock);
Quoted 22 lines
Reported-and-tested-by [email protected]
Fixes 28fb4e59a47d ("net: qrtr: Expose tunneling endpoint to user space")
Cc Bjorn Andersson <[email protected]>
---
 net/qrtr/tun.c | 1 +
 1 file changed, 1 insertion(+)
net/qrtr/tun.c +1 -0
index 15ce9b642b25..54a565dcfef3 100644
@@ -20,6 +20,7 @@ static int qrtr_tun_send(struct qrtr_endpoint *ep, struct sk_buff *skb)
 {
 	struct qrtr_tun *tun = container_of(ep, struct qrtr_tun, ep);
 
+	skb_orphan(skb);
 	skb_queue_tail(&tun->queue, skb);
 
 	/* wake up any blocking processes, waiting for new data */
author
#2 lore raw
Quoted 9 lines · Eric Dumazet
Quoted 3 lines · Cong Wang

Similar to tun_net_xmit(), we have to orphan the skb before queuing it, otherwise we may use the socket when purging the queue after it is freed by user-space.

Which socket ?

sk->sk_wq points to &sock->wq. The socket is of course from qrtr_create().

Quoted 4 lines

By not calling skb_orphan(skb), this skb should own a reference on skb->sk preventing skb->sk to disappear.

I said socket, not sock. I believe the socket can be gone while the sock is still there.

Quoted 2 lines

It seems that instead of skb_orphan() here, we could avoid calling skb_set_owner_w() in the first place, because this is confusing.

Not sure about this, at least tun calls skb_set_owner_w() too. More importantly, sock_alloc_send_skb() calls it too. :)

author
#3 lore raw
Quoted 3 lines · Cong Wang

I said socket, not sock. I believe the socket can be gone while the sock is still there.

Hmm, looks llike sock_orphan() should be called...

#4 lore raw
In reply to Cong Wang
Quoted 23 lines · Cong Wang
Quoted 9 lines · Eric Dumazet
Quoted 3 lines · Cong Wang

Similar to tun_net_xmit(), we have to orphan the skb before queuing it, otherwise we may use the socket when purging the queue after it is freed by user-space.

Which socket ?

sk->sk_wq points to &sock->wq. The socket is of course from qrtr_create().

Quoted 4 lines

By not calling skb_orphan(skb), this skb should own a reference on skb->sk preventing skb->sk to disappear.

I said socket, not sock. I believe the socket can be gone while the sock is still there.

Quoted 6 lines
Quoted 2 lines

It seems that instead of skb_orphan() here, we could avoid calling skb_set_owner_w() in the first place, because this is confusing.

Not sure about this, at least tun calls skb_set_owner_w() too. More importantly, sock_alloc_send_skb() calls it too. :)

tun is very different : skbs reaching it came come from all over the places, like TCP stack. Their skb->sk is not pointing to the tun sock.

Here, the skbs are cooked from net/qrtr/qrtr.c locations only.