Skip to contentSkip to conversation

nfc: pn533: Fix double free when pn533_fill_fragment_skbs() fails

PATCH Applied Chengfeng Ye 4 participants 4 messages Nov 5, 2021 – Nov 7, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 nfc: pn533: Fix double free when pn533_fill_fragment_skbs() fails

skb is already freed by dev_kfree_skb in pn533_fill_fragment_skbs, but follow error handler branch when pn533_fill_fragment_skbs() fails, skb is freed again, results in double free issue. Fix this by not free skb in error path of pn533_fill_fragment_skbs.

---
 drivers/nfc/pn533/pn533.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/nfc/pn533/pn533.c +3 -3
index 787bcbd290f7..a491db46e3bd 100644
@@ -2216,7 +2216,7 @@ static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb)
 		frag = pn533_alloc_skb(dev, frag_size);
 		if (!frag) {
 			skb_queue_purge(&dev->fragment_skb);
-			break;
+			return -ENOMEM;
 		}
 
 		if (!dev->tgt_mode) {
@@ -2285,7 +2285,7 @@ static int pn533_transceive(struct nfc_dev *nfc_dev,
 		/* jumbo frame ? */
 		if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
 			rc = pn533_fill_fragment_skbs(dev, skb);
-			if (rc <= 0)
+			if (rc < 0)
 				goto error;
 
 			skb = skb_dequeue(&dev->fragment_skb);
@@ -2353,7 +2353,7 @@ static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
 	/* let's split in multiple chunks if size's too big */
 	if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
 		rc = pn533_fill_fragment_skbs(dev, skb);
-		if (rc <= 0)
+		if (rc < 0)
 			goto error;
 
 		/* get the first skb */
Signature · 1 line
2.17.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · Chengfeng Ye

skb is already freed by dev_kfree_skb in pn533_fill_fragment_skbs, but follow error handler branch when pn533_fill_fragment_skbs() fails, skb is freed again, results in double free issue. Fix this by not free skb in error path of pn533_fill_fragment_skbs.

I sort of wish the commit message talked more about the how this changes the failure return from 0 to -ENOMEM. But the patch is good.

regards,
dan carpenter

Reviewed-by on v2 1/1
#2 lore raw
In reply to Chengfeng Ye
Quoted 9 lines · Chengfeng Ye

skb is already freed by dev_kfree_skb in pn533_fill_fragment_skbs, but follow error handler branch when pn533_fill_fragment_skbs() fails, skb is freed again, results in double free issue. Fix this by not free skb in error path of pn533_fill_fragment_skbs.

---
 drivers/nfc/pn533/pn533.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Looks good, thanks:

Please do not forget about fixes tag. Here it is trickier because pn533_fill_fragment_skbs() usage was introduced in two commits:

Fixes 963a82e07d4e ("NFC: pn533: Split large Tx frames in chunks")
Fixes 93ad42020c2d ("NFC: pn533: Target mode Tx fragmentation support")

Best regards,
Krzysztof

#3 lore raw
In reply to Chengfeng Ye
This patch was applied to netdev/net.git (master) by David S. Miller <[email protected]>:

Hello:

This patch was applied to netdev/net.git (master)
by David S. Miller <[email protected]>:

Quoted 8 lines · On Fri, 5 Nov 2021 06:36:36 -0700 you wrote:

skb is already freed by dev_kfree_skb in pn533_fill_fragment_skbs, but follow error handler branch when pn533_fill_fragment_skbs() fails, skb is freed again, results in double free issue. Fix this by not free skb in error path of pn533_fill_fragment_skbs.

[...]

Here is the summary with links:
- [v2] nfc: pn533: Fix double free when pn533_fill_fragment_skbs() fails

    https://git.kernel.org/netdev/net/c/9fec40f85065

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html