Skip to contentSkip to conversation

Fix possible NULL Pointer Dereference in 'extcon_rtk_type_c_probe'

PATCH In review Gax-c 3 participants 3 messages Sep 29, 2024 – Oct 1, 2024

Subsystems (2)
lore
Share & tools
lore

Gax-c

author
#0 lore raw

PATCH 1/1 Fix possible NULL Pointer Dereference in 'extcon_rtk_type_c_probe'

A 'devm_kzalloc' in 'extcon_rtk_type_c_probe' could possibly return null pointer. Add a null check for the returned pointer.

Fixes 8a590d7371f0 ("extcon: add Realtek DHC RTD SoC Type-C driver")
---
 drivers/extcon/extcon-rtk-type-c.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/extcon/extcon-rtk-type-c.c +6 -0
index a592bab77538..9a0824fc7cbe 100644
@@ -1371,6 +1371,12 @@ static int extcon_rtk_type_c_probe(struct platform_device *pdev)
 
 	type_c->type_c_cfg = devm_kzalloc(dev, sizeof(*type_c_cfg), GFP_KERNEL);
 
+	if (!type_c->type_c_cfg) {
+		dev_err(dev, "Failed to allocate memory for type_c_cfg!\n");
+		ret = -ENOMEM;
+		goto err;
+	}
+
 	memcpy(type_c->type_c_cfg, type_c_cfg, sizeof(*type_c_cfg));
 
 	if (setup_type_c_parameter(type_c)) {
Signature · 1 line
2.25.1
#1 lore raw
Quoted 2 lines

A 'devm_kzalloc' in 'extcon_rtk_type_c_probe' could possibly return null pointer. Add a null check for the returned pointer.

How do you think about a wording variant like the following?

A devm_kzalloc() call can fail in this function implementation. Thus add a null pointer check and corresponding exception handling for the data structure member “type_c_cfg”.

…

Quoted 2 lines

…

How good does such a tag combination fit together for the same person?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.11#n525

Can a subject like “[PATCH] extcon: realtek: Prevent null pointer dereference in extcon_rtk_type_c_probe()” be more appropriate?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.11#n613

…

drivers/extcon/extcon-rtk-type-c.c static int extcon_rtk_type_c_probe(struct platform_device *pdev) · line 1380

Quoted 12 lines

+++ b/drivers/extcon/extcon-rtk-type-c.c

drivers/extcon/extcon-rtk-type-c.c +6 -0
@@ -1371,6 +1371,12 @@ static int extcon_rtk_type_c_probe(struct platform_device *pdev)
 
 	type_c->type_c_cfg = devm_kzalloc(dev, sizeof(*type_c_cfg), GFP_KERNEL);
 
+	if (!type_c->type_c_cfg) {
+		dev_err(dev, "Failed to allocate memory for type_c_cfg!\n");
+		ret = -ENOMEM;
+		goto err;
+	}
+
 	memcpy(type_c->type_c_cfg, type_c_cfg, sizeof(*type_c_cfg));

…

I suggest to omit an extra error message here.

By the way:
Would you become interested to add a jump target for the statement “ret = -EINVAL;” (in two other if branches)?

Regards,
Markus

#2 lore raw
In reply to Gax-c
Quoted 8 lines · Gax-c

A 'devm_kzalloc' in 'extcon_rtk_type_c_probe' could possibly return null pointer. Add a null check for the returned pointer.

Fixes 8a590d7371f0 ("extcon: add Realtek DHC RTD SoC Type-C driver")

All previous comments apply.

drivers/extcon/extcon-rtk-type-c.c static int extcon_rtk_type_c_probe(struct platform_device *pdev) · line 1373

Quoted 12 lines
---
 drivers/extcon/extcon-rtk-type-c.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/extcon/extcon-rtk-type-c.c +0 -0
index a592bab77538..9a0824fc7cbe 100644
@@ -1371,6 +1371,12 @@ static int extcon_rtk_type_c_probe(struct platform_device *pdev)
 
 	type_c->type_c_cfg = devm_kzalloc(dev, sizeof(*type_c_cfg), GFP_KERNEL);
 

Same as in other patches - drop blank line.

drivers/extcon/extcon-rtk-type-c.c static int extcon_rtk_type_c_probe(struct platform_device *pdev) · line 1375

Quoted 2 lines
drivers/extcon/extcon-rtk-type-c.c +2 -0
+	if (!type_c->type_c_cfg) {
+		dev_err(dev, "Failed to allocate memory for type_c_cfg!\n");

No, drop.

drivers/extcon/extcon-rtk-type-c.c static int extcon_rtk_type_c_probe(struct platform_device *pdev) · line 1377

Quoted 2 lines
drivers/extcon/extcon-rtk-type-c.c +2 -0
+		ret = -ENOMEM;
+		goto err;

Best regards,
Krzysztof