* [PATCH v4] soc: aspeed: Add NULL check in aspeed_lpc_enable_snoop()
@ 2025-04-01 7:46 Henry Martin
2025-04-03 10:15 ` Andrew Jeffery
0 siblings, 1 reply; 2+ messages in thread
From: Henry Martin @ 2025-04-01 7:46 UTC (permalink / raw)
To: joel, andrew, gsomlo, heiko, arnd, herve.codina, andersson,
u.kleine-koenig
Cc: linux-arm-kernel, linux-aspeed, linux-kernel, Henry Martin
devm_kasprintf() returns NULL when memory allocation fails. Currently,
aspeed_lpc_enable_snoop() does not check for this case, which results in a
NULL pointer dereference.
Add NULL check after devm_kasprintf() to prevent this issue.
Fixes: 3772e5da4454 ("soc: aspeed: Aspeed LPC snoop output using misc chardev")
Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
---
V3 -> V4:
- Add proper cleanup when devm_kasprintf() fails
- Ensure kfifo and miscdevice get released on error
V2 -> V3:
- Simplify the array access and correct commit message.
V1 -> V2:
- Remove blank line between tags.
drivers/soc/aspeed/aspeed-lpc-snoop.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/drivers/soc/aspeed/aspeed-lpc-snoop.c b/drivers/soc/aspeed/aspeed-lpc-snoop.c
index 9ab5ba9cf1d6..3e3f178b1226 100644
--- a/drivers/soc/aspeed/aspeed-lpc-snoop.c
+++ b/drivers/soc/aspeed/aspeed-lpc-snoop.c
@@ -200,11 +200,15 @@ static int aspeed_lpc_enable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
lpc_snoop->chan[channel].miscdev.minor = MISC_DYNAMIC_MINOR;
lpc_snoop->chan[channel].miscdev.name =
devm_kasprintf(dev, GFP_KERNEL, "%s%d", DEVICE_NAME, channel);
+ if (!lpc_snoop->chan[channel].miscdev.name) {
+ rc = -ENOMEM;
+ goto err_free_fifo;
+ }
lpc_snoop->chan[channel].miscdev.fops = &snoop_fops;
lpc_snoop->chan[channel].miscdev.parent = dev;
rc = misc_register(&lpc_snoop->chan[channel].miscdev);
if (rc)
- return rc;
+ goto err_free_fifo;
/* Enable LPC snoop channel at requested port */
switch (channel) {
@@ -221,7 +225,8 @@ static int aspeed_lpc_enable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
hicrb_en = HICRB_ENSNP1D;
break;
default:
- return -EINVAL;
+ rc = -EINVAL;
+ goto err_misc_deregister;
}
regmap_update_bits(lpc_snoop->regmap, HICR5, hicr5_en, hicr5_en);
@@ -231,6 +236,12 @@ static int aspeed_lpc_enable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
regmap_update_bits(lpc_snoop->regmap, HICRB,
hicrb_en, hicrb_en);
+ return 0;
+
+err_misc_deregister:
+ misc_deregister(&lpc_snoop->chan[channel].miscdev);
+err_free_fifo:
+ kfifo_free(&lpc_snoop->chan[channel].fifo);
return rc;
}
--
2.34.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v4] soc: aspeed: Add NULL check in aspeed_lpc_enable_snoop()
2025-04-01 7:46 [PATCH v4] soc: aspeed: Add NULL check in aspeed_lpc_enable_snoop() Henry Martin
@ 2025-04-03 10:15 ` Andrew Jeffery
0 siblings, 0 replies; 2+ messages in thread
From: Andrew Jeffery @ 2025-04-03 10:15 UTC (permalink / raw)
To: joel, gsomlo, heiko, arnd, herve.codina, andersson,
u.kleine-koenig, Henry Martin
Cc: linux-arm-kernel, linux-aspeed, linux-kernel
On Tue, 01 Apr 2025 15:46:47 +0800, Henry Martin wrote:
> devm_kasprintf() returns NULL when memory allocation fails. Currently,
> aspeed_lpc_enable_snoop() does not check for this case, which results in a
> NULL pointer dereference.
>
> Add NULL check after devm_kasprintf() to prevent this issue.
>
>
> [...]
Thanks, I've applied this to be picked up through the BMC tree.
--
Andrew Jeffery <andrew@codeconstruct.com.au>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-04-03 10:15 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-01 7:46 [PATCH v4] soc: aspeed: Add NULL check in aspeed_lpc_enable_snoop() Henry Martin
2025-04-03 10:15 ` Andrew Jeffery
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox