virtio-net: fix the race between refill work and close
Subsystems (4)
PATCH v2 1/1 virtio-net: fix the race between refill work and close
We try using cancel_delayed_work_sync() to prevent the work from enabling NAPI. This is insufficient since we don't disable the source of the refill work scheduling. This means an NAPI poll callback after cancel_delayed_work_sync() can schedule the refill work then can re-enable the NAPI that leads to use-after-free [1].
Since the work can enable NAPI, we can't simply disable NAPI before calling cancel_delayed_work_sync(). So fix this by introducing a dedicated boolean to control whether or not the work could be scheduled from NAPI.
[1]
==================================================================
BUG: KASAN: use-after-free in refill_work+0x43/0xd4 Read of size 2 at addr ffff88810562c92e by task kworker/2:1/42 CPU: 2 PID: 42 Comm: kworker/2:1 Not tainted 5.19.0-rc1+ #480 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014 Workqueue: events refill_work Call Trace: <TASK> dump_stack_lvl+0x34/0x44 print_report.cold+0xbb/0x6ac ? _printk+0xad/0xde ? refill_work+0x43/0xd4 kasan_report+0xa8/0x130 ? refill_work+0x43/0xd4 refill_work+0x43/0xd4 process_one_work+0x43d/0x780 worker_thread+0x2a0/0x6f0 ? process_one_work+0x780/0x780 kthread+0x167/0x1a0 ? kthread_exit+0x50/0x50 ret_from_fork+0x22/0x30 </TASK>
...
--- drivers/net/virtio_net.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-)
drivers/net/virtio_net.c +36 -2
@@ -251,6 +251,12 @@ struct virtnet_info {/* Does the affinity hint is set for virtqueues? */ bool affinity_hint_set; +/* Is refill work enabled? */ +bool refill_work_enabled; + +/* The lock to synchronize the access to refill_work_enabled */ +spinlock_t refill_lock; +/* CPU hotplug instances for online & dead */ struct hlist_node node;struct hlist_node node_dead;
@@ -348,6 +354,20 @@ static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)return p; } +static void enable_refill_work (struct virtnet_info *vi) +{ +spin_lock (&vi->refill_lock); + vi->refill_work_enabled = true; +spin_unlock (&vi->refill_lock); +} + +static void disable_refill_work (struct virtnet_info *vi) +{ +spin_lock (&vi->refill_lock); + vi->refill_work_enabled = false; +spin_unlock (&vi->refill_lock); +} +static void virtqueue_napi_schedule (struct napi_struct *napi,struct virtqueue *vq) {
@@ -1527,8 +1547,12 @@ static int virtnet_receive(struct receive_queue *rq, int budget, }if (rq->vq->num_free >min ((unsigned int )budget,virtqueue_get_vring_size (rq->vq)) /2 ) { -if (!try_fill_recv (vi, rq,GFP_ATOMIC )) -schedule_delayed_work (&vi->refill,0 ); +if (!try_fill_recv (vi, rq,GFP_ATOMIC )) { +spin_lock (&vi->refill_lock); +if (vi->refill_work_enabled) +schedule_delayed_work (&vi->refill,0 ); +spin_unlock (&vi->refill_lock); + } }u64_stats_update_begin (&rq->stats.syncp);
@@ -1651,6 +1675,8 @@ static int virtnet_open(struct net_device *dev)struct virtnet_info *vi =netdev_priv (dev);int i, err; +enable_refill_work (vi); +for (i =0 ; i < vi->max_queue_pairs; i++) {if (i < vi->curr_queue_pairs)/* Make sure we have some buffers: if oom use wq. */
@@ -2033,6 +2059,8 @@ static int virtnet_close(struct net_device *dev)struct virtnet_info *vi =netdev_priv (dev);int i; +/* Make sure NAPI doesn't schedule refill work */ +disable_refill_work (vi);/* Make sure refill_work doesn't re-enable napi! */ cancel_delayed_work_sync (&vi->refill);
@@ -2776,6 +2804,9 @@ static void virtnet_freeze_down(struct virtio_device *vdev)netif_tx_lock_bh (vi->dev);netif_device_detach (vi->dev);netif_tx_unlock_bh (vi->dev); +/* Make sure NAPI doesn't schedule refill work */ +disable_refill_work (vi); +/* Make sure refill_work doesn't re-enable napi! */ cancel_delayed_work_sync (&vi->refill);if (netif_running (vi->dev)) {
@@ -2799,6 +2830,8 @@ static int virtnet_restore_up(struct virtio_device *vdev)virtio_device_ready (vdev); +enable_refill_work (vi); +if (netif_running (vi->dev)) {for (i =0 ; i < vi->curr_queue_pairs; i++)if (!try_fill_recv (vi, &vi->rq[i],GFP_KERNEL ))
@@ -3548,6 +3581,7 @@ static int virtnet_probe(struct virtio_device *vdev) vdev->priv = vi;INIT_WORK (&vi->config_work, virtnet_config_changed_work); +spin_lock_init (&vi->refill_lock);/* If we can receive ANY GSO packets, we must allocate large ones. */ if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
Signature · 1 line
2.25.1
PATCH v2 1/1 virtio-net: fix the race between refill work and close
We try using cancel_delayed_work_sync() to prevent the work from enabling NAPI. This is insufficient since we don't disable the source of the refill work scheduling. This means an NAPI poll callback after cancel_delayed_work_sync() can schedule the refill work then can re-enable the NAPI that leads to use-after-free [1].
Since the work can enable NAPI, we can't simply disable NAPI before calling cancel_delayed_work_sync(). So fix this by introducing a dedicated boolean to control whether or not the work could be scheduled from NAPI.
[1]
==================================================================
BUG: KASAN: use-after-free in refill_work+0x43/0xd4 Read of size 2 at addr ffff88810562c92e by task kworker/2:1/42 CPU: 2 PID: 42 Comm: kworker/2:1 Not tainted 5.19.0-rc1+ #480 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014 Workqueue: events refill_work Call Trace: <TASK> dump_stack_lvl+0x34/0x44 print_report.cold+0xbb/0x6ac ? _printk+0xad/0xde ? refill_work+0x43/0xd4 kasan_report+0xa8/0x130 ? refill_work+0x43/0xd4 refill_work+0x43/0xd4 process_one_work+0x43d/0x780 worker_thread+0x2a0/0x6f0 ? process_one_work+0x780/0x780 kthread+0x167/0x1a0 ? kthread_exit+0x50/0x50 ret_from_fork+0x22/0x30 </TASK>
...
--- drivers/net/virtio_net.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-)
drivers/net/virtio_net.c +36 -2
@@ -251,6 +251,12 @@ struct virtnet_info {/* Does the affinity hint is set for virtqueues? */ bool affinity_hint_set; +/* Is refill work enabled? */ +bool refill_work_enabled; + +/* The lock to synchronize the access to refill_work_enabled */ +spinlock_t refill_lock; +/* CPU hotplug instances for online & dead */ struct hlist_node node;struct hlist_node node_dead;
@@ -348,6 +354,20 @@ static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)return p; } +static void enable_refill_work (struct virtnet_info *vi) +{ +spin_lock (&vi->refill_lock); + vi->refill_work_enabled = true; +spin_unlock (&vi->refill_lock); +} + +static void disable_refill_work (struct virtnet_info *vi) +{ +spin_lock (&vi->refill_lock); + vi->refill_work_enabled = false; +spin_unlock (&vi->refill_lock); +} +static void virtqueue_napi_schedule (struct napi_struct *napi,struct virtqueue *vq) {
@@ -1527,8 +1547,12 @@ static int virtnet_receive(struct receive_queue *rq, int budget, }if (rq->vq->num_free >min ((unsigned int )budget,virtqueue_get_vring_size (rq->vq)) /2 ) { -if (!try_fill_recv (vi, rq,GFP_ATOMIC )) -schedule_delayed_work (&vi->refill,0 ); +if (!try_fill_recv (vi, rq,GFP_ATOMIC )) { +spin_lock (&vi->refill_lock); +if (vi->refill_work_enabled) +schedule_delayed_work (&vi->refill,0 ); +spin_unlock (&vi->refill_lock); + } }u64_stats_update_begin (&rq->stats.syncp);
@@ -1651,6 +1675,8 @@ static int virtnet_open(struct net_device *dev)struct virtnet_info *vi =netdev_priv (dev);int i, err; +enable_refill_work (vi); +for (i =0 ; i < vi->max_queue_pairs; i++) {if (i < vi->curr_queue_pairs)/* Make sure we have some buffers: if oom use wq. */
@@ -2033,6 +2059,8 @@ static int virtnet_close(struct net_device *dev)struct virtnet_info *vi =netdev_priv (dev);int i; +/* Make sure NAPI doesn't schedule refill work */ +disable_refill_work (vi);/* Make sure refill_work doesn't re-enable napi! */ cancel_delayed_work_sync (&vi->refill);
@@ -2776,6 +2804,9 @@ static void virtnet_freeze_down(struct virtio_device *vdev)netif_tx_lock_bh (vi->dev);netif_device_detach (vi->dev);netif_tx_unlock_bh (vi->dev); +/* Make sure NAPI doesn't schedule refill work */ +disable_refill_work (vi); +/* Make sure refill_work doesn't re-enable napi! */ cancel_delayed_work_sync (&vi->refill);if (netif_running (vi->dev)) {
@@ -2799,6 +2830,8 @@ static int virtnet_restore_up(struct virtio_device *vdev)virtio_device_ready (vdev); +enable_refill_work (vi); +if (netif_running (vi->dev)) {for (i =0 ; i < vi->curr_queue_pairs; i++)if (!try_fill_recv (vi, &vi->rq[i],GFP_KERNEL ))
@@ -3548,6 +3581,7 @@ static int virtnet_probe(struct virtio_device *vdev) vdev->priv = vi;INIT_WORK (&vi->config_work, virtnet_config_changed_work); +spin_lock_init (&vi->refill_lock);/* If we can receive ANY GSO packets, we must allocate large ones. */ if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
Signature · 1 line
2.25.1
Xuan Zhuo
reviewerQuoted 5 lines · Jason Wang
We try using
cancel_delayed_work_sync()to prevent the work from enabling NAPI. This is insufficient since we don't disable the source of the refill work scheduling. This means an NAPI poll callback aftercancel_delayed_work_sync()can schedule the refill work then can re-enable the NAPI that leads to use-after-free [1].
Can you explain in more detail how this happened?
napi_disable() is normally called after cancel_delayed_work_sync(). This ensures that all napi callbacks will end, and the new napi_disable() will wait. There will be no re-enable napi.
So I guess the use-after-free is caused by refill_work being called after dev/vi/napi is released. In this way, we can just call cancel_delayed_work_sync() after napi_disalbe().
Thanks.
Quoted 139 lines
Since the work can enable NAPI, we can't simply disable NAPI before calling
cancel_delayed_work_sync(). So fix this by introducing a dedicated boolean to control whether or not the work could be scheduled from NAPI.[1]
==================================================================BUG: KASAN: use-after-free in refill_work+0x43/0xd4 Read of size 2 at addr ffff88810562c92e by task kworker/2:1/42 CPU: 2 PID: 42 Comm: kworker/2:1 Not tainted 5.19.0-rc1+ #480 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014 Workqueue: events refill_work Call Trace: <TASK> dump_stack_lvl+0x34/0x44 print_report.cold+0xbb/0x6ac ? _printk+0xad/0xde ? refill_work+0x43/0xd4 kasan_report+0xa8/0x130 ? refill_work+0x43/0xd4 refill_work+0x43/0xd4 process_one_work+0x43d/0x780 worker_thread+0x2a0/0x6f0 ? process_one_work+0x780/0x780 kthread+0x167/0x1a0 ? kthread_exit+0x50/0x50 ret_from_fork+0x22/0x30 </TASK>...
Fixes b2baed69e605c ("virtio_net: set/cancel work on ndo_open/ndo_stop")Signed-off-by Jason Wang <[email protected]>--- drivers/net/virtio_net.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-)drivers/net/virtio_net.c +36
-3@@ -251,6 +251,12 @@ struct virtnet_info {/* Does the affinity hint is set for virtqueues? */ bool affinity_hint_set; +/* Is refill work enabled? */ +bool refill_work_enabled; + +/* The lock to synchronize the access to refill_work_enabled */ +spinlock_t refill_lock; +/* CPU hotplug instances for online & dead */ struct hlist_node node;struct hlist_node node_dead;@@ -348,6 +354,20 @@ static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)return p; } +static void enable_refill_work (struct virtnet_info *vi) +{ +spin_lock (&vi->refill_lock); + vi->refill_work_enabled = true; +spin_unlock (&vi->refill_lock); +} + +static void disable_refill_work (struct virtnet_info *vi) +{ +spin_lock (&vi->refill_lock); + vi->refill_work_enabled = false; +spin_unlock (&vi->refill_lock); +} +static void virtqueue_napi_schedule (struct napi_struct *napi,struct virtqueue *vq) {@@ -1527,8 +1547,12 @@ static int virtnet_receive(struct receive_queue *rq, int budget, }if (rq->vq->num_free >min ((unsigned int )budget,virtqueue_get_vring_size (rq->vq)) /2 ) { -if (!try_fill_recv (vi, rq,GFP_ATOMIC )) -schedule_delayed_work (&vi->refill,0 ); +if (!try_fill_recv (vi, rq,GFP_ATOMIC )) { +spin_lock (&vi->refill_lock); +if (vi->refill_work_enabled) +schedule_delayed_work (&vi->refill,0 ); +spin_unlock (&vi->refill_lock); + } }u64_stats_update_begin (&rq->stats.syncp);@@ -1651,6 +1675,8 @@ static int virtnet_open(struct net_device *dev)struct virtnet_info *vi =netdev_priv (dev);int i, err; +enable_refill_work (vi); +for (i =0 ; i < vi->max_queue_pairs; i++) {if (i < vi->curr_queue_pairs)/* Make sure we have some buffers: if oom use wq. */ @@ -2033,6 +2059,8 @@ static int virtnet_close(struct net_device *dev)struct virtnet_info *vi =netdev_priv (dev);int i; +/* Make sure NAPI doesn't schedule refill work */ +disable_refill_work (vi);/* Make sure refill_work doesn't re-enable napi! */ cancel_delayed_work_sync (&vi->refill);@@ -2776,6 +2804,9 @@ static void virtnet_freeze_down(struct virtio_device *vdev)netif_tx_lock_bh (vi->dev);netif_device_detach (vi->dev);netif_tx_unlock_bh (vi->dev); +/* Make sure NAPI doesn't schedule refill work */ +disable_refill_work (vi); +/* Make sure refill_work doesn't re-enable napi! */ cancel_delayed_work_sync (&vi->refill);if (netif_running (vi->dev)) {@@ -2799,6 +2830,8 @@ static int virtnet_restore_up(struct virtio_device *vdev)virtio_device_ready (vdev); +enable_refill_work (vi); +if (netif_running (vi->dev)) {for (i =0 ; i < vi->curr_queue_pairs; i++)if (!try_fill_recv (vi, &vi->rq[i],GFP_KERNEL ))@@ -3548,6 +3581,7 @@ static int virtnet_probe(struct virtio_device *vdev) vdev->priv = vi;INIT_WORK (&vi->config_work, virtnet_config_changed_work); +spin_lock_init (&vi->refill_lock);/* If we can receive ANY GSO packets, we must allocate large ones. */ if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || --2.25.1
Quoted 9 lines · Xuan Zhuo
Quoted 5 lines · Jason Wang
We try using
cancel_delayed_work_sync()to prevent the work from enabling NAPI. This is insufficient since we don't disable the source of the refill work scheduling. This means an NAPI poll callback aftercancel_delayed_work_sync()can schedule the refill work then can re-enable the NAPI that leads to use-after-free [1].
Quoted 5 lines
Can you explain in more detail how this happened?
napi_disable()is normally called aftercancel_delayed_work_sync(). This ensures that all napi callbacks will end, and the newnapi_disable()will wait. There will be no re-enable napi.
An rx interrupt that may come between after the cancel_delayed_work() but before the napi_disable(). It schedules a refill_work that may run after the napi_disable() in virtnet_close().
Quoted 4 lines
So I guess the use-after-free is caused by
refill_workbeing called after dev/vi/napi is released. In this way, we can just callcancel_delayed_work_sync()afternapi_disalbe().
So the refill_work can re-enable the NAPI when it is run after napi_disable() in this case.
Thanks
Quoted 143 lines
Thanks.
Quoted 139 lines
Since the work can enable NAPI, we can't simply disable NAPI before calling
cancel_delayed_work_sync(). So fix this by introducing a dedicated boolean to control whether or not the work could be scheduled from NAPI.[1]
==================================================================BUG: KASAN: use-after-free in refill_work+0x43/0xd4 Read of size 2 at addr ffff88810562c92e by task kworker/2:1/42 CPU: 2 PID: 42 Comm: kworker/2:1 Not tainted 5.19.0-rc1+ #480 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014 Workqueue: events refill_work Call Trace: <TASK> dump_stack_lvl+0x34/0x44 print_report.cold+0xbb/0x6ac ? _printk+0xad/0xde ? refill_work+0x43/0xd4 kasan_report+0xa8/0x130 ? refill_work+0x43/0xd4 refill_work+0x43/0xd4 process_one_work+0x43d/0x780 worker_thread+0x2a0/0x6f0 ? process_one_work+0x780/0x780 kthread+0x167/0x1a0 ? kthread_exit+0x50/0x50 ret_from_fork+0x22/0x30 </TASK>...
Fixes b2baed69e605c ("virtio_net: set/cancel work on ndo_open/ndo_stop")Signed-off-by Jason Wang <[email protected]>--- drivers/net/virtio_net.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-)drivers/net/virtio_net.c +36
-3@@ -251,6 +251,12 @@ struct virtnet_info {/* Does the affinity hint is set for virtqueues? */ bool affinity_hint_set; +/* Is refill work enabled? */ +bool refill_work_enabled; + +/* The lock to synchronize the access to refill_work_enabled */ +spinlock_t refill_lock; +/* CPU hotplug instances for online & dead */ struct hlist_node node;struct hlist_node node_dead;@@ -348,6 +354,20 @@ static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)return p; } +static void enable_refill_work (struct virtnet_info *vi) +{ +spin_lock (&vi->refill_lock); + vi->refill_work_enabled = true; +spin_unlock (&vi->refill_lock); +} + +static void disable_refill_work (struct virtnet_info *vi) +{ +spin_lock (&vi->refill_lock); + vi->refill_work_enabled = false; +spin_unlock (&vi->refill_lock); +} +static void virtqueue_napi_schedule (struct napi_struct *napi,struct virtqueue *vq) {@@ -1527,8 +1547,12 @@ static int virtnet_receive(struct receive_queue *rq, int budget, }if (rq->vq->num_free >min ((unsigned int )budget,virtqueue_get_vring_size (rq->vq)) /2 ) { -if (!try_fill_recv (vi, rq,GFP_ATOMIC )) -schedule_delayed_work (&vi->refill,0 ); +if (!try_fill_recv (vi, rq,GFP_ATOMIC )) { +spin_lock (&vi->refill_lock); +if (vi->refill_work_enabled) +schedule_delayed_work (&vi->refill,0 ); +spin_unlock (&vi->refill_lock); + } }u64_stats_update_begin (&rq->stats.syncp);@@ -1651,6 +1675,8 @@ static int virtnet_open(struct net_device *dev)struct virtnet_info *vi =netdev_priv (dev);int i, err; +enable_refill_work (vi); +for (i =0 ; i < vi->max_queue_pairs; i++) {if (i < vi->curr_queue_pairs)/* Make sure we have some buffers: if oom use wq. */ @@ -2033,6 +2059,8 @@ static int virtnet_close(struct net_device *dev)struct virtnet_info *vi =netdev_priv (dev);int i; +/* Make sure NAPI doesn't schedule refill work */ +disable_refill_work (vi);/* Make sure refill_work doesn't re-enable napi! */ cancel_delayed_work_sync (&vi->refill);@@ -2776,6 +2804,9 @@ static void virtnet_freeze_down(struct virtio_device *vdev)netif_tx_lock_bh (vi->dev);netif_device_detach (vi->dev);netif_tx_unlock_bh (vi->dev); +/* Make sure NAPI doesn't schedule refill work */ +disable_refill_work (vi); +/* Make sure refill_work doesn't re-enable napi! */ cancel_delayed_work_sync (&vi->refill);if (netif_running (vi->dev)) {@@ -2799,6 +2830,8 @@ static int virtnet_restore_up(struct virtio_device *vdev)virtio_device_ready (vdev); +enable_refill_work (vi); +if (netif_running (vi->dev)) {for (i =0 ; i < vi->curr_queue_pairs; i++)if (!try_fill_recv (vi, &vi->rq[i],GFP_KERNEL ))@@ -3548,6 +3581,7 @@ static int virtnet_probe(struct virtio_device *vdev) vdev->priv = vi;INIT_WORK (&vi->config_work, virtnet_config_changed_work); +spin_lock_init (&vi->refill_lock);/* If we can receive ANY GSO packets, we must allocate large ones. */ if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || --2.25.1
Xuan Zhuo
reviewerQuoted 12 lines · Jason Wang
Quoted 11 lines · Xuan Zhuo
Quoted 5 lines · Jason Wang
We try using
cancel_delayed_work_sync()to prevent the work from enabling NAPI. This is insufficient since we don't disable the source of the refill work scheduling. This means an NAPI poll callback aftercancel_delayed_work_sync()can schedule the refill work then can re-enable the NAPI that leads to use-after-free [1].Can you explain in more detail how this happened?
Quoted 7 lines
Quoted 3 lines
napi_disable()is normally called aftercancel_delayed_work_sync(). This ensures that all napi callbacks will end, and the newnapi_disable()will wait. There will be no re-enable napi.An rx interrupt that may come between after the
cancel_delayed_work()but before thenapi_disable(). It schedules arefill_workthat may run after thenapi_disable()invirtnet_close().
Yes
Quoted 8 lines
Quoted 5 lines
So I guess the use-after-free is caused by
refill_workbeing called after dev/vi/napi is released. In this way, we can just callcancel_delayed_work_sync()afternapi_disalbe().So the
refill_workcan re-enable the NAPI when it is run afternapi_disable()in this case.
Since napi_disable() has been called in virtnet_close(), it will get stuck when napi_disable() in refill_work(). I think use-after-free is because vi/napi etc. have been released, refill_work() going to access again causes an exception.
napi will not be re-enable.
I would like to call cancel_delayed_work_sync() after napi_disable() to solve this problem. But this also has a problem, refill_work() can get stuck on napi_disable() and cannot exit. In this way, we want napi_disable() to check that the current state is disabled and exit directly.
Thanks.
Quoted 148 lines
Thanks
Quoted 143 lines
Thanks.
Quoted 139 lines
Since the work can enable NAPI, we can't simply disable NAPI before calling
cancel_delayed_work_sync(). So fix this by introducing a dedicated boolean to control whether or not the work could be scheduled from NAPI.[1]
==================================================================BUG: KASAN: use-after-free in refill_work+0x43/0xd4 Read of size 2 at addr ffff88810562c92e by task kworker/2:1/42 CPU: 2 PID: 42 Comm: kworker/2:1 Not tainted 5.19.0-rc1+ #480 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014 Workqueue: events refill_work Call Trace: <TASK> dump_stack_lvl+0x34/0x44 print_report.cold+0xbb/0x6ac ? _printk+0xad/0xde ? refill_work+0x43/0xd4 kasan_report+0xa8/0x130 ? refill_work+0x43/0xd4 refill_work+0x43/0xd4 process_one_work+0x43d/0x780 worker_thread+0x2a0/0x6f0 ? process_one_work+0x780/0x780 kthread+0x167/0x1a0 ? kthread_exit+0x50/0x50 ret_from_fork+0x22/0x30 </TASK>...
Fixes b2baed69e605c ("virtio_net: set/cancel work on ndo_open/ndo_stop")Signed-off-by Jason Wang <[email protected]>--- drivers/net/virtio_net.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-)drivers/net/virtio_net.c +36
-3@@ -251,6 +251,12 @@ struct virtnet_info {/* Does the affinity hint is set for virtqueues? */ bool affinity_hint_set; +/* Is refill work enabled? */ +bool refill_work_enabled; + +/* The lock to synchronize the access to refill_work_enabled */ +spinlock_t refill_lock; +/* CPU hotplug instances for online & dead */ struct hlist_node node;struct hlist_node node_dead;@@ -348,6 +354,20 @@ static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)return p; } +static void enable_refill_work (struct virtnet_info *vi) +{ +spin_lock (&vi->refill_lock); + vi->refill_work_enabled = true; +spin_unlock (&vi->refill_lock); +} + +static void disable_refill_work (struct virtnet_info *vi) +{ +spin_lock (&vi->refill_lock); + vi->refill_work_enabled = false; +spin_unlock (&vi->refill_lock); +} +static void virtqueue_napi_schedule (struct napi_struct *napi,struct virtqueue *vq) {@@ -1527,8 +1547,12 @@ static int virtnet_receive(struct receive_queue *rq, int budget, }if (rq->vq->num_free >min ((unsigned int )budget,virtqueue_get_vring_size (rq->vq)) /2 ) { -if (!try_fill_recv (vi, rq,GFP_ATOMIC )) -schedule_delayed_work (&vi->refill,0 ); +if (!try_fill_recv (vi, rq,GFP_ATOMIC )) { +spin_lock (&vi->refill_lock); +if (vi->refill_work_enabled) +schedule_delayed_work (&vi->refill,0 ); +spin_unlock (&vi->refill_lock); + } }u64_stats_update_begin (&rq->stats.syncp);@@ -1651,6 +1675,8 @@ static int virtnet_open(struct net_device *dev)struct virtnet_info *vi =netdev_priv (dev);int i, err; +enable_refill_work (vi); +for (i =0 ; i < vi->max_queue_pairs; i++) {if (i < vi->curr_queue_pairs)/* Make sure we have some buffers: if oom use wq. */ @@ -2033,6 +2059,8 @@ static int virtnet_close(struct net_device *dev)struct virtnet_info *vi =netdev_priv (dev);int i; +/* Make sure NAPI doesn't schedule refill work */ +disable_refill_work (vi);/* Make sure refill_work doesn't re-enable napi! */ cancel_delayed_work_sync (&vi->refill);@@ -2776,6 +2804,9 @@ static void virtnet_freeze_down(struct virtio_device *vdev)netif_tx_lock_bh (vi->dev);netif_device_detach (vi->dev);netif_tx_unlock_bh (vi->dev); +/* Make sure NAPI doesn't schedule refill work */ +disable_refill_work (vi); +/* Make sure refill_work doesn't re-enable napi! */ cancel_delayed_work_sync (&vi->refill);if (netif_running (vi->dev)) {@@ -2799,6 +2830,8 @@ static int virtnet_restore_up(struct virtio_device *vdev)virtio_device_ready (vdev); +enable_refill_work (vi); +if (netif_running (vi->dev)) {for (i =0 ; i < vi->curr_queue_pairs; i++)if (!try_fill_recv (vi, &vi->rq[i],GFP_KERNEL ))@@ -3548,6 +3581,7 @@ static int virtnet_probe(struct virtio_device *vdev) vdev->priv = vi;INIT_WORK (&vi->config_work, virtnet_config_changed_work); +spin_lock_init (&vi->refill_lock);/* If we can receive ANY GSO packets, we must allocate large ones. */ if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || --2.25.1
Quoted 31 lines · Xuan Zhuo
Quoted 12 lines · Jason Wang
Quoted 11 lines · Xuan Zhuo
Quoted 5 lines · Jason Wang
We try using
cancel_delayed_work_sync()to prevent the work from enabling NAPI. This is insufficient since we don't disable the source of the refill work scheduling. This means an NAPI poll callback aftercancel_delayed_work_sync()can schedule the refill work then can re-enable the NAPI that leads to use-after-free [1].Can you explain in more detail how this happened?
Quoted 7 lines
Quoted 3 lines
napi_disable()is normally called aftercancel_delayed_work_sync(). This ensures that all napi callbacks will end, and the newnapi_disable()will wait. There will be no re-enable napi.An rx interrupt that may come between after the
cancel_delayed_work()but before thenapi_disable(). It schedules arefill_workthat may run after thenapi_disable()invirtnet_close().Yes
Quoted 6 lines
Quoted 5 lines
So I guess the use-after-free is caused by
refill_workbeing called after dev/vi/napi is released. In this way, we can just callcancel_delayed_work_sync()afternapi_disalbe().
Quoted 6 lines
Quoted 2 lines
So the
refill_workcan re-enable the NAPI when it is run afternapi_disable()in this case.Since
napi_disable()has been called invirtnet_close(), it will get stuck whennapi_disable()inrefill_work().
Right because e.g NAPIF_STATE_SCHED has been set by napi_disable() before.
Quoted 2 lines
I think use-after-free is because vi/napi etc.
have been released,refill_work()going to access again causes an exception.
Yes, this is the use-after-free I mentioned above.
Quoted 7 lines
napi will not be re-enable.
I would like to call
cancel_delayed_work_sync()afternapi_disable()to solve this problem. But this also has a problem,refill_work()can get stuck onnapi_disable()and cannot exit. In this way, we wantnapi_disable()to check that the current state is disabled and exit directly.
Not sure this is a good design and it doesn't fit for -stable.
I think the design of NAPI is to pair napi_enable() and napi_disable() instead of allowing napi_disable() to be called twice.
Thanks
Quoted 153 lines
Thanks.
Quoted 148 lines
Thanks
Quoted 143 lines
Thanks.
Quoted 139 lines
Since the work can enable NAPI, we can't simply disable NAPI before calling
cancel_delayed_work_sync(). So fix this by introducing a dedicated boolean to control whether or not the work could be scheduled from NAPI.[1]
==================================================================BUG: KASAN: use-after-free in refill_work+0x43/0xd4 Read of size 2 at addr ffff88810562c92e by task kworker/2:1/42 CPU: 2 PID: 42 Comm: kworker/2:1 Not tainted 5.19.0-rc1+ #480 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014 Workqueue: events refill_work Call Trace: <TASK> dump_stack_lvl+0x34/0x44 print_report.cold+0xbb/0x6ac ? _printk+0xad/0xde ? refill_work+0x43/0xd4 kasan_report+0xa8/0x130 ? refill_work+0x43/0xd4 refill_work+0x43/0xd4 process_one_work+0x43d/0x780 worker_thread+0x2a0/0x6f0 ? process_one_work+0x780/0x780 kthread+0x167/0x1a0 ? kthread_exit+0x50/0x50 ret_from_fork+0x22/0x30 </TASK>...
Fixes b2baed69e605c ("virtio_net: set/cancel work on ndo_open/ndo_stop")Signed-off-by Jason Wang <[email protected]>--- drivers/net/virtio_net.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-)drivers/net/virtio_net.c +36
-3@@ -251,6 +251,12 @@ struct virtnet_info {/* Does the affinity hint is set for virtqueues? */ bool affinity_hint_set; +/* Is refill work enabled? */ +bool refill_work_enabled; + +/* The lock to synchronize the access to refill_work_enabled */ +spinlock_t refill_lock; +/* CPU hotplug instances for online & dead */ struct hlist_node node;struct hlist_node node_dead;@@ -348,6 +354,20 @@ static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)return p; } +static void enable_refill_work (struct virtnet_info *vi) +{ +spin_lock (&vi->refill_lock); + vi->refill_work_enabled = true; +spin_unlock (&vi->refill_lock); +} + +static void disable_refill_work (struct virtnet_info *vi) +{ +spin_lock (&vi->refill_lock); + vi->refill_work_enabled = false; +spin_unlock (&vi->refill_lock); +} +static void virtqueue_napi_schedule (struct napi_struct *napi,struct virtqueue *vq) {@@ -1527,8 +1547,12 @@ static int virtnet_receive(struct receive_queue *rq, int budget, }if (rq->vq->num_free >min ((unsigned int )budget,virtqueue_get_vring_size (rq->vq)) /2 ) { -if (!try_fill_recv (vi, rq,GFP_ATOMIC )) -schedule_delayed_work (&vi->refill,0 ); +if (!try_fill_recv (vi, rq,GFP_ATOMIC )) { +spin_lock (&vi->refill_lock); +if (vi->refill_work_enabled) +schedule_delayed_work (&vi->refill,0 ); +spin_unlock (&vi->refill_lock); + } }u64_stats_update_begin (&rq->stats.syncp);@@ -1651,6 +1675,8 @@ static int virtnet_open(struct net_device *dev)struct virtnet_info *vi =netdev_priv (dev);int i, err; +enable_refill_work (vi); +for (i =0 ; i < vi->max_queue_pairs; i++) {if (i < vi->curr_queue_pairs)/* Make sure we have some buffers: if oom use wq. */ @@ -2033,6 +2059,8 @@ static int virtnet_close(struct net_device *dev)struct virtnet_info *vi =netdev_priv (dev);int i; +/* Make sure NAPI doesn't schedule refill work */ +disable_refill_work (vi);/* Make sure refill_work doesn't re-enable napi! */ cancel_delayed_work_sync (&vi->refill);@@ -2776,6 +2804,9 @@ static void virtnet_freeze_down(struct virtio_device *vdev)netif_tx_lock_bh (vi->dev);netif_device_detach (vi->dev);netif_tx_unlock_bh (vi->dev); +/* Make sure NAPI doesn't schedule refill work */ +disable_refill_work (vi); +/* Make sure refill_work doesn't re-enable napi! */ cancel_delayed_work_sync (&vi->refill);if (netif_running (vi->dev)) {@@ -2799,6 +2830,8 @@ static int virtnet_restore_up(struct virtio_device *vdev)virtio_device_ready (vdev); +enable_refill_work (vi); +if (netif_running (vi->dev)) {for (i =0 ; i < vi->curr_queue_pairs; i++)if (!try_fill_recv (vi, &vi->rq[i],GFP_KERNEL ))@@ -3548,6 +3581,7 @@ static int virtnet_probe(struct virtio_device *vdev) vdev->priv = vi;INIT_WORK (&vi->config_work, virtnet_config_changed_work); +spin_lock_init (&vi->refill_lock);/* If we can receive ANY GSO packets, we must allocate large ones. */ if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || --2.25.1
Jakub Kicinski
maintainerQuoted 18 lines · On Thu, 30 Jun 2022 10:08:04 +0800 Jason Wang wrote:
drivers/net/virtio_net.c +14
-0+static void enable_refill_work (struct virtnet_info *vi) +{ +spin_lock (&vi->refill_lock); + vi->refill_work_enabled = true; +spin_unlock (&vi->refill_lock); +} + +static void disable_refill_work (struct virtnet_info *vi) +{ +spin_lock (&vi->refill_lock); + vi->refill_work_enabled = false; +spin_unlock (&vi->refill_lock); +} +static void virtqueue_napi_schedule (struct napi_struct *napi,struct virtqueue *vq) {
drivers/net/virtio_net.c static int virtnet_receive(struct receive_queue *rq, int budget, · line 1554
Quoted 10 lines
drivers/net/virtio_net.c +5
-2@@ -1527,8 +1547,12 @@ static int virtnet_receive(struct receive_queue *rq, int budget, }if (rq->vq->num_free >min ((unsigned int )budget,virtqueue_get_vring_size (rq->vq)) /2 ) { -if (!try_fill_recv (vi, rq,GFP_ATOMIC )) -schedule_delayed_work (&vi->refill,0 ); +if (!try_fill_recv (vi, rq,GFP_ATOMIC )) { +spin_lock (&vi->refill_lock); +if (vi->refill_work_enabled) +schedule_delayed_work (&vi->refill,0 ); +spin_unlock (&vi->refill_lock);
Are you sure you can use the basic spin_lock() flavor in all cases? Isn't the disable/enable called from a different context than this thing here?
The entire delayed work construct seems a little risky because the work may go to sleep after disabling napi, causing large latency spikes. I guess you must have a good reason no to simply reschedule the NAPI and keep retrying with GFP_ATOMIC...
Please add the target tree name to the subject.
Quoted 22 lines · Jakub Kicinski
Quoted 20 lines · On Thu, 30 Jun 2022 10:08:04 +0800 Jason Wang wrote:
drivers/net/virtio_net.c +14
-0+static void enable_refill_work (struct virtnet_info *vi) +{ +spin_lock (&vi->refill_lock); + vi->refill_work_enabled = true; +spin_unlock (&vi->refill_lock); +} + +static void disable_refill_work (struct virtnet_info *vi) +{ +spin_lock (&vi->refill_lock); + vi->refill_work_enabled = false; +spin_unlock (&vi->refill_lock); +} +static void virtqueue_napi_schedule (struct napi_struct *napi,struct virtqueue *vq) {@@ -1527,8 +1547,12 @@ static int virtnet_receive(struct receive_queue *rq, int budget, }
Quoted 12 lines
drivers/net/virtio_net.c static int virtnet_receive(struct receive_queue *rq, int budget, · line 1554
Quoted 8 lines
drivers/net/virtio_net.c +5
-2if (rq->vq->num_free >min ((unsigned int )budget,virtqueue_get_vring_size (rq->vq)) /2 ) { -if (!try_fill_recv (vi, rq,GFP_ATOMIC )) -schedule_delayed_work (&vi->refill,0 ); +if (!try_fill_recv (vi, rq,GFP_ATOMIC )) { +spin_lock (&vi->refill_lock); +if (vi->refill_work_enabled) +schedule_delayed_work (&vi->refill,0 ); +spin_unlock (&vi->refill_lock);Are you sure you can use the basic
spin_lock()flavor in all cases? Isn't the disable/enable called from a different context than this thing here?
This function will only be called in bh so it's safe.
Quoted 3 lines
The entire delayed work construct seems a little risky because the work may go to sleep after disabling napi, causing large latency spikes.
Yes, but it only happens on OOM.
Quoted 2 lines
I guess you must have a good reason no to simply reschedule the NAPI and keep retrying with
GFP_ATOMIC...
Less pressure on the memory allocator on OOM probably, but it looks like an independent issue that might be optimized in the future.
Quoted 2 lines
Please add the target tree name to the subject.
Ok
Thanks
Quoted 1 line
Quoted 37 lines · Jason Wang
Quoted 22 lines · Jakub Kicinski
Quoted 20 lines · On Thu, 30 Jun 2022 10:08:04 +0800 Jason Wang wrote:
drivers/net/virtio_net.c +14
-0+static void enable_refill_work (struct virtnet_info *vi) +{ +spin_lock (&vi->refill_lock); + vi->refill_work_enabled = true; +spin_unlock (&vi->refill_lock); +} + +static void disable_refill_work (struct virtnet_info *vi) +{ +spin_lock (&vi->refill_lock); + vi->refill_work_enabled = false; +spin_unlock (&vi->refill_lock); +} +static void virtqueue_napi_schedule (struct napi_struct *napi,struct virtqueue *vq) {@@ -1527,8 +1547,12 @@ static int virtnet_receive(struct receive_queue *rq, int budget, }Quoted 12 lines
drivers/net/virtio_net.c static int virtnet_receive(struct receive_queue *rq, int budget, · line 1554
Quoted 8 lines
drivers/net/virtio_net.c +5
-2if (rq->vq->num_free >min ((unsigned int )budget,virtqueue_get_vring_size (rq->vq)) /2 ) { -if (!try_fill_recv (vi, rq,GFP_ATOMIC )) -schedule_delayed_work (&vi->refill,0 ); +if (!try_fill_recv (vi, rq,GFP_ATOMIC )) { +spin_lock (&vi->refill_lock); +if (vi->refill_work_enabled) +schedule_delayed_work (&vi->refill,0 ); +spin_unlock (&vi->refill_lock);Are you sure you can use the basic
spin_lock()flavor in all cases? Isn't the disable/enable called from a different context than this thing here?
Quoted 1 line
This function will only be called in bh so it's safe.
Ok, so it looks like we should use the bh variant in close. Otherwise we may have a deadlock. Will fix it.
Thanks
Quoted 21 lines
Quoted 4 lines
The entire delayed work construct seems a little risky because the work may go to sleep after disabling napi, causing large latency spikes.
Yes, but it only happens on OOM.
Quoted 2 lines
I guess you must have a good reason no to simply reschedule the NAPI and keep retrying with
GFP_ATOMIC...Less pressure on the memory allocator on OOM probably, but it looks like an independent issue that might be optimized in the future.
Quoted 2 lines
Please add the target tree name to the subject.
Ok
Thanks
Quoted 1 line