Use exclusive lock for file_remove_privs
Subsystems (2)
v2 cover Use exclusive lock for file_remove_privs
While adding shared direct IO write locks to fuse Miklos noticed that file_remove_privs() needs an exclusive lock. I then noticed that btrfs actually has the same issue as I had in my patch, it was calling into that function with a shared lock. This series adds a new exported function file_needs_remove_privs(), which used by the follow up btrfs patch and will be used by the DIO code path in fuse as well. If that function returns any mask the shared lock needs to be dropped and replaced by the exclusive variant.
Note: Compilation tested only.
v2:
Already check for IS_NOSEC in btrfs_direct_write before the first lock is taken.
Slight modification to make the code easier to read (boolean pointer is passed to btrfs_write_check, instead of flags).
Bernd Schubert (2):
fs: Add and export file_needs_remove_privs
btrfs: file_remove_privs needs an exclusive lock
fs/btrfs/file.c | 37 +++++++++++++++++++++++++++++-------- fs/inode.c | 8 ++++++++ include/linux/fs.h | 1 + 3 files changed, 38 insertions(+), 8 deletions(-)
Signature · 1 line
2.39.2
PATCH v2 1/2 fs: Add and export file_needs_remove_privs
File systems want to hold a shared lock for DIO writes, but may need to drop file priveliges - that a requires an exclusive lock. The new export function file_needs_remove_privs() is added in order to first check if that is needed.
--- fs/inode.c | 8 ++++++++ include/linux/fs.h | 1 + 2 files changed, 9 insertions(+)
fs/inode.c +8 -0
@@ -2013,6 +2013,14 @@ int dentry_needs_remove_privs(struct mnt_idmap *idmap,return mask; } +int file_needs_remove_privs (struct file *file) +{ +struct dentry *dentry =file_dentry (file); + +return dentry_needs_remove_privs (file_mnt_idmap (file), dentry); +} +EXPORT_SYMBOL_GPL (file_needs_remove_privs); +static int __remove_privs (struct mnt_idmap *idmap,struct dentry *dentry,int kill) {
include/linux/fs.h +1 -0
@@ -2721,6 +2721,7 @@ extern struct inode *new_inode_pseudo(struct super_block *sb);extern struct inode *new_inode (struct super_block *sb);extern void free_inode_nonrcu (struct inode *inode);extern int setattr_should_drop_suidgid (struct mnt_idmap *,struct inode *); +int file_needs_remove_privs (struct file *);extern int file_remove_privs (struct file *);int setattr_should_drop_sgid (struct mnt_idmap *idmap,const struct inode *inode);
Signature · 1 line
2.39.2
Christian Brauner
maintainerRe: [PATCH 1/2] fs: Add and export file_needs_remove_privs
Quoted 2 lines · Bernd Schubert
File systems want to hold a shared lock for DIO writes, but may need to drop file priveliges - that a requires an
s/priveliges/privileges/
s/that a requires/that requires/
Quoted 50 lines
exclusive lock. The new export function
file_needs_remove_privs()is added in order to first check if that is needed.Cc Miklos Szeredi <[email protected]>Cc Dharmendra Singh <[email protected]>Cc Josef Bacik <[email protected]>Cc Alexander Viro <[email protected]>Cc Christian Brauner <[email protected]>Signed-off-by Bernd Schubert <[email protected]>--- fs/inode.c | 8 ++++++++ include/linux/fs.h | 1 + 2 files changed, 9 insertions(+)fs/inode.c +8
-0@@ -2013,6 +2013,14 @@ int dentry_needs_remove_privs(struct mnt_idmap *idmap,return mask; } +int file_needs_remove_privs (struct file *file) +{ +struct dentry *dentry =file_dentry (file); + +return dentry_needs_remove_privs (file_mnt_idmap (file), dentry); +} +EXPORT_SYMBOL_GPL (file_needs_remove_privs); +static int __remove_privs (struct mnt_idmap *idmap,struct dentry *dentry,int kill) {include/linux/fs.h +1
-0@@ -2721,6 +2721,7 @@ extern struct inode *new_inode_pseudo(struct super_block *sb);extern struct inode *new_inode (struct super_block *sb);extern void free_inode_nonrcu (struct inode *inode);extern int setattr_should_drop_suidgid (struct mnt_idmap *,struct inode *); +int file_needs_remove_privs (struct file *);extern int file_remove_privs (struct file *);int setattr_should_drop_sgid (struct mnt_idmap *idmap,const struct inode *inode);Signature · 1 line
2.39.2
Christian Brauner
maintainerRe: [PATCH 1/2] fs: Add and export file_needs_remove_privs
In reply to Bernd SchubertQuoted 23 lines · Bernd Schubert
File systems want to hold a shared lock for DIO writes, but may need to drop file priveliges - that a requires an exclusive lock. The new export function
file_needs_remove_privs()is added in order to first check if that is needed.Cc Miklos Szeredi <[email protected]>Cc Dharmendra Singh <[email protected]>Cc Josef Bacik <[email protected]>Cc Alexander Viro <[email protected]>Cc Christian Brauner <[email protected]>Signed-off-by Bernd Schubert <[email protected]>--- fs/inode.c | 8 ++++++++ include/linux/fs.h | 1 + 2 files changed, 9 insertions(+)fs/inode.c +0
-0
fs/inode.c int dentry_needs_remove_privs(struct mnt_idmap *idmap, · line 2020
Quoted 8 lines
fs/inode.c +5
-0@@ -2013,6 +2013,14 @@ int dentry_needs_remove_privs(struct mnt_idmap *idmap,return mask; } +int file_needs_remove_privs (struct file *file) +{ +struct dentry *dentry =file_dentry (file); + +return dentry_needs_remove_privs (file_mnt_idmap (file), dentry);
Ugh, I wanted to propose to get rid of this dentry dance but I propsed that before and remembered it's because of __vfs_getxattr() which is called from the capability security hook that we need it...
Re: [PATCH 1/2] fs: Add and export file_needs_remove_privs
Quoted 27 lines · Christian Brauner
Quoted 26 lines · Bernd Schubert
File systems want to hold a shared lock for DIO writes, but may need to drop file priveliges - that a requires an exclusive lock. The new export function
file_needs_remove_privs()is added in order to first check if that is needed.Cc Miklos Szeredi <[email protected]>Cc Dharmendra Singh <[email protected]>Cc Josef Bacik <[email protected]>Cc Alexander Viro <[email protected]>Cc Christian Brauner <[email protected]>Signed-off-by Bernd Schubert <[email protected]>--- fs/inode.c | 8 ++++++++ include/linux/fs.h | 1 + 2 files changed, 9 insertions(+)fs/inode.c +0
-0@@ -2013,6 +2013,14 @@ int dentry_needs_remove_privs(struct mnt_idmap *idmap,return mask; }
Quoted 9 lines
fs/inode.c int dentry_needs_remove_privs(struct mnt_idmap *idmap, · line 2020
Quoted 5 lines
fs/inode.c +5
-0+int file_needs_remove_privs (struct file *file) +{ +struct dentry *dentry =file_dentry (file); + +return dentry_needs_remove_privs (file_mnt_idmap (file), dentry);Ugh, I wanted to propose to get rid of this dentry dance but I propsed that before and remembered it's because of
__vfs_getxattr()which is called from the capability security hook that we need it...
Is there anything specific you are suggesting?
And thanks for typo/grammar annotations, I'm going to wait for btrfs feedback before I'm going to send a new version.
Thanks,
Bernd
Christian Brauner
maintainerRe: [PATCH 1/2] fs: Add and export file_needs_remove_privs
Quoted 39 lines · Bernd Schubert
Quoted 19 lines · Christian Brauner
Quoted 18 lines · Bernd Schubert
File systems want to hold a shared lock for DIO writes, but may need to drop file priveliges - that a requires an exclusive lock. The new export function
file_needs_remove_privs()is added in order to first check if that is needed.Cc Miklos Szeredi <[email protected]>Cc Dharmendra Singh <[email protected]>Cc Josef Bacik <[email protected]>Cc Alexander Viro <[email protected]>Cc Christian Brauner <[email protected]>Signed-off-by Bernd Schubert <[email protected]>--- fs/inode.c | 8 ++++++++ include/linux/fs.h | 1 + 2 files changed, 9 insertions(+)Quoted 16 lines
fs/inode.c int dentry_needs_remove_privs(struct mnt_idmap *idmap, · line 2019
Quoted 12 lines
fs/inode.c +5
-0@@ -2013,6 +2013,14 @@ int dentry_needs_remove_privs(struct mnt_idmap *idmap,return mask; } +int file_needs_remove_privs (struct file *file) +{ +struct dentry *dentry =file_dentry (file); + +return dentry_needs_remove_privs (file_mnt_idmap (file), dentry);Ugh, I wanted to propose to get rid of this dentry dance but I propsed that before and remembered it's because of
__vfs_getxattr()which is called from the capability security hook that we need it...
Quoted 1 line
Is there anything specific you are suggesting?
No, it's not actionable for you here. It would require adding inode methods to set and get filesystem capabilities and then converting it in such a way that we don't need to rely on passing dentries around. That's a separate larger patchset that we would need with surgery across a bunch of filesystems and the vfs - Seth (Forshee) has been working on this.
The callchains are just pointless which I remembered when I saw the patchset:
file_needs_remove_privs(file)
-> dentry_needs_remove_privs(dentry)
-> inode = d_inode(dentry)
// do inode stuff
// security_needs_*(dentry)point is ideally we shouldn't need the dentry in *remove_privs() at all.
PATCH v2 2/2 btrfs: file_remove_privs needs an exclusive lock
In reply to Bernd Schubertfile_remove_privs might call into notify_change(), which requires to hold an exclusive lock.
In order to keep the shared lock for most IOs it now first checks if privilege changes are needed, then switches to the exclusive lock, rechecks and only then calls file_remove_privs. This makes usage of the new exported function file_needs_remove_privs().
The file_remove_privs code path is not optimized, under the assumption that it would be a rare call (file_remove_privs calls file_needs_remove_privs a 2nd time).
--- fs/btrfs/file.c | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-)
fs/btrfs/file.c +32 -9
@@ -1125,7 +1125,7 @@ static void update_time_for_write(struct inode *inode) }static int btrfs_write_check (struct kiocb *iocb,struct iov_iter *from, -size_t count) +size_t count,bool *shared_lock) {struct file *file = iocb->ki_filp;struct inode *inode =file_inode (file);
@@ -1145,9 +1145,17 @@ static int btrfs_write_check(struct kiocb *iocb, struct iov_iter *from, !(BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)))return -EAGAIN ; - ret =file_remove_privs (file); -if (ret) -return ret; + ret =file_needs_remove_privs (file); +if (ret) { +if (shared_lock && *shared_lock) { + *shared_lock = false; +return -EAGAIN ; + } + + ret =file_remove_privs (file); +if (ret) +return ret; + }/* * We reserve space for updating the inode when we reserve space for the
@@ -1204,7 +1212,7 @@ static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb,if (ret <=0 ) goto out; - ret =btrfs_write_check (iocb, i, ret); + ret =btrfs_write_check (iocb, i, ret,NULL );if (ret <0 ) goto out;
@@ -1462,13 +1470,20 @@ static ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from)ssize_t err;unsigned int ilock_flags =0 ;struct iomap_dio *dio; +bool shared_lock;if (iocb->ki_flags &IOCB_NOWAIT ) ilock_flags |=BTRFS_ILOCK_TRY ; -/* If the write DIO is within EOF, use a shared lock */ -if (iocb->ki_pos +iov_iter_count (from) <=i_size_read (inode)) +/* If the write DIO is within EOF, use a shared lock and also only +* if security bits will likely not be dropped. Either will need +* to be rechecked after the lock was acquired. +*/ +if (iocb->ki_pos +iov_iter_count (from) <=i_size_read (inode) && +IS_NOSEC (inode)) { ilock_flags |=BTRFS_ILOCK_SHARED ; + shared_lock = true; + } relock: err =btrfs_inode_lock (BTRFS_I (inode), ilock_flags);
@@ -1481,8 +1496,15 @@ static ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from)return err; } - err =btrfs_write_check (iocb, from, err); + err =btrfs_write_check (iocb, from, err, &shared_lock);if (err <0 ) { +if (err == -EAGAIN && ilock_flags &BTRFS_ILOCK_SHARED && + !shared_lock) { +btrfs_inode_unlock (BTRFS_I (inode), ilock_flags); + ilock_flags &= ~BTRFS_ILOCK_SHARED ; + goto relock; + } +btrfs_inode_unlock (BTRFS_I (inode), ilock_flags); goto out; }
@@ -1496,6 +1518,7 @@ static ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from) pos + iov_iter_count(from) > i_size_read(inode)) {btrfs_inode_unlock (BTRFS_I (inode), ilock_flags); ilock_flags &= ~BTRFS_ILOCK_SHARED ; + shared_lock = false; goto relock; }
@@ -1632,7 +1655,7 @@ static ssize_t btrfs_encoded_write(struct kiocb *iocb, struct iov_iter *from,if (ret || encoded->len ==0 ) goto out; - ret =btrfs_write_check (iocb, from, encoded->len); + ret =btrfs_write_check (iocb, from, encoded->len,NULL );if (ret <0 ) goto out;
Signature · 1 line
2.39.2
Quoted 11 lines · Bernd Schubert
While adding shared direct IO write locks to fuse Miklos noticed that
file_remove_privs()needs an exclusive lock. I then noticed that btrfs actually has the same issue as I had in my patch, it was calling into that function with a shared lock. This series adds a new exported functionfile_needs_remove_privs(), which used by the follow up btrfs patch and will be used by the DIO code path in fuse as well. If that function returns any mask the shared lock needs to be dropped and replaced by the exclusive variant.Note: Compilation tested only.
The fix makes sense, there should be no noticeable performance impact, basically the same check is done in the newly exported helper for the IS_NOSEC bit. I can give it a test locally for the default case, I'm not sure if we have specific tests for the security layers in fstests.
Regarding merge, I can take the two patches via btrfs tree or can wait until the export is present in Linus' tree in case FUSE needs it independently.
Christian Brauner
maintainerQuoted 13 lines · David Sterba
Quoted 11 lines · Bernd Schubert
While adding shared direct IO write locks to fuse Miklos noticed that
file_remove_privs()needs an exclusive lock. I then noticed that btrfs actually has the same issue as I had in my patch, it was calling into that function with a shared lock. This series adds a new exported functionfile_needs_remove_privs(), which used by the follow up btrfs patch and will be used by the DIO code path in fuse as well. If that function returns any mask the shared lock needs to be dropped and replaced by the exclusive variant.Note: Compilation tested only.
Quoted 8 lines
The fix makes sense, there should be no noticeable performance impact, basically the same check is done in the newly exported helper for the
IS_NOSECbit. I can give it a test locally for the default case, I'm not sure if we have specific tests for the security layers in fstests.Regarding merge, I can take the two patches via btrfs tree or can wait until the export is present in Linus' tree in case FUSE needs it independently.
Both fuse and btrfs need it afaict. We can grab it and provide a tag post -rc1? Whatever works best.
Quoted 19 lines · Christian Brauner
Quoted 18 lines · David Sterba
Quoted 11 lines · Bernd Schubert
While adding shared direct IO write locks to fuse Miklos noticed that
file_remove_privs()needs an exclusive lock. I then noticed that btrfs actually has the same issue as I had in my patch, it was calling into that function with a shared lock. This series adds a new exported functionfile_needs_remove_privs(), which used by the follow up btrfs patch and will be used by the DIO code path in fuse as well. If that function returns any mask the shared lock needs to be dropped and replaced by the exclusive variant.Note: Compilation tested only.
The fix makes sense, there should be no noticeable performance impact, basically the same check is done in the newly exported helper for the
IS_NOSECbit. I can give it a test locally for the default case, I'm not sure if we have specific tests for the security layers in fstests.
Quoted 6 lines
Quoted 3 lines
Regarding merge, I can take the two patches via btrfs tree or can wait until the export is present in Linus' tree in case FUSE needs it independently.
Both fuse and btrfs need it afaict. We can grab it and provide a tag post -rc1? Whatever works best.
fuse will need it for my direct IO patches - hopefully in 6.7. For btrfs it is a bug fix, should go in asap?
Christoph has some objections for to use the new exported helper (file_needs_remove_privs). Maybe I better send a version for btrfs that only uses S_NOSEC? For fuse we cannot use it, unfortunately.
Thanks,
Bernd
Christian Brauner
maintainerQuoted 29 lines · Bernd Schubert
Quoted 19 lines · Christian Brauner
Quoted 18 lines · David Sterba
Quoted 11 lines · Bernd Schubert
While adding shared direct IO write locks to fuse Miklos noticed that
file_remove_privs()needs an exclusive lock. I then noticed that btrfs actually has the same issue as I had in my patch, it was calling into that function with a shared lock. This series adds a new exported functionfile_needs_remove_privs(), which used by the follow up btrfs patch and will be used by the DIO code path in fuse as well. If that function returns any mask the shared lock needs to be dropped and replaced by the exclusive variant.Note: Compilation tested only.
The fix makes sense, there should be no noticeable performance impact, basically the same check is done in the newly exported helper for the
IS_NOSECbit. I can give it a test locally for the default case, I'm not sure if we have specific tests for the security layers in fstests.Quoted 6 lines
Quoted 3 lines
Regarding merge, I can take the two patches via btrfs tree or can wait until the export is present in Linus' tree in case FUSE needs it independently.
Both fuse and btrfs need it afaict. We can grab it and provide a tag post -rc1? Whatever works best.
Quoted 6 lines
fuse will need it for my direct IO patches - hopefully in 6.7. For btrfs it is a bug fix, should go in asap?
Christoph has some objections for to use the new exported helper (
file_needs_remove_privs). Maybe I better send a version for btrfs that only usesS_NOSEC? For fuse we cannot use it, unfortunately.
Sure.
Quoted 19 lines · Christian Brauner
Quoted 18 lines · David Sterba
Quoted 11 lines · Bernd Schubert
While adding shared direct IO write locks to fuse Miklos noticed that
file_remove_privs()needs an exclusive lock. I then noticed that btrfs actually has the same issue as I had in my patch, it was calling into that function with a shared lock. This series adds a new exported functionfile_needs_remove_privs(), which used by the follow up btrfs patch and will be used by the DIO code path in fuse as well. If that function returns any mask the shared lock needs to be dropped and replaced by the exclusive variant.Note: Compilation tested only.
The fix makes sense, there should be no noticeable performance impact, basically the same check is done in the newly exported helper for the
IS_NOSECbit. I can give it a test locally for the default case, I'm not sure if we have specific tests for the security layers in fstests.
Quoted 6 lines
Quoted 3 lines
Regarding merge, I can take the two patches via btrfs tree or can wait until the export is present in Linus' tree in case FUSE needs it independently.
Both fuse and btrfs need it afaict. We can grab it and provide a tag post -rc1? Whatever works best.
Git tree sync won't be needed, Bernd sent the fix within btrfs code.