i2c: mt7621: fix copying uninitialized kernel stack with reduced page_len
PATCH RESEND 1/1 i2c: mt7621: fix copying uninitialized kernel stack with reduced page_len
When a page_len is less than 5 bytes in mtk_i2c_xfer, uninitialized kernel stack data is copied and written to REG_SM0D1_REG. Equally on the read part, an unnecessary read is done.
Guard these extra copy/write and read for the page_len to fix leaking uninitialized stack memory.
--- drivers/i2c/busses/i2c-mt7621.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/i2c/busses/i2c-mt7621.c +6 -3
@@ -144,7 +144,6 @@ static int mtk_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,u16 addr;int i, j, ret, len, page_len;u32 cmd; -u32 data[2 ]; i2c =i2c_get_adapdata (adap);
@@ -186,6 +185,8 @@ static int mtk_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,/* transfer data */ for (len = pmsg->len, j =0 ; len >0 ; len -=8 , j +=8 ) { +u32 data[2 ] = { }; + page_len = (len >=8 ) ?8 : len;if (pmsg->flags &I2C_M_RD ) {
@@ -194,7 +195,8 @@ static int mtk_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, }else {memcpy (data, &pmsg->buf[j], page_len);iowrite32 (data[0 ], i2c->base +REG_SM0D0_REG ); -iowrite32 (data[1 ], i2c->base +REG_SM0D1_REG ); +if (page_len >4 ) +iowrite32 (data[1 ], i2c->base +REG_SM0D1_REG ); cmd =SM0CTL1_WRITE ; }
@@ -204,7 +206,8 @@ static int mtk_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,if (pmsg->flags &I2C_M_RD ) { data[0 ] =ioread32 (i2c->base +REG_SM0D0_REG ); - data[1 ] =ioread32 (i2c->base +REG_SM0D1_REG ); +if (page_len >4 ) + data[1 ] =ioread32 (i2c->base +REG_SM0D1_REG );memcpy (&pmsg->buf[j], data, page_len); }else {if (!(pmsg->flags &I2C_M_IGNORE_NAK )) {
Signature · 1 line
2.55.0
Hi Christian,
Quoted 9 lines · Christian Marangi
When a
page_lenis less than 5 bytes inmtk_i2c_xfer, uninitialized kernel stack data is copied and written toREG_SM0D1_REG. Equally on the read part, an unnecessary read is done.Guard these extra copy/write and read for the
page_lento fix leaking uninitialized stack memory.Fixes d04913ec5f89 ("i2c: mt7621: Add MediaTek MT7621/7628/7688 I2C driver")Signed-off-by Christian Marangi <[email protected]>
pushed to i2c/i2c.
Thanks,
Andi
Andi Shyti
maintainerQuoted 10 lines · Christian Marangi
When a
page_lenis less than 5 bytes inmtk_i2c_xfer, uninitialized kernel stack data is copied and written toREG_SM0D1_REG. Equally on the read part, an unnecessary read is done.Guard these extra copy/write and read for the
page_lento fix leaking uninitialized stack memory.Fixes d04913ec5f89 ("i2c: mt7621: Add MediaTek MT7621/7628/7688 I2C driver")Signed-off-by Christian Marangi <[email protected]>---
BTW, this is not a RESEND, this is a V2 because the patch is completely different from the supposed first send.
Next time, please version it properly and add the changelog.
Andi
Il giorno lun 26 ott 2026 alle ore 23:59 Andi Shyti <[email protected]> ha scritto:
Quoted 9 lines · Christian Marangi
Quoted 7 lines
When a
page_lenis less than 5 bytes inmtk_i2c_xfer, uninitialized kernel stack data is copied and written toREG_SM0D1_REG. Equally on the read part, an unnecessary read is done.Guard these extra copy/write and read for the
page_lento fix leaking uninitialized stack memory.
Quoted 9 lines
Quoted 3 lines
Fixes d04913ec5f89 ("i2c: mt7621: Add MediaTek MT7621/7628/7688 I2C driver")Signed-off-by Christian Marangi <[email protected]>---
BTW, this is not a RESEND, this is a V2 because the patch is completely different from the supposed first send.
Next time, please version it properly and add the changelog.
I used the RESEND tag for this as the first submission was completely wrong and didn't reflect the actual changes.
(file got mixed on saving and committing)
Had to be more clear and maybe just say ignore the previous patch? (a v2 implied that i made changes to that patch but there weren't)
Anyway corner case... sorry won't happen again :(
Quoted 35 lines · On Thu, 22 Oct 2026 21:38:51 +0200 Christian Marangi <[email protected]>…
When a
page_lenis less than 5 bytes inmtk_i2c_xfer, uninitialized kernel stack data is copied and written toREG_SM0D1_REG. Equally on the read part, an unnecessary read is done.Guard these extra copy/write and read for the
page_lento fix leaking uninitialized stack memory.Fixes d04913ec5f89 ("i2c: mt7621: Add MediaTek MT7621/7628/7688 I2C driver")Signed-off-by Christian Marangi <[email protected]>--- drivers/i2c/busses/i2c-mt7621.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)drivers/i2c/busses/i2c-mt7621.c +2
-1@@ -144,7 +144,6 @@ static int mtk_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,u16 addr;int i, j, ret, len, page_len;u32 cmd; -u32 data[2 ]; i2c =i2c_get_adapdata (adap);@@ -186,6 +185,8 @@ static int mtk_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,/* transfer data */ for (len = pmsg->len, j =0 ; len >0 ; len -=8 , j +=8 ) { +u32 data[2 ] = { }; + page_len = (len >=8 ) ?8 : len;if (pmsg->flags &I2C_M_RD ) {
drivers/i2c/busses/i2c-mt7621.c static int mtk_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, · line 199
Quoted 6 lines
drivers/i2c/busses/i2c-mt7621.c +2
-1@@ -194,7 +195,8 @@ static int mtk_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, }else {memcpy (data, &pmsg->buf[j], page_len);iowrite32 (data[0 ], i2c->base +REG_SM0D0_REG ); -iowrite32 (data[1 ], i2c->base +REG_SM0D1_REG ); +if (page_len >4 ) +iowrite32 (data[1 ], i2c->base +REG_SM0D1_REG );
1) Why is this using memcpy() rather then just get_unaligned_u32(). (Yes, short transfers get tricky...)
2) Have you checked whether the hardware requires both registers be read/written for correct operation.
David
Quoted 13 lines
drivers/i2c/busses/i2c-mt7621.c +2
-1cmd =SM0CTL1_WRITE ; }@@ -204,7 +206,8 @@ static int mtk_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,if (pmsg->flags &I2C_M_RD ) { data[0 ] =ioread32 (i2c->base +REG_SM0D0_REG ); - data[1 ] =ioread32 (i2c->base +REG_SM0D1_REG ); +if (page_len >4 ) + data[1 ] =ioread32 (i2c->base +REG_SM0D1_REG );memcpy (&pmsg->buf[j], data, page_len); }else {if (!(pmsg->flags &I2C_M_IGNORE_NAK )) {