mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-11-24 12:44:11 +08:00
mnt_idmapping: add vfs{g,u}id_t
Introduces new vfs{g,u}id_t types. Similar to k{g,u}id_t the new types are just simple wrapper structs around regular {g,u}id_t types. They allows to establish a type safety boundary between {g,u}ids on idmapped mounts and {g,u}ids as they are represented in filesystems themselves. A vfs{g,u}id_t is always created from a k{g,u}id_t, never directly from a {g,u}id_t as idmapped mounts remap a given {g,u}id according to the mount's idmapping. This is expressed in the VFS{G,U}IDT_INIT() macros. A vfs{g,u}id_t may be used as a k{g,u}id_t via AS_K{G,U}IDT(). This often happens when we need to check whether a {g,u}id mapped according to an idmapped mount is identical to a given k{g,u}id_t. For an example, see vfsgid_in_group_p() which determines whether the value of vfsgid_t matches the value of any of the caller's groups. Similar logic is expressed in the k{g,u}id_eq_vfs{g,u}id(). The from_vfs{g,u}id() helpers map a given vfs{g,u}id_t from the mount's idmapping into the filesystem idmapping. They make it possible to update a filesystem object such as inode->i_{g,u}id with the correct value. This makes it harder to accidently write a wrong {g,u}id anwywhere. The vfs{g,u}id_has_fsmapping() helpers check whether a given vfs{g,u}id_t can be mapped into the filesystem idmapping. All new helpers are nops on non-idmapped mounts. I've done work on this roughly 7 months ago but dropped it to focus on the testsuite. Linus brought this up independently just last week and it's time to move this along (see [1]). [1]: https://lore.kernel.org/lkml/CAHk-=win6+ahs1EwLkcq8apqLi_1wXFWbrPf340zYEhObpz4jA@mail.gmail.com Link: https://lore.kernel.org/r/20220621141454.2914719-2-brauner@kernel.org Cc: Seth Forshee <sforshee@digitalocean.com> Cc: Christoph Hellwig <hch@lst.de> Cc: Aleksa Sarai <cyphar@cyphar.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Al Viro <viro@zeniv.linux.org.uk> CC: linux-fsdevel@vger.kernel.org Reviewed-by: Seth Forshee <sforshee@digitalocean.com> Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
This commit is contained in:
parent
a111daf0c5
commit
1e5267cd08
@ -13,6 +13,122 @@ struct user_namespace;
|
||||
*/
|
||||
extern struct user_namespace init_user_ns;
|
||||
|
||||
typedef struct {
|
||||
uid_t val;
|
||||
} vfsuid_t;
|
||||
|
||||
typedef struct {
|
||||
gid_t val;
|
||||
} vfsgid_t;
|
||||
|
||||
#ifdef CONFIG_MULTIUSER
|
||||
static inline uid_t __vfsuid_val(vfsuid_t uid)
|
||||
{
|
||||
return uid.val;
|
||||
}
|
||||
|
||||
static inline gid_t __vfsgid_val(vfsgid_t gid)
|
||||
{
|
||||
return gid.val;
|
||||
}
|
||||
#else
|
||||
static inline uid_t __vfsuid_val(vfsuid_t uid)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline gid_t __vfsgid_val(vfsgid_t gid)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static inline bool vfsuid_valid(vfsuid_t uid)
|
||||
{
|
||||
return __vfsuid_val(uid) != (uid_t)-1;
|
||||
}
|
||||
|
||||
static inline bool vfsgid_valid(vfsgid_t gid)
|
||||
{
|
||||
return __vfsgid_val(gid) != (gid_t)-1;
|
||||
}
|
||||
|
||||
static inline bool vfsuid_eq(vfsuid_t left, vfsuid_t right)
|
||||
{
|
||||
return __vfsuid_val(left) == __vfsuid_val(right);
|
||||
}
|
||||
|
||||
static inline bool vfsgid_eq(vfsgid_t left, vfsgid_t right)
|
||||
{
|
||||
return __vfsgid_val(left) == __vfsgid_val(right);
|
||||
}
|
||||
|
||||
/**
|
||||
* vfsuid_eq_kuid - check whether kuid and vfsuid have the same value
|
||||
* @kuid: the kuid to compare
|
||||
* @vfsuid: the vfsuid to compare
|
||||
*
|
||||
* Check whether @kuid and @vfsuid have the same values.
|
||||
*
|
||||
* Return: true if @kuid and @vfsuid have the same value, false if not.
|
||||
*/
|
||||
static inline bool vfsuid_eq_kuid(vfsuid_t vfsuid, kuid_t kuid)
|
||||
{
|
||||
return __vfsuid_val(vfsuid) == __kuid_val(kuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* vfsgid_eq_kgid - check whether kgid and vfsgid have the same value
|
||||
* @kgid: the kgid to compare
|
||||
* @vfsgid: the vfsgid to compare
|
||||
*
|
||||
* Check whether @kgid and @vfsgid have the same values.
|
||||
*
|
||||
* Return: true if @kgid and @vfsgid have the same value, false if not.
|
||||
*/
|
||||
static inline bool vfsgid_eq_kgid(kgid_t kgid, vfsgid_t vfsgid)
|
||||
{
|
||||
return __vfsgid_val(vfsgid) == __kgid_val(kgid);
|
||||
}
|
||||
|
||||
/*
|
||||
* vfs{g,u}ids are created from k{g,u}ids.
|
||||
* We don't allow them to be created from regular {u,g}id.
|
||||
*/
|
||||
#define VFSUIDT_INIT(val) (vfsuid_t){ __kuid_val(val) }
|
||||
#define VFSGIDT_INIT(val) (vfsgid_t){ __kgid_val(val) }
|
||||
|
||||
#define INVALID_VFSUID VFSUIDT_INIT(INVALID_UID)
|
||||
#define INVALID_VFSGID VFSGIDT_INIT(INVALID_GID)
|
||||
|
||||
/*
|
||||
* Allow a vfs{g,u}id to be used as a k{g,u}id where we want to compare
|
||||
* whether the mapped value is identical to value of a k{g,u}id.
|
||||
*/
|
||||
#define AS_KUIDT(val) (kuid_t){ __vfsuid_val(val) }
|
||||
#define AS_KGIDT(val) (kgid_t){ __vfsgid_val(val) }
|
||||
|
||||
#ifdef CONFIG_MULTIUSER
|
||||
/**
|
||||
* vfsgid_in_group_p() - check whether a vfsuid matches the caller's groups
|
||||
* @vfsgid: the mnt gid to match
|
||||
*
|
||||
* This function can be used to determine whether @vfsuid matches any of the
|
||||
* caller's groups.
|
||||
*
|
||||
* Return: 1 if vfsuid matches caller's groups, 0 if not.
|
||||
*/
|
||||
static inline int vfsgid_in_group_p(vfsgid_t vfsgid)
|
||||
{
|
||||
return in_group_p(AS_KGIDT(vfsgid));
|
||||
}
|
||||
#else
|
||||
static inline int vfsgid_in_group_p(vfsgid_t vfsgid)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* initial_idmapping - check whether this is the initial mapping
|
||||
* @ns: idmapping to check
|
||||
@ -67,21 +183,29 @@ static inline bool no_idmapping(const struct user_namespace *mnt_userns,
|
||||
* If @kuid has no mapping in either @mnt_userns or @fs_userns INVALID_UID is
|
||||
* returned.
|
||||
*/
|
||||
static inline kuid_t mapped_kuid_fs(struct user_namespace *mnt_userns,
|
||||
struct user_namespace *fs_userns,
|
||||
kuid_t kuid)
|
||||
|
||||
static inline vfsuid_t make_vfsuid(struct user_namespace *mnt_userns,
|
||||
struct user_namespace *fs_userns,
|
||||
kuid_t kuid)
|
||||
{
|
||||
uid_t uid;
|
||||
|
||||
if (no_idmapping(mnt_userns, fs_userns))
|
||||
return kuid;
|
||||
return VFSUIDT_INIT(kuid);
|
||||
if (initial_idmapping(fs_userns))
|
||||
uid = __kuid_val(kuid);
|
||||
else
|
||||
uid = from_kuid(fs_userns, kuid);
|
||||
if (uid == (uid_t)-1)
|
||||
return INVALID_UID;
|
||||
return make_kuid(mnt_userns, uid);
|
||||
return INVALID_VFSUID;
|
||||
return VFSUIDT_INIT(make_kuid(mnt_userns, uid));
|
||||
}
|
||||
|
||||
static inline kuid_t mapped_kuid_fs(struct user_namespace *mnt_userns,
|
||||
struct user_namespace *fs_userns,
|
||||
kuid_t kuid)
|
||||
{
|
||||
return AS_KUIDT(make_vfsuid(mnt_userns, fs_userns, kuid));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -104,21 +228,56 @@ static inline kuid_t mapped_kuid_fs(struct user_namespace *mnt_userns,
|
||||
* If @kgid has no mapping in either @mnt_userns or @fs_userns INVALID_GID is
|
||||
* returned.
|
||||
*/
|
||||
static inline kgid_t mapped_kgid_fs(struct user_namespace *mnt_userns,
|
||||
struct user_namespace *fs_userns,
|
||||
kgid_t kgid)
|
||||
|
||||
static inline vfsgid_t make_vfsgid(struct user_namespace *mnt_userns,
|
||||
struct user_namespace *fs_userns,
|
||||
kgid_t kgid)
|
||||
{
|
||||
gid_t gid;
|
||||
|
||||
if (no_idmapping(mnt_userns, fs_userns))
|
||||
return kgid;
|
||||
return VFSGIDT_INIT(kgid);
|
||||
if (initial_idmapping(fs_userns))
|
||||
gid = __kgid_val(kgid);
|
||||
else
|
||||
gid = from_kgid(fs_userns, kgid);
|
||||
if (gid == (gid_t)-1)
|
||||
return INVALID_GID;
|
||||
return make_kgid(mnt_userns, gid);
|
||||
return INVALID_VFSGID;
|
||||
return VFSGIDT_INIT(make_kgid(mnt_userns, gid));
|
||||
}
|
||||
|
||||
static inline kgid_t mapped_kgid_fs(struct user_namespace *mnt_userns,
|
||||
struct user_namespace *fs_userns,
|
||||
kgid_t kgid)
|
||||
{
|
||||
return AS_KGIDT(make_vfsgid(mnt_userns, fs_userns, kgid));
|
||||
}
|
||||
|
||||
/**
|
||||
* from_vfsuid - map a vfsuid into the filesystem idmapping
|
||||
* @mnt_userns: the mount's idmapping
|
||||
* @fs_userns: the filesystem's idmapping
|
||||
* @vfsuid : vfsuid to be mapped
|
||||
*
|
||||
* Map @vfsuid into the filesystem idmapping. This function has to be used in
|
||||
* order to e.g. write @vfsuid to inode->i_uid.
|
||||
*
|
||||
* Return: @vfsuid mapped into the filesystem idmapping
|
||||
*/
|
||||
static inline kuid_t from_vfsuid(struct user_namespace *mnt_userns,
|
||||
struct user_namespace *fs_userns,
|
||||
vfsuid_t vfsuid)
|
||||
{
|
||||
uid_t uid;
|
||||
|
||||
if (no_idmapping(mnt_userns, fs_userns))
|
||||
return AS_KUIDT(vfsuid);
|
||||
uid = from_kuid(mnt_userns, AS_KUIDT(vfsuid));
|
||||
if (uid == (uid_t)-1)
|
||||
return INVALID_UID;
|
||||
if (initial_idmapping(fs_userns))
|
||||
return KUIDT_INIT(uid);
|
||||
return make_kuid(fs_userns, uid);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -145,16 +304,53 @@ static inline kuid_t mapped_kuid_user(struct user_namespace *mnt_userns,
|
||||
struct user_namespace *fs_userns,
|
||||
kuid_t kuid)
|
||||
{
|
||||
uid_t uid;
|
||||
return from_vfsuid(mnt_userns, fs_userns, VFSUIDT_INIT(kuid));
|
||||
}
|
||||
|
||||
/**
|
||||
* vfsuid_has_fsmapping - check whether a vfsuid maps into the filesystem
|
||||
* @mnt_userns: the mount's idmapping
|
||||
* @fs_userns: the filesystem's idmapping
|
||||
* @vfsuid: vfsuid to be mapped
|
||||
*
|
||||
* Check whether @vfsuid has a mapping in the filesystem idmapping. Use this
|
||||
* function to check whether the filesystem idmapping has a mapping for
|
||||
* @vfsuid.
|
||||
*
|
||||
* Return: true if @vfsuid has a mapping in the filesystem, false if not.
|
||||
*/
|
||||
static inline bool vfsuid_has_fsmapping(struct user_namespace *mnt_userns,
|
||||
struct user_namespace *fs_userns,
|
||||
vfsuid_t vfsuid)
|
||||
{
|
||||
return uid_valid(from_vfsuid(mnt_userns, fs_userns, vfsuid));
|
||||
}
|
||||
|
||||
/**
|
||||
* from_vfsgid - map a vfsgid into the filesystem idmapping
|
||||
* @mnt_userns: the mount's idmapping
|
||||
* @fs_userns: the filesystem's idmapping
|
||||
* @vfsgid : vfsgid to be mapped
|
||||
*
|
||||
* Map @vfsgid into the filesystem idmapping. This function has to be used in
|
||||
* order to e.g. write @vfsgid to inode->i_gid.
|
||||
*
|
||||
* Return: @vfsgid mapped into the filesystem idmapping
|
||||
*/
|
||||
static inline kgid_t from_vfsgid(struct user_namespace *mnt_userns,
|
||||
struct user_namespace *fs_userns,
|
||||
vfsgid_t vfsgid)
|
||||
{
|
||||
gid_t gid;
|
||||
|
||||
if (no_idmapping(mnt_userns, fs_userns))
|
||||
return kuid;
|
||||
uid = from_kuid(mnt_userns, kuid);
|
||||
if (uid == (uid_t)-1)
|
||||
return INVALID_UID;
|
||||
return AS_KGIDT(vfsgid);
|
||||
gid = from_kgid(mnt_userns, AS_KGIDT(vfsgid));
|
||||
if (gid == (gid_t)-1)
|
||||
return INVALID_GID;
|
||||
if (initial_idmapping(fs_userns))
|
||||
return KUIDT_INIT(uid);
|
||||
return make_kuid(fs_userns, uid);
|
||||
return KGIDT_INIT(gid);
|
||||
return make_kgid(fs_userns, gid);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -181,16 +377,26 @@ static inline kgid_t mapped_kgid_user(struct user_namespace *mnt_userns,
|
||||
struct user_namespace *fs_userns,
|
||||
kgid_t kgid)
|
||||
{
|
||||
gid_t gid;
|
||||
return from_vfsgid(mnt_userns, fs_userns, VFSGIDT_INIT(kgid));
|
||||
}
|
||||
|
||||
if (no_idmapping(mnt_userns, fs_userns))
|
||||
return kgid;
|
||||
gid = from_kgid(mnt_userns, kgid);
|
||||
if (gid == (gid_t)-1)
|
||||
return INVALID_GID;
|
||||
if (initial_idmapping(fs_userns))
|
||||
return KGIDT_INIT(gid);
|
||||
return make_kgid(fs_userns, gid);
|
||||
/**
|
||||
* vfsgid_has_fsmapping - check whether a vfsgid maps into the filesystem
|
||||
* @mnt_userns: the mount's idmapping
|
||||
* @fs_userns: the filesystem's idmapping
|
||||
* @vfsgid: vfsgid to be mapped
|
||||
*
|
||||
* Check whether @vfsgid has a mapping in the filesystem idmapping. Use this
|
||||
* function to check whether the filesystem idmapping has a mapping for
|
||||
* @vfsgid.
|
||||
*
|
||||
* Return: true if @vfsgid has a mapping in the filesystem, false if not.
|
||||
*/
|
||||
static inline bool vfsgid_has_fsmapping(struct user_namespace *mnt_userns,
|
||||
struct user_namespace *fs_userns,
|
||||
vfsgid_t vfsgid)
|
||||
{
|
||||
return gid_valid(from_vfsgid(mnt_userns, fs_userns, vfsgid));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user