Skip to content

Commit

Permalink
fs: move mapping helpers
Browse files Browse the repository at this point in the history
The low-level mapping helpers were so far crammed into fs.h. They are
out of place there. The fs.h header should just contain the higher-level
mapping helpers that interact directly with vfs objects such as struct
super_block or struct inode and not the bare mapping helpers. Similarly,
only vfs and specific fs code shall interact with low-level mapping
helpers. And so they won't be made accessible automatically through
regular {g,u}id helpers.

Link: https://lore.kernel.org/r/[email protected] (v1)
Link: https://lore.kernel.org/r/[email protected] (v2)
Link: https://lore.kernel.org/r/[email protected]
Cc: Seth Forshee <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: Al Viro <[email protected]>
CC: [email protected]
Reviewed-by: Amir Goldstein <[email protected]>
Reviewed-by: Seth Forshee <[email protected]>
Signed-off-by: Christian Brauner <[email protected]>
  • Loading branch information
Christian Brauner committed Dec 3, 2021
1 parent bb49e9e commit a793d79
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 90 deletions.
1 change: 1 addition & 0 deletions fs/ksmbd/smbacl.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/mnt_idmapping.h>

#include "smbacl.h"
#include "smb_common.h"
Expand Down
1 change: 1 addition & 0 deletions fs/ksmbd/smbacl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <linux/fs.h>
#include <linux/namei.h>
#include <linux/posix_acl.h>
#include <linux/mnt_idmapping.h>

#include "mgmt/tree_connect.h"

Expand Down
1 change: 1 addition & 0 deletions fs/open.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <linux/ima.h>
#include <linux/dnotify.h>
#include <linux/compat.h>
#include <linux/mnt_idmapping.h>

#include "internal.h"

Expand Down
1 change: 1 addition & 0 deletions fs/posix_acl.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <linux/export.h>
#include <linux/user_namespace.h>
#include <linux/namei.h>
#include <linux/mnt_idmapping.h>

static struct posix_acl **acl_by_type(struct inode *inode, int type)
{
Expand Down
1 change: 1 addition & 0 deletions fs/xfs/xfs_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ typedef __u32 xfs_nlink_t;
#include <linux/ratelimit.h>
#include <linux/rhashtable.h>
#include <linux/xattr.h>
#include <linux/mnt_idmapping.h>

#include <asm/page.h>
#include <asm/div64.h>
Expand Down
91 changes: 1 addition & 90 deletions include/linux/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <linux/stddef.h>
#include <linux/mount.h>
#include <linux/cred.h>
#include <linux/mnt_idmapping.h>

#include <asm/byteorder.h>
#include <uapi/linux/fs.h>
Expand Down Expand Up @@ -1624,34 +1625,6 @@ static inline void i_gid_write(struct inode *inode, gid_t gid)
inode->i_gid = make_kgid(inode->i_sb->s_user_ns, gid);
}

/**
* kuid_into_mnt - map a kuid down into a mnt_userns
* @mnt_userns: user namespace of the relevant mount
* @kuid: kuid to be mapped
*
* Return: @kuid mapped according to @mnt_userns.
* If @kuid has no mapping INVALID_UID is returned.
*/
static inline kuid_t kuid_into_mnt(struct user_namespace *mnt_userns,
kuid_t kuid)
{
return make_kuid(mnt_userns, __kuid_val(kuid));
}

/**
* kgid_into_mnt - map a kgid down into a mnt_userns
* @mnt_userns: user namespace of the relevant mount
* @kgid: kgid to be mapped
*
* Return: @kgid mapped according to @mnt_userns.
* If @kgid has no mapping INVALID_GID is returned.
*/
static inline kgid_t kgid_into_mnt(struct user_namespace *mnt_userns,
kgid_t kgid)
{
return make_kgid(mnt_userns, __kgid_val(kgid));
}

/**
* i_uid_into_mnt - map an inode's i_uid down into a mnt_userns
* @mnt_userns: user namespace of the mount the inode was found from
Expand Down Expand Up @@ -1680,68 +1653,6 @@ static inline kgid_t i_gid_into_mnt(struct user_namespace *mnt_userns,
return kgid_into_mnt(mnt_userns, inode->i_gid);
}

/**
* kuid_from_mnt - map a kuid up into a mnt_userns
* @mnt_userns: user namespace of the relevant mount
* @kuid: kuid to be mapped
*
* Return: @kuid mapped up according to @mnt_userns.
* If @kuid has no mapping INVALID_UID is returned.
*/
static inline kuid_t kuid_from_mnt(struct user_namespace *mnt_userns,
kuid_t kuid)
{
return KUIDT_INIT(from_kuid(mnt_userns, kuid));
}

/**
* kgid_from_mnt - map a kgid up into a mnt_userns
* @mnt_userns: user namespace of the relevant mount
* @kgid: kgid to be mapped
*
* Return: @kgid mapped up according to @mnt_userns.
* If @kgid has no mapping INVALID_GID is returned.
*/
static inline kgid_t kgid_from_mnt(struct user_namespace *mnt_userns,
kgid_t kgid)
{
return KGIDT_INIT(from_kgid(mnt_userns, kgid));
}

/**
* mapped_fsuid - return caller's fsuid mapped up into a mnt_userns
* @mnt_userns: user namespace of the relevant mount
*
* Use this helper to initialize a new vfs or filesystem object based on
* the caller's fsuid. A common example is initializing the i_uid field of
* a newly allocated inode triggered by a creation event such as mkdir or
* O_CREAT. Other examples include the allocation of quotas for a specific
* user.
*
* Return: the caller's current fsuid mapped up according to @mnt_userns.
*/
static inline kuid_t mapped_fsuid(struct user_namespace *mnt_userns)
{
return kuid_from_mnt(mnt_userns, current_fsuid());
}

/**
* mapped_fsgid - return caller's fsgid mapped up into a mnt_userns
* @mnt_userns: user namespace of the relevant mount
*
* Use this helper to initialize a new vfs or filesystem object based on
* the caller's fsgid. A common example is initializing the i_gid field of
* a newly allocated inode triggered by a creation event such as mkdir or
* O_CREAT. Other examples include the allocation of quotas for a specific
* user.
*
* Return: the caller's current fsgid mapped up according to @mnt_userns.
*/
static inline kgid_t mapped_fsgid(struct user_namespace *mnt_userns)
{
return kgid_from_mnt(mnt_userns, current_fsgid());
}

/**
* inode_fsuid_set - initialize inode's i_uid field with callers fsuid
* @inode: inode to initialize
Expand Down
101 changes: 101 additions & 0 deletions include/linux/mnt_idmapping.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _LINUX_MNT_IDMAPPING_H
#define _LINUX_MNT_IDMAPPING_H

#include <linux/types.h>
#include <linux/uidgid.h>

struct user_namespace;
extern struct user_namespace init_user_ns;

/**
* kuid_into_mnt - map a kuid down into a mnt_userns
* @mnt_userns: user namespace of the relevant mount
* @kuid: kuid to be mapped
*
* Return: @kuid mapped according to @mnt_userns.
* If @kuid has no mapping INVALID_UID is returned.
*/
static inline kuid_t kuid_into_mnt(struct user_namespace *mnt_userns,
kuid_t kuid)
{
return make_kuid(mnt_userns, __kuid_val(kuid));
}

/**
* kgid_into_mnt - map a kgid down into a mnt_userns
* @mnt_userns: user namespace of the relevant mount
* @kgid: kgid to be mapped
*
* Return: @kgid mapped according to @mnt_userns.
* If @kgid has no mapping INVALID_GID is returned.
*/
static inline kgid_t kgid_into_mnt(struct user_namespace *mnt_userns,
kgid_t kgid)
{
return make_kgid(mnt_userns, __kgid_val(kgid));
}

/**
* kuid_from_mnt - map a kuid up into a mnt_userns
* @mnt_userns: user namespace of the relevant mount
* @kuid: kuid to be mapped
*
* Return: @kuid mapped up according to @mnt_userns.
* If @kuid has no mapping INVALID_UID is returned.
*/
static inline kuid_t kuid_from_mnt(struct user_namespace *mnt_userns,
kuid_t kuid)
{
return KUIDT_INIT(from_kuid(mnt_userns, kuid));
}

/**
* kgid_from_mnt - map a kgid up into a mnt_userns
* @mnt_userns: user namespace of the relevant mount
* @kgid: kgid to be mapped
*
* Return: @kgid mapped up according to @mnt_userns.
* If @kgid has no mapping INVALID_GID is returned.
*/
static inline kgid_t kgid_from_mnt(struct user_namespace *mnt_userns,
kgid_t kgid)
{
return KGIDT_INIT(from_kgid(mnt_userns, kgid));
}

/**
* mapped_fsuid - return caller's fsuid mapped up into a mnt_userns
* @mnt_userns: user namespace of the relevant mount
*
* Use this helper to initialize a new vfs or filesystem object based on
* the caller's fsuid. A common example is initializing the i_uid field of
* a newly allocated inode triggered by a creation event such as mkdir or
* O_CREAT. Other examples include the allocation of quotas for a specific
* user.
*
* Return: the caller's current fsuid mapped up according to @mnt_userns.
*/
static inline kuid_t mapped_fsuid(struct user_namespace *mnt_userns)
{
return kuid_from_mnt(mnt_userns, current_fsuid());
}

/**
* mapped_fsgid - return caller's fsgid mapped up into a mnt_userns
* @mnt_userns: user namespace of the relevant mount
*
* Use this helper to initialize a new vfs or filesystem object based on
* the caller's fsgid. A common example is initializing the i_gid field of
* a newly allocated inode triggered by a creation event such as mkdir or
* O_CREAT. Other examples include the allocation of quotas for a specific
* user.
*
* Return: the caller's current fsgid mapped up according to @mnt_userns.
*/
static inline kgid_t mapped_fsgid(struct user_namespace *mnt_userns)
{
return kgid_from_mnt(mnt_userns, current_fsgid());
}

#endif /* _LINUX_MNT_IDMAPPING_H */
1 change: 1 addition & 0 deletions security/commoncap.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <linux/user_namespace.h>
#include <linux/binfmts.h>
#include <linux/personality.h>
#include <linux/mnt_idmapping.h>

/*
* If a non-root user executes a setuid-root binary in
Expand Down

0 comments on commit a793d79

Please sign in to comment.