Skip to content

Commit b106776

Browse files
matnymanwesteri
authored andcommitted
thunderbolt: dma_port: 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: 3e13676 ("thunderbolt: Add support for DMA configuration based mailbox") Cc: [email protected] Signed-off-by: Mathias Nyman <[email protected]> Signed-off-by: Mika Westerberg <[email protected]>
1 parent d07f6ca commit b106776

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

drivers/thunderbolt/dma_port.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,15 @@ int dma_port_flash_read(struct tb_dma_port *dma, unsigned int address,
366366
void *buf, size_t size)
367367
{
368368
unsigned int retries = DMA_PORT_RETRIES;
369-
unsigned int offset;
370-
371-
offset = address & 3;
372-
address = address & ~3;
373369

374370
do {
375-
u32 nbytes = min_t(u32, size, MAIL_DATA_DWORDS * 4);
371+
unsigned int offset;
372+
size_t nbytes;
376373
int ret;
377374

375+
offset = address & 3;
376+
nbytes = min_t(size_t, size + offset, MAIL_DATA_DWORDS * 4);
377+
378378
ret = dma_port_flash_read_block(dma, address, dma->buf,
379379
ALIGN(nbytes, 4));
380380
if (ret) {
@@ -386,6 +386,7 @@ int dma_port_flash_read(struct tb_dma_port *dma, unsigned int address,
386386
return ret;
387387
}
388388

389+
nbytes -= offset;
389390
memcpy(buf, dma->buf + offset, nbytes);
390391

391392
size -= nbytes;

0 commit comments

Comments
 (0)