Skip to content

Commit 22c7a18

Browse files
matnymanwesteri
authored andcommitted
thunderbolt: usb4: Fix NVM read buffer bounds and offset issue
Up to 64 bytes of data can be read from NVM in one go. Read address must be dword aligned. Data is read into a local buffer. If caller asks to read data starting at an unaligned address then full dword is anyway read from NVM into a local buffer. Data is then copied from the local buffer starting at the unaligned offset to the caller buffer. In cases where asked data length + unaligned offset is over 64 bytes we need to make sure we don't read past the 64 bytes in the local buffer when copying to caller buffer, and make sure that we don't skip copying unaligned offset bytes from local buffer anymore after the first round of 64 byte NVM data read. Fixes: b040798 ("thunderbolt: Add initial support for USB4") Cc: [email protected] Signed-off-by: Mathias Nyman <[email protected]> Signed-off-by: Mika Westerberg <[email protected]>
1 parent b106776 commit 22c7a18

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

drivers/thunderbolt/usb4.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ static int usb4_do_read_data(u16 address, void *buf, size_t size,
6868
unsigned int retries = USB4_DATA_RETRIES;
6969
unsigned int offset;
7070

71-
offset = address & 3;
72-
address = address & ~3;
73-
7471
do {
75-
size_t nbytes = min_t(size_t, size, USB4_DATA_DWORDS * 4);
7672
unsigned int dwaddress, dwords;
7773
u8 data[USB4_DATA_DWORDS * 4];
74+
size_t nbytes;
7875
int ret;
7976

77+
offset = address & 3;
78+
nbytes = min_t(size_t, size + offset, USB4_DATA_DWORDS * 4);
79+
8080
dwaddress = address / 4;
8181
dwords = ALIGN(nbytes, 4) / 4;
8282

@@ -87,6 +87,7 @@ static int usb4_do_read_data(u16 address, void *buf, size_t size,
8787
return ret;
8888
}
8989

90+
nbytes -= offset;
9091
memcpy(buf, data + offset, nbytes);
9192

9293
size -= nbytes;

0 commit comments

Comments
 (0)