All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] pstore/platform: Add check for kstrdup
@ 2023-06-15  2:53 Jiasheng Jiang
  2023-06-22 16:15 ` Kees Cook
  0 siblings, 1 reply; 2+ messages in thread
From: Jiasheng Jiang @ 2023-06-15  2:53 UTC (permalink / raw)
  To: keescook
  Cc: tony.luck, gpiccoli, linux-hardening, linux-kernel, Jiasheng Jiang

Add check for the return value of kstrdup() and return the error
if it fails in order to avoid NULL pointer dereference.

Fixes: 563ca40ddf40 ("pstore/platform: Switch pstore_info::name to const")
Signed-off-by: Jiasheng Jiang <jiasheng@iscas.ac.cn>
---
Changelog:

v1 -> v2:

1. Allocate a copy earlier.
---
 fs/pstore/platform.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/fs/pstore/platform.c b/fs/pstore/platform.c
index cbc0b468c1ab..727f8ce71062 100644
--- a/fs/pstore/platform.c
+++ b/fs/pstore/platform.c
@@ -573,6 +573,8 @@ static int pstore_write_user_compat(struct pstore_record *record,
  */
 int pstore_register(struct pstore_info *psi)
 {
+	char *new_backend;
+
 	if (backend && strcmp(backend, psi->name)) {
 		pr_warn("backend '%s' already in use: ignoring '%s'\n",
 			backend, psi->name);
@@ -593,6 +595,10 @@ int pstore_register(struct pstore_info *psi)
 		return -EINVAL;
 	}
 
+	new_backend = kstrdup(psi->name, GFP_KERNEL);
+	if (!new_backend)
+		return -ENOMEM;
+
 	mutex_lock(&psinfo_lock);
 	if (psinfo) {
 		pr_warn("backend '%s' already loaded: ignoring '%s'\n",
@@ -630,7 +636,7 @@ int pstore_register(struct pstore_info *psi)
 	 * Update the module parameter backend, so it is visible
 	 * through /sys/module/pstore/parameters/backend
 	 */
-	backend = kstrdup(psi->name, GFP_KERNEL);
+	backend = new_backend;
 
 	pr_info("Registered %s as persistent store backend\n", psi->name);
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] pstore/platform: Add check for kstrdup
  2023-06-15  2:53 [PATCH v2] pstore/platform: Add check for kstrdup Jiasheng Jiang
@ 2023-06-22 16:15 ` Kees Cook
  0 siblings, 0 replies; 2+ messages in thread
From: Kees Cook @ 2023-06-22 16:15 UTC (permalink / raw)
  To: Jiasheng Jiang; +Cc: tony.luck, gpiccoli, linux-hardening, linux-kernel

On Thu, Jun 15, 2023 at 10:53:12AM +0800, Jiasheng Jiang wrote:
> Add check for the return value of kstrdup() and return the error
> if it fails in order to avoid NULL pointer dereference.
> 
> Fixes: 563ca40ddf40 ("pstore/platform: Switch pstore_info::name to const")
> Signed-off-by: Jiasheng Jiang <jiasheng@iscas.ac.cn>

Thanks, this is looking better!

> ---
> Changelog:
> 
> v1 -> v2:
> 
> 1. Allocate a copy earlier.
> ---
>  fs/pstore/platform.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/pstore/platform.c b/fs/pstore/platform.c
> index cbc0b468c1ab..727f8ce71062 100644
> --- a/fs/pstore/platform.c
> +++ b/fs/pstore/platform.c
> @@ -573,6 +573,8 @@ static int pstore_write_user_compat(struct pstore_record *record,
>   */
>  int pstore_register(struct pstore_info *psi)
>  {
> +	char *new_backend;
> +
>  	if (backend && strcmp(backend, psi->name)) {
>  		pr_warn("backend '%s' already in use: ignoring '%s'\n",
>  			backend, psi->name);
> @@ -593,6 +595,10 @@ int pstore_register(struct pstore_info *psi)
>  		return -EINVAL;
>  	}
>  
> +	new_backend = kstrdup(psi->name, GFP_KERNEL);
> +	if (!new_backend)
> +		return -ENOMEM;
> +
>  	mutex_lock(&psinfo_lock);
>  	if (psinfo) {
>  		pr_warn("backend '%s' already loaded: ignoring '%s'\n",

In my suggestion I had included a kfree() for new_backend in this above
error path, which is missing here. Can you respin it with that added?

> @@ -630,7 +636,7 @@ int pstore_register(struct pstore_info *psi)
>  	 * Update the module parameter backend, so it is visible
>  	 * through /sys/module/pstore/parameters/backend
>  	 */
> -	backend = kstrdup(psi->name, GFP_KERNEL);
> +	backend = new_backend;
>  
>  	pr_info("Registered %s as persistent store backend\n", psi->name);
>  
> -- 
> 2.25.1
> 

But otherwise, yeah, looks good. Thanks!

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-06-22 16:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-15  2:53 [PATCH v2] pstore/platform: Add check for kstrdup Jiasheng Jiang
2023-06-22 16:15 ` Kees Cook

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.