Skip to contentSkip to conversation

nfc: Fix potential memory leak of skb

PATCH Applied Shang XiaoJing 2 participants 6 messages Oct 27, 2022 – Oct 31, 2022

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

cover nfc: Fix potential memory leak of skb

There are 6 kinds of send functions can be called by nci_send_frame():

	virtual_nci_send(),
	fdp_nci_send(),
	nxp_nci_send(),
	s3fwrn5_nci_send(),
	nfcmrvl_nci_send(),
	st_nci_send();

1. virtual_nci_send() will memleak the skb, and has been fixed before.

2. fdp_nci_send() won't free the skb no matter whether write() succeed.

3-4. nxp_nci_send() and s3fwrn5_nci_send() will only free the skb when write() failed, however write() will not free the skb by itself for when succeeds.

5. nfcmrvl_nci_send() will call nfcmrvl_XXX_nci_send(), where some of them will free the skb, but nfcmrvl_i2c_nci_send() only free the skb when i2c_master_send() return >=0, and memleak will happen when i2c_master_send() failed in nfcmrvl_i2c_nci_send().

6. st_nci_send() will queue the skb into other list and finally be freed.

Fix the potential memory leak of skb.

Shang XiaoJing (4):
nfc: fdp: Fix potential memory leak in fdp_nci_send()
nfc: nxp-nci: Fix potential memory leak in nxp_nci_send()
nfc: s3fwrn5: Fix potential memory leak in s3fwrn5_nci_send()
nfc: nfcmrvl: Fix potential memory leak in nfcmrvl_i2c_nci_send()

 drivers/nfc/fdp/fdp.c      | 10 +++++++++-
 drivers/nfc/nfcmrvl/i2c.c  |  7 ++++++-
 drivers/nfc/nxp-nci/core.c |  7 +++++--
 drivers/nfc/s3fwrn5/core.c |  8 ++++++--
 4 files changed, 26 insertions(+), 6 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/4 nfc: fdp: Fix potential memory leak in fdp_nci_send()

fdp_nci_send() will call fdp_nci_i2c_write that will not free skb in the function. As a result, when fdp_nci_i2c_write() finished, the skb will memleak. fdp_nci_send() should free skb after fdp_nci_i2c_write() finished.

Fixes a06347c04c13 ("NFC: Add Intel Fields Peak NFC solution driver")
---
 drivers/nfc/fdp/fdp.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/nfc/fdp/fdp.c +9 -1
index c6b3334f24c9..f12f903a9dd1 100644
@@ -249,11 +249,19 @@ static int fdp_nci_close(struct nci_dev *ndev)
 static int fdp_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
 {
 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
+	int ret;
 
 	if (atomic_dec_and_test(&info->data_pkt_counter))
 		info->data_pkt_counter_cb(ndev);
 
-	return info->phy_ops->write(info->phy, skb);
+	ret = info->phy_ops->write(info->phy, skb);
+	if (ret < 0) {
+		kfree_skb(skb);
+		return ret;
+	}
+
+	consume_skb(skb);
+	return 0;
 }
 
 static int fdp_nci_request_firmware(struct nci_dev *ndev)
Signature · 1 line
2.17.1
#2 lore raw

PATCH 2/4 nfc: nxp-nci: Fix potential memory leak in nxp_nci_send()

In reply to Shang XiaoJing

nxp_nci_send() will call nxp_nci_i2c_write(), and only free skb when nxp_nci_i2c_write() failed. However, even if the nxp_nci_i2c_write() run succeeds, the skb will not be freed in nxp_nci_i2c_write(). As the result, the skb will memleak. nxp_nci_send() should also free the skb when nxp_nci_i2c_write() succeeds.

Fixes dece45855a8b ("NFC: nxp-nci: Add support for NXP NCI chips")
---
 drivers/nfc/nxp-nci/core.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/nfc/nxp-nci/core.c +5 -2
index 7c93d484dc1b..580cb6ecffee 100644
@@ -80,10 +80,13 @@ static int nxp_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
 		return -EINVAL;
 
 	r = info->phy_ops->write(info->phy_id, skb);
-	if (r < 0)
+	if (r < 0) {
 		kfree_skb(skb);
+		return r;
+	}
 
-	return r;
+	consume_skb(skb);
+	return 0;
 }
 
 static int nxp_nci_rf_pll_unlocked_ntf(struct nci_dev *ndev,
Signature · 1 line
2.17.1
#3 lore raw

PATCH 3/4 nfc: s3fwrn5: Fix potential memory leak in s3fwrn5_nci_send()

In reply to Shang XiaoJing

s3fwrn5_nci_send() will call s3fwrn5_i2c_write() or s3fwrn82_uart_write(), and free the skb if write() failed. However, even if the write() run succeeds, the skb will not be freed in write(). As the result, the skb will memleak. s3fwrn5_nci_send() should also free the skb when write() succeeds.

Fixes c04c674fadeb ("nfc: s3fwrn5: Add driver for Samsung S3FWRN5 NFC Chip")
---
 drivers/nfc/s3fwrn5/core.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/nfc/s3fwrn5/core.c +6 -2
index 1c412007fabb..0270e05b68df 100644
@@ -110,11 +110,15 @@ static int s3fwrn5_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
 	}
 
 	ret = s3fwrn5_write(info, skb);
-	if (ret < 0)
+	if (ret < 0) {
 		kfree_skb(skb);
+		mutex_unlock(&info->mutex);
+		return ret;
+	}
 
+	consume_skb(skb);
 	mutex_unlock(&info->mutex);
-	return ret;
+	return 0;
 }
 
 static int s3fwrn5_nci_post_setup(struct nci_dev *ndev)
Signature · 1 line
2.17.1
#4 lore raw

PATCH 4/4 nfc: nfcmrvl: Fix potential memory leak in nfcmrvl_i2c_nci_send()

In reply to Shang XiaoJing

nfcmrvl_i2c_nci_send() will be called by nfcmrvl_nci_send(), and skb should be freed in nfcmrvl_i2c_nci_send(). However, nfcmrvl_nci_send() will only free skb when i2c_master_send() return >=0, which means skb will memleak when i2c_master_send() failed. Free skb no matter whether i2c_master_send() succeeds.

Fixes b5b3e23e4cac ("NFC: nfcmrvl: add i2c driver")
---
 drivers/nfc/nfcmrvl/i2c.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/nfc/nfcmrvl/i2c.c +6 -1
index acef0cfd76af..24436c9e54c9 100644
@@ -132,10 +132,15 @@ static int nfcmrvl_i2c_nci_send(struct nfcmrvl_private *priv,
 			ret = -EREMOTEIO;
 		} else
 			ret = 0;
+	}
+
+	if (ret) {
 		kfree_skb(skb);
+		return ret;
 	}
 
-	return ret;
+	consume_skb(skb);
+	return 0;
 }
 
 static void nfcmrvl_i2c_nci_update_config(struct nfcmrvl_private *priv,
Signature · 1 line
2.17.1
#5 lore raw
In reply to Shang XiaoJing
This series was applied to netdev/net.git (master) by David S. Miller <[email protected]>:

Hello:

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

Quoted 9 lines · On Thu, 27 Oct 2022 22:03:28 +0800 you wrote:

There are 6 kinds of send functions can be called by nci_send_frame():

	virtual_nci_send(),
	fdp_nci_send(),
	nxp_nci_send(),
	s3fwrn5_nci_send(),
	nfcmrvl_nci_send(),
	st_nci_send();

[...]

Here is the summary with links:
- [1/4] nfc: fdp: Fix potential memory leak in fdp_nci_send()

    https://git.kernel.org/netdev/net/c/8e4aae6b8ca7
  - [2/4] nfc: nxp-nci: Fix potential memory leak in nxp_nci_send()
    https://git.kernel.org/netdev/net/c/7bf1ed6aff0f
  - [3/4] nfc: s3fwrn5: Fix potential memory leak in s3fwrn5_nci_send()
    https://git.kernel.org/netdev/net/c/3a146b7e3099
  - [4/4] nfc: nfcmrvl: Fix potential memory leak in nfcmrvl_i2c_nci_send()
    https://git.kernel.org/netdev/net/c/93d904a734a7

You are awesome, thank you!

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