Skip to contentSkip to conversation

vhost: fix null pointer dereference

PATCH In review Jiayu Hu 3 participants 4 messages Mar 28, 2022 – Jun 9, 2022

lore
Share & tools
lore

Jiayu Hu

author
#0 lore raw

PATCH 1/1 vhost: fix null pointer dereference

NULL check for vq->async must be protected by lock. Otherwise, it is possible that the data plane thread dereferences vq->async with NULL value, since the control plane thread is freeing vq->async.

Fixes ee8024b3d4ad (vhost: move async data in dedicated structure)
---
 lib/vhost/vhost.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
lib/vhost/vhost.c +3 -3
index bc88148347..7f60c2824f 100644
@@ -1887,9 +1887,6 @@ rte_vhost_async_get_inflight(int vid, uint16_t queue_id)
 	if (vq == NULL)
 		return ret;
 
-	if (!vq->async)
-		return ret;
-
 	if (!rte_spinlock_trylock(&vq->access_lock)) {
 		VHOST_LOG_CONFIG(DEBUG,
 			"(%s) failed to check in-flight packets. virtqueue busy.\n",
@@ -1897,6 +1894,9 @@ rte_vhost_async_get_inflight(int vid, uint16_t queue_id)
 		return ret;
 	}
 
+	if (!vq->async)
+		return ret;
+
 	ret = vq->async->pkts_inflight_n;
 	rte_spinlock_unlock(&vq->access_lock);
 
Signature · 1 line
2.25.1
#1 lore raw
Quoted 28 lines · Jiayu Hu

NULL check for vq->async must be protected by lock. Otherwise, it is possible that the data plane thread dereferences vq->async with NULL value, since the control plane thread is freeing vq->async.

Fixes ee8024b3d4ad (vhost: move async data in dedicated structure)
---
 lib/vhost/vhost.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
lib/vhost/vhost.c +0 -3
index bc88148347..7f60c2824f 100644
@@ -1887,9 +1887,6 @@ rte_vhost_async_get_inflight(int vid, uint16_t queue_id)
        if (vq == NULL)
                return ret;
 
-       if (!vq->async)
-               return ret;
-
        if (!rte_spinlock_trylock(&vq->access_lock)) {
                VHOST_LOG_CONFIG(DEBUG,
                        "(%s) failed to check in-flight packets. virtqueue busy.\n",

lib/vhost/vhost.c rte_vhost_async_get_inflight(int vid, uint16_t queue_id) · line 1898

Quoted 5 lines
lib/vhost/vhost.c +2 -0
@@ -1897,6 +1894,9 @@ rte_vhost_async_get_inflight(int vid, uint16_t queue_id)
                return ret;
        }
 
+       if (!vq->async)
+               return ret;

Lock is still taken at this point.

FYI, I'll post a series to instrument locks in vhost, soon.

Quoted 4 lines
lib/vhost/vhost.c +1 -0
+
        ret = vq->async->pkts_inflight_n;
        rte_spinlock_unlock(&vq->access_lock);
 
Signature · 1 line
David Marchand
#2 lore raw

HI Jiayu,

Quoted 32 lines · David Marchand
Quoted 31 lines · Jiayu Hu

NULL check for vq->async must be protected by lock. Otherwise, it is possible that the data plane thread dereferences vq->async with NULL value, since the control plane thread is freeing vq->async.

Fixes ee8024b3d4ad (vhost: move async data in dedicated structure)
---
  lib/vhost/vhost.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
lib/vhost/vhost.c +0 -3
index bc88148347..7f60c2824f 100644
@@ -1887,9 +1887,6 @@ rte_vhost_async_get_inflight(int vid, uint16_t queue_id)
         if (vq == NULL)
                 return ret;
 
-       if (!vq->async)
-               return ret;
-
         if (!rte_spinlock_trylock(&vq->access_lock)) {
                 VHOST_LOG_CONFIG(DEBUG,
                         "(%s) failed to check in-flight packets. virtqueue busy.\n",
@@ -1897,6 +1894,9 @@ rte_vhost_async_get_inflight(int vid, uint16_t queue_id)
                 return ret;
         }
 
Quoted 6 lines

lib/vhost/vhost.c rte_vhost_async_get_inflight(int vid, uint16_t queue_id) · line 1898

Quoted 2 lines
lib/vhost/vhost.c +2 -0
+       if (!vq->async)
+               return ret;

Lock is still taken at this point.

FYI, I'll post a series to instrument locks in vhost, soon.

Could you please send a v2 which does not return with the lock taken?

Quoted 6 lines

lib/vhost/vhost.c rte_vhost_async_get_inflight(int vid, uint16_t queue_id) · line 1902

Quoted 4 lines
lib/vhost/vhost.c +1 -0
+
         ret = vq->async->pkts_inflight_n;
         rte_spinlock_unlock(&vq->access_lock);
 

Thanks,
Maxime

author
#3 lore raw

Hi Maxime,

This issue has fixed in https://patches.dpdk.org/project/dpdk/patch/[email protected]/.

Thanks,
Jiayu

Quoted 63 lines

-----Original Message-----
From: Maxime Coquelin <[email protected]>
Sent: Wednesday, June 1, 2022 3:58 PM
To: Hu, Jiayu <[email protected]>
Cc: dev <[email protected]>; dpdk stable <[email protected]>; David Marchand <[email protected]>
Subject: Re: [PATCH] vhost: fix null pointer dereference

HI Jiayu,

Quoted 14 lines · David Marchand
Quoted 13 lines · Jiayu Hu

NULL check for vq->async must be protected by lock. Otherwise, it is possible that the data plane thread dereferences vq->async with NULL value, since the control plane thread is freeing vq->async.

Fixes ee8024b3d4ad (vhost: move async data in dedicated structure)
---
  lib/vhost/vhost.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
Quoted 5 lines

lib/vhost/vhost.c rte_vhost_async_get_inflight(int vid, uint16_t

Quoted 5 lines
lib/vhost/vhost.c index renamed from lib/vhost/vhost.c +0 -0

bc88148347..7f60c2824f 100644

lib/vhost/vhost.c +0 -0
@@ -1887,9 +1887,6 @@ rte_vhost_async_get_inflight(int vid, uint16_t

queue_id)

Quoted 10 lines
Quoted 10 lines
lib/vhost/vhost.c +0 -3
         if (vq == NULL)
                 return ret;
 
-       if (!vq->async)
-               return ret;
-
         if (!rte_spinlock_trylock(&vq->access_lock)) {
                 VHOST_LOG_CONFIG(DEBUG,
                         "(%s) failed to check in-flight packets.

virtqueue busy.\n", @@ -1897,6 +1894,9 @@

rte_vhost_async_get_inflight(int vid, uint16_t queue_id)

Quoted 9 lines

lib/vhost/vhost.c rte_vhost_async_get_inflight(int vid, uint16_t queue_id) · line 1898

Quoted 5 lines
                 return ret;
         }
lib/vhost/vhost.c +2 -0
+       if (!vq->async)
+               return ret;

Lock is still taken at this point.

FYI, I'll post a series to instrument locks in vhost, soon.

Could you please send a v2 which does not return with the lock taken?

Quoted 6 lines
Quoted 4 lines

+

         ret = vq->async->pkts_inflight_n;
         rte_spinlock_unlock(&vq->access_lock);

Thanks,
Maxime