nfsd: restore owner override for truncate
Subsystems (3)
PATCH 1/1 nfsd: restore owner override for truncate
The switch to vfs_truncate in nfsd_setattr dropped the owner override used for NFS permissions. Add a copy of vfs_truncate with it restored to the nfsd code for now as it's very late in the cycle, but there should be a way to consolidate it back in the future.
--- fs/nfsd/vfs.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-)
fs/nfsd/vfs.c +80 -1
@@ -332,6 +332,85 @@ nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap) } } +/* copy of vfs_truncate with NFS owner override hacked in, sigh.. */ +static long nfsd_truncate (const struct path *path,loff_t length) +{ +struct inode *inode; +struct dentry *upperdentry; +long error; + + inode = path->dentry->d_inode; + +/* For directories it's -EISDIR, for other non-regulars - -EINVAL */ +if (S_ISDIR (inode->i_mode)) +return -EISDIR ; +if (!S_ISREG (inode->i_mode)) +return -EINVAL ; + + error =mnt_want_write (path->mnt); +if (error) + goto out; + +/* +* The file owner always gets access permission for accesses that +* would normally be checked at open time. This is to make +* file access work even when the client has done a fchmod(fd, 0). +* +* However, `cp foo bar' should fail nevertheless when bar is +* readonly. A sensible way to do this might be to reject all +* attempts to truncate a read-only file, because a creat() call +* always implies file truncation. +* ... but this isn't really fair. A process may reasonably call +* ftruncate on an open file descriptor on a file with perm 000. +* We must trust the client to do permission checking - using "ACCESS" +* with NFSv3. +*/ +if (!uid_eq (inode->i_uid,current_fsuid ())) { + error =inode_permission (inode,MAY_WRITE ); +if (error) + goto mnt_drop_write_and_out; + } + + error = -EPERM ; +if (IS_APPEND (inode)) + goto mnt_drop_write_and_out; + +/* +* If this is an overlayfs then do as if opening the file so we get +* write access on the upper inode, not on the overlay inode. For +* non-overlay filesystems d_real() is an identity function. +*/ + upperdentry =d_real (path->dentry,NULL ,O_WRONLY ); + error =PTR_ERR (upperdentry); +if (IS_ERR (upperdentry)) + goto mnt_drop_write_and_out; + + error =get_write_access (upperdentry->d_inode); +if (error) + goto mnt_drop_write_and_out; + +/* +* Make sure that there are no leases. get_write_access() protects +* against the truncate racing with a lease-granting setlease(). +*/ + error =break_lease (inode,O_WRONLY ); +if (error) + goto put_write_and_out; + + error =locks_verify_truncate (inode,NULL , length); +if (!error) + error =security_path_truncate (path); +if (!error) + error =do_truncate (path->dentry, length,0 ,NULL ); + +put_write_and_out: +put_write_access (upperdentry->d_inode); +mnt_drop_write_and_out: +mnt_drop_write (path->mnt); +out: +return error; +} +/* * Set various file attributes. After this call fhp needs an fh_put. */
@@ -398,7 +477,7 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap, ((iap->ia_valid & ~(ATTR_SIZE | ATTR_MTIME)) == 0)) implicit_mtime = true; - host_err =vfs_truncate (&path, iap->ia_size); + host_err =nfsd_truncate (&path, iap->ia_size);if (host_err) goto out_host_err;
Signature · 1 line
2.11.0
Quoted 8 lines · Christoph Hellwig
=20
The switch tovfs_truncateinnfsd_setattrdropped the owner override used for NFS permissions. Add a copy ofvfs_truncatewith it restored to the nfsd code for now as it's very late in the cycle, but there should be a way to consolidate it back in the future.
=20
Quoted 5 lines
Fixes 41f53350 ("nfsd: special case truncates some more")Signed-off-by Christoph Hellwig <[email protected]>Reported-by Chuck Lever <[email protected]>---
fs/nfsd/vfs.c| 81 =
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
fs/nfsd/vfs.c nfsd_sanitize_attrs(struct inode *inode, struct =
Quoted 7 lines
1 file changed, 80 insertions(+), 1 deletion(-)=20
fs/nfsd/vfs.c +0
-0@@ -332,6 +332,85 @@ nfsd_sanitize_attrs(struct inode *inode, struct =
iattr *iap)
fs/nfsd/vfs.c nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
Quoted 13 lines
fs/nfsd/vfs.c +0
-0} }=20
fs/nfsd/vfs.c +10
-0+/* copy of vfs_truncate with NFS owner override hacked in, sigh.. */ +static long nfsd_truncate (const struct path *path,loff_t length) +{ +struct inode *inode; +struct dentry *upperdentry; +long error; + + inode =3D path->dentry->d_inode; + +/* For directories it's -EISDIR, for other non-regulars - =
-EINVAL */
fs/nfsd/vfs.c nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
Quoted 11 lines
fs/nfsd/vfs.c +11
-0+if (S_ISDIR (inode->i_mode)) +return -EISDIR ; +if (!S_ISREG (inode->i_mode)) +return -EINVAL ; + + error =3D mnt_want_write(path->mnt); +if (error) + goto out; + +/* +* The file owner always gets access permission for accesses =
that
fs/nfsd/vfs.c
Quoted 2 lines
fs/nfsd/vfs.c +2
-0+* would normally be checked at open time. This is to make +* file access work even when the client has done a fchmod(fd, =
0).
fs/nfsd/vfs.c nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
Quoted 6 lines
fs/nfsd/vfs.c +6
-0+* +* However, `cp foo bar' should fail nevertheless when bar is +* readonly. A sensible way to do this might be to reject all +* attempts to truncate a read-only file, because a creat() call +* always implies file truncation. +* ... but this isn't really fair. A process may reasonably =
call
fs/nfsd/vfs.c
Quoted 2 lines
fs/nfsd/vfs.c +2
-0+* ftruncate on an open file descriptor on a file with perm 000. +* We must trust the client to do permission checking - using =
"ACCESS"
Quoted 4 lines
fs/nfsd/vfs.c +4
-0+* with NFSv3. +*/ +if (!uid_eq (inode->i_uid,current_fsuid ())) { + error =3D inode_permission(inode, MAY_WRITE);
fs/nfsd/vfs.c nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
Quoted 10 lines
fs/nfsd/vfs.c +10
-0+if (error) + goto mnt_drop_write_and_out; + } + + error =3D -EPERM ; +if (IS_APPEND (inode)) + goto mnt_drop_write_and_out; + +/* +* If this is an overlayfs then do as if opening the file so we =
get
fs/nfsd/vfs.c
Quoted 1 line
fs/nfsd/vfs.c +1
-0+* write access on the upper inode, not on the overlay inode. =
For
fs/nfsd/vfs.c nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
Quoted 13 lines
fs/nfsd/vfs.c +13
-0+* non-overlay filesystems d_real() is an identity function. +*/ + upperdentry =3D d_real(path->dentry, NULL, O_WRONLY); + error =3D PTR_ERR(upperdentry); +if (IS_ERR (upperdentry)) + goto mnt_drop_write_and_out; + + error =3D get_write_access(upperdentry->d_inode); +if (error) + goto mnt_drop_write_and_out; + +/* +* Make sure that there are no leases. get_write_access() =
protects
fs/nfsd/vfs.c nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
Quoted 11 lines
fs/nfsd/vfs.c +11
-0+* against the truncate racing with a lease-granting setlease(). +*/ + error =3D break_lease(inode, O_WRONLY); +if (error) + goto put_write_and_out; + + error =3D locks_verify_truncate(inode, NULL, length); +if (!error) + error =3D security_path_truncate(path); +if (!error) + error =3D do_truncate(path->dentry, length, 0, NULL);
do_truncate does not seem to be available in the EXPORT_SYMBOL pool.
fs/nfsd/vfs.c nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh =
Quoted 13 lines
fs/nfsd/vfs.c +9
-0+ +put_write_and_out: +put_write_access (upperdentry->d_inode); +mnt_drop_write_and_out: +mnt_drop_write (path->mnt); +out: +return error; +} +/*
* Set various file attributes. After this call fhp needs anfh_put. */fs/nfsd/vfs.c +0
-0@@ -398,7 +477,7 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh =
*fhp, struct iattr *iap,
fs/nfsd/vfs.c nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh = · line 477
Quoted 1 line
fs/nfsd/vfs.c +0
-0((iap->ia_valid & ~(ATTR_SIZE | ATTR_MTIME)) =3D=3D =
0))
Quoted 6 lines
fs/nfsd/vfs.c +0
-0implicit_mtime =3D true;=20
fs/nfsd/vfs.c +1
-1- host_err =3D vfs_truncate(&path, iap->ia_size); + host_err =3D nfsd_truncate(&path, iap->ia_size);if (host_err) goto out_host_err;
Quoted 6 lines
=20
--=20
2.11.0
=20
--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" =
in
Quoted 2 lines
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
--
Chuck Lever
[email protected]
Quoted 4 lines · Christoph Hellwig
The switch to
vfs_truncateinnfsd_setattrdropped the owner override used for NFS permissions. Add a copy ofvfs_truncatewith it restored to the nfsd code for now as it's very late in the cycle, but there should be a way to consolidate it back in the future.
This also needs:
fs/open.c +1 -0
@@ -64,6 +64,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,inode_unlock (dentry->d_inode);return ret; } +EXPORT_SYMBOL_GPL (do_truncate);long vfs_truncate (const struct path *path,loff_t length) {
--b.
Quoted 109 lines
Fixes 41f53350 ("nfsd: special case truncates some more")Signed-off-by Christoph Hellwig <[email protected]>Reported-by Chuck Lever <[email protected]>--- fs/nfsd/vfs.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-)fs/nfsd/vfs.c +80
-1@@ -332,6 +332,85 @@ nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap) } } +/* copy of vfs_truncate with NFS owner override hacked in, sigh.. */ +static long nfsd_truncate (const struct path *path,loff_t length) +{ +struct inode *inode; +struct dentry *upperdentry; +long error; + + inode = path->dentry->d_inode; + +/* For directories it's -EISDIR, for other non-regulars - -EINVAL */ +if (S_ISDIR (inode->i_mode)) +return -EISDIR ; +if (!S_ISREG (inode->i_mode)) +return -EINVAL ; + + error =mnt_want_write (path->mnt); +if (error) + goto out; + +/* +* The file owner always gets access permission for accesses that +* would normally be checked at open time. This is to make +* file access work even when the client has done a fchmod(fd, 0). +* +* However, `cp foo bar' should fail nevertheless when bar is +* readonly. A sensible way to do this might be to reject all +* attempts to truncate a read-only file, because a creat() call +* always implies file truncation. +* ... but this isn't really fair. A process may reasonably call +* ftruncate on an open file descriptor on a file with perm 000. +* We must trust the client to do permission checking - using "ACCESS" +* with NFSv3. +*/ +if (!uid_eq (inode->i_uid,current_fsuid ())) { + error =inode_permission (inode,MAY_WRITE ); +if (error) + goto mnt_drop_write_and_out; + } + + error = -EPERM ; +if (IS_APPEND (inode)) + goto mnt_drop_write_and_out; + +/* +* If this is an overlayfs then do as if opening the file so we get +* write access on the upper inode, not on the overlay inode. For +* non-overlay filesystems d_real() is an identity function. +*/ + upperdentry =d_real (path->dentry,NULL ,O_WRONLY ); + error =PTR_ERR (upperdentry); +if (IS_ERR (upperdentry)) + goto mnt_drop_write_and_out; + + error =get_write_access (upperdentry->d_inode); +if (error) + goto mnt_drop_write_and_out; + +/* +* Make sure that there are no leases. get_write_access() protects +* against the truncate racing with a lease-granting setlease(). +*/ + error =break_lease (inode,O_WRONLY ); +if (error) + goto put_write_and_out; + + error =locks_verify_truncate (inode,NULL , length); +if (!error) + error =security_path_truncate (path); +if (!error) + error =do_truncate (path->dentry, length,0 ,NULL ); + +put_write_and_out: +put_write_access (upperdentry->d_inode); +mnt_drop_write_and_out: +mnt_drop_write (path->mnt); +out: +return error; +} +/* * Set various file attributes. After this call fhp needs an fh_put. */ @@ -398,7 +477,7 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap, ((iap->ia_valid & ~(ATTR_SIZE | ATTR_MTIME)) == 0)) implicit_mtime = true; - host_err =vfs_truncate (&path, iap->ia_size); + host_err =nfsd_truncate (&path, iap->ia_size);if (host_err) goto out_host_err;Signature · 1 line
2.11.0
Quoted 7 lines · J. Bruce Fields
Quoted 4 lines · Christoph Hellwig
The switch to
vfs_truncateinnfsd_setattrdropped the owner override used for NFS permissions. Add a copy ofvfs_truncatewith it restored to the nfsd code for now as it's very late in the cycle, but there should be a way to consolidate it back in the future.This also needs:
Yes, indeed.
Chuck Lever
Tested-byQuoted 2 lines
On Feb 9, 2017, at 11:01 AM, J. Bruce Fields <[email protected]> =
Quoted 4 lines · wrote:
=20
Quoted 2 lines · Christoph Hellwig
The switch to
vfs_truncateinnfsd_setattrdropped the owner override used for NFS permissions. Add a copy ofvfs_truncatewith it =
restored
fs/open.c int do_truncate(struct dentry *dentry, loff_t =
Quoted 11 lines
Quoted 2 lines
to the nfsd code for now as it's very late in the cycle, but there should be a way to consolidate it back in the future.
=20
This also needs:
=20
=20fs/open.c +0
-0@@ -64,6 +64,7 @@ int do_truncate(struct dentry *dentry, loff_t =
length, unsigned int time_attrs,
Quoted 7 lines
fs/open.c +1
-0inode_unlock (dentry->d_inode);return ret; } +EXPORT_SYMBOL_GPL (do_truncate);=20
longvfs_truncate(const struct path *path, loff_t length){
After adding this change, I confirmed that t1050-large.sh is working as expected.
Quoted 8 lines
--b.
=20Quoted 6 lines
=20
Fixes 41f53350 ("nfsd: special case truncates some more")Signed-off-by Christoph Hellwig <[email protected]>Reported-by Chuck Lever <[email protected]>---
fs/nfsd/vfs.c| 81 =
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Quoted 7 lines
fs/nfsd/vfs.c nfsd_sanitize_attrs(struct inode *inode, struct =
Quoted 7 lines
1 file changed, 80 insertions(+), 1 deletion(-)=20
fs/nfsd/vfs.c +0
-0@@ -332,6 +332,85 @@ nfsd_sanitize_attrs(struct inode *inode, struct =
iattr *iap)
Quoted 5 lines
Quoted 5 lines
fs/nfsd/vfs.c +0
-0} }=20
fs/nfsd/vfs.c +2
-0+/* copy of vfs_truncate with NFS owner override hacked in, sigh.. */ +static long nfsd_truncate (const struct path *path,loff_t length)
Quoted 8 lines
fs/nfsd/vfs.c
Quoted 8 lines
fs/nfsd/vfs.c +8
-0+{ +struct inode *inode; +struct dentry *upperdentry; +long error; + + inode =3D path->dentry->d_inode; + +/* For directories it's -EISDIR, for other non-regulars - =
-EINVAL */
Quoted 11 lines
fs/nfsd/vfs.c
Quoted 11 lines
fs/nfsd/vfs.c +11
-0+if (S_ISDIR (inode->i_mode)) +return -EISDIR ; +if (!S_ISREG (inode->i_mode)) +return -EINVAL ; + + error =3D mnt_want_write(path->mnt); +if (error) + goto out; + +/* +* The file owner always gets access permission for accesses =
that
Quoted 2 lines
fs/nfsd/vfs.c
Quoted 2 lines
fs/nfsd/vfs.c +2
-0+* would normally be checked at open time. This is to make +* file access work even when the client has done a fchmod(fd, =
0).
Quoted 6 lines
fs/nfsd/vfs.c
Quoted 6 lines
fs/nfsd/vfs.c +6
-0+* +* However, `cp foo bar' should fail nevertheless when bar is +* readonly. A sensible way to do this might be to reject all +* attempts to truncate a read-only file, because a creat() call +* always implies file truncation. +* ... but this isn't really fair. A process may reasonably =
call
Quoted 2 lines
fs/nfsd/vfs.c
Quoted 2 lines
fs/nfsd/vfs.c +2
-0+* ftruncate on an open file descriptor on a file with perm 000. +* We must trust the client to do permission checking - using =
"ACCESS"
Quoted 6 lines
Quoted 6 lines
fs/nfsd/vfs.c +6
-0+* with NFSv3. +*/ +if (!uid_eq (inode->i_uid,current_fsuid ())) { + error =3D inode_permission(inode, MAY_WRITE); +if (error) + goto mnt_drop_write_and_out;
Quoted 8 lines
fs/nfsd/vfs.c
Quoted 8 lines
fs/nfsd/vfs.c +8
-0+ } + + error =3D -EPERM ; +if (IS_APPEND (inode)) + goto mnt_drop_write_and_out; + +/* +* If this is an overlayfs then do as if opening the file so we =
get
Quoted 1 line
fs/nfsd/vfs.c
Quoted 1 line
fs/nfsd/vfs.c +1
-0+* write access on the upper inode, not on the overlay inode. =
For
Quoted 5 lines
Quoted 5 lines
fs/nfsd/vfs.c +5
-0+* non-overlay filesystems d_real() is an identity function. +*/ + upperdentry =3D d_real(path->dentry, NULL, O_WRONLY); + error =3D PTR_ERR(upperdentry); +if (IS_ERR (upperdentry))
Quoted 8 lines
fs/nfsd/vfs.c
Quoted 8 lines
fs/nfsd/vfs.c +8
-0+ goto mnt_drop_write_and_out; + + error =3D get_write_access(upperdentry->d_inode); +if (error) + goto mnt_drop_write_and_out; + +/* +* Make sure that there are no leases. get_write_access() =
protects
Quoted 16 lines
Quoted 16 lines
fs/nfsd/vfs.c +16
-0+* against the truncate racing with a lease-granting setlease(). +*/ + error =3D break_lease(inode, O_WRONLY); +if (error) + goto put_write_and_out; + + error =3D locks_verify_truncate(inode, NULL, length); +if (!error) + error =3D security_path_truncate(path); +if (!error) + error =3D do_truncate(path->dentry, length, 0, NULL); + +put_write_and_out: +put_write_access (upperdentry->d_inode); +mnt_drop_write_and_out: +mnt_drop_write (path->mnt);
Quoted 8 lines
fs/nfsd/vfs.c nfsd_setattr(struct svc_rqst *rqstp, struct =
Quoted 8 lines
fs/nfsd/vfs.c +4
-0+out: +return error; +} +/*
* Set various file attributes. After this call fhp needs anfh_put. */fs/nfsd/vfs.c +0
-0@@ -398,7 +477,7 @@ nfsd_setattr(struct svc_rqst *rqstp, struct =
svc_fh *fhp, struct iattr *iap,
Quoted 1 line
fs/nfsd/vfs.c nfsd_setattr(struct svc_rqst *rqstp, struct = · line 477
Quoted 1 line
fs/nfsd/vfs.c +0
-0((iap->ia_valid & ~(ATTR_SIZE | ATTR_MTIME)) =3D=3D =
0))
Quoted 11 lines
Quoted 9 lines
fs/nfsd/vfs.c +0
-0implicit_mtime =3D true;=20
fs/nfsd/vfs.c +1
-1- host_err =3D vfs_truncate(&path, iap->ia_size); + host_err =3D nfsd_truncate(&path, iap->ia_size);if (host_err) goto out_host_err;=20
--=20
2.11.0--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" =
in
Quoted 2 lines
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
--
Chuck Lever
[email protected]
Quoted 14 lines · J. Bruce Fields
Quoted 4 lines · Christoph Hellwig
The switch to
vfs_truncateinnfsd_setattrdropped the owner override used for NFS permissions. Add a copy ofvfs_truncatewith it restored to the nfsd code for now as it's very late in the cycle, but there should be a way to consolidate it back in the future.This also needs:
fs/open.c +0
-0
fs/open.c int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs, · line 70
Quoted 7 lines
fs/open.c +1
-0@@ -64,6 +64,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,inode_unlock (dentry->d_inode);return ret; } +EXPORT_SYMBOL_GPL (do_truncate);long vfs_truncate (const struct path *path,loff_t length) {
Also there's still a warning about a nested mnt_want_write when called from nfsd4_setattr.
Probably not hard to fix.
But at this point I'd like to revert the original 4af53350 "nfsd:
special case truncates some more". It fixed a real bug (incorrect handling of setattrs with both mode and file size), but a bug we've had for a long time. It can wait another week or two for us to get this all right.
--b.
kbuild test robot
build botreproduce: wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree…
Hi Christoph,
[auto build test ERROR on nfsd/nfsd-next]
[also build test ERROR on v4.10-rc7 next-20170209]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Christoph-Hellwig/nfsd-restore-owner-override-for-truncate/20170210-035117 base: git://linux-nfs.org/~bfields/linux.git nfsd-next config: powerpc-mvme5100_defconfig (attached as .config) compiler: powerpc-linux-gnu-gcc (Debian 6.1.1-9) 6.1.1 20160705
reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=powerpc
All errors (new ones prefixed by >>):
>> ERROR: "do_truncate" [fs/nfsd/nfsd.ko] undefined!
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation