Re: [PATCH] cxl: Fix null pointer dereference in cxl_get_fd

From: Frederic Barrat
Date: Mon Dec 04 2023 - 05:43:52 EST




On 04/12/2023 03:07, Kunwu Chan wrote:
kasprintf() returns a pointer to dynamically allocated memory
which can be NULL upon failure.

Fixes: bdecf76e319a ("cxl: Fix coredump generation when cxl_get_fd() is used")
Signed-off-by: Kunwu Chan <chentao@xxxxxxxxxx>
---
drivers/misc/cxl/api.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/drivers/misc/cxl/api.c b/drivers/misc/cxl/api.c
index d85c56530863..bfd7ccd4d7e1 100644
--- a/drivers/misc/cxl/api.c
+++ b/drivers/misc/cxl/api.c
@@ -419,6 +419,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) {
+ put_unused_fd(fdtmp);
+ return ERR_PTR(-ENOMEM);
+ }


That works, but you might as well follow the existing error path:

name = kasprintf(GFP_KERNEL, "cxl:%d", ctx->pe);
if (!name)
goto err_fd;

Fred


file = cxl_getfile(name, fops, ctx, flags);
kfree(name);
if (IS_ERR(file))