public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: Jiasheng Jiang <jiasheng@iscas.ac.cn>
Cc: tony.luck@intel.com, gpiccoli@igalia.com,
	linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] pstore/platform: Add check for kstrdup
Date: Wed, 14 Jun 2023 12:01:30 -0700	[thread overview]
Message-ID: <202306141155.FCA5D6F@keescook> (raw)
In-Reply-To: <20230614100020.39020-1-jiasheng@iscas.ac.cn>

On Wed, Jun 14, 2023 at 06:00:20PM +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>
> ---
>  fs/pstore/platform.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/fs/pstore/platform.c b/fs/pstore/platform.c
> index cbc0b468c1ab..afe07f0d1216 100644
> --- a/fs/pstore/platform.c
> +++ b/fs/pstore/platform.c
> @@ -631,6 +631,10 @@ int pstore_register(struct pstore_info *psi)
>  	 * through /sys/module/pstore/parameters/backend
>  	 */
>  	backend = kstrdup(psi->name, GFP_KERNEL);
> +	if (!backend) {
> +		mutex_unlock(&psinfo_lock);
> +		return -ENOMEM;
> +	}

Hmm, I think this isn't the right place since there's been a bunch of
other allocations and registrations. I think it would be better to
allocate a copy (but not assign to "backend" yet) earlier, perhaps
before the taking the psinfo_lock lock? Like:

	char *new_backend;
	...
	new_backend = kstrdup(psi->name, GFP_KERNEL);
	if (!new_backend)
		return -ENOMEM;

        mutex_lock(&psinfo_lock);
        if (psinfo) {
		...
                mutex_unlock(&psinfo_lock);
		kfree(new_backend);
                return -EBUSY;
        }
	...
	backend = new_backend;


-Kees

-- 
Kees Cook

  reply	other threads:[~2023-06-14 19:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-14 10:00 [PATCH] pstore/platform: Add check for kstrdup Jiasheng Jiang
2023-06-14 19:01 ` Kees Cook [this message]
  -- strict thread matches above, loose matches on Subject: below --
2023-06-15  2:48 Jiasheng Jiang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=202306141155.FCA5D6F@keescook \
    --to=keescook@chromium.org \
    --cc=gpiccoli@igalia.com \
    --cc=jiasheng@iscas.ac.cn \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tony.luck@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox