[PATCH v2] hw/usb: Fix USB MTP heap OOB read via dlen underflow

Tommaso Califano posted 1 patch 1 month, 2 weeks ago
Failed in applying to current master (apply log)
hw/usb/dev-mtp.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
[PATCH v2] hw/usb: Fix USB MTP heap OOB read via dlen underflow
Posted by Tommaso Califano 1 month, 2 weeks ago
In QEMU’s USB MTP device, usb_mtp_write_metadata() computes
filename_chars = dlen - offsetof(ObjectInfo, filename) where
offsetof(ObjectInfo,filename) = 53. The dlen parameter is the size of the
last USB packet, not the total ObjectInfo size. If the final packet has
fewer than 53 bytes, filename_chars underflows to SIZE_MAX, causing
utf16_to_str() to read far beyond the heap buffer, leaking QEMU host
process memory.

Replace dlen with d->length to avoid this issue, given that the latter
represents the total size of the buffer allocated for the unsegmented object.

Fixes: 90c1a74271ce ("usb-mtp: Limit filename to object information size")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3992
Signed-off-by: Tommaso Califano <tcalifano@redhat.com>
---

v2:
 - Remove the dlen guard and variable in favor of using d->length.

 hw/usb/dev-mtp.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/hw/usb/dev-mtp.c b/hw/usb/dev-mtp.c
index 1d8cfd32dc..53b5904a5c 100644
--- a/hw/usb/dev-mtp.c
+++ b/hw/usb/dev-mtp.c
@@ -1696,7 +1696,7 @@ close:
     s->write_pending = false;
 }
 
-static void usb_mtp_write_metadata(MTPState *s, uint64_t dlen)
+static void usb_mtp_write_metadata(MTPState *s)
 {
     MTPData *d = s->data_out;
     ObjectInfo *dataset = (ObjectInfo *)d->data;
@@ -1704,7 +1704,7 @@ static void usb_mtp_write_metadata(MTPState *s, uint64_t dlen)
     MTPObject *o;
     MTPObject *p = usb_mtp_object_lookup(s, s->dataset.parent_handle);
     uint32_t next_handle = s->next_handle;
-    size_t filename_chars = dlen - offsetof(ObjectInfo, filename);
+    size_t filename_chars = d->length - offsetof(ObjectInfo, filename);
 
     /*
      * filename is utf-16. We're intentionally doing
@@ -1801,7 +1801,7 @@ static void usb_mtp_get_data(MTPState *s, mtp_container *container,
         if (d->data_offset == d->length) {
             /* The operation might have already failed */
             if (!s->result) {
-                usb_mtp_write_metadata(s, dlen);
+                usb_mtp_write_metadata(s);
             }
             usb_mtp_data_free(s->data_out);
             s->data_out = NULL;
-- 
2.55.0


Re: [PATCH v2] hw/usb: Fix USB MTP heap OOB read via dlen underflow
Posted by Luigi Leonardi 1 month, 2 weeks ago
On Mon, Oct 05, 2026 at 06:25:49PM +0200, Tommaso Califano wrote:
>In QEMU’s USB MTP device, usb_mtp_write_metadata() computes
>filename_chars = dlen - offsetof(ObjectInfo, filename) where
>offsetof(ObjectInfo,filename) = 53. The dlen parameter is the size of the
>last USB packet, not the total ObjectInfo size. If the final packet has
>fewer than 53 bytes, filename_chars underflows to SIZE_MAX, causing
>utf16_to_str() to read far beyond the heap buffer, leaking QEMU host
>process memory.
>
>Replace dlen with d->length to avoid this issue, given that the latter
>represents the total size of the buffer allocated for the unsegmented object.
>
>Fixes: 90c1a74271ce ("usb-mtp: Limit filename to object information size")
>Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3992
>Signed-off-by: Tommaso Califano <tcalifano@redhat.com>
>---
>
>v2:
> - Remove the dlen guard and variable in favor of using d->length.
>
> hw/usb/dev-mtp.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
>diff --git a/hw/usb/dev-mtp.c b/hw/usb/dev-mtp.c
>index 1d8cfd32dc..53b5904a5c 100644
>--- a/hw/usb/dev-mtp.c
>+++ b/hw/usb/dev-mtp.c
>@@ -1696,7 +1696,7 @@ close:
>     s->write_pending = false;
> }
>
>-static void usb_mtp_write_metadata(MTPState *s, uint64_t dlen)
>+static void usb_mtp_write_metadata(MTPState *s)
> {
>     MTPData *d = s->data_out;
>     ObjectInfo *dataset = (ObjectInfo *)d->data;
>@@ -1704,7 +1704,7 @@ static void usb_mtp_write_metadata(MTPState *s, uint64_t dlen)
>     MTPObject *o;
>     MTPObject *p = usb_mtp_object_lookup(s, s->dataset.parent_handle);
>     uint32_t next_handle = s->next_handle;
>-    size_t filename_chars = dlen - offsetof(ObjectInfo, filename);
>+    size_t filename_chars = d->length - offsetof(ObjectInfo, filename);
>
>     /*
>      * filename is utf-16. We're intentionally doing
>@@ -1801,7 +1801,7 @@ static void usb_mtp_get_data(MTPState *s, mtp_container *container,
>         if (d->data_offset == d->length) {
>             /* The operation might have already failed */
>             if (!s->result) {
>-                usb_mtp_write_metadata(s, dlen);
>+                usb_mtp_write_metadata(s);
>             }
>             usb_mtp_data_free(s->data_out);
>             s->data_out = NULL;
>-- 
>2.55.0
>

IIUC, `d->length` is controlled by the guest, so I think we still need
the check that was present in the v1.

Thanks,
Luigi


Re: [PATCH v2] hw/usb: Fix USB MTP heap OOB read via dlen underflow
Posted by Tommaso Califano 1 month, 2 weeks ago
On Tue, Oct 06, 2026 at 04:12:07PM +0200, Luigi Leonardi wrote:
>On Mon, Oct 05, 2026 at 06:25:49PM +0200, Tommaso Califano wrote:
>>In QEMU’s USB MTP device, usb_mtp_write_metadata() computes
>>filename_chars = dlen - offsetof(ObjectInfo, filename) where
>>offsetof(ObjectInfo,filename) = 53. The dlen parameter is the size of the
>>last USB packet, not the total ObjectInfo size. If the final packet has
>>fewer than 53 bytes, filename_chars underflows to SIZE_MAX, causing
>>utf16_to_str() to read far beyond the heap buffer, leaking QEMU host
>>process memory.
>>
>>Replace dlen with d->length to avoid this issue, given that the latter
>>represents the total size of the buffer allocated for the unsegmented object.
>>
>>Fixes: 90c1a74271ce ("usb-mtp: Limit filename to object information size")
>>Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3992
>>Signed-off-by: Tommaso Califano <tcalifano@redhat.com>
>>---
>>
>>v2:
>>- Remove the dlen guard and variable in favor of using d->length.
>>
>>hw/usb/dev-mtp.c | 6 +++---
>>1 file changed, 3 insertions(+), 3 deletions(-)
>>
>>diff --git a/hw/usb/dev-mtp.c b/hw/usb/dev-mtp.c
>>index 1d8cfd32dc..53b5904a5c 100644
>>--- a/hw/usb/dev-mtp.c
>>+++ b/hw/usb/dev-mtp.c
>>@@ -1696,7 +1696,7 @@ close:
>>    s->write_pending = false;
>>}
>>
>>-static void usb_mtp_write_metadata(MTPState *s, uint64_t dlen)
>>+static void usb_mtp_write_metadata(MTPState *s)
>>{
>>    MTPData *d = s->data_out;
>>    ObjectInfo *dataset = (ObjectInfo *)d->data;
>>@@ -1704,7 +1704,7 @@ static void usb_mtp_write_metadata(MTPState *s, uint64_t dlen)
>>    MTPObject *o;
>>    MTPObject *p = usb_mtp_object_lookup(s, s->dataset.parent_handle);
>>    uint32_t next_handle = s->next_handle;
>>-    size_t filename_chars = dlen - offsetof(ObjectInfo, filename);
>>+    size_t filename_chars = d->length - offsetof(ObjectInfo, filename);
>>
>>    /*
>>     * filename is utf-16. We're intentionally doing
>>@@ -1801,7 +1801,7 @@ static void usb_mtp_get_data(MTPState *s, mtp_container *container,
>>        if (d->data_offset == d->length) {
>>            /* The operation might have already failed */
>>            if (!s->result) {
>>-                usb_mtp_write_metadata(s, dlen);
>>+                usb_mtp_write_metadata(s);
>>            }
>>            usb_mtp_data_free(s->data_out);
>>            s->data_out = NULL;
>>-- 
>>2.55.0
>>
>
>IIUC, `d->length` is controlled by the guest, so I think we still need
>the check that was present in the v1.
>
>Thanks,
>Luigi
>

Yes, you are right. I missed it. I'll re-add the check from v1.

I also noticed that with the guard in place it is still possible to craft a write where 
d->length == offsetof(ObjectInfo, filename): filename_chars is then computed as 0, so
utf16_to_str() returns an empty string and the device creates a file with no name. This
behavior already existed before, so it is not introduced by the patch. Since I think
that zero-length filename should never be valid, I'm inclined to make the check strict
and reject any ObjectInfo that does not "carry" a file name.