diff options
author | Christian Brauner <brauner@kernel.org> | 2024-06-26 14:23:04 +0200 |
---|---|---|
committer | Christian Brauner <brauner@kernel.org> | 2024-06-27 18:31:17 +0200 |
commit | 9fb9ff7ed1656dbd2168ebfc21b8c33666a994fa (patch) | |
tree | 1599bdee526b94f9354601bc908652c3737c3cfd /fs/namei.c | |
parent | 8e3447822d7d8c0f562c6851a7a31e24d1ede55e (diff) | |
download | lwn-9fb9ff7ed1656dbd2168ebfc21b8c33666a994fa.tar.gz lwn-9fb9ff7ed1656dbd2168ebfc21b8c33666a994fa.zip |
fs: reflow may_create_in_sticky()
This function is so messy and hard to read, let's reflow it to make it
more readable.
Reviewed-by: Jan Kara <jack@suse.cz>
Link: https://lore.kernel.org/r/20240621-affekt-denkzettel-3c115f68355a@brauner
Signed-off-by: Christian Brauner <brauner@kernel.org>
Diffstat (limited to 'fs/namei.c')
-rw-r--r-- | fs/namei.c | 51 |
1 files changed, 35 insertions, 16 deletions
diff --git a/fs/namei.c b/fs/namei.c index 8b40991cb59e..f10b6dde7dab 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -1240,29 +1240,48 @@ int may_linkat(struct mnt_idmap *idmap, const struct path *link) * * Returns 0 if the open is allowed, -ve on error. */ -static int may_create_in_sticky(struct mnt_idmap *idmap, - struct nameidata *nd, struct inode *const inode) +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; - 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 (likely(!(dir_mode & S_ISVTX))) 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 (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; } |