* [PATCH v3] drivers/misc: Add NULL check in aspeed_lpc_enable_snoop
@ 2025-04-01 3:39 Henry Martin
2025-04-01 7:09 ` Markus Elfring
2025-04-01 7:37 ` [PATCH v4?] " Markus Elfring
0 siblings, 2 replies; 5+ messages in thread
From: Henry Martin @ 2025-04-01 3:39 UTC (permalink / raw)
To: joel, andrew, u.kleine-koenig, andersson, arnd, herve.codina,
bsdhenrymartin
Cc: linux-arm-kernel, linux-aspeed, linux-kernel
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 ("drivers/misc: Aspeed LPC snoop output using misc chardev")
Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
---
V2 -> V3: Simplify the arrary access and correct commit message.
V1 -> V2: Removed blank line between tags.
drivers/soc/aspeed/aspeed-lpc-snoop.c | 35 ++++++++++++++++++---------
1 file changed, 24 insertions(+), 11 deletions(-)
diff --git a/drivers/soc/aspeed/aspeed-lpc-snoop.c b/drivers/soc/aspeed/aspeed-lpc-snoop.c
index 9ab5ba9cf1d6..25ebecd14103 100644
--- a/drivers/soc/aspeed/aspeed-lpc-snoop.c
+++ b/drivers/soc/aspeed/aspeed-lpc-snoop.c
@@ -189,22 +189,28 @@ static int aspeed_lpc_enable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
u32 hicr5_en, snpwadr_mask, snpwadr_shift, hicrb_en;
const struct aspeed_lpc_snoop_model_data *model_data =
of_device_get_match_data(dev);
+ struct aspeed_lpc_snoop_channel *snoop_chan = &lpc_snoop->chan[channel];
+ struct miscdevice *mdev = &snoop_chan->miscdev;
+
+ init_waitqueue_head(&snoop_chan->wq);
- init_waitqueue_head(&lpc_snoop->chan[channel].wq);
/* Create FIFO datastructure */
- rc = kfifo_alloc(&lpc_snoop->chan[channel].fifo,
- SNOOP_FIFO_SIZE, GFP_KERNEL);
+ rc = kfifo_alloc(&snoop_chan->fifo, SNOOP_FIFO_SIZE, GFP_KERNEL);
if (rc)
return rc;
- 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);
- lpc_snoop->chan[channel].miscdev.fops = &snoop_fops;
- lpc_snoop->chan[channel].miscdev.parent = dev;
- rc = misc_register(&lpc_snoop->chan[channel].miscdev);
+ mdev->minor = MISC_DYNAMIC_MINOR;
+ mdev->name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", DEVICE_NAME, channel);
+ if (!mdev->name) {
+ rc = -ENOMEM;
+ goto err_free_fifo;
+ }
+
+ mdev->fops = &snoop_fops;
+ mdev->parent = dev;
+ rc = misc_register(mdev);
if (rc)
- return rc;
+ goto err_free_fifo;
/* Enable LPC snoop channel at requested port */
switch (channel) {
@@ -221,7 +227,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);
@@ -232,6 +239,12 @@ static int aspeed_lpc_enable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
hicrb_en, hicrb_en);
return rc;
+
+err_misc_deregister:
+ misc_deregister(mdev);
+err_free_fifo:
+ kfifo_free(&snoop_chan->fifo);
+ return rc;
}
static void aspeed_lpc_disable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v3] drivers/misc: Add NULL check in aspeed_lpc_enable_snoop
2025-04-01 3:39 [PATCH v3] drivers/misc: Add NULL check in aspeed_lpc_enable_snoop Henry Martin
@ 2025-04-01 7:09 ` Markus Elfring
2025-04-01 7:37 ` [PATCH v4?] " Markus Elfring
1 sibling, 0 replies; 5+ messages in thread
From: Markus Elfring @ 2025-04-01 7:09 UTC (permalink / raw)
To: Henry Martin, linux-aspeed, linux-arm-kernel
Cc: LKML, Andrew Jeffery, Arnd Bergmann, Bjorn Andersson,
Herve Codina, Joel Stanley, Uwe Kleine-König
> devm_kasprintf() return NULL if memory allocation fails. Currently,
…
call? failed?
> Add NULL check after devm_kasprintf() to prevent this issue.
Do you propose to improve this function implementation a bit more?
…
> ---
> V2 -> V3: Simplify the arrary access and correct commit message.
…
* Would you like to avoid a typo here?
* I imagine that there is a need to offer such adjustments
in separate update steps.
https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.14#n81
* Please choose a more appropriate subsystem specification.
https://web.git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/log/drivers/soc/aspeed/aspeed-lpc-snoop.c?h=next-20250331
* How do you think about to append parentheses to a function name
also in the summary phrase?
Regards,
Markus
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v4?] drivers/misc: Add NULL check in aspeed_lpc_enable_snoop
2025-04-01 3:39 [PATCH v3] drivers/misc: Add NULL check in aspeed_lpc_enable_snoop Henry Martin
2025-04-01 7:09 ` Markus Elfring
@ 2025-04-01 7:37 ` Markus Elfring
1 sibling, 0 replies; 5+ messages in thread
From: Markus Elfring @ 2025-04-01 7:37 UTC (permalink / raw)
To: Henry Martin, linux-aspeed, linux-arm-kernel
Cc: LKML, Andrew Jeffery, Arnd Bergmann, Bjorn Andersson,
Herve Codina, Joel Stanley, Uwe Kleine-König
…
> ---
> V2 -> V3: Simplify …
Is there a need to reconsider patch version numbers a bit more?
Regards,
Markus
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v3] drivers/misc: Add NULL check in aspeed_lpc_enable_snoop
@ 2025-03-31 15:40 Henry Martin
2025-03-31 16:15 ` Markus Elfring
0 siblings, 1 reply; 5+ messages in thread
From: Henry Martin @ 2025-03-31 15:40 UTC (permalink / raw)
To: joel
Cc: andrew, bsdhenrymartin, gsomlo, arnd, u.kleine-koenig,
linux-arm-kernel, linux-aspeed, linux-kernel
devm_kasprintf() returns NULL if memory allocation fails. Currently,
aspeed_lpc_enable_snoop() does not check for this case, leading to a NULL
pointer dereference.
The corrected code adds error checking and optimizes resource release logic
to ensure no memory or kernel resources are leaked in case of failure.
Fixes: 3772e5da4454 ("drivers/misc: Aspeed LPC snoop output using misc chardev")
Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
---
V2 -> V3: The enhanced code introduces proper error handling and
improves resource cleanup mechanisms to prevent memory or kernel
resource leaks during failure scenarios.
V1 -> V2: Removed blank line between tags.
drivers/soc/aspeed/aspeed-lpc-snoop.c | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/drivers/soc/aspeed/aspeed-lpc-snoop.c b/drivers/soc/aspeed/aspeed-lpc-snoop.c
index 9ab5ba9cf1d6..b79365a34baa 100644
--- a/drivers/soc/aspeed/aspeed-lpc-snoop.c
+++ b/drivers/soc/aspeed/aspeed-lpc-snoop.c
@@ -200,11 +200,16 @@ 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 +226,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);
@@ -232,6 +238,12 @@ static int aspeed_lpc_enable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
hicrb_en, hicrb_en);
return rc;
+
+err_misc_deregister:
+ misc_deregister(&lpc_snoop->chan[channel].miscdev);
+err_free_fifo:
+ kfifo_free(&lpc_snoop->chan[channel].fifo);
+ return rc;
}
static void aspeed_lpc_disable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v3] drivers/misc: Add NULL check in aspeed_lpc_enable_snoop
2025-03-31 15:40 [PATCH v3] " Henry Martin
@ 2025-03-31 16:15 ` Markus Elfring
0 siblings, 0 replies; 5+ messages in thread
From: Markus Elfring @ 2025-03-31 16:15 UTC (permalink / raw)
To: Henry Martin, linux-aspeed, linux-arm-kernel
Cc: LKML, Andrew Jeffery, Arnd Bergmann, Gabriel Somlo, Herve Codina,
Joel Stanley, Uwe Kleine-König
> devm_kasprintf() returns NULL if memory allocation fails. Currently,
…
call? failed?
> The corrected code adds error checking and optimizes resource release logic
…
See also once more:
https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.14#n94
An other subsystem specification might be more desirable.
https://web.git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/log/drivers/soc/aspeed/aspeed-lpc-snoop.c?h=next-20250331
* May the array access be simplified another bit here?
* How do you think about to store a pointer to a corresponding data structure member
in an additional local variable?
Regards,
Markus
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-04-01 7:41 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-01 3:39 [PATCH v3] drivers/misc: Add NULL check in aspeed_lpc_enable_snoop Henry Martin
2025-04-01 7:09 ` Markus Elfring
2025-04-01 7:37 ` [PATCH v4?] " Markus Elfring
-- strict thread matches above, loose matches on Subject: below --
2025-03-31 15:40 [PATCH v3] " Henry Martin
2025-03-31 16:15 ` Markus Elfring
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox