Skip to contentSkip to conversation

nfsd: make a copy of struct iattr before calling notify_change

PATCH In review Jeff Layton 4 participants 11 messages May 17, 2023 – May 23, 2023

Subsystems (2)
lore
Share & tools
lore

Jeff Layton

author maintainer
#0 lore raw

PATCH 1/1 nfsd: make a copy of struct iattr before calling notify_change

notify_change can modify the iattr structure. In particular it can can end up setting ATTR_MODE when ATTR_KILL_SUID is already set, causing a BUG() if the same iattr is passed to notify_change more than once.

Make a copy of the struct iattr before calling notify_change.

Fixes 34b91dda7124 NFSD: Make nfsd4_setattr() wait before returning NFS4ERR_DELAY
---
 fs/nfsd/vfs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/nfsd/vfs.c +3 -1
index c4ef24c5ffd0..ad0c5cd900b1 100644
@@ -538,7 +538,9 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 
 	inode_lock(inode);
 	for (retries = 1;;) {
-		host_err = __nfsd_setattr(dentry, iap);
+		struct iattr attrs = *iap;
+
+		host_err = __nfsd_setattr(dentry, &attrs);
 		if (host_err != -EAGAIN || !retries--)
 			break;
 		if (!nfsd_wait_for_delegreturn(rqstp, inode))
Signature · 1 line
2.40.1
#1 lore raw
Quoted 23 lines · Jeff Layton

notify_change can modify the iattr structure. In particular it can can end up setting ATTR_MODE when ATTR_KILL_SUID is already set, causing a BUG() if the same iattr is passed to notify_change more than once.

Make a copy of the struct iattr before calling notify_change.

Fixes 34b91dda7124 NFSD: Make nfsd4_setattr() wait before returning NFS4ERR_DELAY

---
fs/nfsd/vfs.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
fs/nfsd/vfs.c +0 -0
index c4ef24c5ffd0..ad0c5cd900b1 100644

fs/nfsd/vfs.c nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, · line 541

Quoted 5 lines
fs/nfsd/vfs.c +1 -1
@@ -538,7 +538,9 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 
 inode_lock(inode);
 for (retries = 1;;) {
- host_err = __nfsd_setattr(dentry, iap);
+ struct iattr attrs = *iap;

This construct always makes me queazy. I'm never sure if an initializer inside a loop is "only once" or "every time". I fixed a bug like this once.

But if you've tested it and it addresses the BUG, then let's go with this. I can apply it to nfsd-fixes.

Quoted 8 lines
fs/nfsd/vfs.c +2 -0
+
+ host_err = __nfsd_setattr(dentry, &attrs);
 if (host_err != -EAGAIN || !retries--)
 break;
 if (!nfsd_wait_for_delegreturn(rqstp, inode))
Signature · 1 line
2.40.1

--
Chuck Lever

author maintainer
#2 lore raw
Quoted 32 lines · Chuck Lever III
Quoted 22 lines · Jeff Layton

notify_change can modify the iattr structure. In particular it can can end up setting ATTR_MODE when ATTR_KILL_SUID is already set, causing a BUG() if the same iattr is passed to notify_change more than once.

Make a copy of the struct iattr before calling notify_change.

Fixes 34b91dda7124 NFSD: Make nfsd4_setattr() wait before returning NFS4ERR_DELAY

---
fs/nfsd/vfs.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
fs/nfsd/vfs.c +0 -0
index c4ef24c5ffd0..ad0c5cd900b1 100644

fs/nfsd/vfs.c nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, · line 541

Quoted 5 lines
fs/nfsd/vfs.c +1 -1
@@ -538,7 +538,9 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 
 inode_lock(inode);
 for (retries = 1;;) {
- host_err = __nfsd_setattr(dentry, iap);
+ struct iattr attrs = *iap;

This construct always makes me queazy. I'm never sure if an initializer inside a loop is "only once" or "every time". I fixed a bug like this once.

Quoted 3 lines

But if you've tested it and it addresses the BUG, then let's go with this. I can apply it to nfsd-fixes.

I've done some light testing with this kernel, but this was found by Zhi while testing with the lustre racer test, so it involves some raciness. I've never hit this myself.

I'm pretty sure though that this has to be initialized every time. The assignment is inside the loop, after all. I'm ok with moving the assignment to a different line if you like though:

	struct iattr attrs;

	attrs = *iap;
	...
Quoted 13 lines
Quoted 8 lines
fs/nfsd/vfs.c +2 -0
+
+ host_err = __nfsd_setattr(dentry, &attrs);
 if (host_err != -EAGAIN || !retries--)
 break;
 if (!nfsd_wait_for_delegreturn(rqstp, inode))
Signature · 1 line
2.40.1

--
Chuck Lever

Signature · 1 line
Jeff Layton <[email protected]>
#3 lore raw
Quoted 37 lines · Jeff Layton
Quoted 32 lines · Chuck Lever III
Quoted 22 lines · Jeff Layton

notify_change can modify the iattr structure. In particular it can can end up setting ATTR_MODE when ATTR_KILL_SUID is already set, causing a BUG() if the same iattr is passed to notify_change more than once.

Make a copy of the struct iattr before calling notify_change.

Fixes 34b91dda7124 NFSD: Make nfsd4_setattr() wait before returning NFS4ERR_DELAY

---
fs/nfsd/vfs.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
fs/nfsd/vfs.c +0 -0
index c4ef24c5ffd0..ad0c5cd900b1 100644

fs/nfsd/vfs.c nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, · line 541

Quoted 5 lines
fs/nfsd/vfs.c +1 -1
@@ -538,7 +538,9 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 
 inode_lock(inode);
 for (retries = 1;;) {
- host_err = __nfsd_setattr(dentry, iap);
+ struct iattr attrs = *iap;

This construct always makes me queazy. I'm never sure if an initializer inside a loop is "only once" or "every time". I fixed a bug like this once.

Quoted 8 lines
Quoted 3 lines

But if you've tested it and it addresses the BUG, then let's go with this. I can apply it to nfsd-fixes.

I've done some light testing with this kernel, but this was found by Zhi while testing with the lustre racer test, so it involves some raciness. I've never hit this myself.

Has Zhi tested this fix?

Quoted 8 lines

I'm pretty sure though that this has to be initialized every time. The assignment is inside the loop, after all. I'm ok with moving the assignment to a different line if you like though:

struct iattr attrs;

attrs = *iap;
...

Yeah I could do that. I find that easier to read when a loop is involved; it's unambiguous then what is going on.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
fs/nfsd/vfs.c +2 -0
+
+ host_err = __nfsd_setattr(dentry, &attrs);
 if (host_err != -EAGAIN || !retries--)
 break;
 if (!nfsd_wait_for_delegreturn(rqstp, inode))
Signature · 1 line
2.40.1
Quoted 7 lines
Quoted 4 lines

--
Chuck Lever

Signature · 1 line
Jeff Layton <[email protected]>

--
Chuck Lever

author maintainer
#4 lore raw
Quoted 41 lines · Chuck Lever III
Quoted 41 lines · Jeff Layton
Quoted 35 lines · Chuck Lever III
Quoted 22 lines · Jeff Layton

notify_change can modify the iattr structure. In particular it can can end up setting ATTR_MODE when ATTR_KILL_SUID is already set, causing a BUG() if the same iattr is passed to notify_change more than once.

Make a copy of the struct iattr before calling notify_change.

Fixes 34b91dda7124 NFSD: Make nfsd4_setattr() wait before returning NFS4ERR_DELAY

---
fs/nfsd/vfs.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
fs/nfsd/vfs.c +0 -0
index c4ef24c5ffd0..ad0c5cd900b1 100644

fs/nfsd/vfs.c nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, · line 541

Quoted 5 lines
fs/nfsd/vfs.c +1 -1
@@ -538,7 +538,9 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 
 inode_lock(inode);
 for (retries = 1;;) {
- host_err = __nfsd_setattr(dentry, iap);
+ struct iattr attrs = *iap;

This construct always makes me queazy. I'm never sure if an initializer inside a loop is "only once" or "every time". I fixed a bug like this once.

But if you've tested it and it addresses the BUG, then let's go with this. I can apply it to nfsd-fixes.

Quoted 6 lines
Quoted 3 lines

I've done some light testing with this kernel, but this was found by Zhi while testing with the lustre racer test, so it involves some raciness. I've never hit this myself.

Has Zhi tested this fix?

Not yet. I just cooked it up this morning. I built a test kernel but testing it will take some time since it depends on load.

Quoted 5 lines
Quoted 5 lines

I'm pretty sure though that this has to be initialized every time. The assignment is inside the loop, after all. I'm ok with moving the assignment to a different line if you like though:

Quoted 8 lines
Quoted 4 lines

struct iattr attrs;

attrs = *iap;
...

Yeah I could do that. I find that easier to read when a loop is involved; it's unambiguous then what is going on.

Your call. I'm fairly certain that the patch does the right thing as-is, but if you think it makes it more readable, then OK.

Signature · 1 line
Jeff Layton <[email protected]>
author maintainer Tested-by Zhi Li on v1 1/1
#5 lore raw
In reply to Jeff Layton
Quoted 19 lines · Jeff Layton

notify_change can modify the iattr structure. In particular it can can end up setting ATTR_MODE when ATTR_KILL_SUID is already set, causing a BUG() if the same iattr is passed to notify_change more than once.

Make a copy of the struct iattr before calling notify_change.

Fixes 34b91dda7124 NFSD: Make nfsd4_setattr() wait before returning NFS4ERR_DELAY
---
 fs/nfsd/vfs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/nfsd/vfs.c +0 -0
index c4ef24c5ffd0..ad0c5cd900b1 100644

fs/nfsd/vfs.c nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, · line 546

Quoted 10 lines
fs/nfsd/vfs.c +3 -1
@@ -538,7 +538,9 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 
 	inode_lock(inode);
 	for (retries = 1;;) {
-		host_err = __nfsd_setattr(dentry, iap);
+		struct iattr attrs = *iap;
+
+		host_err = __nfsd_setattr(dentry, &attrs);
 		if (host_err != -EAGAIN || !retries--)
 			break;
 		if (!nfsd_wait_for_delegreturn(rqstp, inode))

Zhi Li tested the test kernel for this today and this seems to have fixed the issue. I think you can add:

Cheers,
Jeff

#6 lore raw
Quoted 38 lines · Jeff Layton
Quoted 19 lines · Jeff Layton

notify_change can modify the iattr structure. In particular it can can end up setting ATTR_MODE when ATTR_KILL_SUID is already set, causing a BUG() if the same iattr is passed to notify_change more than once.

Make a copy of the struct iattr before calling notify_change.

Fixes 34b91dda7124 NFSD: Make nfsd4_setattr() wait before returning NFS4ERR_DELAY

---
fs/nfsd/vfs.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
fs/nfsd/vfs.c +0 -0
index c4ef24c5ffd0..ad0c5cd900b1 100644

fs/nfsd/vfs.c nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, · line 546

Quoted 10 lines
fs/nfsd/vfs.c +3 -1
@@ -538,7 +538,9 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 
 inode_lock(inode);
 for (retries = 1;;) {
- host_err = __nfsd_setattr(dentry, iap);
+ struct iattr attrs = *iap;
+
+ host_err = __nfsd_setattr(dentry, &attrs);
 if (host_err != -EAGAIN || !retries--)
 break;
 if (!nfsd_wait_for_delegreturn(rqstp, inode))

Zhi Li tested the test kernel for this today and this seems to have fixed the issue. I think you can add:

Quoted 1 line

Thanks to you both. Applied to nfsd-fixes for the next 6.4-rc PR.

--
Chuck Lever

#7 lore raw
In reply to Jeff Layton
Quoted 19 lines · Jeff Layton

notify_change can modify the iattr structure. In particular it can can end up setting ATTR_MODE when ATTR_KILL_SUID is already set, causing a BUG() if the same iattr is passed to notify_change more than once.

Make a copy of the struct iattr before calling notify_change.

Fixes 34b91dda7124 NFSD: Make nfsd4_setattr() wait before returning NFS4ERR_DELAY
---
 fs/nfsd/vfs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/nfsd/vfs.c +0 -0
index c4ef24c5ffd0..ad0c5cd900b1 100644

fs/nfsd/vfs.c nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, · line 543

Quoted 7 lines
fs/nfsd/vfs.c +3 -1
@@ -538,7 +538,9 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 
 	inode_lock(inode);
 	for (retries = 1;;) {
-		host_err = __nfsd_setattr(dentry, iap);
+		struct iattr attrs = *iap;
+
+		host_err = __nfsd_setattr(dentry, &attrs);

I think this needs something to ensure a well meaning by-passer doesn't try to "optimise" it back to the way it was.
Maybe make "iap" const? Or add a comment? Or both?

NeilBrown

Quoted 7 lines
fs/nfsd/vfs.c +0 -0
 		if (host_err != -EAGAIN || !retries--)
 			break;
 		if (!nfsd_wait_for_delegreturn(rqstp, inode))
Signature · 1 line
2.40.1
author maintainer
#8 lore raw
Quoted 21 lines · NeilBrown
Quoted 20 lines · Jeff Layton

notify_change can modify the iattr structure. In particular it can can end up setting ATTR_MODE when ATTR_KILL_SUID is already set, causing a BUG() if the same iattr is passed to notify_change more than once.

Make a copy of the struct iattr before calling notify_change.

Fixes 34b91dda7124 NFSD: Make nfsd4_setattr() wait before returning NFS4ERR_DELAY
---
 fs/nfsd/vfs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/nfsd/vfs.c +0 -0
index c4ef24c5ffd0..ad0c5cd900b1 100644
@@ -538,7 +538,9 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 
Quoted 11 lines

fs/nfsd/vfs.c nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, · line 543

Quoted 6 lines
fs/nfsd/vfs.c +3 -1
 	inode_lock(inode);
 	for (retries = 1;;) {
-		host_err = __nfsd_setattr(dentry, iap);
+		struct iattr attrs = *iap;
+
+		host_err = __nfsd_setattr(dentry, &attrs);

I think this needs something to ensure a well meaning by-passer doesn't try to "optimise" it back to the way it was.
Maybe make "iap" const? Or add a comment? Or both?

We can't make iap const, as we have to call nfsd_sanitize_attrs on it, and that will change things in it. I think we'll probably have to settle for a comment. Chuck, can we fold the patch below in to this one?

--------------------8<------------------

fs/nfsd/vfs.c +5 -0
index 2a3687cdf926..817effd63730 100644
@@ -538,6 +538,11 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 	for (retries = 1;;) {
 		struct iattr attrs;
 
+		/*
+		 * notify_change can alter the iattr in ways that make it
+		 * unsuitable for submission multiple times. Make a copy
+		 * for every loop.
+		 */
 		attrs = *iap;
 		host_err = __nfsd_setattr(dentry, &attrs);
 		if (host_err != -EAGAIN || !retries--)
maintainer
#9 lore raw
Quoted 34 lines · Jeff Layton
Quoted 21 lines · NeilBrown
Quoted 20 lines · Jeff Layton

notify_change can modify the iattr structure. In particular it can can end up setting ATTR_MODE when ATTR_KILL_SUID is already set, causing a BUG() if the same iattr is passed to notify_change more than once.

Make a copy of the struct iattr before calling notify_change.

Fixes 34b91dda7124 NFSD: Make nfsd4_setattr() wait before returning NFS4ERR_DELAY
---
 fs/nfsd/vfs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/nfsd/vfs.c +0 -0
index c4ef24c5ffd0..ad0c5cd900b1 100644
@@ -538,7 +538,9 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 
Quoted 11 lines

fs/nfsd/vfs.c nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, · line 543

Quoted 6 lines
fs/nfsd/vfs.c +3 -1
 	inode_lock(inode);
 	for (retries = 1;;) {
-		host_err = __nfsd_setattr(dentry, iap);
+		struct iattr attrs = *iap;
+
+		host_err = __nfsd_setattr(dentry, &attrs);

I think this needs something to ensure a well meaning by-passer doesn't try to "optimise" it back to the way it was.
Maybe make "iap" const? Or add a comment? Or both?

Quoted 3 lines

We can't make iap const, as we have to call nfsd_sanitize_attrs on it, and that will change things in it. I think we'll probably have to settle for a comment. Chuck, can we fold the patch below in to this one?

Done, and pushed to nfsd-fixes.

Quoted 19 lines

--------------------8<------------------

fs/nfsd/vfs.c +5 -0
index 2a3687cdf926..817effd63730 100644
@@ -538,6 +538,11 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 	for (retries = 1;;) {
 		struct iattr attrs;
 
+		/*
+		 * notify_change can alter the iattr in ways that make it
+		 * unsuitable for submission multiple times. Make a copy
+		 * for every loop.
+		 */
 		attrs = *iap;
 		host_err = __nfsd_setattr(dentry, &attrs);
 		if (host_err != -EAGAIN || !retries--)
#10 lore raw
In reply to Jeff Layton
Quoted 34 lines · Jeff Layton
Quoted 21 lines · NeilBrown
Quoted 20 lines · Jeff Layton

notify_change can modify the iattr structure. In particular it can can end up setting ATTR_MODE when ATTR_KILL_SUID is already set, causing a BUG() if the same iattr is passed to notify_change more than once.

Make a copy of the struct iattr before calling notify_change.

Fixes 34b91dda7124 NFSD: Make nfsd4_setattr() wait before returning NFS4ERR_DELAY
---
 fs/nfsd/vfs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/nfsd/vfs.c +0 -0
index c4ef24c5ffd0..ad0c5cd900b1 100644
@@ -538,7 +538,9 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 
Quoted 11 lines

fs/nfsd/vfs.c nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, · line 543

Quoted 6 lines
fs/nfsd/vfs.c +3 -1
 	inode_lock(inode);
 	for (retries = 1;;) {
-		host_err = __nfsd_setattr(dentry, iap);
+		struct iattr attrs = *iap;
+
+		host_err = __nfsd_setattr(dentry, &attrs);

I think this needs something to ensure a well meaning by-passer doesn't try to "optimise" it back to the way it was.
Maybe make "iap" const? Or add a comment? Or both?

Quoted 3 lines

We can't make iap const, as we have to call nfsd_sanitize_attrs on it, and that will change things in it. I think we'll probably have to settle for a comment. Chuck, can we fold the patch below in to this one?

Thanks :-)

NeilBrown

Quoted 21 lines

--------------------8<------------------

fs/nfsd/vfs.c +5 -0
index 2a3687cdf926..817effd63730 100644
@@ -538,6 +538,11 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 	for (retries = 1;;) {
 		struct iattr attrs;
 
+		/*
+		 * notify_change can alter the iattr in ways that make it
+		 * unsuitable for submission multiple times. Make a copy
+		 * for every loop.
+		 */
 		attrs = *iap;
 		host_err = __nfsd_setattr(dentry, &attrs);
 		if (host_err != -EAGAIN || !retries--)