Skip to contentSkip to conversation

Fix pointer arithmetic in indirect read for libvhost-user and libvduse

PATCH In review Temir Zharaspayev 4 participants 8 messages Jan 13, 2024 – Apr 18, 2024

lore
Share & tools
lore
#1 lore raw

PATCH 1/2 libvhost-user: Fix pointer arithmetic in indirect read

In reply to Temir Zharaspayev

When zero-copy usage of indirect descriptors buffer table isn't possible, library gather scattered memory chunks in a local copy. This commit fixes the issue with pointer arithmetic for the local copy buffer.

---
 subprojects/libvhost-user/libvhost-user.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
subprojects/libvhost-user/libvhost-user.c +6 -5
index 6684057370..e952c098a3 100644
@@ -2307,7 +2307,7 @@ static int
 virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc,
                              uint64_t addr, size_t len)
 {
-    struct vring_desc *ori_desc;
+    uint8_t *src_cursor, *dst_cursor;
     uint64_t read_len;
 
     if (len > (VIRTQUEUE_MAX_SIZE * sizeof(struct vring_desc))) {
@@ -2318,17 +2318,18 @@ virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc,
         return -1;
     }
 
+    dst_cursor = (uint8_t *) desc;
     while (len) {
         read_len = len;
-        ori_desc = vu_gpa_to_va(dev, &read_len, addr);
-        if (!ori_desc) {
+        src_cursor = vu_gpa_to_va(dev, &read_len, addr);
+        if (!src_cursor) {
             return -1;
         }
 
-        memcpy(desc, ori_desc, read_len);
+        memcpy(dst_cursor, src_cursor, read_len);
         len -= read_len;
         addr += read_len;
-        desc += read_len;
+        dst_cursor += read_len;
     }
 
     return 0;
Signature · 1 line
2.34.1
#4 lore raw

PATCH 2/2 libvduse: Fix pointer arithmetic in indirect read

In reply to Temir Zharaspayev

When zero-copy usage of indirect descriptors buffer table isn't possible, library gather scattered memory chunks in a local copy. This commit fixes the issue with pointer arithmetic for the local copy buffer.

---
 subprojects/libvduse/libvduse.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
subprojects/libvduse/libvduse.c +6 -5
index 21ffbb5b8d..0b445fbc76 100644
@@ -465,7 +465,7 @@ static int
 vduse_queue_read_indirect_desc(VduseDev *dev, struct vring_desc *desc,
                                uint64_t addr, size_t len)
 {
-    struct vring_desc *ori_desc;
+    uint8_t *src_cursor, *dst_cursor;
     uint64_t read_len;
 
     if (len > (VIRTQUEUE_MAX_SIZE * sizeof(struct vring_desc))) {
@@ -476,17 +476,18 @@ vduse_queue_read_indirect_desc(VduseDev *dev, struct vring_desc *desc,
         return -1;
     }
 
+    dst_cursor = (uint8_t *) desc;
     while (len) {
         read_len = len;
-        ori_desc = iova_to_va(dev, &read_len, addr);
-        if (!ori_desc) {
+        src_cursor = iova_to_va(dev, &read_len, addr);
+        if (!src_cursor) {
             return -1;
         }
 
-        memcpy(desc, ori_desc, read_len);
+        memcpy(dst_cursor, src_cursor, read_len);
         len -= read_len;
         addr += read_len;
-        desc += read_len;
+        dst_cursor += read_len;
     }
 
     return 0;
Signature · 1 line
2.34.1
#0 lore raw

cover Fix pointer arithmetic in indirect read for libvhost-user and libvduse

Hello! I have found a problem with virtqueue_read_indirect_desc function, which was advancing pointer to struct as it was a byte pointer, so every element comming after first chunk would be copied somewhere out of buffer.

As I understand this is cold path, but nevertheless worth fixing.

Also, exacly same problem in vduse_queue_read_indirect_desc function, because as I understand it is a copy of virtqueue_read_indirect_desc with vduse backend.

I was not sure if element of scattered buffer may end in the middle of vring_desc struct data, so instead of writing
desc += read_len/sizeof(struct vring_desc)
have implemented fix with proper byte pointer arithmetic.

Sincerely,
Temir.

Temir Zharaspayev (2):
libvhost-user: Fix pointer arithmetic in indirect read
libvduse: Fix pointer arithmetic in indirect read

 subprojects/libvduse/libvduse.c           | 11 ++++++-----
 subprojects/libvhost-user/libvhost-user.c | 11 ++++++-----
 2 files changed, 12 insertions(+), 10 deletions(-)
Signature · 1 line
2.34.1
author
#5 lore raw

Hello, I am very sorry for bothering community on a such minor problem again, but I got no response for a few weeks, so maybe I have started thread on a wrong mailing list, so I made an issue in gitlab issue tracker:
https://gitlab.com/qemu-project/qemu/-/issues/2149 referencing this thread.

Maybe, it would help attract proper eyes to such a simple problem, so no one bothers in trying to fix it, albeit it lives in the codebase for some time already and is being copied around.

Sincerely,
Temir.

сб, 13 янв. 2024 г. в 04:28, Temir Zharaspayev <[email protected]>:

Quoted 32 lines

Hello! I have found a problem with virtqueue_read_indirect_desc function, which
was advancing pointer to struct as it was a byte pointer, so every element comming after first chunk would be copied somewhere out of buffer.

As I understand this is cold path, but nevertheless worth fixing.

Also, exacly same problem in vduse_queue_read_indirect_desc function, because
as I understand it is a copy of virtqueue_read_indirect_desc with vduse backend.

I was not sure if element of scattered buffer may end in the middle of vring_desc struct data, so instead of writing
desc += read_len/sizeof(struct vring_desc)
have implemented fix with proper byte pointer arithmetic.

Sincerely,
Temir.

Temir Zharaspayev (2):
libvhost-user: Fix pointer arithmetic in indirect read
libvduse: Fix pointer arithmetic in indirect read

 subprojects/libvduse/libvduse.c           | 11 ++++++-----
 subprojects/libvhost-user/libvhost-user.c | 11 ++++++-----
 2 files changed, 12 insertions(+), 10 deletions(-)

--
2.34.1

#6 lore raw

Temir: yeah, this was our fault, apologies for not responding.

Michael, David, Raphael -- looks like we unfortunately lost track of this patchset -- could one of you have a look and review it, please?

thanks
-- PMM

Quoted 38 lines · Тимур

Hello, I am very sorry for bothering community on a such minor problem again, but I got no response for a few weeks, so maybe I have started thread on a wrong mailing list, so I made an issue in gitlab issue tracker: https://gitlab.com/qemu-project/qemu/-/issues/2149 referencing this thread.

Maybe, it would help attract proper eyes to such a simple problem, so no one bothers in trying to fix it, albeit it lives in the codebase for some time already and is being copied around.

Sincerely,
Temir.

сб, 13 янв. 2024 г. в 04:28, Temir Zharaspayev <[email protected]>:

Quoted 29 lines

Hello! I have found a problem with virtqueue_read_indirect_desc function, which was advancing pointer to struct as it was a byte pointer, so every element comming after first chunk would be copied somewhere out of buffer.

As I understand this is cold path, but nevertheless worth fixing.

Also, exacly same problem in vduse_queue_read_indirect_desc function, because as I understand it is a copy of virtqueue_read_indirect_desc with vduse backend.

I was not sure if element of scattered buffer may end in the middle of vring_desc struct data, so instead of writing
desc += read_len/sizeof(struct vring_desc)
have implemented fix with proper byte pointer arithmetic.

Sincerely,
Temir.

Temir Zharaspayev (2):
libvhost-user: Fix pointer arithmetic in indirect read
libvduse: Fix pointer arithmetic in indirect read

 subprojects/libvduse/libvduse.c           | 11 ++++++-----
 subprojects/libvhost-user/libvhost-user.c | 11 ++++++-----
 2 files changed, 12 insertions(+), 10 deletions(-)

--
2.34.

#2 lore raw

Re: [PATCH 1/2] libvhost-user: Fix pointer arithmetic in indirect read

In reply to Temir Zharaspayev
Quoted 33 lines · Temir Zharaspayev

When zero-copy usage of indirect descriptors buffer table isn't possible, library gather scattered memory chunks in a local copy. This commit fixes the issue with pointer arithmetic for the local copy buffer.

---
 subprojects/libvhost-user/libvhost-user.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
subprojects/libvhost-user/libvhost-user.c +3 -3
index 6684057370..e952c098a3 100644
@@ -2307,7 +2307,7 @@ static int
 virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc,
                              uint64_t addr, size_t len)
 {
-    struct vring_desc *ori_desc;
+    uint8_t *src_cursor, *dst_cursor;
     uint64_t read_len;
 
     if (len > (VIRTQUEUE_MAX_SIZE * sizeof(struct vring_desc))) {
@@ -2318,17 +2318,18 @@ virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc,
         return -1;
     }
 
+    dst_cursor = (uint8_t *) desc;
     while (len) {
         read_len = len;
-        ori_desc = vu_gpa_to_va(dev, &read_len, addr);
-        if (!ori_desc) {
+        src_cursor = vu_gpa_to_va(dev, &read_len, addr);

subprojects/libvhost-user/libvhost-user.c virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc, · line 2332

Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +3 -2
+        if (!src_cursor) {
             return -1;
         }
 
-        memcpy(desc, ori_desc, read_len);
+        memcpy(dst_cursor, src_cursor, read_len);
         len -= read_len;
         addr += read_len;
-        desc += read_len;
+        dst_cursor += read_len;

The ori_desc -> src_cursor changes don't look to have any functional effect. Having that change present obscures the functional part of the patch, which is this line. FWIW, it is generally preferrable to not mix functional and non-functional changes in the same patch

It now interprets 'read_len' as the number of bytes to increment the address by, rather than incrementing by the number of elements of size 'sizeof(struct vring_desc)'.

I don't know enough about this area of QEMU code to say which semantics were desired, so I'll defer to the Michael as maintainer to give a formal review.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#7 lore raw
In reply to Тимур

Adding Michael back to the CC, since he's the designated maintainer for libvhost-user/

Michael, could you give these patches a review since they've been pending for many months now.

Quoted 5 lines · Тимур

Hello, I am very sorry for bothering community on a such minor problem again, but I got no response for a few weeks, so maybe I have started thread on a wrong mailing list, so I made an issue in gitlab issue tracker:
https://gitlab.com/qemu-project/qemu/-/issues/2149 referencing this thread.

Quoted 41 lines

Maybe, it would help attract proper eyes to such a simple problem, so no one bothers in trying to fix it, albeit it lives in the codebase for some time already and is being copied around.

Sincerely,
Temir.

сб, 13 янв. 2024 г. в 04:28, Temir Zharaspayev <[email protected]>:

Quoted 32 lines

Hello! I have found a problem with virtqueue_read_indirect_desc function, which
was advancing pointer to struct as it was a byte pointer, so every element comming after first chunk would be copied somewhere out of buffer.

As I understand this is cold path, but nevertheless worth fixing.

Also, exacly same problem in vduse_queue_read_indirect_desc function, because
as I understand it is a copy of virtqueue_read_indirect_desc with vduse backend.

I was not sure if element of scattered buffer may end in the middle of vring_desc struct data, so instead of writing
desc += read_len/sizeof(struct vring_desc)
have implemented fix with proper byte pointer arithmetic.

Sincerely,
Temir.

Temir Zharaspayev (2):
libvhost-user: Fix pointer arithmetic in indirect read
libvduse: Fix pointer arithmetic in indirect read

 subprojects/libvduse/libvduse.c           | 11 ++++++-----
 subprojects/libvhost-user/libvhost-user.c | 11 ++++++-----
 2 files changed, 12 insertions(+), 10 deletions(-)

--
2.34.1

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw

Re: [PATCH 1/2] libvhost-user: Fix pointer arithmetic in indirect read

In reply to Daniel P. Berrangé

The change looks right to me. As is, it looks like the code is skipping over descriptors when the intent should be to bounce data into a single descriptor.

I agree the variable rename should go in as a separate change.

Quoted 24 lines · Daniel P. Berrangé
Quoted 22 lines · Temir Zharaspayev

When zero-copy usage of indirect descriptors buffer table isn't possible, library gather scattered memory chunks in a local copy. This commit fixes the issue with pointer arithmetic for the local copy buffer.

---
 subprojects/libvhost-user/libvhost-user.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
subprojects/libvhost-user/libvhost-user.c +1 -1
index 6684057370..e952c098a3 100644
@@ -2307,7 +2307,7 @@ static int
 virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc,
                              uint64_t addr, size_t len)
 {
-    struct vring_desc *ori_desc;
+    uint8_t *src_cursor, *dst_cursor;
     uint64_t read_len;
 
Quoted 6 lines

subprojects/libvhost-user/libvhost-user.c virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc, · line 2321

Quoted 6 lines
subprojects/libvhost-user/libvhost-user.c +1 -0
     if (len > (VIRTQUEUE_MAX_SIZE * sizeof(struct vring_desc))) {
@@ -2318,17 +2318,18 @@ virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc,
         return -1;
     }
 
+    dst_cursor = (uint8_t *) desc;

Nit - no space on cast

Quoted 38 lines
Quoted 5 lines
subprojects/libvhost-user/libvhost-user.c +1 -2
     while (len) {
         read_len = len;
-        ori_desc = vu_gpa_to_va(dev, &read_len, addr);
-        if (!ori_desc) {
+        src_cursor = vu_gpa_to_va(dev, &read_len, addr);

subprojects/libvhost-user/libvhost-user.c virtqueue_read_indirect_desc(VuDev *dev, struct vring_desc *desc, · line 2332

Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +3 -2
+        if (!src_cursor) {
             return -1;
         }
 
-        memcpy(desc, ori_desc, read_len);
+        memcpy(dst_cursor, src_cursor, read_len);
         len -= read_len;
         addr += read_len;
-        desc += read_len;
+        dst_cursor += read_len;

The ori_desc -> src_cursor changes don't look to have any functional effect. Having that change present obscures the functional part of the patch, which is this line. FWIW, it is generally preferrable to not mix functional and non-functional changes in the same patch

It now interprets 'read_len' as the number of bytes to increment the address by, rather than incrementing by the number of elements of size 'sizeof(struct vring_desc)'.

I don't know enough about this area of QEMU code to say which semantics were desired, so I'll defer to the Michael as maintainer to give a formal review.

With regards,
Daniel
--

|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|