Skip to contentSkip to conversation

virtio_net: fix race on control_buf

PATCH In review Heng Qi 4 participants 12 messages May 28, 2024 – Jun 23, 2024

Subsystems (4)
lore
Share & tools
lore

Heng Qi

author
#0 lore raw

cover virtio_net: fix race on control_buf

Patch 1 did a simple rename, leaving 'ret' for patch 2. Patch 2 fixed a race between reading the device response and the new command submission.

Heng Qi (2):
virtio_net: rename ret to err
virtio_net: fix missing lock protection on control_buf access

 drivers/net/virtio_net.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
Signature · 1 line
2.32.0.3.g01195cf9f
author
#1 lore raw

PATCH 1/2 virtio_net: rename ret to err

The integer variable 'ret', denoting the return code, is mismatched with the boolean return type of virtnet_send_command_reply(); hence, it is renamed to 'err'.

The usage of 'ret' is deferred to the next patch.

---
 drivers/net/virtio_net.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/virtio_net.c +4 -4
index 4a802c0ea2cb..6b0512a628e0 100644
@@ -2686,7 +2686,7 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 {
 	struct scatterlist *sgs[5], hdr, stat;
 	u32 out_num = 0, tmp, in_num = 0;
-	int ret;
+	int err;
 
 	/* Caller should know better */
 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
@@ -2710,10 +2710,10 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 		sgs[out_num + in_num++] = in;
 
 	BUG_ON(out_num + in_num > ARRAY_SIZE(sgs));
-	ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi, GFP_ATOMIC);
-	if (ret < 0) {
+	err = virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi, GFP_ATOMIC);
+	if (err < 0) {
 		dev_warn(&vi->vdev->dev,
-			 "Failed to add sgs for command vq: %d\n.", ret);
+			 "Failed to add sgs for command vq: %d\n.", err);
 		mutex_unlock(&vi->cvq_lock);
 		return false;
 	}
Signature · 1 line
2.32.0.3.g01195cf9f
#2 lore raw

PATCH 1/2 virtio_net: rename ret to err

Quoted 60 lines

-----Original Message-----
From: Heng Qi <[email protected]>
Sent: Tuesday, May 28, 2024 1:22 PM
To: [email protected]; [email protected]
Cc: Michael S. Tsirkin <[email protected]>; Jason Wang <[email protected]>; Xuan Zhuo <[email protected]>; Eugenio Pérez <[email protected]>; David S. Miller <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>; Jiri Pirko <[email protected]>; Daniel Jurgens <[email protected]>
Subject: [EXTERNAL] [PATCH net 1/2] virtio_net: rename ret to err

Prioritize security for external emails: Confirm sender and content safety before clicking links or opening attachments

----------------------------------------------------------------------
The integer variable 'ret', denoting the return code, is mismatched with the boolean return type of virtnet_send_command_reply(); hence, it is renamed to 'err'.

The usage of 'ret' is deferred to the next patch.

---
 drivers/net/virtio_net.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/virtio_net.c index renamed from drivers/net/virtio_net.c +0 -0

4a802c0ea2cb..6b0512a628e0 100644

drivers/net/virtio_net.c +1 -1
@@ -2686,7 +2686,7 @@ static bool virtnet_send_command_reply(struct
 virtnet_info *vi, u8 class, u8 cmd  {
 	struct scatterlist *sgs[5], hdr, stat;
 	u32 out_num = 0, tmp, in_num = 0;
-	int ret;
+	int err;
 
 	/* Caller should know better */
 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
@@ -2710,10 +2710,10 @@ static bool virtnet_send_command_reply(struct

virtnet_info *vi, u8 class, u8 cmd

drivers/net/virtio_net.c +0 -1
 		sgs[out_num + in_num++] = in;
 
 	BUG_ON(out_num + in_num > ARRAY_SIZE(sgs));
-	ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi,

GFP_ATOMIC);

drivers/net/virtio_net.c +1 -1
-	if (ret < 0) {
+	err = virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi,

GFP_ATOMIC);

drivers/net/virtio_net.c +2 -2
+	if (err < 0) {
 		dev_warn(&vi->vdev->dev,
-			 "Failed to add sgs for command vq: %d\n.", ret);
+			 "Failed to add sgs for command vq: %d\n.", err);
 		mutex_unlock(&vi->cvq_lock);
 		return false;
 	}
--
Quoted 2 lines

2.32.0.3.g01195cf9f

maintainer
#3 lore raw

Re: [PATCH net 1/2] virtio_net: rename ret to err

In reply to Heng Qi
Quoted 5 lines · On Tue, 28 May 2024 15:52:25 +0800 Heng Qi wrote:

The integer variable 'ret', denoting the return code, is mismatched with the boolean return type of virtnet_send_command_reply(); hence, it is renamed to 'err'.

The usage of 'ret' is deferred to the next patch.

That seems a bit much. Can you call the variable in patch 2 "ok" instead?

Signature · 1 line
pw-bot: cr
author
#4 lore raw

Re: [PATCH net 1/2] virtio_net: rename ret to err

Quoted 9 lines · Jakub Kicinski
Quoted 5 lines · On Tue, 28 May 2024 15:52:25 +0800 Heng Qi wrote:

The integer variable 'ret', denoting the return code, is mismatched with the boolean return type of virtnet_send_command_reply(); hence, it is renamed to 'err'.

The usage of 'ret' is deferred to the next patch.

That seems a bit much. Can you call the variable in patch 2 "ok" instead?

Sure.

Thanks.

Quoted 2 lines
Signature · 1 line
pw-bot: cr
author
#5 lore raw

PATCH 2/2 virtio_net: fix missing lock protection on control_buf access

In reply to Heng Qi

Refactored the handling of control_buf to be within the cvq_lock critical section, mitigating race conditions between reading device responses and new command submissions.

Fixes 6f45ab3e0409 ("virtio_net: Add a lock for the command VQ.")
---
 drivers/net/virtio_net.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +3 -1
index 6b0512a628e0..3d8407d9e3d2 100644
@@ -2686,6 +2686,7 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 {
 	struct scatterlist *sgs[5], hdr, stat;
 	u32 out_num = 0, tmp, in_num = 0;
+	bool ret;
 	int err;
 
 	/* Caller should know better */
@@ -2731,8 +2732,9 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 	}
 
 unlock:
+	ret = vi->ctrl->status == VIRTIO_NET_OK;
 	mutex_unlock(&vi->cvq_lock);
-	return vi->ctrl->status == VIRTIO_NET_OK;
+	return ret;
 }
 
 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
Signature · 1 line
2.32.0.3.g01195cf9f
#6 lore raw

PATCH 2/2 virtio_net: fix missing lock protection on control_buf access

Quoted 37 lines

Refactored the handling of control_buf to be within the cvq_lock critical section, mitigating race conditions between reading device responses and new command submissions.

Fixes 6f45ab3e0409 ("virtio_net: Add a lock for the command VQ.")
---
 drivers/net/virtio_net.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c index renamed from drivers/net/virtio_net.c +0 -0

6b0512a628e0..3d8407d9e3d2 100644

drivers/net/virtio_net.c +1 -0
@@ -2686,6 +2686,7 @@ static bool virtnet_send_command_reply(struct
 virtnet_info *vi, u8 class, u8 cmd  {
 	struct scatterlist *sgs[5], hdr, stat;
 	u32 out_num = 0, tmp, in_num = 0;
+	bool ret;
 	int err;
 
 	/* Caller should know better */
@@ -2731,8 +2732,9 @@ static bool virtnet_send_command_reply(struct

virtnet_info *vi, u8 class, u8 cmd

drivers/net/virtio_net.c +2 -2
 	}
 
 unlock:
+	ret = vi->ctrl->status == VIRTIO_NET_OK;
 	mutex_unlock(&vi->cvq_lock);
-	return vi->ctrl->status == VIRTIO_NET_OK;
+	return ret;
 }
 
 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
--
Quoted 2 lines

2.32.0.3.g01195cf9f

#7 lore raw

Re: [PATCH net 2/2] virtio_net: fix missing lock protection on control_buf access

In reply to Heng Qi
Quoted 6 lines · Heng Qi

Refactored the handling of control_buf to be within the cvq_lock critical section, mitigating race conditions between reading device responses and new command submissions.

Fixes 6f45ab3e0409 ("virtio_net: Add a lock for the command VQ.")

I don't get what does this change. status can change immediately after you drop the mutex, can it not? what exactly is the race conditions you are worried about?

Quoted 29 lines
---
 drivers/net/virtio_net.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +3 -1
index 6b0512a628e0..3d8407d9e3d2 100644
@@ -2686,6 +2686,7 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 {
 	struct scatterlist *sgs[5], hdr, stat;
 	u32 out_num = 0, tmp, in_num = 0;
+	bool ret;
 	int err;
 
 	/* Caller should know better */
@@ -2731,8 +2732,9 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 	}
 
 unlock:
+	ret = vi->ctrl->status == VIRTIO_NET_OK;
 	mutex_unlock(&vi->cvq_lock);
-	return vi->ctrl->status == VIRTIO_NET_OK;
+	return ret;
 }
 
 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
Signature · 1 line
2.32.0.3.g01195cf9f
author
#8 lore raw

Re: [PATCH net 2/2] virtio_net: fix missing lock protection on control_buf access

Quoted 5 lines · Michael S. Tsirkin
Quoted 4 lines · Heng Qi

Refactored the handling of control_buf to be within the cvq_lock critical section, mitigating race conditions between reading device responses and new command submissions.

Quoted 7 lines
Quoted 2 lines
Fixes 6f45ab3e0409 ("virtio_net: Add a lock for the command VQ.")

I don't get what does this change. status can change immediately after you drop the mutex, can it not? what exactly is the race conditions you are worried about?

See the following case:

1. Command A is acknowledged and successfully executed by the device.
2. After releasing the mutex (mutex_unlock), process P1 gets preempted before it can read vi->ctrl->status, *which should be VIRTIO_NET_OK*.
3. A new command B (like the DIM command) is issued.
4. Post vi->ctrl->status being set to VIRTIO_NET_ERR by virtnet_send_command_reply(), process P2 gets preempted.
5. Process P1 resumes, reads *vi->ctrl->status as VIRTIO_NET_ERR*, and reports this error back for Command A. <-- Race causes incorrect results to be read.

Thanks.

Quoted 31 lines
Quoted 30 lines
---
 drivers/net/virtio_net.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +3 -1
index 6b0512a628e0..3d8407d9e3d2 100644
@@ -2686,6 +2686,7 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 {
 	struct scatterlist *sgs[5], hdr, stat;
 	u32 out_num = 0, tmp, in_num = 0;
+	bool ret;
 	int err;
 
 	/* Caller should know better */
@@ -2731,8 +2732,9 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 	}
 
 unlock:
+	ret = vi->ctrl->status == VIRTIO_NET_OK;
 	mutex_unlock(&vi->cvq_lock);
-	return vi->ctrl->status == VIRTIO_NET_OK;
+	return ret;
 }
 
 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
Signature · 1 line
2.32.0.3.g01195cf9f
#9 lore raw

Re: [PATCH net 2/2] virtio_net: fix missing lock protection on control_buf access

Quoted 25 lines · Heng Qi
Quoted 5 lines · Michael S. Tsirkin
Quoted 4 lines · Heng Qi

Refactored the handling of control_buf to be within the cvq_lock critical section, mitigating race conditions between reading device responses and new command submissions.

Quoted 7 lines
Quoted 2 lines
Fixes 6f45ab3e0409 ("virtio_net: Add a lock for the command VQ.")

I don't get what does this change. status can change immediately after you drop the mutex, can it not? what exactly is the race conditions you are worried about?

See the following case:

1. Command A is acknowledged and successfully executed by the device.
2. After releasing the mutex (mutex_unlock), process P1 gets preempted before it can read vi->ctrl->status, *which should be VIRTIO_NET_OK*.
3. A new command B (like the DIM command) is issued.
4. Post vi->ctrl->status being set to VIRTIO_NET_ERR by virtnet_send_command_reply(), process P2 gets preempted.
5. Process P1 resumes, reads *vi->ctrl->status as VIRTIO_NET_ERR*, and reports this error back for Command A. <-- Race causes incorrect results to be read.

Quoted 1 line

Thanks.

Why is it important that P1 gets VIRTIO_NET_OK?
After all it is no longer the state.

Quoted 31 lines
Quoted 31 lines
Quoted 30 lines
---
 drivers/net/virtio_net.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +3 -1
index 6b0512a628e0..3d8407d9e3d2 100644
@@ -2686,6 +2686,7 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 {
 	struct scatterlist *sgs[5], hdr, stat;
 	u32 out_num = 0, tmp, in_num = 0;
+	bool ret;
 	int err;
 
 	/* Caller should know better */
@@ -2731,8 +2732,9 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 	}
 
 unlock:
+	ret = vi->ctrl->status == VIRTIO_NET_OK;
 	mutex_unlock(&vi->cvq_lock);
-	return vi->ctrl->status == VIRTIO_NET_OK;
+	return ret;
 }
 
 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
Signature · 1 line
2.32.0.3.g01195cf9f
author
#10 lore raw

Re: [PATCH net 2/2] virtio_net: fix missing lock protection on control_buf access

Quoted 26 lines · Michael S. Tsirkin
Quoted 25 lines · Heng Qi
Quoted 5 lines · Michael S. Tsirkin
Quoted 4 lines · Heng Qi

Refactored the handling of control_buf to be within the cvq_lock critical section, mitigating race conditions between reading device responses and new command submissions.

Quoted 7 lines
Quoted 2 lines
Fixes 6f45ab3e0409 ("virtio_net: Add a lock for the command VQ.")

I don't get what does this change. status can change immediately after you drop the mutex, can it not? what exactly is the race conditions you are worried about?

See the following case:

1. Command A is acknowledged and successfully executed by the device.
2. After releasing the mutex (mutex_unlock), process P1 gets preempted before it can read vi->ctrl->status, *which should be VIRTIO_NET_OK*.
3. A new command B (like the DIM command) is issued.
4. Post vi->ctrl->status being set to VIRTIO_NET_ERR by virtnet_send_command_reply(), process P2 gets preempted.
5. Process P1 resumes, reads *vi->ctrl->status as VIRTIO_NET_ERR*, and reports this error back for Command A. <-- Race causes incorrect results to be read.

Quoted 5 lines
Quoted 1 line

Thanks.

Why is it important that P1 gets VIRTIO_NET_OK?
After all it is no longer the state.

The driver needs to know whether the command actually executed success.

Thanks.

Quoted 33 lines
Quoted 32 lines
Quoted 32 lines
Quoted 31 lines

---
 drivers/net/virtio_net.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +3 -1
index 6b0512a628e0..3d8407d9e3d2 100644
@@ -2686,6 +2686,7 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 {
 	struct scatterlist *sgs[5], hdr, stat;
 	u32 out_num = 0, tmp, in_num = 0;
+	bool ret;
 	int err;
 
 	/* Caller should know better */
@@ -2731,8 +2732,9 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd
 	}
 
 unlock:
+	ret = vi->ctrl->status == VIRTIO_NET_OK;
 	mutex_unlock(&vi->cvq_lock);
-	return vi->ctrl->status == VIRTIO_NET_OK;
+	return ret;
 }
 
 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
Signature · 1 line
2.32.0.3.g01195cf9f
maintainer Acked-by on the v1 series
#11 lore raw
In reply to Heng Qi
Quoted 3 lines · Heng Qi

Patch 1 did a simple rename, leaving 'ret' for patch 2. Patch 2 fixed a race between reading the device response and the new command submission.

Quoted 9 lines

Heng Qi (2):
virtio_net: rename ret to err
virtio_net: fix missing lock protection on control_buf access

 drivers/net/virtio_net.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
Signature · 1 line
2.32.0.3.g01195cf9f