Skip to contentSkip to conversation

nfsd: fix minor issues with atomic_create() use in dentry_create()

PATCH In review NeilBrown 4 participants 10 messages May 26 – May 27

Subsystems (2)
lore
Share & tools
lore

NeilBrown

author
#0 lore raw

PATCH 1/1 nfsd: fix minor issues with atomic_create() use in dentry_create()

    https://sashiko.dev/#/patchset/[email protected]?part=1

reported a couple of edge-case problems with the use of atomic_open() in dentry_create(), called from nfsd4_create_file.

These patches attempt to address those issues.

NeilBrown

[PATCH 1/2] nfsd: fix possible fh_compose of wrong dentry in [PATCH 2/2] nfsd: ensure nfsd_file_to_acquire() does not use a

author
#1 lore raw

PATCH 1/2 nfsd: fix possible fh_compose of wrong dentry in nfsd4_create_file()

From: NeilBrown <[email protected]>

dentry_create() can hypothetically provide a different dentry than the one passed in. This could happen, for example, if the exported filesystem is NFS, and the server return to OPEN a filehandle which matched a directory that was already in the dcache. Clearly this would not be expected!

If this were to happen the dentry (child) that was already stored in resfhp could be freed and later dereferenced.

We shouldn't call fh_compose() until we are certain that we have the final dentry, so this patch moved the fh_compose() call to two places:
one for the case where the target already exists, and one after dentry_create() where it was created.

Fixes 64a989dbd144 ("VFS/knfsd: Teach dentry_create() to use atomic_open()")
---
 fs/nfsd/nfs4proc.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
fs/nfsd/nfs4proc.c +8 -4
index ab39ec885440..c7adf791b6d1 100644
@@ -302,10 +302,6 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 			goto out;
 	}
 
-	status = fh_compose(resfhp, fhp->fh_export, child, fhp);
-	if (status != nfs_ok)
-		goto out;
-
 	v_mtime = 0;
 	v_atime = 0;
 	if (nfsd4_create_is_exclusive(open->op_createmode)) {
@@ -331,6 +327,10 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		if (status != nfs_ok)
 			goto out;
 
+		status = fh_compose(resfhp, fhp->fh_export, child, fhp);
+		if (status != nfs_ok)
+			goto out;
+
 		switch (open->op_createmode) {
 		case NFS4_CREATE_UNCHECKED:
 			if (!d_is_reg(child))
@@ -381,6 +381,10 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 	open->op_created = true;
 	fh_fill_post_attrs(fhp);
 
+	status = fh_compose(resfhp, fhp->fh_export, child, fhp);
+	if (status != nfs_ok)
+		goto out;
+
 	/* A newly created file already has a file size of zero. */
 	if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
 		iap->ia_valid &= ~ATTR_SIZE;
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
maintainer
#2 lore raw

Re: [PATCH 1/2] nfsd: fix possible fh_compose of wrong dentry in nfsd4_create_file()

From: Chuck Lever <[email protected]>

Quoted 10 lines · NeilBrown

dentry_create() can hypothetically provide a different dentry than the one passed in. This could happen, for example, if the exported filesystem is NFS, and the server return to OPEN a filehandle which matched a directory that was already in the dcache. Clearly this would not be expected!

If this were to happen the dentry (child) that was already stored in resfhp could be freed and later dereferenced.

[...]

Applied to nfsd-testing, thanks!

[1/2] nfsd: fix possible fh_compose of wrong dentry in nfsd4_create_file()

      commit: 2dfca2b82c375e2a534447331ecd7056cb139dce

[2/2] nfsd: ensure nfsd_file_to_acquire() does not use a non-opened file.
(no commit info)

--
Chuck Lever <[email protected]>

author
#3 lore raw

PATCH 2/2 nfsd: ensure nfsd_file_to_acquire() does not use a non-opened file.

In reply to NeilBrown

From: NeilBrown <[email protected]>

->atomic_open is permitted to return success without actually opening the file. It indicates this by calling finish_no_open(). This mean dentry_create() can return a file which hasn't been opened. This is extremely unlikely as ->atomic_open handlers typically use finish_no_open() only for already existing files, and dentry_create() isn't called in that case, and the parent being locked should prevent races.

However out of an abundance of caution it seems wise to teach nfsd to only use the file returned by dentry_create() if FMODE_OPENED is set, indicating that it has in fact been opened.

Fixes 64a989dbd144 ("VFS/knfsd: Teach dentry_create() to use atomic_open()")
---
 fs/nfsd/filecache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/nfsd/filecache.c +1 -1
index 24511c3208db..0a7df62c31d0 100644
@@ -1227,7 +1227,7 @@ nfsd_file_do_acquire(struct svc_rqst *rqstp, struct net *net,
 		nf->nf_mark = nfsd_file_mark_find_or_create(inode);
 
 	if (type != S_IFREG || nf->nf_mark) {
-		if (file) {
+		if (file && (file->f_mode & FMODE_OPENED)) {
 			get_file(file);
 			nf->nf_file = file;
 			status = nfs_ok;
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
maintainer Reviewed-by on v1 1/1
#4 lore raw
In reply to NeilBrown
Quoted 5 lines · NeilBrown
    https://sashiko.dev/#/patchset/[email protected]?part=1

reported a couple of edge-case problems with the use of atomic_open() in dentry_create(), called from nfsd4_create_file.

Quoted 7 lines

These patches attempt to address those issues.

NeilBrown

[PATCH 1/2] nfsd: fix possible fh_compose of wrong dentry in [PATCH 2/2] nfsd: ensure nfsd_file_to_acquire() does not use a

For both:

maintainer
#5 lore raw
In reply to NeilBrown
Quoted 5 lines · NeilBrown

https://sashiko.dev/#/patchset/[email protected]?part=1

reported a couple of edge-case problems with the use of atomic_open() in dentry_create(), called from nfsd4_create_file.

Quoted 7 lines

These patches attempt to address those issues.

NeilBrown

[PATCH 1/2] nfsd: fix possible fh_compose of wrong dentry in [PATCH 2/2] nfsd: ensure nfsd_file_to_acquire() does not use a

Hi Neil -

These do not apply to nfsd-testing. Where should I apply them?

Signature · 1 line
Chuck Lever
author
#6 lore raw
Quoted 12 lines · Chuck Lever
Quoted 10 lines · NeilBrown

https://sashiko.dev/#/patchset/[email protected]?part=1

reported a couple of edge-case problems with the use of atomic_open() in dentry_create(), called from nfsd4_create_file.

These patches attempt to address those issues.

NeilBrown

Quoted 6 lines
Quoted 2 lines

[PATCH 1/2] nfsd: fix possible fh_compose of wrong dentry in [PATCH 2/2] nfsd: ensure nfsd_file_to_acquire() does not use a

Hi Neil -

These do not apply to nfsd-testing. Where should I apply them?

I created them against nfsd-next, but I just tried applying them ti nfsd-testing with "patch -p1".
The first works perfectly. The second works with
Hunk #1 succeeded at 1181 (offset -46 lines).

Is that what you mean by "does not apply" ??

Thanks,
NeilBrown

maintainer
#7 lore raw
Quoted 18 lines · NeilBrown
Quoted 17 lines · Chuck Lever
Quoted 5 lines · NeilBrown

https://sashiko.dev/#/patchset/[email protected]?part=1

reported a couple of edge-case problems with the use of atomic_open() in dentry_create(), called from nfsd4_create_file.

Quoted 7 lines

These patches attempt to address those issues.

NeilBrown

[PATCH 1/2] nfsd: fix possible fh_compose of wrong dentry in [PATCH 2/2] nfsd: ensure nfsd_file_to_acquire() does not use a

Hi Neil -

Quoted 8 lines
Quoted 1 line

These do not apply to nfsd-testing. Where should I apply them?

I created them against nfsd-next, but I just tried applying them ti nfsd-testing with "patch -p1".
The first works perfectly. The second works with
Hunk #1 succeeded at 1181 (offset -46 lines).

Is that what you mean by "does not apply" ??

$ b4 am https://lore.kernel.org/ yada yada
$ stg import -M <mbox file>

The first patch applied, the second didn’t. Since you didn’t specify a base commit, it’s always best to ask.

Signature · 1 line
Chuck Lever
maintainer
#8 lore raw
Quoted 26 lines · Chuck Lever
Quoted 25 lines · NeilBrown
Quoted 12 lines · Chuck Lever
Quoted 10 lines · NeilBrown

https://sashiko.dev/#/patchset/[email protected]?part=1

reported a couple of edge-case problems with the use of atomic_open() in dentry_create(), called from nfsd4_create_file.

These patches attempt to address those issues.

NeilBrown

Quoted 6 lines
Quoted 2 lines

[PATCH 1/2] nfsd: fix possible fh_compose of wrong dentry in [PATCH 2/2] nfsd: ensure nfsd_file_to_acquire() does not use a

Hi Neil -

These do not apply to nfsd-testing. Where should I apply them?

I created them against nfsd-next, but I just tried applying them ti nfsd-testing with "patch -p1".
The first works perfectly. The second works with
Hunk #1 succeeded at 1181 (offset -46 lines).

Quoted 7 lines
Quoted 1 line

Is that what you mean by "does not apply" ??

$ b4 am https://lore.kernel.org/ yada yada
$ stg import -M <mbox file>

The first patch applied, the second didn’t. Since you didn’t specify a base commit, it’s always best to ask.

This comment is directed both to you and to the room:

The second patch doesn't apply to nfsd-testing without conflicts. It's not just a "Hunk succeeded" issue for me. That's a red flag.

Also, any tool that tries to retrieve these patches directly from lore (like "curl") is blocked by Anubis. So the "b4 am" then "git apply" or "stg import" flow is the only one that is reliable. Also "b4 am" does DKIM checking of the email sender.

I will apply these two by hand, but that really isn't scalable for me.

Going forward, I prefer that you base your patches on current nfsd-testing so that I can leverage the careful checking that all the scripts do. I believe that is how contribution is documented in nfsd-maintainer-entry-profile.rst.

Signature · 1 line
Chuck Lever
Reviewed-by on v1 1/1
#9 lore raw
In reply to NeilBrown
Quoted 5 lines · NeilBrown
    https://sashiko.dev/#/patchset/[email protected]?part=1

reported a couple of edge-case problems with the use of atomic_open() in dentry_create(), called from nfsd4_create_file.

Quoted 7 lines

These patches attempt to address those issues.

NeilBrown

[PATCH 1/2] nfsd: fix possible fh_compose of wrong dentry in [PATCH 2/2] nfsd: ensure nfsd_file_to_acquire() does not use a

Except for the subject in 2/2 ( _do_acquire), these look correct to me. Thanks Neil.

For both:

Ben