virtio_net: fix use after free
Subsystems (4)
Michael S. Tsirkin
maintainerPATCH 1/1 virtio_net: fix use after free
commit 0b725a2ca61bedc33a2a63d0451d528b268cf975
net: Remove ndo_xmit_flush netdev operation, use signalling instead.
added code that looks at skb->xmit_more after the skb has been put in TX VQ. Since some paths process the ring and free the skb immediately, this can cause use after free.
Fix by storing xmit_more in a local variable.
---
David, am I using the API correctly?
Seems to work for me.
You used __netif_subqueue_stopped but that seems to use
a slightly more expensive test_bit internally.
The reason I added a variable for the txq here is because it's handy for BQL patch later on.
drivers/net/virtio_net.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +3 -1
@@ -920,6 +920,8 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)int qnum =skb_get_queue_mapping (skb);struct send_queue *sq = &vi->sq[qnum];int err; +struct netdev_queue *txq =netdev_get_tx_queue (dev, qnum); +bool kick = !skb->xmit_more;/* Free up any pending old buffers before queueing new ones. */ free_old_xmit_skbs (sq);
@@ -956,7 +958,7 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) } } -if (__netif_subqueue_stopped (dev, qnum) || !skb->xmit_more) +if (kick ||netif_xmit_stopped (txq))virtqueue_kick (sq->vq);return NETDEV_TX_OK ;
Signature · 1 line
MST
From: Michael S. Tsirkin
Quoted 29 lines
commit
0b725a2ca61bedc33a2a63d0451d528b268cf975
net: Removendo_xmit_flushnetdev operation, use signalling instead.added code that looks at
skb->xmit_moreafter the skb has been put in TX VQ. Since some paths process the ring and free the skb immediately, this can cause use after free.Fix by storing
xmit_morein a local variable.Cc David S. Miller <[email protected]>Signed-off-by Michael S. Tsirkin <[email protected]>---
David, am I using the API correctly?
Seems to work for me.
You used__netif_subqueue_stoppedbut that seems to use
a slightly more expensivetest_bitinternally.
The reason I added a variable for the txq here is because it's handy for BQL patch later on.drivers/net/virtio_net.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/net/virtio_net.c +0
-0
drivers/net/virtio_net.c static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) · line 923
Quoted 4 lines
drivers/net/virtio_net.c +1
-0@@ -920,6 +920,8 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)int qnum =skb_get_queue_mapping (skb);struct send_queue *sq = &vi->sq[qnum];int err; +struct netdev_queue *txq =netdev_get_tx_queue (dev, qnum);
Do you need to cache 'txq' on stack for the entire call? Looks like it is only needed when 'kick' is true.
I've not looked to see if saves both 'dev' and 'qnum' being kept.
In any case it isn't mentioned in the commit message.
David
Quoted 19 lines
drivers/net/virtio_net.c +2
-2+bool kick = !skb->xmit_more;/* Free up any pending old buffers before queueing new ones. */ free_old_xmit_skbs (sq);@@ -956,7 +958,7 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) } } -if (__netif_subqueue_stopped (dev, qnum) || !skb->xmit_more) +if (kick ||netif_xmit_stopped (txq))virtqueue_kick (sq->vq);return NETDEV_TX_OK ; --MST
--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Michael S. Tsirkin
maintainerQuoted 39 lines · David Laight
From: Michael S. Tsirkin
Quoted 29 lines
commit
0b725a2ca61bedc33a2a63d0451d528b268cf975
net: Removendo_xmit_flushnetdev operation, use signalling instead.added code that looks at
skb->xmit_moreafter the skb has been put in TX VQ. Since some paths process the ring and free the skb immediately, this can cause use after free.Fix by storing
xmit_morein a local variable.Cc David S. Miller <[email protected]>Signed-off-by Michael S. Tsirkin <[email protected]>---
David, am I using the API correctly?
Seems to work for me.
You used__netif_subqueue_stoppedbut that seems to use
a slightly more expensivetest_bitinternally.
The reason I added a variable for the txq here is because it's handy for BQL patch later on.drivers/net/virtio_net.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/net/virtio_net.c +0
-0drivers/net/virtio_net.c static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) · line 923
Quoted 4 lines
drivers/net/virtio_net.c +1
-0@@ -920,6 +920,8 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)int qnum =skb_get_queue_mapping (skb);struct send_queue *sq = &vi->sq[qnum];int err; +struct netdev_queue *txq =netdev_get_tx_queue (dev, qnum);Do you need to cache 'txq' on stack for the entire call? Looks like it is only needed when 'kick' is true.
I've not looked to see if saves both 'dev' and 'qnum' being kept.
Quoted 3 lines
In any case it isn't mentioned in the commit message.
David
Code seems slightly neater this way, I haven't bothered to micro-optimize it to this level yet.
Want to benchmark and send a patch on top?
Quoted 20 lines
Quoted 19 lines
drivers/net/virtio_net.c +2
-2+bool kick = !skb->xmit_more;/* Free up any pending old buffers before queueing new ones. */ free_old_xmit_skbs (sq);@@ -956,7 +958,7 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) } } -if (__netif_subqueue_stopped (dev, qnum) || !skb->xmit_more) +if (kick ||netif_xmit_stopped (txq))virtqueue_kick (sq->vq);return NETDEV_TX_OK ; --MST
--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
From: Michael S. Tsirkin
Quoted 29 lines
commit
0b725a2ca61bedc33a2a63d0451d528b268cf975
net: Removendo_xmit_flushnetdev operation, use signalling instead.added code that looks at
skb->xmit_moreafter the skb has been put in TX VQ. Since some paths process the ring and free the skb immediately, this can cause use after free.Fix by storing
xmit_morein a local variable.Cc David S. Miller <[email protected]>Signed-off-by Michael S. Tsirkin <[email protected]>---
David, am I using the API correctly?
Seems to work for me.
You used__netif_subqueue_stoppedbut that seems to use
a slightly more expensivetest_bitinternally.
The reason I added a variable for the txq here is because it's handy for BQL patch later on.drivers/net/virtio_net.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/net/virtio_net.c +0
-0
drivers/net/virtio_net.c static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) · line 923
Quoted 4 lines
drivers/net/virtio_net.c +1
-0@@ -920,6 +920,8 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)int qnum =skb_get_queue_mapping (skb);struct send_queue *sq = &vi->sq[qnum];int err; +struct netdev_queue *txq =netdev_get_tx_queue (dev, qnum);
Do you need to cache 'txq' on stack for the entire call? Looks like it is only needed when 'kick' is true.
I've not looked to see if saves both 'dev' and 'qnum' being kept.
In any case it isn't mentioned in the commit message.
David
Quoted 19 lines
drivers/net/virtio_net.c +2
-2+bool kick = !skb->xmit_more;/* Free up any pending old buffers before queueing new ones. */ free_old_xmit_skbs (sq);@@ -956,7 +958,7 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) } } -if (__netif_subqueue_stopped (dev, qnum) || !skb->xmit_more) +if (kick ||netif_xmit_stopped (txq))virtqueue_kick (sq->vq);return NETDEV_TX_OK ; --MST
--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
David Miller
maintainerFrom: "Michael S. Tsirkin" <[email protected]>
Date: Wed, 15 Oct 2014 16:23:28 +0300
Quoted 2 lines
You used
__netif_subqueue_stoppedbut that seems to use a slightly more expensivetest_bitinternally.
More expensive in what sense? It should be roughly the same as "x & y" sans the volatile.
Anyways I'm ambivalent and I want to see this bug fixes, so I'll apply your patch.
Thanks!
Michael S. Tsirkin
maintainerQuoted 8 lines · David Miller
From: "Michael S. Tsirkin" <[email protected]>
Date: Wed, 15 Oct 2014 16:23:28 +0300Quoted 2 lines
You used
__netif_subqueue_stoppedbut that seems to use a slightly more expensivetest_bitinternally.More expensive in what sense? It should be roughly the same as "x & y" sans the volatile.
I really just meant volatile - this might prevent some compiler optimizations. I have't actually checked the produced binary so I don't know for sure.
Quoted 4 lines
Anyways I'm ambivalent and I want to see this bug fixes, so I'll apply your patch.
Thanks!
David Miller
maintainerFrom: "Michael S. Tsirkin" <[email protected]>
Date: Wed, 15 Oct 2014 16:23:28 +0300
Quoted 2 lines
You used
__netif_subqueue_stoppedbut that seems to use a slightly more expensivetest_bitinternally.
More expensive in what sense? It should be roughly the same as "x & y" sans the volatile.
Anyways I'm ambivalent and I want to see this bug fixes, so I'll apply your patch.
Thanks!
Quoted 38 lines · Michael S. Tsirkin
commit
0b725a2ca61bedc33a2a63d0451d528b268cf975
net: Removendo_xmit_flushnetdev operation, use signalling instead.added code that looks at
skb->xmit_moreafter the skb has been put in TX VQ. Since some paths process the ring and free the skb immediately, this can cause use after free.Fix by storing
xmit_morein a local variable.Cc David S. Miller <[email protected]>Signed-off-by Michael S. Tsirkin <[email protected]>---
David, am I using the API correctly?
Seems to work for me.
You used__netif_subqueue_stoppedbut that seems to use
a slightly more expensivetest_bitinternally.
The reason I added a variable for the txq here is because it's handy for BQL patch later on.drivers/net/virtio_net.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/net/virtio_net.c +2
-0@@ -920,6 +920,8 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)int qnum =skb_get_queue_mapping (skb);struct send_queue *sq = &vi->sq[qnum];int err; +struct netdev_queue *txq =netdev_get_tx_queue (dev, qnum); +bool kick = !skb->xmit_more;/* Free up any pending old buffers before queueing new ones. */ free_old_xmit_skbs (sq);
drivers/net/virtio_net.c static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) · line 964
Quoted 8 lines
drivers/net/virtio_net.c +1
-1@@ -956,7 +958,7 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) } } -if (__netif_subqueue_stopped (dev, qnum) || !skb->xmit_more) +if (kick ||netif_xmit_stopped (txq))virtqueue_kick (sq->vq);return NETDEV_TX_OK ;
I must say I am kind of confused by this patch.
Why the skb_orphan(skb) & nf_reset(skb) do not have the same issue ?
It looks like following patch is needed ?
drivers/net/virtio_net.c +4 -4
@@ -927,6 +927,10 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)/* Free up any pending old buffers before queueing new ones. */ free_old_xmit_skbs (sq); +/* Don't wait up for transmitted skbs to be freed. */ +skb_orphan (skb); +nf_reset (skb); +/* Try to transmit */ err =xmit_skb (sq, skb);
@@ -941,10 +945,6 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)return NETDEV_TX_OK ; } -/* Don't wait up for transmitted skbs to be freed. */ -skb_orphan (skb); -nf_reset (skb); -/* Apparently nice girls don't return TX_BUSY; stop the queue * before it gets out of hand. Naturally, this wastes entries. */ if (sq->vq->num_free <2 +MAX_SKB_FRAGS ) {
Quoted 42 lines · Eric Dumazet
Quoted 41 lines · Michael S. Tsirkin
commit
0b725a2ca61bedc33a2a63d0451d528b268cf975
net: Removendo_xmit_flushnetdev operation, use signalling instead.added code that looks at
skb->xmit_moreafter the skb has been put in TX VQ. Since some paths process the ring and free the skb immediately, this can cause use after free.Fix by storing
xmit_morein a local variable.Cc David S. Miller <[email protected]>Signed-off-by Michael S. Tsirkin <[email protected]>---
David, am I using the API correctly?
Seems to work for me.
You used__netif_subqueue_stoppedbut that seems to use
a slightly more expensivetest_bitinternally.
The reason I added a variable for the txq here is because it's handy for BQL patch later on.drivers/net/virtio_net.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/net/virtio_net.c +2
-0@@ -920,6 +920,8 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)int qnum =skb_get_queue_mapping (skb);struct send_queue *sq = &vi->sq[qnum];int err; +struct netdev_queue *txq =netdev_get_tx_queue (dev, qnum); +bool kick = !skb->xmit_more;/* Free up any pending old buffers before queueing new ones. */ free_old_xmit_skbs (sq);@@ -956,7 +958,7 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) } }
Quoted 9 lines
drivers/net/virtio_net.c static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) · line 964
Quoted 5 lines
drivers/net/virtio_net.c +1
-1-if (__netif_subqueue_stopped (dev, qnum) || !skb->xmit_more) +if (kick ||netif_xmit_stopped (txq))virtqueue_kick (sq->vq);return NETDEV_TX_OK ;I must say I am kind of confused by this patch.
Why the
skb_orphan(skb)&nf_reset(skb)do not have the same issue ?
Since they are called before the possible free_old_xmit_skbs(), skb won't get freed at this time.
Quoted 2 lines · Jason Wang
Since they are called before the possible
free_old_xmit_skbs(), skb won't get freed at this time.
Oh right, I forgot there is no completion handler yet, timer based or whatever.
Thanks.