From b18bdeac1c4d0ad0a137486cc6c42baa0a50b33c Mon Sep 17 00:00:00 2001 From: Zeng Heng Date: Thu, 27 Jun 2024 20:46:55 +0800 Subject: [PATCH 1/2] pinctrl: devicetree: fix null pointer dereferencing in pinctrl_dt_to_map stable inclusion from stable-v4.19.267 commit a988dcd3dd9e691c5ccc3324b209688f3b5453e9 category: bugfix bugzilla: https://gitee.com/openeuler/kernel/issues/IA8W1B Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=a988dcd3dd9e691c5ccc3324b209688f3b5453e9 -------------------------------------------------------- [ Upstream commit 91d5c5060ee24fe8da88cd585bb43b843d2f0dce ] Here is the BUG report by KASAN about null pointer dereference: BUG: KASAN: null-ptr-deref in strcmp+0x2e/0x50 Read of size 1 at addr 0000000000000000 by task python3/2640 Call Trace: strcmp __of_find_property of_find_property pinctrl_dt_to_map kasprintf() would return NULL pointer when kmalloc() fail to allocate. So directly return ENOMEM, if kasprintf() return NULL pointer. Fixes: 57291ce295c0 ("pinctrl: core device tree mapping table parsing support") Signed-off-by: Zeng Heng Link: https://lore.kernel.org/r/20221110082056.2014898-1-zengheng4@huawei.com Signed-off-by: Linus Walleij Signed-off-by: Sasha Levin Signed-off-by: Zhang Zekun --- drivers/pinctrl/devicetree.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/pinctrl/devicetree.c b/drivers/pinctrl/devicetree.c index 177ee1136e34..6f5acfcba57c 100644 --- a/drivers/pinctrl/devicetree.c +++ b/drivers/pinctrl/devicetree.c @@ -235,6 +235,8 @@ int pinctrl_dt_to_map(struct pinctrl *p, struct pinctrl_dev *pctldev) for (state = 0; ; state++) { /* Retrieve the pinctrl-* property */ propname = kasprintf(GFP_KERNEL, "pinctrl-%d", state); + if (!propname) + return -ENOMEM; prop = of_find_property(np, propname, &size); kfree(propname); if (!prop) { -- Gitee From 2fd89bb731383ea22c7ca9b8152da2909227676b Mon Sep 17 00:00:00 2001 From: Zeng Heng Date: Thu, 27 Jun 2024 20:46:56 +0800 Subject: [PATCH 2/2] pinctrl: devicetree: fix refcount leak in pinctrl_dt_to_map() stable inclusion from stable-v4.19.314 commit 06780473cb8a858d1d6cab2673e021b072a852d1 category: bugfix bugzilla: https://gitee.com/openeuler/kernel/issues/IA8B9H Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=06780473cb8a858d1d6cab2673e021b072a852d1 ---------------------------------------------------- [ Upstream commit a0cedbcc8852d6c77b00634b81e41f17f29d9404 ] If we fail to allocate propname buffer, we need to drop the reference count we just took. Because the pinctrl_dt_free_maps() includes the droping operation, here we call it directly. Fixes: 91d5c5060ee2 ("pinctrl: devicetree: fix null pointer dereferencing in pinctrl_dt_to_map") Suggested-by: Dan Carpenter Signed-off-by: Zeng Heng Reviewed-by: Dan Carpenter Message-ID: <20240415105328.3651441-1-zengheng4@huawei.com> Signed-off-by: Linus Walleij Signed-off-by: Sasha Levin Signed-off-by: Zhang Zekun --- drivers/pinctrl/devicetree.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/drivers/pinctrl/devicetree.c b/drivers/pinctrl/devicetree.c index 6f5acfcba57c..01cc09e2bccb 100644 --- a/drivers/pinctrl/devicetree.c +++ b/drivers/pinctrl/devicetree.c @@ -235,14 +235,16 @@ int pinctrl_dt_to_map(struct pinctrl *p, struct pinctrl_dev *pctldev) for (state = 0; ; state++) { /* Retrieve the pinctrl-* property */ propname = kasprintf(GFP_KERNEL, "pinctrl-%d", state); - if (!propname) - return -ENOMEM; + if (!propname) { + ret = -ENOMEM; + goto err; + } prop = of_find_property(np, propname, &size); kfree(propname); if (!prop) { if (state == 0) { - of_node_put(np); - return -ENODEV; + ret = -ENODEV; + goto err; } break; } -- Gitee