mirror of
https://github.com/hardkernel/linux.git
synced 2026-06-09 12:17:12 +09:00
attr: add in_group_or_capable()
commit 11c2a8700c upstream.
[backported to 5.10.y, prior to idmapped mounts]
In setattr_{copy,prepare}() we need to perform the same permission
checks to determine whether we need to drop the setgid bit or not.
Instead of open-coding it twice add a simple helper the encapsulates the
logic. We will reuse this helpers to make dropping the setgid bit during
write operations more consistent in a follow up patch.
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
committed by
Greg Kroah-Hartman
parent
94ac142c19
commit
24378d6f74
11
fs/attr.c
11
fs/attr.c
@@ -18,6 +18,8 @@
|
|||||||
#include <linux/evm.h>
|
#include <linux/evm.h>
|
||||||
#include <linux/ima.h>
|
#include <linux/ima.h>
|
||||||
|
|
||||||
|
#include "internal.h"
|
||||||
|
|
||||||
static bool chown_ok(const struct inode *inode, kuid_t uid)
|
static bool chown_ok(const struct inode *inode, kuid_t uid)
|
||||||
{
|
{
|
||||||
if (uid_eq(current_fsuid(), inode->i_uid) &&
|
if (uid_eq(current_fsuid(), inode->i_uid) &&
|
||||||
@@ -90,9 +92,8 @@ int setattr_prepare(struct dentry *dentry, struct iattr *attr)
|
|||||||
if (!inode_owner_or_capable(inode))
|
if (!inode_owner_or_capable(inode))
|
||||||
return -EPERM;
|
return -EPERM;
|
||||||
/* Also check the setgid bit! */
|
/* Also check the setgid bit! */
|
||||||
if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
|
if (!in_group_or_capable(inode, (ia_valid & ATTR_GID) ?
|
||||||
inode->i_gid) &&
|
attr->ia_gid : inode->i_gid))
|
||||||
!capable_wrt_inode_uidgid(inode, CAP_FSETID))
|
|
||||||
attr->ia_mode &= ~S_ISGID;
|
attr->ia_mode &= ~S_ISGID;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,9 +194,7 @@ void setattr_copy(struct inode *inode, const struct iattr *attr)
|
|||||||
inode->i_ctime = attr->ia_ctime;
|
inode->i_ctime = attr->ia_ctime;
|
||||||
if (ia_valid & ATTR_MODE) {
|
if (ia_valid & ATTR_MODE) {
|
||||||
umode_t mode = attr->ia_mode;
|
umode_t mode = attr->ia_mode;
|
||||||
|
if (!in_group_or_capable(inode, inode->i_gid))
|
||||||
if (!in_group_p(inode->i_gid) &&
|
|
||||||
!capable_wrt_inode_uidgid(inode, CAP_FSETID))
|
|
||||||
mode &= ~S_ISGID;
|
mode &= ~S_ISGID;
|
||||||
inode->i_mode = mode;
|
inode->i_mode = mode;
|
||||||
}
|
}
|
||||||
|
|||||||
25
fs/inode.c
25
fs/inode.c
@@ -2379,6 +2379,26 @@ int vfs_ioc_fssetxattr_check(struct inode *inode, const struct fsxattr *old_fa,
|
|||||||
}
|
}
|
||||||
EXPORT_SYMBOL(vfs_ioc_fssetxattr_check);
|
EXPORT_SYMBOL(vfs_ioc_fssetxattr_check);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* in_group_or_capable - check whether caller is CAP_FSETID privileged
|
||||||
|
* @inode: inode to check
|
||||||
|
* @gid: the new/current gid of @inode
|
||||||
|
*
|
||||||
|
* Check wether @gid is in the caller's group list or if the caller is
|
||||||
|
* privileged with CAP_FSETID over @inode. This can be used to determine
|
||||||
|
* whether the setgid bit can be kept or must be dropped.
|
||||||
|
*
|
||||||
|
* Return: true if the caller is sufficiently privileged, false if not.
|
||||||
|
*/
|
||||||
|
bool in_group_or_capable(const struct inode *inode, kgid_t gid)
|
||||||
|
{
|
||||||
|
if (in_group_p(gid))
|
||||||
|
return true;
|
||||||
|
if (capable_wrt_inode_uidgid(inode, CAP_FSETID))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* mode_strip_sgid - handle the sgid bit for non-directories
|
* mode_strip_sgid - handle the sgid bit for non-directories
|
||||||
* @dir: parent directory inode
|
* @dir: parent directory inode
|
||||||
@@ -2398,11 +2418,8 @@ umode_t mode_strip_sgid(const struct inode *dir, umode_t mode)
|
|||||||
return mode;
|
return mode;
|
||||||
if (S_ISDIR(mode) || !dir || !(dir->i_mode & S_ISGID))
|
if (S_ISDIR(mode) || !dir || !(dir->i_mode & S_ISGID))
|
||||||
return mode;
|
return mode;
|
||||||
if (in_group_p(dir->i_gid))
|
if (in_group_or_capable(dir, dir->i_gid))
|
||||||
return mode;
|
return mode;
|
||||||
if (capable_wrt_inode_uidgid(dir, CAP_FSETID))
|
|
||||||
return mode;
|
|
||||||
|
|
||||||
return mode & ~S_ISGID;
|
return mode & ~S_ISGID;
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL(mode_strip_sgid);
|
EXPORT_SYMBOL(mode_strip_sgid);
|
||||||
|
|||||||
@@ -149,6 +149,7 @@ extern int vfs_open(const struct path *, struct file *);
|
|||||||
extern long prune_icache_sb(struct super_block *sb, struct shrink_control *sc);
|
extern long prune_icache_sb(struct super_block *sb, struct shrink_control *sc);
|
||||||
extern void inode_add_lru(struct inode *inode);
|
extern void inode_add_lru(struct inode *inode);
|
||||||
extern int dentry_needs_remove_privs(struct dentry *dentry);
|
extern int dentry_needs_remove_privs(struct dentry *dentry);
|
||||||
|
bool in_group_or_capable(const struct inode *inode, kgid_t gid);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* fs-writeback.c
|
* fs-writeback.c
|
||||||
|
|||||||
Reference in New Issue
Block a user