Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp27571689rwd; Tue, 4 Jul 2023 05:03:27 -0700 (PDT) X-Google-Smtp-Source: APBJJlExQ2IioWS9mE/Kyj6T885++01T2yVP+DVCRon9Bq5mC+k60LKEAxf21yxUUr44s2Lsjcga X-Received: by 2002:a05:6a20:2587:b0:12d:9f20:fcaa with SMTP id k7-20020a056a20258700b0012d9f20fcaamr11371767pzd.57.1688472206903; Tue, 04 Jul 2023 05:03:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688472206; cv=none; d=google.com; s=arc-20160816; b=s7+Km/Uu4rUVmMEjnouNRTBdejpqqbjZf4CtEIy9Qi7wLnsQtN5w1/UAw1Ln1GAT2i zqcmylbZQTnZZTkDFgwHQUL8eFZ+pfXISXRSz1+eR5xig9nGksUZ9ivWVJ/QGx/xaqDq n2bZF5RKE8wuvL+neNWNNL/aeZpxVWP7KJI7VmvdLfjdbxXKq/3rOhbKXzMFyZFs+s4C 6dTm0R1s7HK0jL6geL6Ti8+CW1lI/9LrvyxmdhoDxA3Tjh3/xXIGkmCdJxd2GHn+hzJy BnK39sgCXkx005p4PVg3VxCPhkdpz97jOy/3a/9YxTG3bJSld7fvf9rWOhkDwuLAxcnl QiMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=mPsHuZvwXcILAdGh7vNDTaFps6aYEW8i11rMEKHlXEM=; fh=JnP/V/YEk17g9WDdOjs+jVo17CaBfLjihS0WAOoIhio=; b=RVSHr6VjJU/UF4rtLPV03BtMbVM9RtB7fGN9Vs0IXx88n4RpJ0sKl/xPHpPkCxzlJO PhqFWs8fVqUDitcw6gJ5pGAEaXtg2krcxcjZE3u7uWKvw7BNgVMWd7pQawg0FD4cIrRi oQGCGIJ84jSox1s7/Q5mNbS8KehMh/FcGjKsScuxS4X5JYqzLEnq+GQynRAMNqrjcA2H 7Ng4D5KzCP0hEmSA6D1NmXtXXDqRHYlygSi35AxQUpr8QXDsN1wcj/j5vFL6LKzFdnYD 3GTiI8eum7VWT/HH3poQTT3m0nlidwDKS8L2XSt9GGWEI5tPY7ZF+9fCnR+TxXNQD4Lg drGA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x12-20020a1709027c0c00b001b55070e154si17365187pll.96.2023.07.04.05.03.13; Tue, 04 Jul 2023 05:03:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229647AbjGDLnJ (ORCPT + 99 others); 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: Syh0CgCXf929BaRkmuBrNA--.48562S2 X-Coremail-Antispam: 1UD129KBjvJXoWxZw45CF17XF43Xr1DGF1DAwb_yoW5tw13pF 4FkFn5Cr40qryagw1fJFyF9F4YgF97C3WUZr1xW3s8Ar1qgr1ftF4Ik3Z8Xr4rJr4kGw18 WF1jgrWkury3JrJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUkjb4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x 0267AKxVW0oVCq3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG 6I80ewAv7VC0I7IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFV Cjc4AY6r1j6r4UM4x0Y48IcVAKI48JMxk0xIA0c2IEe2xFo4CEbIxvr21l42xK82IYc2Ij 64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x 8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0xvE 2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE42 xK8VAvwI8IcIk0rVWrZr1j6s0DMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIE c7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x07UWE__UUUUU= X-CM-SenderInfo: xkrx3t3r6k3tpzhluzxrxghudrp/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,NICE_REPLY_A, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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);