Skip to contentSkip to conversation

NFSD: Prevent a NULL pointer dereference in fh_getattr()

PATCH In review Chuck Lever 2 participants 5 messages Nov 4, 2025 – Nov 5, 2025

Subsystems (2)
lore
Share & tools
lore

Chuck Lever

author maintainer
#0 lore raw

PATCH 1/1 NFSD: Prevent a NULL pointer dereference in fh_getattr()

From: Chuck Lever <[email protected]>

In general, fh_getattr() can be called after the target dentry has gone negative. For a negative dentry, d_inode(p.dentry) will return NULL. S_ISREG() will dereference that pointer.

Avoid this potential regression by using the d_is_reg() helper instead.

Fixes d11f6cd1bb4a ("NFSD: filecache: add STATX_DIOALIGN and STATX_DIO_READ_ALIGN support")
---
 fs/nfsd/nfsfh.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Hi Anna -

nfsd-fixes is still based on v6.17-rc, so this patch does not apply to it. Can you take it for v6.18-rc ?

fs/nfsd/nfsfh.c +1 -2
index ed85dd43da18..16182936828f 100644
@@ -696,10 +696,9 @@ __be32 fh_getattr(const struct svc_fh *fhp, struct kstat *stat)
 		.mnt		= fhp->fh_export->ex_path.mnt,
 		.dentry		= fhp->fh_dentry,
 	};
-	struct inode *inode = d_inode(p.dentry);
 	u32 request_mask = STATX_BASIC_STATS;
 
-	if (S_ISREG(inode->i_mode))
+	if (d_is_reg(p.dentry))
 		request_mask |= (STATX_DIOALIGN | STATX_DIO_READ_ALIGN);
 
 	if (fhp->fh_maxsize == NFS4_FHSIZE)
Signature · 1 line
2.51.0
#1 lore raw
Quoted 5 lines · Chuck Lever

From: Chuck Lever <[email protected]>

In general, fh_getattr() can be called after the target dentry has gone negative. For a negative dentry, d_inode(p.dentry) will return NULL. S_ISREG() will dereference that pointer.

That isn't correct. While a reference to a dentry is held the inode cannot become NULL asynchronously.
It can change from NULL to non-NULL if another thread "creates". It can become NULL if *this* thread calls unlink and no other thread has a reference.
But it cannot suddenly become NULL.

I like the patch as it avoids a dereference and so puts less pressure on the dcache, but it does not change correctness.

Sorry if I implied otherwise when I suggested it.

NeilBrown

Quoted 39 lines

Avoid this potential regression by using the d_is_reg() helper instead.

Fixes d11f6cd1bb4a ("NFSD: filecache: add STATX_DIOALIGN and STATX_DIO_READ_ALIGN support")
---
 fs/nfsd/nfsfh.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Hi Anna -

nfsd-fixes is still based on v6.17-rc, so this patch does not apply to it. Can you take it for v6.18-rc ?

fs/nfsd/nfsfh.c +1 -2
index ed85dd43da18..16182936828f 100644
@@ -696,10 +696,9 @@ __be32 fh_getattr(const struct svc_fh *fhp, struct kstat *stat)
 		.mnt		= fhp->fh_export->ex_path.mnt,
 		.dentry		= fhp->fh_dentry,
 	};
-	struct inode *inode = d_inode(p.dentry);
 	u32 request_mask = STATX_BASIC_STATS;
 
-	if (S_ISREG(inode->i_mode))
+	if (d_is_reg(p.dentry))
 		request_mask |= (STATX_DIOALIGN | STATX_DIO_READ_ALIGN);
 
 	if (fhp->fh_maxsize == NFS4_FHSIZE)
Signature · 1 line
2.51.0
author maintainer
#2 lore raw
Quoted 14 lines · NeilBrown
Quoted 5 lines · Chuck Lever

From: Chuck Lever <[email protected]>

In general, fh_getattr() can be called after the target dentry has gone negative. For a negative dentry, d_inode(p.dentry) will return NULL. S_ISREG() will dereference that pointer.

That isn't correct. While a reference to a dentry is held the inode cannot become NULL asynchronously.
It can change from NULL to non-NULL if another thread "creates". It can become NULL if *this* thread calls unlink and no other thread has a reference.
But it cannot suddenly become NULL.

Quoted 2 lines

I like the patch as it avoids a dereference and so puts less pressure on the dcache, but it does not change correctness.

I think the steps I'm worried about is if NFSD unlinks the file, and then something subsequently invokes fh_getattr() assuming that is safe to do.

How should I update the patch description?

Quoted 46 lines

Sorry if I implied otherwise when I suggested it.

NeilBrown

Quoted 39 lines

Avoid this potential regression by using the d_is_reg() helper instead.

Fixes d11f6cd1bb4a ("NFSD: filecache: add STATX_DIOALIGN and STATX_DIO_READ_ALIGN support")
---
 fs/nfsd/nfsfh.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Hi Anna -

nfsd-fixes is still based on v6.17-rc, so this patch does not apply to it. Can you take it for v6.18-rc ?

fs/nfsd/nfsfh.c +1 -2
index ed85dd43da18..16182936828f 100644
@@ -696,10 +696,9 @@ __be32 fh_getattr(const struct svc_fh *fhp, struct kstat *stat)
 		.mnt		= fhp->fh_export->ex_path.mnt,
 		.dentry		= fhp->fh_dentry,
 	};
-	struct inode *inode = d_inode(p.dentry);
 	u32 request_mask = STATX_BASIC_STATS;
 
-	if (S_ISREG(inode->i_mode))
+	if (d_is_reg(p.dentry))
 		request_mask |= (STATX_DIOALIGN | STATX_DIO_READ_ALIGN);
 
 	if (fhp->fh_maxsize == NFS4_FHSIZE)
Signature · 1 line
2.51.0
Signature · 1 line
Chuck Lever
#3 lore raw
Quoted 15 lines · Chuck Lever
Quoted 14 lines · NeilBrown
Quoted 5 lines · Chuck Lever

From: Chuck Lever <[email protected]>

In general, fh_getattr() can be called after the target dentry has gone negative. For a negative dentry, d_inode(p.dentry) will return NULL. S_ISREG() will dereference that pointer.

That isn't correct. While a reference to a dentry is held the inode cannot become NULL asynchronously.
It can change from NULL to non-NULL if another thread "creates". It can become NULL if *this* thread calls unlink and no other thread has a reference.
But it cannot suddenly become NULL.

Quoted 6 lines
Quoted 2 lines

I like the patch as it avoids a dereference and so puts less pressure on the dcache, but it does not change correctness.

I think the steps I'm worried about is if NFSD unlinks the file, and then something subsequently invokes fh_getattr() assuming that is safe to do.

nfsd never creates a svc_fh for the dentry is it about the unlink (or rmdir).
A delete involves an fh for the
I'm guessing
Commit: 1519fbc8832b ("minmax.h: use BUILD_BUG_ON_MSG() for the lo < hi test in clamp()")

is the problem.
parent, and a name. No fh for the child.

Quoted 2 lines

How should I update the patch description?

Maybe just drop the patch for now. There is no regression and nothing to fix. Maybe we can make the change latter as part of a cleanup.

NeilBrown

Quoted 53 lines
Quoted 48 lines

Sorry if I implied otherwise when I suggested it.

NeilBrown

Quoted 39 lines

Avoid this potential regression by using the d_is_reg() helper instead.

Fixes d11f6cd1bb4a ("NFSD: filecache: add STATX_DIOALIGN and STATX_DIO_READ_ALIGN support")
---
 fs/nfsd/nfsfh.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Hi Anna -

nfsd-fixes is still based on v6.17-rc, so this patch does not apply to it. Can you take it for v6.18-rc ?

fs/nfsd/nfsfh.c +1 -2
index ed85dd43da18..16182936828f 100644
@@ -696,10 +696,9 @@ __be32 fh_getattr(const struct svc_fh *fhp, struct kstat *stat)
 		.mnt		= fhp->fh_export->ex_path.mnt,
 		.dentry		= fhp->fh_dentry,
 	};
-	struct inode *inode = d_inode(p.dentry);
 	u32 request_mask = STATX_BASIC_STATS;
 
-	if (S_ISREG(inode->i_mode))
+	if (d_is_reg(p.dentry))
 		request_mask |= (STATX_DIOALIGN | STATX_DIO_READ_ALIGN);
 
 	if (fhp->fh_maxsize == NFS4_FHSIZE)
Signature · 1 line
2.51.0
Signature · 1 line
Chuck Lever
author maintainer
#4 lore raw
Quoted 29 lines · NeilBrown
Quoted 15 lines · Chuck Lever
Quoted 14 lines · NeilBrown
Quoted 5 lines · Chuck Lever

From: Chuck Lever <[email protected]>

In general, fh_getattr() can be called after the target dentry has gone negative. For a negative dentry, d_inode(p.dentry) will return NULL. S_ISREG() will dereference that pointer.

That isn't correct. While a reference to a dentry is held the inode cannot become NULL asynchronously.
It can change from NULL to non-NULL if another thread "creates". It can become NULL if *this* thread calls unlink and no other thread has a reference.
But it cannot suddenly become NULL.

Quoted 6 lines
Quoted 2 lines

I like the patch as it avoids a dereference and so puts less pressure on the dcache, but it does not change correctness.

I think the steps I'm worried about is if NFSD unlinks the file, and then something subsequently invokes fh_getattr() assuming that is safe to do.

nfsd never creates a svc_fh for the dentry is it about the unlink (or rmdir).
A delete involves an fh for the
I'm guessing
Commit: 1519fbc8832b ("minmax.h: use BUILD_BUG_ON_MSG() for the lo < hi test in clamp()")

Quoted 8 lines

is the problem.
parent, and a name. No fh for the child.

Quoted 2 lines

How should I update the patch description?

Maybe just drop the patch for now. There is no regression and nothing to fix. Maybe we can make the change latter as part of a cleanup.

OK, then. Anna, you can ignore this one and I will drop it from nfsd-testing.

Signature · 1 line
Chuck Lever