Skip to contentSkip to conversation

vdpa: Signedness bugs in vdpasim_work()

PATCH In review Dan Carpenter 2 participants 4 messages Apr 6, 2020 – Apr 9, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 vdpa: Signedness bugs in vdpasim_work()

The "read" and "write" variables need to be signed for the error handling to work.

Fixes 2c53d0f64c06 ("vdpasim: vDPA device simulator")
---
 drivers/vdpa/vdpa_sim/vdpa_sim.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/vdpa/vdpa_sim/vdpa_sim.c +2 -1
index 6e8a0cf2fdeb..b3c800653056 100644
@@ -132,7 +132,8 @@ static void vdpasim_work(struct work_struct *work)
 						 vdpasim, work);
 	struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
 	struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
-	size_t read, write, total_write;
+	ssize_t read, write;
+	size_t total_write;
 	int err;
 	int pkts = 0;
 
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/2 vdpa: Fix pointer math bug in vdpasim_get_config()

If "offset" is non-zero then we end up copying from beyond the end of the config because of pointer math. We can fix this by casting the struct to a u8 pointer.

Fixes 2c53d0f64c06 ("vdpasim: vDPA device simulator")

---
Is it really worth letting people specify the offset?

 drivers/vdpa/vdpa_sim/vdpa_sim.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vdpa/vdpa_sim/vdpa_sim.c +1 -1
index b3c800653056..e03c25765513 100644
@@ -509,7 +509,7 @@ static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset,
 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
 
 	if (offset + len < sizeof(struct virtio_net_config))
-		memcpy(buf, &vdpasim->config + offset, len);
+		memcpy(buf, (u8 *)&vdpasim->config + offset, len);
 }
 
 static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset,
Signature · 1 line
2.25.1
Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] vdpa: Fix pointer math bug in vdpasim_get_config()

Quoted 17 lines · Dan Carpenter

If "offset" is non-zero then we end up copying from beyond the end of the config because of pointer math. We can fix this by casting the struct to a u8 pointer.

Fixes 2c53d0f64c06 ("vdpasim: vDPA device simulator")

---
Is it really worth letting people specify the offset?

  drivers/vdpa/vdpa_sim/vdpa_sim.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vdpa/vdpa_sim/vdpa_sim.c +0 -0
index b3c800653056..e03c25765513 100644

drivers/vdpa/vdpa_sim/vdpa_sim.c static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset, · line 515

Quoted 8 lines
drivers/vdpa/vdpa_sim/vdpa_sim.c +1 -1
@@ -509,7 +509,7 @@ static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset,
  	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
 
  	if (offset + len < sizeof(struct virtio_net_config))
-		memcpy(buf, &vdpasim->config + offset, len);
+		memcpy(buf, (u8 *)&vdpasim->config + offset, len);
  }
 
  static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset,
Acked-by on v1 1/2
#3 lore raw
In reply to Dan Carpenter
Quoted 14 lines · Dan Carpenter

The "read" and "write" variables need to be signed for the error handling to work.

Fixes 2c53d0f64c06 ("vdpasim: vDPA device simulator")
---
  drivers/vdpa/vdpa_sim/vdpa_sim.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/vdpa/vdpa_sim/vdpa_sim.c +0 -0
index 6e8a0cf2fdeb..b3c800653056 100644

drivers/vdpa/vdpa_sim/vdpa_sim.c static void vdpasim_work(struct work_struct *work) · line 139

Quoted 9 lines
drivers/vdpa/vdpa_sim/vdpa_sim.c +2 -1
@@ -132,7 +132,8 @@ static void vdpasim_work(struct work_struct *work)
  						 vdpasim, work);
  	struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
  	struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
-	size_t read, write, total_write;
+	ssize_t read, write;
+	size_t total_write;
  	int err;
  	int pkts = 0;