[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <c198f1fa-5a42-42d9-a5f0-713ffbabd4ee@intel.com>
Date: Mon, 26 Jan 2026 10:39:21 -0700
From: Dave Jiang <dave.jiang@...el.com>
To: Zhaoyang Yu <2426767509@...com>, dan.j.williams@...el.com
Cc: vishal.l.verma@...el.com, ira.weiny@...el.com, nvdimm@...ts.linux.dev,
linux-kernel@...r.kernel.org, gszhai@...u.edu.cn
Subject: Re: [PATCH] nvdimm: Add check for devm_kmalloc() and fix NULL pointer
dereference in nd_pfn_probe() and nd_dax_probe()
On 1/26/26 6:04 AM, Zhaoyang Yu wrote:
> The devm_kmalloc() function may return NULL when memory allocation fails.
> In nd_pfn_probe() and nd_dax_probe(), the return values of devm_kmalloc()
> are not checked. If pfn_sb is NULL, it will cause a NULL pointer
> dereference in the subsequent calls to nd_pfn_validate().
>
> Additionally, if the allocation fails, the devices initialized by
> nd_pfn_devinit() or nd_dax_devinit() are not properly released, leading
> to memory leaks.
>
> Fix this by checking the return value of devm_kmalloc() in both functions.
> If the allocation fails, use put_device() to release the initialized device
> and return -ENOMEM.
>
> Signed-off-by: Zhaoyang Yu <2426767509@...com>
Please provide a Fixes tag. Otherwise LGTM.
Reviewed-by: Dave Jiang <dave.jiang@...el.com>
> ---
> drivers/nvdimm/dax_devs.c | 4 ++++
> drivers/nvdimm/pfn_devs.c | 4 ++++
> 2 files changed, 8 insertions(+)
>
> diff --git a/drivers/nvdimm/dax_devs.c b/drivers/nvdimm/dax_devs.c
> index ba4c409ede65..aa51a9022d12 100644
> --- a/drivers/nvdimm/dax_devs.c
> +++ b/drivers/nvdimm/dax_devs.c
> @@ -111,6 +111,10 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
> return -ENOMEM;
> }
> pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
> + if (!pfn_sb) {
> + put_device(dax_dev);
> + return -ENOMEM;
> + }
> nd_pfn = &nd_dax->nd_pfn;
> nd_pfn->pfn_sb = pfn_sb;
> rc = nd_pfn_validate(nd_pfn, DAX_SIG);
> diff --git a/drivers/nvdimm/pfn_devs.c b/drivers/nvdimm/pfn_devs.c
> index 42b172fc5576..6a69d8bfeb7c 100644
> --- a/drivers/nvdimm/pfn_devs.c
> +++ b/drivers/nvdimm/pfn_devs.c
> @@ -635,6 +635,10 @@ int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
> if (!pfn_dev)
> return -ENOMEM;
> pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
> + if (!pfn_sb) {
> + put_device(pfn_dev);
> + return -ENOMEM;
> + }
> nd_pfn = to_nd_pfn(pfn_dev);
> nd_pfn->pfn_sb = pfn_sb;
> rc = nd_pfn_validate(nd_pfn, PFN_SIG);
Powered by blists - more mailing lists