Skip to content

Commit cbdf35b

Browse files
author
Miklos Szeredi
committed
vfs: export check_sticky()
It's already duplicated in btrfs and about to be used in overlayfs too. Move the sticky bit check to an inline helper and call the out-of-line helper only in the unlikly case of the sticky bit being set. Signed-off-by: Miklos Szeredi <mszeredi@suse.cz>
1 parent c771d68 commit cbdf35b

3 files changed

Lines changed: 12 additions & 26 deletions

File tree

‎fs/btrfs/ioctl.c‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -765,23 +765,6 @@ static int create_snapshot(struct btrfs_root *root, struct inode *dir,
765765
return ret;
766766
}
767767

768-
/* copy of check_sticky in fs/namei.c()
769-
* It's inline, so penalty for filesystems that don't use sticky bit is
770-
* minimal.
771-
*/
772-
static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
773-
{
774-
kuid_t fsuid = current_fsuid();
775-
776-
if (!(dir->i_mode & S_ISVTX))
777-
return 0;
778-
if (uid_eq(inode->i_uid, fsuid))
779-
return 0;
780-
if (uid_eq(dir->i_uid, fsuid))
781-
return 0;
782-
return !capable(CAP_FOWNER);
783-
}
784-
785768
/* copy of may_delete in fs/namei.c()
786769
* Check whether we can remove a link victim from directory dir, check
787770
* whether the type of victim is right.
@@ -817,8 +800,7 @@ static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
817800
return error;
818801
if (IS_APPEND(dir))
819802
return -EPERM;
820-
if (btrfs_check_sticky(dir, victim->d_inode)||
821-
IS_APPEND(victim->d_inode)||
803+
if (check_sticky(dir, victim->d_inode) || IS_APPEND(victim->d_inode) ||
822804
IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
823805
return -EPERM;
824806
if (isdir) {

‎fs/namei.c‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,22 +2384,17 @@ kern_path_mountpoint(int dfd, const char *name, struct path *path,
23842384
}
23852385
EXPORT_SYMBOL(kern_path_mountpoint);
23862386

2387-
/*
2388-
* It's inline, so penalty for filesystems that don't use sticky bit is
2389-
* minimal.
2390-
*/
2391-
static inline int check_sticky(struct inode *dir, struct inode *inode)
2387+
int __check_sticky(struct inode *dir, struct inode *inode)
23922388
{
23932389
kuid_t fsuid = current_fsuid();
23942390

2395-
if (!(dir->i_mode & S_ISVTX))
2396-
return 0;
23972391
if (uid_eq(inode->i_uid, fsuid))
23982392
return 0;
23992393
if (uid_eq(dir->i_uid, fsuid))
24002394
return 0;
24012395
return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
24022396
}
2397+
EXPORT_SYMBOL(__check_sticky);
24032398

24042399
/*
24052400
* Check whether we can remove a link victim from directory dir, check

‎include/linux/fs.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,6 +2259,7 @@ extern int notify_change(struct dentry *, struct iattr *, struct inode **);
22592259
extern int inode_permission(struct inode *, int);
22602260
extern int __inode_permission(struct inode *, int);
22612261
extern int generic_permission(struct inode *, int);
2262+
extern int __check_sticky(struct inode *dir, struct inode *inode);
22622263

22632264
static inline bool execute_ok(struct inode *inode)
22642265
{
@@ -2745,6 +2746,14 @@ static inline int is_sxid(umode_t mode)
27452746
return (mode & S_ISUID) || ((mode & S_ISGID) && (mode & S_IXGRP));
27462747
}
27472748

2749+
static inline int check_sticky(struct inode *dir, struct inode *inode)
2750+
{
2751+
if (!(dir->i_mode & S_ISVTX))
2752+
return 0;
2753+
2754+
return __check_sticky(dir, inode);
2755+
}
2756+
27482757
static inline void inode_has_no_xattr(struct inode *inode)
27492758
{
27502759
if (!is_sxid(inode->i_mode) && (inode->i_sb->s_flags & MS_NOSEC))

0 commit comments

Comments
 (0)