Skip to contentSkip to conversation

pinctrl: th1520: Fix potential null pointer dereference on func

PATCH In review Colin Ian King 3 participants 4 messages Oct 16, 2024

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

PATCH 1/1 pinctrl: th1520: Fix potential null pointer dereference on func

The initialization of muxtype deferences pointer func before func is sanity checked with a null pointer check, hence we have a null pointer deference issue. Fix this by only deferencing func with the assignment to muxtype after func has been null pointer checked.

Fixes 1fc30cd92770 ("pinctrl: th1520: Factor out casts")
---
 drivers/pinctrl/pinctrl-th1520.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/pinctrl/pinctrl-th1520.c +2 -1
index 7474d8da32f9..e641bad6728c 100644
@@ -803,11 +803,12 @@ static int th1520_pinmux_set_mux(struct pinctrl_dev *pctldev,
 {
 	struct th1520_pinctrl *thp = pinctrl_dev_get_drvdata(pctldev);
 	const struct function_desc *func = pinmux_generic_get_function(pctldev, fsel);
-	enum th1520_muxtype muxtype = (uintptr_t)func->data;
+	enum th1520_muxtype muxtype;
 
 	if (!func)
 		return -EINVAL;
 
+	muxtype = (uintptr_t)func->data;
 	return th1520_pinmux_set(thp, thp->desc.pins[gsel].number,
 				 th1520_pad_muxdata(thp->desc.pins[gsel].drv_data),
 				 muxtype);
Signature · 1 line
2.39.5
Acked-by on v1 1/1
#1 lore raw
Quoted 32 lines · Colin Ian King

The initialization of muxtype deferences pointer func before func is sanity checked with a null pointer check, hence we have a null pointer deference issue. Fix this by only deferencing func with the assignment to muxtype after func has been null pointer checked.

Fixes 1fc30cd92770 ("pinctrl: th1520: Factor out casts")
---
 drivers/pinctrl/pinctrl-th1520.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/pinctrl/pinctrl-th1520.c +2 -1
index 7474d8da32f9..e641bad6728c 100644
@@ -803,11 +803,12 @@ static int th1520_pinmux_set_mux(struct pinctrl_dev *pctldev,
 {
 	struct th1520_pinctrl *thp = pinctrl_dev_get_drvdata(pctldev);
 	const struct function_desc *func = pinmux_generic_get_function(pctldev, fsel);
-	enum th1520_muxtype muxtype = (uintptr_t)func->data;
+	enum th1520_muxtype muxtype;
 
 	if (!func)
 		return -EINVAL;
 
+	muxtype = (uintptr_t)func->data;
 	return th1520_pinmux_set(thp, thp->desc.pins[gsel].number,
 				 th1520_pad_muxdata(thp->desc.pins[gsel].drv_data),
 				 muxtype);
Signature · 1 line
2.39.5

I've tested this on top of next-20241016 on the BeagleV Ahead and LicheePi 4A.

thanks,
drew

#2 lore raw
In reply to Colin Ian King
Quoted 7 lines · Colin Ian King

The initialization of muxtype deferences pointer func before func is sanity checked with a null pointer check, hence we have a null pointer deference issue. Fix this by only deferencing func with the assignment to muxtype after func has been null pointer checked.

Fixes 1fc30cd92770 ("pinctrl: th1520: Factor out casts")

Patch applied, added Reported-by Kees since he mailed about this too.

Yours,
Linus Walleij

#3 lore raw
Quoted 11 lines · Linus Walleij
Quoted 7 lines · Colin Ian King

The initialization of muxtype deferences pointer func before func is sanity checked with a null pointer check, hence we have a null pointer deference issue. Fix this by only deferencing func with the assignment to muxtype after func has been null pointer checked.

Fixes 1fc30cd92770 ("pinctrl: th1520: Factor out casts")

Patch applied, added Reported-by Kees since he mailed about this too.

...which was actually the "other Kees" (Bakker), not the one the mailer suggested.

Yours,
Linus Walleij