All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Gerd Hoffmann" <kraxel@redhat.com>,
	"Bandan Das" <bsd@redhat.com>, "Thomas Huth" <thuth@redhat.com>,
	"Greg Kurz" <groug@kaod.org>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	"Daniel P. Berrangé" <berrange@redhat.com>
Subject: [Qemu-devel] [PATCH 3/3] usb-mtp: fix alignment of access of ObjectInfo filename field
Date: Mon, 15 Apr 2019 16:45:03 +0100	[thread overview]
Message-ID: <20190415154503.6758-4-berrange@redhat.com> (raw)
In-Reply-To: <20190415154503.6758-1-berrange@redhat.com>

The ObjectInfo struct's "filename" field is following a uint8_t
field in a packed struct and thus has bad alignment for a 16-bit
field. Switch the field to to uint8_t and use the helper function
for accessing unaligned 16-bit data.

Note that although the MTP spec specifies big endian, when transported
over the USB protocol, data is little endian.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 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 6b7d1296e4..963449ec7d 100644
--- a/hw/usb/dev-mtp.c
+++ b/hw/usb/dev-mtp.c
@@ -226,7 +226,7 @@ typedef struct {
     uint32_t assoc_desc;
     uint32_t seq_no; /*unused*/
     uint8_t length; /*part of filename field*/
-    uint16_t filename[0];
+    uint8_t filename[0]; /* UTF-16 encoded */
     char date_created[0]; /*unused*/
     char date_modified[0]; /*unused*/
     char keywords[0]; /*unused*/
@@ -1551,7 +1551,7 @@ static void usb_mtp_cancel_packet(USBDevice *dev, USBPacket *p)
     fprintf(stderr, "%s\n", __func__);
 }
 
-static char *utf16_to_str(uint8_t len, uint16_t *arr)
+static char *utf16_to_str(uint8_t len, uint8_t *str16)
 {
     wchar_t *wstr = g_new0(wchar_t, len + 1);
     int count, dlen;
@@ -1559,7 +1559,7 @@ static char *utf16_to_str(uint8_t len, uint16_t *arr)
 
     for (count = 0; count < len; count++) {
         /* FIXME: not working for surrogate pairs */
-        wstr[count] = (wchar_t)arr[count];
+        wstr[count] = lduw_le_p(str16 + (count * 2));
     }
     wstr[count] = 0;
 
-- 
2.20.1

WARNING: multiple messages have this Message-ID (diff)
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
	Thomas Huth <thuth@redhat.com>, Greg Kurz <groug@kaod.org>,
	Bandan Das <bsd@redhat.com>, Gerd Hoffmann <kraxel@redhat.com>
Subject: [Qemu-devel] [PATCH 3/3] usb-mtp: fix alignment of access of ObjectInfo filename field
Date: Mon, 15 Apr 2019 16:45:03 +0100	[thread overview]
Message-ID: <20190415154503.6758-4-berrange@redhat.com> (raw)
Message-ID: <20190415154503.vRByk2j7_dFAH63lFAUshdSUtpqqxZOsi6hUTA-EUjs@z> (raw)
In-Reply-To: <20190415154503.6758-1-berrange@redhat.com>

The ObjectInfo struct's "filename" field is following a uint8_t
field in a packed struct and thus has bad alignment for a 16-bit
field. Switch the field to to uint8_t and use the helper function
for accessing unaligned 16-bit data.

Note that although the MTP spec specifies big endian, when transported
over the USB protocol, data is little endian.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 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 6b7d1296e4..963449ec7d 100644
--- a/hw/usb/dev-mtp.c
+++ b/hw/usb/dev-mtp.c
@@ -226,7 +226,7 @@ typedef struct {
     uint32_t assoc_desc;
     uint32_t seq_no; /*unused*/
     uint8_t length; /*part of filename field*/
-    uint16_t filename[0];
+    uint8_t filename[0]; /* UTF-16 encoded */
     char date_created[0]; /*unused*/
     char date_modified[0]; /*unused*/
     char keywords[0]; /*unused*/
@@ -1551,7 +1551,7 @@ static void usb_mtp_cancel_packet(USBDevice *dev, USBPacket *p)
     fprintf(stderr, "%s\n", __func__);
 }
 
-static char *utf16_to_str(uint8_t len, uint16_t *arr)
+static char *utf16_to_str(uint8_t len, uint8_t *str16)
 {
     wchar_t *wstr = g_new0(wchar_t, len + 1);
     int count, dlen;
@@ -1559,7 +1559,7 @@ static char *utf16_to_str(uint8_t len, uint16_t *arr)
 
     for (count = 0; count < len; count++) {
         /* FIXME: not working for surrogate pairs */
-        wstr[count] = (wchar_t)arr[count];
+        wstr[count] = lduw_le_p(str16 + (count * 2));
     }
     wstr[count] = 0;
 
-- 
2.20.1



  parent reply	other threads:[~2019-04-15 15:45 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-15 15:45 [Qemu-devel] [PATCH 0/3] usb-mtp: fix ObjectInfo request handling Daniel P. Berrangé
2019-04-15 15:45 ` Daniel P. Berrangé
2019-04-15 15:45 ` [Qemu-devel] [PATCH 1/3] usb-mtp: fix string length for filename when writing metadata Daniel P. Berrangé
2019-04-15 15:45   ` Daniel P. Berrangé
2019-04-15 17:02   ` Bandan Das
2019-04-15 17:02     ` Bandan Das
2019-04-15 15:45 ` [Qemu-devel] [PATCH 2/3] usb-mtp: fix bounds check for guest provided filename Daniel P. Berrangé
2019-04-15 15:45   ` Daniel P. Berrangé
2019-04-16 19:41   ` Bandan Das
2019-04-16 19:41     ` Bandan Das
2019-04-15 15:45 ` Daniel P. Berrangé [this message]
2019-04-15 15:45   ` [Qemu-devel] [PATCH 3/3] usb-mtp: fix alignment of access of ObjectInfo filename field Daniel P. Berrangé
2019-04-15 16:52 ` [Qemu-devel] [PATCH 0/3] usb-mtp: fix ObjectInfo request handling Bandan Das
2019-04-15 16:52   ` Bandan Das
2019-04-15 16:54   ` Daniel P. Berrangé
2019-04-15 16:54     ` Daniel P. Berrangé
2019-04-16  8:40     ` Daniel P. Berrangé
2019-04-16  8:40       ` Daniel P. Berrangé
2019-04-16 16:10       ` Bandan Das
2019-04-16 16:10         ` Bandan Das
2019-04-16 16:12         ` Daniel P. Berrangé
2019-04-16 16:12           ` Daniel P. Berrangé
2019-04-16 16:45           ` Bandan Das
2019-04-16 16:45             ` Bandan Das
2019-04-16 16:52             ` Daniel P. Berrangé
2019-04-16 16:52               ` Daniel P. Berrangé
2019-04-16 17:20               ` Bandan Das
2019-04-16 17:20                 ` Bandan Das
2019-04-15 17:09 ` [Qemu-devel] [PATCH for-4.0? " Eric Blake
2019-04-15 17:09   ` Eric Blake
2019-04-15 17:18   ` Peter Maydell
2019-04-15 17:18     ` Peter Maydell
2019-04-16  8:48     ` Daniel P. Berrangé
2019-04-16  8:48       ` Daniel P. Berrangé
2019-04-16 13:35 ` [Qemu-devel] [PATCH " Peter Maydell
2019-04-16 13:35   ` Peter Maydell
2019-04-16 17:27   ` Peter Maydell
2019-04-16 17:27     ` Peter Maydell
2019-04-16 19:33     ` Peter Maydell
2019-04-16 19:33       ` Peter Maydell
2019-04-16 22:27       ` Peter Maydell
2019-04-16 22:27         ` Peter Maydell
2019-04-17  8:27         ` Gerd Hoffmann
2019-04-17  8:27           ` Gerd Hoffmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190415154503.6758-4-berrange@redhat.com \
    --to=berrange@redhat.com \
    --cc=bsd@redhat.com \
    --cc=groug@kaod.org \
    --cc=kraxel@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.