Skip to contentSkip to conversation

nfc: fdp: add null check of devm_kmalloc_array in fdp_nci_i2c_read_device_properties

PATCH Superseded Kang Chen 4 participants 9 messages Feb 26, 2023 – Feb 28, 2023

Subsystems (3)
lore
Share & tools
lore

Kang Chen

author
#0 lore raw

PATCH 1/1 nfc: fdp: add null check of devm_kmalloc_array in fdp_nci_i2c_read_device_properties

devm_kmalloc_array may fails, *fw_vsc_cfg might be null and cause out-of-bounds write in device_property_read_u8_array later.

---
 drivers/nfc/fdp/i2c.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/nfc/fdp/i2c.c +3 -0
index 2d53e0f88..d95d20efa 100644
@@ -247,6 +247,9 @@ static void fdp_nci_i2c_read_device_properties(struct device *dev,
 					   len, sizeof(**fw_vsc_cfg),
 					   GFP_KERNEL);
 
+		if (!*fw_vsc_cfg)
+			goto vsc_read_err;
+
 		r = device_property_read_u8_array(dev, FDP_DP_FW_VSC_CFG_NAME,
 						  *fw_vsc_cfg, len);
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 4 lines · Kang Chen

devm_kmalloc_array may fails, *fw_vsc_cfg might be null and cause out-of-bounds write in device_property_read_u8_array later.

I'm not sure if this is a bug-fix (for stable).
But if so, I think the following is the appropriate fixes tag.

Fixes a06347c04c13 ("NFC: Add Intel Fields Peak NFC solution driver")
Quoted 9 lines
---
 drivers/nfc/fdp/i2c.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/nfc/fdp/i2c.c +0 -0
index 2d53e0f88..d95d20efa 100644

drivers/nfc/fdp/i2c.c static void fdp_nci_i2c_read_device_properties(struct device *dev, · line 251

Quoted 5 lines
drivers/nfc/fdp/i2c.c +2 -0
@@ -247,6 +247,9 @@ static void fdp_nci_i2c_read_device_properties(struct device *dev,
 					   len, sizeof(**fw_vsc_cfg),
 					   GFP_KERNEL);
 
+		if (!*fw_vsc_cfg)
+			goto vsc_read_err;

This leads to:

	dev_dbg(dev, "FW vendor specific commands not present\n");

Which seems a little misleading for this error condition.

Quoted 7 lines
drivers/nfc/fdp/i2c.c +1 -0
+
 		r = device_property_read_u8_array(dev, FDP_DP_FW_VSC_CFG_NAME,
 						  *fw_vsc_cfg, len);
 
Signature · 1 line
2.34.1
author
#2 lore raw

Hi Simon,

I will update the patch later.
Thank you for your review.

Quoted 40 lines · Simon Horman
Quoted 4 lines · Kang Chen

devm_kmalloc_array may fails, *fw_vsc_cfg might be null and cause out-of-bounds write in device_property_read_u8_array later.

I'm not sure if this is a bug-fix (for stable).
But if so, I think the following is the appropriate fixes tag.

Fixes a06347c04c13 ("NFC: Add Intel Fields Peak NFC solution driver")
Quoted 9 lines
---
 drivers/nfc/fdp/i2c.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/nfc/fdp/i2c.c +0 -0
index 2d53e0f88..d95d20efa 100644

drivers/nfc/fdp/i2c.c static void fdp_nci_i2c_read_device_properties(struct device *dev, · line 251

Quoted 5 lines
drivers/nfc/fdp/i2c.c +2 -0
@@ -247,6 +247,9 @@ static void fdp_nci_i2c_read_device_properties(struct device *dev,
                                         len, sizeof(**fw_vsc_cfg),
                                         GFP_KERNEL);
 
+             if (!*fw_vsc_cfg)
+                     goto vsc_read_err;

This leads to:

        dev_dbg(dev, "FW vendor specific commands not present\n");

Which seems a little misleading for this error condition.

Quoted 7 lines
drivers/nfc/fdp/i2c.c +1 -1
+
              r = device_property_read_u8_array(dev, FDP_DP_FW_VSC_CFG_NAME,
                                                *fw_vsc_cfg, len);
 
--

2.34.1

author
#3 lore raw

PATCH v2 1/1 nfc: fdp: add null check of devm_kmalloc_array in fdp_nci_i2c_read_device_properties

In reply to Simon Horman

devm_kmalloc_array may fails, *fw_vsc_cfg might be null and cause out-of-bounds write in device_property_read_u8_array later.

Fixes a06347c04c13 ("NFC: Add Intel Fields Peak NFC solution driver")

---
v2 -> v1: add debug prompt and Fixes tag

 drivers/nfc/fdp/i2c.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/nfc/fdp/i2c.c +6 -1
index 2d53e0f88..d3272a54b 100644
@@ -247,6 +247,11 @@ static void fdp_nci_i2c_read_device_properties(struct device *dev,
 					   len, sizeof(**fw_vsc_cfg),
 					   GFP_KERNEL);
 
+		if (!*fw_vsc_cfg) {
+			dev_dbg(dev, "Not enough memory\n");
+			goto out;
+		}
+
 		r = device_property_read_u8_array(dev, FDP_DP_FW_VSC_CFG_NAME,
 						  *fw_vsc_cfg, len);
 
@@ -259,7 +264,7 @@ static void fdp_nci_i2c_read_device_properties(struct device *dev,
 		dev_dbg(dev, "FW vendor specific commands not present\n");
 		*fw_vsc_cfg = NULL;
 	}
-
+out:
 	dev_dbg(dev, "Clock type: %d, clock frequency: %d, VSC: %s",
 		*clock_type, *clock_freq, *fw_vsc_cfg != NULL ? "yes" : "no");
 }
Signature · 1 line
2.34.1
#4 lore raw
Quoted 5 lines · Kang Chen

devm_kmalloc_array may fails, *fw_vsc_cfg might be null and cause out-of-bounds write in device_property_read_u8_array later.

Fixes a06347c04c13 ("NFC: Add Intel Fields Peak NFC solution driver")

No blank lines between tags.

Quoted 12 lines

---
v2 -> v1: add debug prompt and Fixes tag

 drivers/nfc/fdp/i2c.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/nfc/fdp/i2c.c +0 -0
index 2d53e0f88..d3272a54b 100644

drivers/nfc/fdp/i2c.c static void fdp_nci_i2c_read_device_properties(struct device *dev, · line 251

Quoted 5 lines
drivers/nfc/fdp/i2c.c +2 -0
@@ -247,6 +247,11 @@ static void fdp_nci_i2c_read_device_properties(struct device *dev,
 					   len, sizeof(**fw_vsc_cfg),
 					   GFP_KERNEL);
 
+		if (!*fw_vsc_cfg) {
+			dev_dbg(dev, "Not enough memory\n");

No prints for memory allocation errors. Core prints it. Just go to err_kmalloc.

drivers/nfc/fdp/i2c.c static void fdp_nci_i2c_read_device_properties(struct device *dev, · line 262

Quoted 11 lines
drivers/nfc/fdp/i2c.c +3 -1
+			goto out;
+		}
+
 		r = device_property_read_u8_array(dev, FDP_DP_FW_VSC_CFG_NAME,
 						  *fw_vsc_cfg, len);
 
@@ -259,7 +264,7 @@ static void fdp_nci_i2c_read_device_properties(struct device *dev,
 		dev_dbg(dev, "FW vendor specific commands not present\n");
 		*fw_vsc_cfg = NULL;
 	}
-

Why? Line break seems nice here.

drivers/nfc/fdp/i2c.c static void fdp_nci_i2c_read_device_properties(struct device *dev, · line 270

Quoted 4 lines
drivers/nfc/fdp/i2c.c +1 -0
+out:
 	dev_dbg(dev, "Clock type: %d, clock frequency: %d, VSC: %s",
 		*clock_type, *clock_freq, *fw_vsc_cfg != NULL ? "yes" : "no");
 }

Best regards,
Krzysztof

author
#5 lore raw

PATCH v3 1/1 nfc: fdp: add null check of devm_kmalloc_array in fdp_nci_i2c_read_device_properties

From: Kang Chen <[email protected]>

devm_kmalloc_array may fails, *fw_vsc_cfg might be null and cause out-of-bounds write in device_property_read_u8_array later.

Fixes a06347c04c13 ("NFC: Add Intel Fields Peak NFC solution driver")

---
v3 -> v2: remove useless prompt and blank lines between tags. v2 -> v1: add debug prompt and Fixes tag

 drivers/nfc/fdp/i2c.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/nfc/fdp/i2c.c +4 -0
index 2d53e0f88..1e0f2297f 100644
@@ -247,6 +247,9 @@ static void fdp_nci_i2c_read_device_properties(struct device *dev,
 					   len, sizeof(**fw_vsc_cfg),
 					   GFP_KERNEL);
 
+		if (!*fw_vsc_cfg)
+			goto alloc_err;
+
 		r = device_property_read_u8_array(dev, FDP_DP_FW_VSC_CFG_NAME,
 						  *fw_vsc_cfg, len);
 
@@ -260,6 +263,7 @@ static void fdp_nci_i2c_read_device_properties(struct device *dev,
 		*fw_vsc_cfg = NULL;
 	}
 
+alloc_err:
 	dev_dbg(dev, "Clock type: %d, clock frequency: %d, VSC: %s",
 		*clock_type, *clock_freq, *fw_vsc_cfg != NULL ? "yes" : "no");
 }
Signature · 1 line
2.34.1
Reviewed-by on v3 1/1
#6 lore raw
Quoted 9 lines · On 27/02/2023 10:30, void0red wrote:

From: Kang Chen <[email protected]>

devm_kmalloc_array may fails, *fw_vsc_cfg might be null and cause out-of-bounds write in device_property_read_u8_array later.

Fixes a06347c04c13 ("NFC: Add Intel Fields Peak NFC solution driver")

---
v3 -> v2: remove useless prompt and blank lines between tags.

Best regards,
Krzysztof

Reviewed-by on v3 1/1
#7 lore raw
In reply to void0red
Quoted 9 lines · On Mon, Feb 27, 2023 at 05:30:37PM +0800, void0red wrote:

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

From: Kang Chen <[email protected]>

devm_kmalloc_array may fails, *fw_vsc_cfg might be null and cause out-of-bounds write in device_property_read_u8_array later.

Fixes a06347c04c13 ("NFC: Add Intel Fields Peak NFC solution driver")
#8 lore raw
In reply to void0red
This patch was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:

Quoted 9 lines · On Mon, 27 Feb 2023 17:30:37 +0800 you wrote:

From: Kang Chen <[email protected]>

devm_kmalloc_array may fails, *fw_vsc_cfg might be null and cause out-of-bounds write in device_property_read_u8_array later.

Fixes a06347c04c13 ("NFC: Add Intel Fields Peak NFC solution driver")

[...]

Here is the summary with links:
- [v3] nfc: fdp: add null check of devm_kmalloc_array in fdp_nci_i2c_read_device_properties

    https://git.kernel.org/netdev/net/c/11f180a5d62a

You are awesome, thank you!

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