From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 08DBFEB64D9 for ; Tue, 4 Jul 2023 11:43:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230521AbjGDLnJ (ORCPT ); Tue, 4 Jul 2023 07:43:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230374AbjGDLnD (ORCPT ); Tue, 4 Jul 2023 07:43:03 -0400 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6ADD3119; Tue, 4 Jul 2023 04:43:01 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4QwLWg24Y8z4f3p02; Tue, 4 Jul 2023 19:42:55 +0800 (CST) Received: from [10.174.176.117] (unknown [10.174.176.117]) by APP2 (Coremail) with SMTP id Syh0CgCXf929BaRkmuBrNA--.48562S2; Tue, 04 Jul 2023 19:42:57 +0800 (CST) Subject: Re: [PATCH v1] fs: Add kfuncs to handle idmapped mounts To: Alexey Gladkov Cc: bpf@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Alexander Viro , Alexei Starovoitov , Christian Brauner References: From: Hou Tao Message-ID: Date: Tue, 4 Jul 2023 19:42:53 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Content-Language: en-US X-CM-TRANSID: Syh0Cg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Info: xkrx3t3r6k3tpzhluzxrxghudrp/ X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Hi, On 6/30/2023 7:08 PM, Alexey Gladkov wrote: > Since the introduction of idmapped mounts, file handling has become > somewhat more complicated. If the inode has been found through an > idmapped mount the idmap of the vfsmount must be used to get proper > i_uid / i_gid. This is important, for example, to correctly take into > account idmapped files when caching, LSM or for an audit. Could you please add a bpf selftest for these newly added kfuncs ? > > Signed-off-by: Alexey Gladkov > --- > fs/mnt_idmapping.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 69 insertions(+) > > diff --git a/fs/mnt_idmapping.c b/fs/mnt_idmapping.c > index 4905665c47d0..ba98ce26b883 100644 > --- a/fs/mnt_idmapping.c > +++ b/fs/mnt_idmapping.c > @@ -6,6 +6,7 @@ > #include > #include > #include > +#include > > #include "internal.h" > > @@ -271,3 +272,71 @@ void mnt_idmap_put(struct mnt_idmap *idmap) > kfree(idmap); > } > } > + > +__diag_push(); > +__diag_ignore_all("-Wmissing-prototypes", > + "Global functions as their definitions will be in vmlinux BTF"); > + > +/** > + * bpf_is_idmapped_mnt - check whether a mount is idmapped > + * @mnt: the mount to check > + * > + * Return: true if mount is mapped, false if not. > + */ > +__bpf_kfunc bool bpf_is_idmapped_mnt(struct vfsmount *mnt) > +{ > + return is_idmapped_mnt(mnt); > +} > + > +/** > + * bpf_file_mnt_idmap - get file idmapping > + * @file: the file from which to get mapping > + * > + * Return: The idmap for the @file. > + */ > +__bpf_kfunc struct mnt_idmap *bpf_file_mnt_idmap(struct file *file) > +{ > + return file_mnt_idmap(file); > +} A dummy question here: the implementation of file_mnt_idmap() is file->f_path.mnt->mnt_idmap, so if the passed file is a BTF pointer, is there any reason why we could not do such dereference directly in bpf program ? > + > +/** > + * bpf_inode_into_vfs_ids - map an inode's i_uid and i_gid down according to an idmapping > + * @idmap: idmap of the mount the inode was found from > + * @inode: inode to map > + * > + * The inode's i_uid and i_gid mapped down according to @idmap. If the inode's > + * i_uid or i_gid has no mapping INVALID_VFSUID or INVALID_VFSGID is returned in > + * the corresponding position. > + * > + * Return: A 64-bit integer containing the current GID and UID, and created as > + * such: *gid* **<< 32 \|** *uid*. > + */ > +__bpf_kfunc uint64_t bpf_inode_into_vfs_ids(struct mnt_idmap *idmap, > + const struct inode *inode) > +{ > + vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode); > + vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode); > + > + return (u64) __vfsgid_val(vfsgid) << 32 | > + __vfsuid_val(vfsuid); > +} > + > +__diag_pop(); > + > +BTF_SET8_START(idmap_btf_ids) > +BTF_ID_FLAGS(func, bpf_is_idmapped_mnt) > +BTF_ID_FLAGS(func, bpf_file_mnt_idmap) > +BTF_ID_FLAGS(func, bpf_inode_into_vfs_ids) > +BTF_SET8_END(idmap_btf_ids) > + > +static const struct btf_kfunc_id_set idmap_kfunc_set = { > + .owner = THIS_MODULE, > + .set = &idmap_btf_ids, > +}; > + > +static int __init bpf_idmap_kfunc_init(void) > +{ > + return register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, &idmap_kfunc_set); > +} > + Is BPF_PROG_TYPE_TRACING sufficient for your use case ? It seems BPF_PROG_TYPE_UNSPEC will make these kfuncs be available for all bpf program types. > +late_initcall(bpf_idmap_kfunc_init);