Skip to contentSkip to conversation

virtio-net: fix wild pointer when remove virtio-net queues

PATCH In review Yunjian Wang 2 participants 4 messages Apr 26, 2017 – May 2, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio-net: fix wild pointer when remove virtio-net queues

The tx_bh or tx_timer will free in virtio_net_del_queue() function, when removing virtio-net queues if the guest doesn't support multiqueue. But it might be still referenced by virtio_net_set_status(), which needs to be set NULL. And also the tx_waiting needs to be set zero to prevent virtio_net_set_status() accessing tx_bh or tx_timer.

---
 hw/net/virtio-net.c | 3 +++
 1 file changed, 3 insertions(+)
hw/net/virtio-net.c +3 -0
index 7d091c9..98bd683 100644
@@ -1522,9 +1522,12 @@ static void virtio_net_del_queue(VirtIONet *n, int index)
     if (q->tx_timer) {
         timer_del(q->tx_timer);
         timer_free(q->tx_timer);
+        q->tx_timer = NULL;
     } else {
         qemu_bh_delete(q->tx_bh);
+        q->tx_bh = NULL;
     }
+    q->tx_waiting = 0;
     virtio_del_queue(vdev, index * 2 + 1);
 }
 
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 18 lines · Yunjian Wang

The tx_bh or tx_timer will free in virtio_net_del_queue() function, when removing virtio-net queues if the guest doesn't support multiqueue. But it might be still referenced by virtio_net_set_status(), which needs to be set NULL. And also the tx_waiting needs to be set zero to prevent virtio_net_set_status() accessing tx_bh or tx_timer.

---
  hw/net/virtio-net.c | 3 +++
  1 file changed, 3 insertions(+)
hw/net/virtio-net.c +0 -0
index 7d091c9..98bd683 100644
@@ -1522,9 +1522,12 @@ static void virtio_net_del_queue(VirtIONet *n, int index)
      if (q->tx_timer) {
          timer_del(q->tx_timer);

hw/net/virtio-net.c static void virtio_net_del_queue(VirtIONet *n, int index) · line 1533

Quoted 10 lines
hw/net/virtio-net.c +3 -0
          timer_free(q->tx_timer);
+        q->tx_timer = NULL;
      } else {
          qemu_bh_delete(q->tx_bh);
+        q->tx_bh = NULL;
      }
+    q->tx_waiting = 0;
      virtio_del_queue(vdev, index * 2 + 1);
  }
 

Thanks for the patch.

It looks to me that clearing tx_waiting is sufficient or is there any other reason that you need set tx_timer/tx_bh to NULL?

Thanks

#2 lore raw
Quoted 42 lines

-----Original Message-----
From: Jason Wang [mailto:[email protected]]
Sent: Wednesday, April 26, 2017 5:18 PM
To: wangyunjian <[email protected]>; [email protected]; [email protected]
Cc: caihe <[email protected]>
Subject: Re: [Qemu-devel] [PATCH] virtio-net: fix wild pointer when remove virtio-net queues

Quoted 19 lines · Yunjian Wang

The tx_bh or tx_timer will free in virtio_net_del_queue() function, when removing virtio-net queues if the guest doesn't support multiqueue. But it might be still referenced by virtio_net_set_status(), which needs to be set NULL. And also the tx_waiting needs to be set zero to prevent virtio_net_set_status() accessing tx_bh or tx_timer.

---
  hw/net/virtio-net.c | 3 +++
  1 file changed, 3 insertions(+)
hw/net/virtio-net.c index renamed from hw/net/virtio-net.c +0 -0

7d091c9..98bd683 100644

hw/net/virtio-net.c +0 -0
@@ -1522,9 +1522,12 @@ static void virtio_net_del_queue(VirtIONet *n, int index)
      if (q->tx_timer) {
          timer_del(q->tx_timer);

hw/net/virtio-net.c static void virtio_net_del_queue(VirtIONet *n, int index) · line 1533

Quoted 10 lines
hw/net/virtio-net.c +3 -0
          timer_free(q->tx_timer);
+        q->tx_timer = NULL;
      } else {
          qemu_bh_delete(q->tx_bh);
+        q->tx_bh = NULL;
      }
+    q->tx_waiting = 0;
      virtio_del_queue(vdev, index * 2 + 1);
  }
 

Thanks for the patch.

Quoted 2 lines

It looks to me that clearing tx_waiting is sufficient or is there any other reason that youneed set tx_timer/tx_bh to NULL?

It's just coding habit to avoid access wild pointer, such as used-after-free porblem, it hard to locate the code that cause the bug.

Thanks

Quoted 2 lines

Thanks

#3 lore raw
Quoted 41 lines · wangyunjian
Quoted 41 lines

-----Original Message-----
From: Jason Wang [mailto:[email protected]]
Sent: Wednesday, April 26, 2017 5:18 PM
To: wangyunjian <[email protected]>; [email protected]; [email protected]
Cc: caihe <[email protected]>
Subject: Re: [Qemu-devel] [PATCH] virtio-net: fix wild pointer when remove virtio-net queues

Quoted 19 lines · Yunjian Wang

The tx_bh or tx_timer will free in virtio_net_del_queue() function, when removing virtio-net queues if the guest doesn't support multiqueue. But it might be still referenced by virtio_net_set_status(), which needs to be set NULL. And also the tx_waiting needs to be set zero to prevent virtio_net_set_status() accessing tx_bh or tx_timer.

---
   hw/net/virtio-net.c | 3 +++
   1 file changed, 3 insertions(+)
hw/net/virtio-net.c index renamed from hw/net/virtio-net.c +0 -0

7d091c9..98bd683 100644

hw/net/virtio-net.c +0 -0
@@ -1522,9 +1522,12 @@ static void virtio_net_del_queue(VirtIONet *n, int index)
       if (q->tx_timer) {
           timer_del(q->tx_timer);

hw/net/virtio-net.c static void virtio_net_del_queue(VirtIONet *n, int index) · line 1533

Quoted 10 lines
hw/net/virtio-net.c +3 -0
           timer_free(q->tx_timer);
+        q->tx_timer = NULL;
       } else {
           qemu_bh_delete(q->tx_bh);
+        q->tx_bh = NULL;
       }
+    q->tx_waiting = 0;
       virtio_del_queue(vdev, index * 2 + 1);
   }
 

Thanks for the patch.

Quoted 6 lines
Quoted 2 lines

It looks to me that clearing tx_waiting is sufficient or is there any other reason that youneed set tx_timer/tx_bh to NULL?

It's just coding habit to avoid access wild pointer, such as used-after-free porblem, it hard to locate the code that cause the bug.

Thanks

Applied and queued for -stable.

Thanks

Quoted 1 line
Quoted 1 line

Thanks