* [PATCH] vfs: reorder checks in may_create_in_sticky @ 2024-06-20 12:03 Mateusz Guzik 2024-06-20 12:46 ` Jan Kara ` (2 more replies) 0 siblings, 3 replies; 6+ messages in thread From: Mateusz Guzik @ 2024-06-20 12:03 UTC (permalink / raw) To: brauner; +Cc: viro, jack, linux-kernel, linux-fsdevel, Mateusz Guzik The routine is called for all directories on file creation and weirdly postpones the check if the dir is sticky to begin with. Instead it first checks fifos and regular files (in that order), while avoidably pulling globals. No functional changes. Signed-off-by: Mateusz Guzik <mjguzik@gmail.com> --- fs/namei.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index 63d1fb06da6b..b1600060ecfb 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -1246,9 +1246,9 @@ static int may_create_in_sticky(struct mnt_idmap *idmap, umode_t dir_mode = nd->dir_mode; vfsuid_t dir_vfsuid = nd->dir_vfsuid; - if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) || - (!sysctl_protected_regular && S_ISREG(inode->i_mode)) || - likely(!(dir_mode & S_ISVTX)) || + if (likely(!(dir_mode & S_ISVTX)) || + (S_ISREG(inode->i_mode) && !sysctl_protected_regular) || + (S_ISFIFO(inode->i_mode) && !sysctl_protected_fifos) || vfsuid_eq(i_uid_into_vfsuid(idmap, inode), dir_vfsuid) || vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), current_fsuid())) return 0; -- 2.43.0 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH] vfs: reorder checks in may_create_in_sticky 2024-06-20 12:03 [PATCH] vfs: reorder checks in may_create_in_sticky Mateusz Guzik @ 2024-06-20 12:46 ` Jan Kara 2024-06-20 13:59 ` Christian Brauner 2024-06-21 7:45 ` Christian Brauner 2 siblings, 0 replies; 6+ messages in thread From: Jan Kara @ 2024-06-20 12:46 UTC (permalink / raw) To: Mateusz Guzik; +Cc: brauner, viro, jack, linux-kernel, linux-fsdevel On Thu 20-06-24 14:03:59, Mateusz Guzik wrote: > The routine is called for all directories on file creation and weirdly > postpones the check if the dir is sticky to begin with. Instead it first > checks fifos and regular files (in that order), while avoidably pulling > globals. > > No functional changes. > > Signed-off-by: Mateusz Guzik <mjguzik@gmail.com> Yeah, putting the sticky bit check first makes sense also logically. Feel free to add: Reviewed-by: Jan Kara <jack@suse.cz> Honza > --- > fs/namei.c | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-) > > diff --git a/fs/namei.c b/fs/namei.c > index 63d1fb06da6b..b1600060ecfb 100644 > --- a/fs/namei.c > +++ b/fs/namei.c > @@ -1246,9 +1246,9 @@ static int may_create_in_sticky(struct mnt_idmap *idmap, > umode_t dir_mode = nd->dir_mode; > vfsuid_t dir_vfsuid = nd->dir_vfsuid; > > - if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) || > - (!sysctl_protected_regular && S_ISREG(inode->i_mode)) || > - likely(!(dir_mode & S_ISVTX)) || > + if (likely(!(dir_mode & S_ISVTX)) || > + (S_ISREG(inode->i_mode) && !sysctl_protected_regular) || > + (S_ISFIFO(inode->i_mode) && !sysctl_protected_fifos) || > vfsuid_eq(i_uid_into_vfsuid(idmap, inode), dir_vfsuid) || > vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), current_fsuid())) > return 0; > -- > 2.43.0 > -- Jan Kara <jack@suse.com> SUSE Labs, CR ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] vfs: reorder checks in may_create_in_sticky 2024-06-20 12:03 [PATCH] vfs: reorder checks in may_create_in_sticky Mateusz Guzik 2024-06-20 12:46 ` Jan Kara @ 2024-06-20 13:59 ` Christian Brauner 2024-06-21 7:45 ` Christian Brauner 2 siblings, 0 replies; 6+ messages in thread From: Christian Brauner @ 2024-06-20 13:59 UTC (permalink / raw) To: Mateusz Guzik; +Cc: Christian Brauner, viro, jack, linux-kernel, linux-fsdevel On Thu, 20 Jun 2024 14:03:59 +0200, Mateusz Guzik wrote: > The routine is called for all directories on file creation and weirdly > postpones the check if the dir is sticky to begin with. Instead it first > checks fifos and regular files (in that order), while avoidably pulling > globals. > > No functional changes. > > [...] The list of checks is unspeakably irritating. I really dislike looking at this function. --- Applied to the vfs.misc branch of the vfs/vfs.git tree. Patches in the vfs.misc branch should appear in linux-next soon. Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it. It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated. Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch. tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.misc [1/1] vfs: reorder checks in may_create_in_sticky https://git.kernel.org/vfs/vfs/c/6cc620b3a050 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] vfs: reorder checks in may_create_in_sticky 2024-06-20 12:03 [PATCH] vfs: reorder checks in may_create_in_sticky Mateusz Guzik 2024-06-20 12:46 ` Jan Kara 2024-06-20 13:59 ` Christian Brauner @ 2024-06-21 7:45 ` Christian Brauner 2024-06-21 11:17 ` Mateusz Guzik 2024-06-21 11:25 ` Jan Kara 2 siblings, 2 replies; 6+ messages in thread From: Christian Brauner @ 2024-06-21 7:45 UTC (permalink / raw) To: Mateusz Guzik; +Cc: viro, jack, linux-kernel, linux-fsdevel On Thu, Jun 20, 2024 at 02:03:59PM GMT, Mateusz Guzik wrote: > The routine is called for all directories on file creation and weirdly > postpones the check if the dir is sticky to begin with. Instead it first > checks fifos and regular files (in that order), while avoidably pulling > globals. > > No functional changes. > > Signed-off-by: Mateusz Guzik <mjguzik@gmail.com> > --- > fs/namei.c | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-) > > diff --git a/fs/namei.c b/fs/namei.c > index 63d1fb06da6b..b1600060ecfb 100644 > --- a/fs/namei.c > +++ b/fs/namei.c > @@ -1246,9 +1246,9 @@ static int may_create_in_sticky(struct mnt_idmap *idmap, > umode_t dir_mode = nd->dir_mode; > vfsuid_t dir_vfsuid = nd->dir_vfsuid; > > - if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) || > - (!sysctl_protected_regular && S_ISREG(inode->i_mode)) || > - likely(!(dir_mode & S_ISVTX)) || > + if (likely(!(dir_mode & S_ISVTX)) || > + (S_ISREG(inode->i_mode) && !sysctl_protected_regular) || > + (S_ISFIFO(inode->i_mode) && !sysctl_protected_fifos) || > vfsuid_eq(i_uid_into_vfsuid(idmap, inode), dir_vfsuid) || > vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), current_fsuid())) > return 0; I think we really need to unroll this unoly mess to make it more readable? diff --git a/fs/namei.c b/fs/namei.c index 3e23fbb8b029..1dd2d328bae3 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -1244,25 +1244,43 @@ static int may_create_in_sticky(struct mnt_idmap *idmap, struct nameidata *nd, struct inode *const inode) { umode_t dir_mode = nd->dir_mode; - vfsuid_t dir_vfsuid = nd->dir_vfsuid; + vfsuid_t dir_vfsuid = nd->dir_vfsuid, i_vfsuid; + int ret; + + if (likely(!(dir_mode & S_ISVTX))) + return 0; + + if (S_ISREG(inode->i_mode) && !sysctl_protected_regular) + return 0; + + if (S_ISFIFO(inode->i_mode) && !sysctl_protected_fifos) + return 0; + + i_vfsuid = i_uid_into_vfsuid(idmap, inode); + + if (vfsuid_eq(i_vfsuid, dir_vfsuid)) + return 0; - if (likely(!(dir_mode & S_ISVTX)) || - (S_ISREG(inode->i_mode) && !sysctl_protected_regular) || - (S_ISFIFO(inode->i_mode) && !sysctl_protected_fifos) || - vfsuid_eq(i_uid_into_vfsuid(idmap, inode), dir_vfsuid) || - vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), current_fsuid())) + if (vfsuid_eq_kuid(i_vfsuid, current_fsuid())) return 0; - if (likely(dir_mode & 0002) || - (dir_mode & 0020 && - ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) || - (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) { - const char *operation = S_ISFIFO(inode->i_mode) ? - "sticky_create_fifo" : - "sticky_create_regular"; - audit_log_path_denied(AUDIT_ANOM_CREAT, operation); + if (likely(dir_mode & 0002)) { + audit_log_path_denied(AUDIT_ANOM_CREAT, "sticky_create"); return -EACCES; } + + if (dir_mode & 0020) { + if (sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) { + audit_log_path_denied(AUDIT_ANOM_CREAT, "sticky_create_fifo"); + return -EACCES; + } + + if (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode)) { + audit_log_path_denied(AUDIT_ANOM_CREAT, "sticky_create_regular"); + return -EACCES; + } + } + return 0; } That gives us: static int may_create_in_sticky(struct mnt_idmap *idmap, struct nameidata *nd, struct inode *const inode) { umode_t dir_mode = nd->dir_mode; vfsuid_t dir_vfsuid = nd->dir_vfsuid, i_vfsuid; int ret; if (likely(!(dir_mode & S_ISVTX))) return 0; if (S_ISREG(inode->i_mode) && !sysctl_protected_regular) return 0; if (S_ISFIFO(inode->i_mode) && !sysctl_protected_fifos) return 0; i_vfsuid = i_uid_into_vfsuid(idmap, inode); if (vfsuid_eq(i_vfsuid, dir_vfsuid)) return 0; if (vfsuid_eq_kuid(i_vfsuid, current_fsuid())) return 0; if (likely(dir_mode & 0002)) { audit_log_path_denied(AUDIT_ANOM_CREAT, "sticky_create"); return -EACCES; } if (dir_mode & 0020) { if (sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) { audit_log_path_denied(AUDIT_ANOM_CREAT, "sticky_create_fifo"); return -EACCES; } if (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode)) { audit_log_path_denied(AUDIT_ANOM_CREAT, "sticky_create_regular"); return -EACCES; } } return 0; } ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH] vfs: reorder checks in may_create_in_sticky 2024-06-21 7:45 ` Christian Brauner @ 2024-06-21 11:17 ` Mateusz Guzik 2024-06-21 11:25 ` Jan Kara 1 sibling, 0 replies; 6+ messages in thread From: Mateusz Guzik @ 2024-06-21 11:17 UTC (permalink / raw) To: Christian Brauner; +Cc: viro, jack, linux-kernel, linux-fsdevel On Fri, Jun 21, 2024 at 9:45 AM Christian Brauner <brauner@kernel.org> wrote: > > On Thu, Jun 20, 2024 at 02:03:59PM GMT, Mateusz Guzik wrote: > > The routine is called for all directories on file creation and weirdly > > postpones the check if the dir is sticky to begin with. Instead it first > > checks fifos and regular files (in that order), while avoidably pulling > > globals. > > > > No functional changes. > > > > Signed-off-by: Mateusz Guzik <mjguzik@gmail.com> > > --- > > fs/namei.c | 6 +++--- > > 1 file changed, 3 insertions(+), 3 deletions(-) > > > > diff --git a/fs/namei.c b/fs/namei.c > > index 63d1fb06da6b..b1600060ecfb 100644 > > --- a/fs/namei.c > > +++ b/fs/namei.c > > @@ -1246,9 +1246,9 @@ static int may_create_in_sticky(struct mnt_idmap *idmap, > > umode_t dir_mode = nd->dir_mode; > > vfsuid_t dir_vfsuid = nd->dir_vfsuid; > > > > - if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) || > > - (!sysctl_protected_regular && S_ISREG(inode->i_mode)) || > > - likely(!(dir_mode & S_ISVTX)) || > > + if (likely(!(dir_mode & S_ISVTX)) || > > + (S_ISREG(inode->i_mode) && !sysctl_protected_regular) || > > + (S_ISFIFO(inode->i_mode) && !sysctl_protected_fifos) || > > vfsuid_eq(i_uid_into_vfsuid(idmap, inode), dir_vfsuid) || > > vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), current_fsuid())) > > return 0; > > I think we really need to unroll this unoly mess to make it more readable? > > diff --git a/fs/namei.c b/fs/namei.c > index 3e23fbb8b029..1dd2d328bae3 100644 > --- a/fs/namei.c > +++ b/fs/namei.c > @@ -1244,25 +1244,43 @@ static int may_create_in_sticky(struct mnt_idmap *idmap, > struct nameidata *nd, struct inode *const inode) > { > umode_t dir_mode = nd->dir_mode; > - vfsuid_t dir_vfsuid = nd->dir_vfsuid; > + vfsuid_t dir_vfsuid = nd->dir_vfsuid, i_vfsuid; > + int ret; > + > + if (likely(!(dir_mode & S_ISVTX))) > + return 0; > + > + if (S_ISREG(inode->i_mode) && !sysctl_protected_regular) > + return 0; > + > + if (S_ISFIFO(inode->i_mode) && !sysctl_protected_fifos) > + return 0; > + > + i_vfsuid = i_uid_into_vfsuid(idmap, inode); > + > + if (vfsuid_eq(i_vfsuid, dir_vfsuid)) > + return 0; > > - if (likely(!(dir_mode & S_ISVTX)) || > - (S_ISREG(inode->i_mode) && !sysctl_protected_regular) || > - (S_ISFIFO(inode->i_mode) && !sysctl_protected_fifos) || > - vfsuid_eq(i_uid_into_vfsuid(idmap, inode), dir_vfsuid) || > - vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), current_fsuid())) > + if (vfsuid_eq_kuid(i_vfsuid, current_fsuid())) > return 0; > > - if (likely(dir_mode & 0002) || > - (dir_mode & 0020 && > - ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) || > - (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) { > - const char *operation = S_ISFIFO(inode->i_mode) ? > - "sticky_create_fifo" : > - "sticky_create_regular"; > - audit_log_path_denied(AUDIT_ANOM_CREAT, operation); > + if (likely(dir_mode & 0002)) { > + audit_log_path_denied(AUDIT_ANOM_CREAT, "sticky_create"); > return -EACCES; > } > + > + if (dir_mode & 0020) { > + if (sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) { > + audit_log_path_denied(AUDIT_ANOM_CREAT, "sticky_create_fifo"); > + return -EACCES; > + } > + > + if (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode)) { > + audit_log_path_denied(AUDIT_ANOM_CREAT, "sticky_create_regular"); > + return -EACCES; > + } > + } > + > return 0; > } > > That gives us: > > static int may_create_in_sticky(struct mnt_idmap *idmap, > struct nameidata *nd, struct inode *const inode) > { > umode_t dir_mode = nd->dir_mode; > vfsuid_t dir_vfsuid = nd->dir_vfsuid, i_vfsuid; > int ret; > > if (likely(!(dir_mode & S_ISVTX))) > return 0; > > if (S_ISREG(inode->i_mode) && !sysctl_protected_regular) > return 0; > > if (S_ISFIFO(inode->i_mode) && !sysctl_protected_fifos) > return 0; > > i_vfsuid = i_uid_into_vfsuid(idmap, inode); > > if (vfsuid_eq(i_vfsuid, dir_vfsuid)) > return 0; > > if (vfsuid_eq_kuid(i_vfsuid, current_fsuid())) > return 0; > > if (likely(dir_mode & 0002)) { > audit_log_path_denied(AUDIT_ANOM_CREAT, "sticky_create"); > return -EACCES; > } > > if (dir_mode & 0020) { > if (sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) { > audit_log_path_denied(AUDIT_ANOM_CREAT, "sticky_create_fifo"); > return -EACCES; > } > > if (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode)) { > audit_log_path_denied(AUDIT_ANOM_CREAT, "sticky_create_regular"); > return -EACCES; > } > } > > return 0; > } That does look better. :) So as far as I'm concerned my patch can be just dropped, just in case I'll note there is no need to mention me anywhere near this. -- Mateusz Guzik <mjguzik gmail.com> ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] vfs: reorder checks in may_create_in_sticky 2024-06-21 7:45 ` Christian Brauner 2024-06-21 11:17 ` Mateusz Guzik @ 2024-06-21 11:25 ` Jan Kara 1 sibling, 0 replies; 6+ messages in thread From: Jan Kara @ 2024-06-21 11:25 UTC (permalink / raw) To: Christian Brauner; +Cc: Mateusz Guzik, viro, jack, linux-kernel, linux-fsdevel On Fri 21-06-24 09:45:03, Christian Brauner wrote: > On Thu, Jun 20, 2024 at 02:03:59PM GMT, Mateusz Guzik wrote: > > The routine is called for all directories on file creation and weirdly > > postpones the check if the dir is sticky to begin with. Instead it first > > checks fifos and regular files (in that order), while avoidably pulling > > globals. > > > > No functional changes. > > > > Signed-off-by: Mateusz Guzik <mjguzik@gmail.com> > > --- > > fs/namei.c | 6 +++--- > > 1 file changed, 3 insertions(+), 3 deletions(-) > > > > diff --git a/fs/namei.c b/fs/namei.c > > index 63d1fb06da6b..b1600060ecfb 100644 > > --- a/fs/namei.c > > +++ b/fs/namei.c > > @@ -1246,9 +1246,9 @@ static int may_create_in_sticky(struct mnt_idmap *idmap, > > umode_t dir_mode = nd->dir_mode; > > vfsuid_t dir_vfsuid = nd->dir_vfsuid; > > > > - if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) || > > - (!sysctl_protected_regular && S_ISREG(inode->i_mode)) || > > - likely(!(dir_mode & S_ISVTX)) || > > + if (likely(!(dir_mode & S_ISVTX)) || > > + (S_ISREG(inode->i_mode) && !sysctl_protected_regular) || > > + (S_ISFIFO(inode->i_mode) && !sysctl_protected_fifos) || > > vfsuid_eq(i_uid_into_vfsuid(idmap, inode), dir_vfsuid) || > > vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), current_fsuid())) > > return 0; > > I think we really need to unroll this unoly mess to make it more readable? I guess my neural network has adapted to these kind of things in the kernel :) > diff --git a/fs/namei.c b/fs/namei.c > index 3e23fbb8b029..1dd2d328bae3 100644 > --- a/fs/namei.c > +++ b/fs/namei.c > @@ -1244,25 +1244,43 @@ static int may_create_in_sticky(struct mnt_idmap *idmap, > struct nameidata *nd, struct inode *const inode) > { > umode_t dir_mode = nd->dir_mode; > - vfsuid_t dir_vfsuid = nd->dir_vfsuid; > + vfsuid_t dir_vfsuid = nd->dir_vfsuid, i_vfsuid; > + int ret; > + > + if (likely(!(dir_mode & S_ISVTX))) > + return 0; > + > + if (S_ISREG(inode->i_mode) && !sysctl_protected_regular) > + return 0; > + > + if (S_ISFIFO(inode->i_mode) && !sysctl_protected_fifos) > + return 0; > + > + i_vfsuid = i_uid_into_vfsuid(idmap, inode); > + > + if (vfsuid_eq(i_vfsuid, dir_vfsuid)) > + return 0; > > - if (likely(!(dir_mode & S_ISVTX)) || > - (S_ISREG(inode->i_mode) && !sysctl_protected_regular) || > - (S_ISFIFO(inode->i_mode) && !sysctl_protected_fifos) || > - vfsuid_eq(i_uid_into_vfsuid(idmap, inode), dir_vfsuid) || > - vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), current_fsuid())) > + if (vfsuid_eq_kuid(i_vfsuid, current_fsuid())) > return 0; > > - if (likely(dir_mode & 0002) || > - (dir_mode & 0020 && > - ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) || > - (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) { > - const char *operation = S_ISFIFO(inode->i_mode) ? > - "sticky_create_fifo" : > - "sticky_create_regular"; > - audit_log_path_denied(AUDIT_ANOM_CREAT, operation); > + if (likely(dir_mode & 0002)) { > + audit_log_path_denied(AUDIT_ANOM_CREAT, "sticky_create"); > return -EACCES; > } > + > + if (dir_mode & 0020) { > + if (sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) { > + audit_log_path_denied(AUDIT_ANOM_CREAT, "sticky_create_fifo"); > + return -EACCES; > + } > + > + if (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode)) { > + audit_log_path_denied(AUDIT_ANOM_CREAT, "sticky_create_regular"); > + return -EACCES; > + } > + } > + > return 0; > } But this definitely looks nicer to read... Feel free to add: Reviewed-by: Jan Kara <jack@suse.cz> Honza -- Jan Kara <jack@suse.com> SUSE Labs, CR ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-06-21 11:25 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2024-06-20 12:03 [PATCH] vfs: reorder checks in may_create_in_sticky Mateusz Guzik 2024-06-20 12:46 ` Jan Kara 2024-06-20 13:59 ` Christian Brauner 2024-06-21 7:45 ` Christian Brauner 2024-06-21 11:17 ` Mateusz Guzik 2024-06-21 11:25 ` Jan Kara
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).