NVDIMM Device and Persistent Memory development
 help / color / mirror / Atom feed
* [PATCH] nvdimm: fix possible null-ptr-deref in nd_dax_probe()
@ 2024-10-26  1:06 Yi Yang
  2024-10-28 14:17 ` Ira Weiny
  2024-10-28 16:26 ` Dan Williams
  0 siblings, 2 replies; 7+ messages in thread
From: Yi Yang @ 2024-10-26  1:06 UTC (permalink / raw)
  To: dan.j.williams, vishal.l.verma, dave.jiang, ira.weiny
  Cc: nvdimm, wangweiyang2

It will cause null-ptr-deref when nd_dax_alloc() returns NULL, fix it by
add check for nd_dax_alloc().

Fixes: c5ed9268643c ("libnvdimm, dax: autodetect support")
Signed-off-by: Yi Yang <yiyang13@huawei.com>
---
 drivers/nvdimm/dax_devs.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/nvdimm/dax_devs.c b/drivers/nvdimm/dax_devs.c
index 6b4922de3047..70a7e401f90d 100644
--- a/drivers/nvdimm/dax_devs.c
+++ b/drivers/nvdimm/dax_devs.c
@@ -106,6 +106,10 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
 
 	nvdimm_bus_lock(&ndns->dev);
 	nd_dax = nd_dax_alloc(nd_region);
+	if (!nd_dax) {
+		nvdimm_bus_unlock(&ndns->dev);
+		return -ENOMEM;
+	}
 	nd_pfn = &nd_dax->nd_pfn;
 	dax_dev = nd_pfn_devinit(nd_pfn, ndns);
 	nvdimm_bus_unlock(&ndns->dev);
-- 
2.25.1


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

* Re: [PATCH] nvdimm: fix possible null-ptr-deref in nd_dax_probe()
  2024-10-26  1:06 [PATCH] nvdimm: fix possible null-ptr-deref in nd_dax_probe() Yi Yang
@ 2024-10-28 14:17 ` Ira Weiny
  2024-10-29  2:10   ` yiyang (D)
  2024-10-28 16:26 ` Dan Williams
  1 sibling, 1 reply; 7+ messages in thread
From: Ira Weiny @ 2024-10-28 14:17 UTC (permalink / raw)
  To: Yi Yang, dan.j.williams, vishal.l.verma, dave.jiang, ira.weiny
  Cc: nvdimm, wangweiyang2

Yi Yang wrote:
> It will cause null-ptr-deref when nd_dax_alloc() returns NULL, fix it by
> add check for nd_dax_alloc().

Was this found with a real workload or just by inspection?

Ira

> 
> Fixes: c5ed9268643c ("libnvdimm, dax: autodetect support")
> Signed-off-by: Yi Yang <yiyang13@huawei.com>
> ---
>  drivers/nvdimm/dax_devs.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/drivers/nvdimm/dax_devs.c b/drivers/nvdimm/dax_devs.c
> index 6b4922de3047..70a7e401f90d 100644
> --- a/drivers/nvdimm/dax_devs.c
> +++ b/drivers/nvdimm/dax_devs.c
> @@ -106,6 +106,10 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
>  
>  	nvdimm_bus_lock(&ndns->dev);
>  	nd_dax = nd_dax_alloc(nd_region);
> +	if (!nd_dax) {
> +		nvdimm_bus_unlock(&ndns->dev);
> +		return -ENOMEM;
> +	}
>  	nd_pfn = &nd_dax->nd_pfn;
>  	dax_dev = nd_pfn_devinit(nd_pfn, ndns);
>  	nvdimm_bus_unlock(&ndns->dev);
> -- 
> 2.25.1
> 
> 



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

* Re: [PATCH] nvdimm: fix possible null-ptr-deref in nd_dax_probe()
  2024-10-26  1:06 [PATCH] nvdimm: fix possible null-ptr-deref in nd_dax_probe() Yi Yang
  2024-10-28 14:17 ` Ira Weiny
@ 2024-10-28 16:26 ` Dan Williams
  2024-10-29  1:57   ` yiyang (D)
  2024-11-08  8:55   ` [PATCH] nvdimm: rectify the illogical code within nd_dax_probe() Yi Yang
  1 sibling, 2 replies; 7+ messages in thread
From: Dan Williams @ 2024-10-28 16:26 UTC (permalink / raw)
  To: Yi Yang, dan.j.williams, vishal.l.verma, dave.jiang, ira.weiny
  Cc: nvdimm, wangweiyang2

Yi Yang wrote:
> It will cause null-ptr-deref when nd_dax_alloc() returns NULL, fix it by
> add check for nd_dax_alloc().
> 
> Fixes: c5ed9268643c ("libnvdimm, dax: autodetect support")
> Signed-off-by: Yi Yang <yiyang13@huawei.com>
> ---
>  drivers/nvdimm/dax_devs.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/drivers/nvdimm/dax_devs.c b/drivers/nvdimm/dax_devs.c
> index 6b4922de3047..70a7e401f90d 100644
> --- a/drivers/nvdimm/dax_devs.c
> +++ b/drivers/nvdimm/dax_devs.c
> @@ -106,6 +106,10 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
>  
>  	nvdimm_bus_lock(&ndns->dev);
>  	nd_dax = nd_dax_alloc(nd_region);
> +	if (!nd_dax) {
> +		nvdimm_bus_unlock(&ndns->dev);
> +		return -ENOMEM;
> +	}
>  	nd_pfn = &nd_dax->nd_pfn;

No, this isn't a NULL pointer de-reference, but it is indeed
unreasonably subtle.

If nd_dax is NULL, then nd_pfn is NULL because nd_dax is just a
type-wrapper around nd_pfn.

When nd_pfn is NULL then nd_pfn_devinit will fail.

What I think this needs to make this clear is something like this:

---

diff --git a/drivers/nvdimm/dax_devs.c b/drivers/nvdimm/dax_devs.c
index 6b4922de3047..37b743acbb7b 100644
--- a/drivers/nvdimm/dax_devs.c
+++ b/drivers/nvdimm/dax_devs.c
@@ -106,12 +106,12 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
 
 	nvdimm_bus_lock(&ndns->dev);
 	nd_dax = nd_dax_alloc(nd_region);
-	nd_pfn = &nd_dax->nd_pfn;
-	dax_dev = nd_pfn_devinit(nd_pfn, ndns);
+	dax_dev = nd_dax_devinit(nd_dax, ndns);
 	nvdimm_bus_unlock(&ndns->dev);
 	if (!dax_dev)
 		return -ENOMEM;
 	pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
+	nd_pfn = &nd_dax->nd_pfn;
 	nd_pfn->pfn_sb = pfn_sb;
 	rc = nd_pfn_validate(nd_pfn, DAX_SIG);
 	dev_dbg(dev, "dax: %s\n", rc == 0 ? dev_name(dax_dev) : "<none>");
diff --git a/drivers/nvdimm/nd.h b/drivers/nvdimm/nd.h
index 2dbb1dca17b5..5ca06e9a2d29 100644
--- a/drivers/nvdimm/nd.h
+++ b/drivers/nvdimm/nd.h
@@ -600,6 +600,13 @@ struct nd_dax *to_nd_dax(struct device *dev);
 int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns);
 bool is_nd_dax(const struct device *dev);
 struct device *nd_dax_create(struct nd_region *nd_region);
+static inline struct device *nd_dax_devinit(struct nd_dax *nd_dax,
+					    struct nd_namespace_common *ndns)
+{
+	if (!nd_dax)
+		return NULL;
+	return nd_pfn_devinit(&nd_dax->nd_pfn, ndns);
+}
 #else
 static inline int nd_dax_probe(struct device *dev,
 		struct nd_namespace_common *ndns)

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

* Re: [PATCH] nvdimm: fix possible null-ptr-deref in nd_dax_probe()
  2024-10-28 16:26 ` Dan Williams
@ 2024-10-29  1:57   ` yiyang (D)
  2024-11-08  8:55   ` [PATCH] nvdimm: rectify the illogical code within nd_dax_probe() Yi Yang
  1 sibling, 0 replies; 7+ messages in thread
From: yiyang (D) @ 2024-10-29  1:57 UTC (permalink / raw)
  To: Dan Williams, vishal.l.verma, dave.jiang, ira.weiny; +Cc: nvdimm, wangweiyang2


On 2024/10/29 0:26, Dan Williams wrote:
> Yi Yang wrote:
>> It will cause null-ptr-deref when nd_dax_alloc() returns NULL, fix it by
>> add check for nd_dax_alloc().
>>
>> Fixes: c5ed9268643c ("libnvdimm, dax: autodetect support")
>> Signed-off-by: Yi Yang <yiyang13@huawei.com>
>> ---
>>   drivers/nvdimm/dax_devs.c | 4 ++++
>>   1 file changed, 4 insertions(+)
>>
>> diff --git a/drivers/nvdimm/dax_devs.c b/drivers/nvdimm/dax_devs.c
>> index 6b4922de3047..70a7e401f90d 100644
>> --- a/drivers/nvdimm/dax_devs.c
>> +++ b/drivers/nvdimm/dax_devs.c
>> @@ -106,6 +106,10 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
>>   
>>   	nvdimm_bus_lock(&ndns->dev);
>>   	nd_dax = nd_dax_alloc(nd_region);
>> +	if (!nd_dax) {
>> +		nvdimm_bus_unlock(&ndns->dev);
>> +		return -ENOMEM;
>> +	}
>>   	nd_pfn = &nd_dax->nd_pfn;
> 
> No, this isn't a NULL pointer de-reference, but it is indeed
> unreasonably subtle.
> 
> If nd_dax is NULL, then nd_pfn is NULL because nd_dax is just a
> type-wrapper around nd_pfn.
> 
> When nd_pfn is NULL then nd_pfn_devinit will fail.
> 
> What I think this needs to make this clear is something like this:
> 
> ---
> 
> diff --git a/drivers/nvdimm/dax_devs.c b/drivers/nvdimm/dax_devs.c
> index 6b4922de3047..37b743acbb7b 100644
> --- a/drivers/nvdimm/dax_devs.c
> +++ b/drivers/nvdimm/dax_devs.c
> @@ -106,12 +106,12 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
>   
>   	nvdimm_bus_lock(&ndns->dev);
>   	nd_dax = nd_dax_alloc(nd_region);
> -	nd_pfn = &nd_dax->nd_pfn;
> -	dax_dev = nd_pfn_devinit(nd_pfn, ndns);
> +	dax_dev = nd_dax_devinit(nd_dax, ndns);
>   	nvdimm_bus_unlock(&ndns->dev);
>   	if (!dax_dev)
>   		return -ENOMEM;
>   	pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
> +	nd_pfn = &nd_dax->nd_pfn;
>   	nd_pfn->pfn_sb = pfn_sb;
>   	rc = nd_pfn_validate(nd_pfn, DAX_SIG);
>   	dev_dbg(dev, "dax: %s\n", rc == 0 ? dev_name(dax_dev) : "<none>");
> diff --git a/drivers/nvdimm/nd.h b/drivers/nvdimm/nd.h
> index 2dbb1dca17b5..5ca06e9a2d29 100644
> --- a/drivers/nvdimm/nd.h
> +++ b/drivers/nvdimm/nd.h
> @@ -600,6 +600,13 @@ struct nd_dax *to_nd_dax(struct device *dev);
>   int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns);
>   bool is_nd_dax(const struct device *dev);
>   struct device *nd_dax_create(struct nd_region *nd_region);
> +static inline struct device *nd_dax_devinit(struct nd_dax *nd_dax,
> +					    struct nd_namespace_common *ndns)
> +{
> +	if (!nd_dax)
> +		return NULL;
> +	return nd_pfn_devinit(&nd_dax->nd_pfn, ndns);
> +}
>   #else
>   static inline int nd_dax_probe(struct device *dev,
>   		struct nd_namespace_common *ndns)
> 
> .
> 

LGTM,.
Your code is better.

Best regards,
Yiyang

-- 


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

* Re: [PATCH] nvdimm: fix possible null-ptr-deref in nd_dax_probe()
  2024-10-28 14:17 ` Ira Weiny
@ 2024-10-29  2:10   ` yiyang (D)
  0 siblings, 0 replies; 7+ messages in thread
From: yiyang (D) @ 2024-10-29  2:10 UTC (permalink / raw)
  To: Ira Weiny, dan.j.williams, vishal.l.verma, dave.jiang
  Cc: nvdimm, wangweiyang2


On 2024/10/28 22:17, Ira Weiny wrote:
> Yi Yang wrote:
>> It will cause null-ptr-deref when nd_dax_alloc() returns NULL, fix it by
>> add check for nd_dax_alloc().
> 
> Was this found with a real workload or just by inspection?
> 
> Ira
> 

I just found the problem by reading the code.

Regards,
Yiyang
>>
>> Fixes: c5ed9268643c ("libnvdimm, dax: autodetect support")
>> Signed-off-by: Yi Yang <yiyang13@huawei.com>
>> ---
>>   drivers/nvdimm/dax_devs.c | 4 ++++
>>   1 file changed, 4 insertions(+)
>>
>> diff --git a/drivers/nvdimm/dax_devs.c b/drivers/nvdimm/dax_devs.c
>> index 6b4922de3047..70a7e401f90d 100644
>> --- a/drivers/nvdimm/dax_devs.c
>> +++ b/drivers/nvdimm/dax_devs.c
>> @@ -106,6 +106,10 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
>>   
>>   	nvdimm_bus_lock(&ndns->dev);
>>   	nd_dax = nd_dax_alloc(nd_region);
>> +	if (!nd_dax) {
>> +		nvdimm_bus_unlock(&ndns->dev);
>> +		return -ENOMEM;
>> +	}
>>   	nd_pfn = &nd_dax->nd_pfn;
>>   	dax_dev = nd_pfn_devinit(nd_pfn, ndns);
>>   	nvdimm_bus_unlock(&ndns->dev);
>> -- 
>> 2.25.1
>>
>>
> 
> 
> 
> .
> 

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

* [PATCH] nvdimm: rectify the illogical code within nd_dax_probe()
  2024-10-28 16:26 ` Dan Williams
  2024-10-29  1:57   ` yiyang (D)
@ 2024-11-08  8:55   ` Yi Yang
  2024-11-13 17:08     ` Dave Jiang
  1 sibling, 1 reply; 7+ messages in thread
From: Yi Yang @ 2024-11-08  8:55 UTC (permalink / raw)
  To: dan.j.williams, vishal.l.verma, dave.jiang, ira.weiny; +Cc: nvdimm

When nd_dax is NULL, nd_pfn is consequently NULL as well. Nevertheless,
it is inadvisable to perform pointer arithmetic or address-taking on a
NULL pointer.
Introduce the nd_dax_devinit() function to enhance the code's logic and
improve its readability.

Signed-off-by: Yi Yang <yiyang13@huawei.com>
---
 drivers/nvdimm/dax_devs.c | 4 ++--
 drivers/nvdimm/nd.h       | 7 +++++++
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/drivers/nvdimm/dax_devs.c b/drivers/nvdimm/dax_devs.c
index 6b4922de3047..37b743acbb7b 100644
--- a/drivers/nvdimm/dax_devs.c
+++ b/drivers/nvdimm/dax_devs.c
@@ -106,12 +106,12 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
 
 	nvdimm_bus_lock(&ndns->dev);
 	nd_dax = nd_dax_alloc(nd_region);
-	nd_pfn = &nd_dax->nd_pfn;
-	dax_dev = nd_pfn_devinit(nd_pfn, ndns);
+	dax_dev = nd_dax_devinit(nd_dax, ndns);
 	nvdimm_bus_unlock(&ndns->dev);
 	if (!dax_dev)
 		return -ENOMEM;
 	pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
+	nd_pfn = &nd_dax->nd_pfn;
 	nd_pfn->pfn_sb = pfn_sb;
 	rc = nd_pfn_validate(nd_pfn, DAX_SIG);
 	dev_dbg(dev, "dax: %s\n", rc == 0 ? dev_name(dax_dev) : "<none>");
diff --git a/drivers/nvdimm/nd.h b/drivers/nvdimm/nd.h
index 2dbb1dca17b5..5ca06e9a2d29 100644
--- a/drivers/nvdimm/nd.h
+++ b/drivers/nvdimm/nd.h
@@ -600,6 +600,13 @@ struct nd_dax *to_nd_dax(struct device *dev);
 int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns);
 bool is_nd_dax(const struct device *dev);
 struct device *nd_dax_create(struct nd_region *nd_region);
+static inline struct device *nd_dax_devinit(struct nd_dax *nd_dax,
+					    struct nd_namespace_common *ndns)
+{
+	if (!nd_dax)
+		return NULL;
+	return nd_pfn_devinit(&nd_dax->nd_pfn, ndns);
+}
 #else
 static inline int nd_dax_probe(struct device *dev,
 		struct nd_namespace_common *ndns)
-- 
2.25.1


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

* Re: [PATCH] nvdimm: rectify the illogical code within nd_dax_probe()
  2024-11-08  8:55   ` [PATCH] nvdimm: rectify the illogical code within nd_dax_probe() Yi Yang
@ 2024-11-13 17:08     ` Dave Jiang
  0 siblings, 0 replies; 7+ messages in thread
From: Dave Jiang @ 2024-11-13 17:08 UTC (permalink / raw)
  To: Yi Yang, dan.j.williams, vishal.l.verma, ira.weiny; +Cc: nvdimm



On 11/8/24 1:55 AM, Yi Yang wrote:
> When nd_dax is NULL, nd_pfn is consequently NULL as well. Nevertheless,
> it is inadvisable to perform pointer arithmetic or address-taking on a
> NULL pointer.
> Introduce the nd_dax_devinit() function to enhance the code's logic and
> improve its readability.
> 
> Signed-off-by: Yi Yang <yiyang13@huawei.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
but a few comments for future patches: 

Is this v2 of the patch? Please label patch subject prefix as so. 
> ---

Please include change history here from previous versions. 

>  drivers/nvdimm/dax_devs.c | 4 ++--
>  drivers/nvdimm/nd.h       | 7 +++++++
>  2 files changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/nvdimm/dax_devs.c b/drivers/nvdimm/dax_devs.c
> index 6b4922de3047..37b743acbb7b 100644
> --- a/drivers/nvdimm/dax_devs.c
> +++ b/drivers/nvdimm/dax_devs.c
> @@ -106,12 +106,12 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
>  
>  	nvdimm_bus_lock(&ndns->dev);
>  	nd_dax = nd_dax_alloc(nd_region);
> -	nd_pfn = &nd_dax->nd_pfn;
> -	dax_dev = nd_pfn_devinit(nd_pfn, ndns);
> +	dax_dev = nd_dax_devinit(nd_dax, ndns);
>  	nvdimm_bus_unlock(&ndns->dev);
>  	if (!dax_dev)
>  		return -ENOMEM;
>  	pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
> +	nd_pfn = &nd_dax->nd_pfn;
>  	nd_pfn->pfn_sb = pfn_sb;
>  	rc = nd_pfn_validate(nd_pfn, DAX_SIG);
>  	dev_dbg(dev, "dax: %s\n", rc == 0 ? dev_name(dax_dev) : "<none>");
> diff --git a/drivers/nvdimm/nd.h b/drivers/nvdimm/nd.h
> index 2dbb1dca17b5..5ca06e9a2d29 100644
> --- a/drivers/nvdimm/nd.h
> +++ b/drivers/nvdimm/nd.h
> @@ -600,6 +600,13 @@ struct nd_dax *to_nd_dax(struct device *dev);
>  int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns);
>  bool is_nd_dax(const struct device *dev);
>  struct device *nd_dax_create(struct nd_region *nd_region);
> +static inline struct device *nd_dax_devinit(struct nd_dax *nd_dax,
> +					    struct nd_namespace_common *ndns)
> +{
> +	if (!nd_dax)
> +		return NULL;
> +	return nd_pfn_devinit(&nd_dax->nd_pfn, ndns);
> +}
>  #else
>  static inline int nd_dax_probe(struct device *dev,
>  		struct nd_namespace_common *ndns)


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

end of thread, other threads:[~2024-11-13 17:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-26  1:06 [PATCH] nvdimm: fix possible null-ptr-deref in nd_dax_probe() Yi Yang
2024-10-28 14:17 ` Ira Weiny
2024-10-29  2:10   ` yiyang (D)
2024-10-28 16:26 ` Dan Williams
2024-10-29  1:57   ` yiyang (D)
2024-11-08  8:55   ` [PATCH] nvdimm: rectify the illogical code within nd_dax_probe() Yi Yang
2024-11-13 17:08     ` Dave Jiang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox