linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Frederic Barrat <fbarrat@linux.ibm.com>
To: Kunwu Chan <chentao@kylinos.cn>,
	ajd@linux.ibm.com, arnd@arndb.de, mpe@ellerman.id.au,
	mrochs@linux.vnet.ibm.com
Cc: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org,
	Kunwu Chan <kunwu.chan@hotmail.com>
Subject: Re: [PATCH v2] cxl: Fix null pointer dereference in cxl_get_fd
Date: Mon, 15 Jan 2024 12:05:22 +0100	[thread overview]
Message-ID: <80f1ba0d-afae-4af7-8de7-9813cb22e741@linux.ibm.com> (raw)
In-Reply-To: <20240112054903.133145-1-chentao@kylinos.cn>



On 12/01/2024 06:49, Kunwu Chan wrote:
> kasprintf() returns a pointer to dynamically allocated memory
> which can be NULL upon failure.
> 
> Uniformly handle resource release in error paths. And when an
> error occurs, an error pointer should be returned.
> 
> Fixes: bdecf76e319a ("cxl: Fix coredump generation when cxl_get_fd() is used")
> Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
> Cc: Kunwu Chan <kunwu.chan@hotmail.com>
> Suggested-by: Frederic Barrat <fbarrat@linux.ibm.com>
> ---

Thanks!
Acked-by: Frederic Barrat <fbarrat@linux.ibm.com>

   Fred


> v2: Deal with error path
> ---
>   drivers/misc/cxl/api.c | 18 ++++++++++++++----
>   1 file changed, 14 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/misc/cxl/api.c b/drivers/misc/cxl/api.c
> index d85c56530863..b49bc3d29fc0 100644
> --- a/drivers/misc/cxl/api.c
> +++ b/drivers/misc/cxl/api.c
> @@ -389,19 +389,22 @@ struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
>   			int *fd)
>   {
>   	struct file *file;
> -	int rc, flags, fdtmp;
> +	int rc = 0, flags, fdtmp;
>   	char *name = NULL;
>   
>   	/* only allow one per context */
> -	if (ctx->mapping)
> -		return ERR_PTR(-EEXIST);
> +	if (ctx->mapping) {
> +		rc = -EEXIST;
> +		goto err;
> +	}
>   
>   	flags = O_RDWR | O_CLOEXEC;
>   
>   	/* This code is similar to anon_inode_getfd() */
>   	rc = get_unused_fd_flags(flags);
>   	if (rc < 0)
> -		return ERR_PTR(rc);
> +		goto err;
> +
>   	fdtmp = rc;
>   
>   	/*
> @@ -419,6 +422,10 @@ struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
>   		fops = (struct file_operations *)&afu_fops;
>   
>   	name = kasprintf(GFP_KERNEL, "cxl:%d", ctx->pe);
> +	if (!name) {
> +		rc = -ENOMEM;
> +		goto err_fd;
> +	}
>   	file = cxl_getfile(name, fops, ctx, flags);
>   	kfree(name);
>   	if (IS_ERR(file))
> @@ -430,6 +437,9 @@ struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
>   
>   err_fd:
>   	put_unused_fd(fdtmp);
> +err:
> +	if (rc < 0)
> +		return ERR_PTR(rc);
>   	return NULL;
>   }
>   EXPORT_SYMBOL_GPL(cxl_get_fd);

      parent reply	other threads:[~2024-01-15 11:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-12  5:49 [PATCH v2] cxl: Fix null pointer dereference in cxl_get_fd Kunwu Chan
2024-01-15  5:02 ` Andrew Donnellan
2024-01-15 11:05 ` Frederic Barrat [this message]

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=80f1ba0d-afae-4af7-8de7-9813cb22e741@linux.ibm.com \
    --to=fbarrat@linux.ibm.com \
    --cc=ajd@linux.ibm.com \
    --cc=arnd@arndb.de \
    --cc=chentao@kylinos.cn \
    --cc=kunwu.chan@hotmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=mpe@ellerman.id.au \
    --cc=mrochs@linux.vnet.ibm.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;
as well as URLs for NNTP newsgroup(s).