nvdimm: fix possible null-ptr-deref in nd_dax_probe()
PATCH 1/1 nvdimm: fix possible null-ptr-deref in nd_dax_probe()
It will cause null-ptr-deref when nd_dax_alloc() returns NULL, fix it by add check for nd_dax_alloc().
--- drivers/nvdimm/dax_devs.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/nvdimm/dax_devs.c +4 -0
@@ -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);
Signature · 1 line
2.25.1
Quoted 2 lines · Yi Yang
It will cause null-ptr-deref when
nd_dax_alloc()returns NULL, fix it by add check fornd_dax_alloc().
Was this found with a real workload or just by inspection?
Ira
Quoted 26 lines
Fixes c5ed9268643c ("libnvdimm, dax: autodetect support")Signed-off-by Yi Yang <[email protected]>--- drivers/nvdimm/dax_devs.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/nvdimm/dax_devs.c +4
-0@@ -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);Signature · 1 line
2.25.1
Quoted 8 lines · Ira Weiny
Quoted 2 lines · Yi Yang
It will cause null-ptr-deref when
nd_dax_alloc()returns NULL, fix it by add check fornd_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
Quoted 31 lines
Quoted 26 lines
Fixes c5ed9268643c ("libnvdimm, dax: autodetect support")Signed-off-by Yi Yang <[email protected]>--- drivers/nvdimm/dax_devs.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/nvdimm/dax_devs.c +4
-0@@ -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);Signature · 1 line
2.25.1.
Quoted 14 lines · Yi Yang
It will cause null-ptr-deref when
nd_dax_alloc()returns NULL, fix it by add check fornd_dax_alloc().Fixes c5ed9268643c ("libnvdimm, dax: autodetect support")Signed-off-by Yi Yang <[email protected]>--- drivers/nvdimm/dax_devs.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/nvdimm/dax_devs.c +0
-0
drivers/nvdimm/dax_devs.c int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns) · line 113
Quoted 8 lines
drivers/nvdimm/dax_devs.c +4
-0@@ -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:
---
drivers/nvdimm/dax_devs.c +2 -2
@@ -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>" );
drivers/nvdimm/nd.h +7 -0
@@ -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)
Quoted 74 lines · Dan Williams
Quoted 14 lines · Yi Yang
It will cause null-ptr-deref when
nd_dax_alloc()returns NULL, fix it by add check fornd_dax_alloc().Fixes c5ed9268643c ("libnvdimm, dax: autodetect support")Signed-off-by Yi Yang <[email protected]>--- drivers/nvdimm/dax_devs.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/nvdimm/dax_devs.c +0
-0drivers/nvdimm/dax_devs.c int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns) · line 113
Quoted 8 lines
drivers/nvdimm/dax_devs.c +4
-0@@ -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_daxis NULL, thennd_pfnis NULL becausend_daxis just a type-wrapper aroundnd_pfn.When
nd_pfnis NULL thennd_pfn_devinitwill fail.What I think this needs to make this clear is something like this:
---
drivers/nvdimm/dax_devs.c +2
-2@@ -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>" );drivers/nvdimm/nd.h +7
-0@@ -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)
Quoted 2 lines
.
LGTM,.
Your code is better.
Best regards,
Yiyang
PATCH 1/1 nvdimm: rectify the illogical code within nd_dax_probe()
In reply to Dan WilliamsWhen 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.
--- drivers/nvdimm/dax_devs.c | 4 ++-- drivers/nvdimm/nd.h | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-)
drivers/nvdimm/dax_devs.c +2 -2
@@ -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>" );
drivers/nvdimm/nd.h +7 -0
@@ -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)
Signature · 1 line
2.25.1
Re: [PATCH] nvdimm: rectify the illogical code within nd_dax_probe()
Quoted 7 lines · Yi Yang
When
nd_daxis NULL,nd_pfnis consequently NULL as well. Nevertheless, it is inadvisable to perform pointer arithmetic or address-taking on a NULL pointer.
Introduce thend_dax_devinit()function to enhance the code's logic and improve its readability.Signed-off-by Yi Yang <[email protected]>
but a few comments for future patches:
Is this v2 of the patch? Please label patch subject prefix as so.
Quoted 1 line
---
Please include change history here from previous versions.
Quoted 41 lines
drivers/nvdimm/dax_devs.c | 4 ++-- drivers/nvdimm/nd.h | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-)drivers/nvdimm/dax_devs.c +2
-2@@ -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>" );drivers/nvdimm/nd.h +7
-0@@ -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)