nfc: fdp: bound the device-supplied read size in fdp_nci_i2c_read()
Subsystems (4)
PATCH 1/1 nfc: fdp: bound the device-supplied read size in fdp_nci_i2c_read()
fdp_nci_i2c_read() reads a "length packet" from the FDP I2C controller and computes the size of the next I2C transfer from two device-supplied bytes:
phy->next_read_size = (tmp[2] << 8) + tmp[3] + 3;
next_read_size is a u16 (up to 65535) and is never bounded. On the next loop iteration it is used directly as the length passed to
i2c_master_recv(client, tmp, len);
which reads into the fixed 261-byte stack buffer tmp[FDP_NCI_I2C_MAX_PAYLOAD]. A malicious or malfunctioning controller that reports a large length thus overflows the stack buffer -- the r != len check runs only after the read has already happened.
Reject a next-read size larger than the buffer and resynchronize.
Found by 0sec (https://0sec.ai) using automated source analysis; the missing bound is evident from source. Compile-tested.
--- drivers/nfc/fdp/i2c.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/nfc/fdp/i2c.c +8 -1
@@ -128,7 +128,7 @@ static const struct nfc_phy_ops i2c_phy_ops = {static int fdp_nci_i2c_read (struct fdp_i2c_phy *phy,struct sk_buff **skb) { -int r, len; +int r = -EREMOTEIO , len;u8 tmp[FDP_NCI_I2C_MAX_PAYLOAD ], lrc, k;u16 i;struct i2c_client *client = phy->i2c_dev;
@@ -140,6 +140,13 @@ static int fdp_nci_i2c_read(struct fdp_i2c_phy *phy, struct sk_buff **skb) len = phy->next_read_size; +if (len >FDP_NCI_I2C_MAX_PAYLOAD ) { +dev_dbg (&client->dev,"%s: read size %d too large\n" , + __func__, len); + phy->next_read_size =FDP_NCI_I2C_MIN_PAYLOAD ; + goto flush; + } + r =i2c_master_recv (client, tmp, len);if (r != len) { dev_dbg(&client->dev, "%s: i2c recv err: %d\n",
Signature · 1 line
2.43.0
Simon Horman
reviewerQuoted 42 lines · Doruk Tan Ozturk
fdp_nci_i2c_read()reads a "length packet" from the FDP I2C controller and computes the size of the next I2C transfer from two device-supplied bytes:phy->next_read_size = (tmp[2] << 8) + tmp[3] + 3;
next_read_sizeis a u16 (up to 65535) and is never bounded. On the next loop iteration it is used directly as the length passed toi2c_master_recv(client, tmp, len);which reads into the fixed 261-byte stack buffer tmp[FDP_NCI_I2C_MAX_PAYLOAD]. A malicious or malfunctioning controller that reports a large length thus overflows the stack buffer -- the r != len check runs only after the read has already happened.
Reject a next-read size larger than the buffer and resynchronize.
Found by 0sec (https://0sec.ai) using automated source analysis; the missing bound is evident from source. Compile-tested.
Fixes a06347c04c13 ("NFC: Add Intel Fields Peak NFC solution driver")Assisted-by 0sec:claude-opus-4-8Signed-off-by Doruk Tan Ozturk <[email protected]>--- drivers/nfc/fdp/i2c.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)drivers/nfc/fdp/i2c.c +1
-1@@ -128,7 +128,7 @@ static const struct nfc_phy_ops i2c_phy_ops = {static int fdp_nci_i2c_read (struct fdp_i2c_phy *phy,struct sk_buff **skb) { -int r, len; +int r = -EREMOTEIO , len;u8 tmp[FDP_NCI_I2C_MAX_PAYLOAD ], lrc, k;u16 i;struct i2c_client *client = phy->i2c_dev;
drivers/nfc/fdp/i2c.c static int fdp_nci_i2c_read(struct fdp_i2c_phy *phy, struct sk_buff **skb) · line 146
Quoted 7 lines
drivers/nfc/fdp/i2c.c +4
-0@@ -140,6 +140,13 @@ static int fdp_nci_i2c_read(struct fdp_i2c_phy *phy, struct sk_buff **skb) len = phy->next_read_size; +if (len >FDP_NCI_I2C_MAX_PAYLOAD ) { +dev_dbg (&client->dev,"%s: read size %d too large\n" , + __func__, len); + phy->next_read_size =FDP_NCI_I2C_MIN_PAYLOAD ;
I think it would be more robust to explicitly set r here. Because it is assigned a little later in the loop, overriding the default assignment made by the first hunk of this patch.
Quoted 9 lines
drivers/nfc/fdp/i2c.c +3
-0+ goto flush; + } + r =i2c_master_recv (client, tmp, len);if (r != len) { dev_dbg(&client->dev, "%s: i2c recv err: %d\n",Signature · 1 line
2.43.0
David Heidelberg
maintainerQuoted 46 lines · Simon Horman
Quoted 45 lines · Doruk Tan Ozturk
fdp_nci_i2c_read()reads a "length packet" from the FDP I2C controller and computes the size of the next I2C transfer from two device-supplied bytes:phy->next_read_size = (tmp[2] << 8) + tmp[3] + 3;
next_read_sizeis a u16 (up to 65535) and is never bounded. On the next loop iteration it is used directly as the length passed toi2c_master_recv(client, tmp, len);which reads into the fixed 261-byte stack buffer tmp[FDP_NCI_I2C_MAX_PAYLOAD]. A malicious or malfunctioning controller that reports a large length thus overflows the stack buffer -- the r != len check runs only after the read has already happened.
Reject a next-read size larger than the buffer and resynchronize.
Found by 0sec (https://0sec.ai) using automated source analysis; the missing bound is evident from source. Compile-tested.
Fixes a06347c04c13 ("NFC: Add Intel Fields Peak NFC solution driver")Assisted-by 0sec:claude-opus-4-8Signed-off-by Doruk Tan Ozturk <[email protected]>--- drivers/nfc/fdp/i2c.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)drivers/nfc/fdp/i2c.c +1
-1@@ -128,7 +128,7 @@ static const struct nfc_phy_ops i2c_phy_ops = {static int fdp_nci_i2c_read (struct fdp_i2c_phy *phy,struct sk_buff **skb) { -int r, len; +int r = -EREMOTEIO , len;u8 tmp[FDP_NCI_I2C_MAX_PAYLOAD ], lrc, k;u16 i;struct i2c_client *client = phy->i2c_dev;@@ -140,6 +140,13 @@ static int fdp_nci_i2c_read(struct fdp_i2c_phy *phy, struct sk_buff **skb) len = phy->next_read_size;
Quoted 8 lines
drivers/nfc/fdp/i2c.c static int fdp_nci_i2c_read(struct fdp_i2c_phy *phy, struct sk_buff **skb) · line 146
Quoted 4 lines
drivers/nfc/fdp/i2c.c +4
-0+if (len >FDP_NCI_I2C_MAX_PAYLOAD ) { +dev_dbg (&client->dev,"%s: read size %d too large\n" , + __func__, len); + phy->next_read_size =FDP_NCI_I2C_MIN_PAYLOAD ;I think it would be more robust to explicitly set r here. Because it is assigned a little later in the loop, overriding the default assignment made by the first hunk of this patch.
I agree, I was looking at this patch yesterday and the current format feels unfavorable.
Please send next revision with Simon suggestion :)
Thank you both!
David
Quoted 10 lines
Quoted 10 lines
drivers/nfc/fdp/i2c.c +3
-0+ goto flush; + } + r =i2c_master_recv (client, tmp, len);if (r != len) { dev_dbg(&client->dev, "%s: i2c recv err: %d\n",Signature · 1 line
2.43.0
Signature · 1 line
David Heidelberg
Quoted 2 lines · David Heidelberg
I agree, I was looking at this patch yesterday and the current format feels unfavorable. Please send next revision with Simon suggestion :)
Will do. One heads-up before I resend: Bryam Vargas posted a v2 for the same bug back in June [1] that is more complete than mine. It also rejects len < FDP_NCI_I2C_MIN_PAYLOAD (the lower bound Simon flagged, reachable via the u16 truncation) and fixes an skb leak in the same loop, with a KASAN reproducer, and it carries Simon's Suggested-by. It looks like it just stalled.
If you would rather pick that one up, it covers more than mine does. Happy to send my (narrower) v2 with Simon's suggestion if you prefer it instead, just let me know.
-Doruk
Doruk,
That's generous of you. We landed on the same missing bound; your patch is a clean fix for the overflow itself. I'll repost my v2 as v3 rebased on current net so David has something current to pick up -- it keeps the lower-bound reject (len < FDP_NCI_I2C_MIN_PAYLOAD, the one Simon flagged) and the skb leak fix in the same loop.
I'll Cc you on the v3.
Thanks,
Bryam