Message ID | c35fbb4cb0a3a9b4653f9a032698469d94ca6e9c.1688123230.git.legion@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp10281602vqr; Fri, 30 Jun 2023 04:24:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ52OO12bz4O0/YJN6/eNoSGBgyMR/5G6K7jncOIkp08UOzjr9rxXjrKKGemLyEByYTuCfjp X-Received: by 2002:a17:90a:86ca:b0:259:a7a6:26f9 with SMTP id y10-20020a17090a86ca00b00259a7a626f9mr8684567pjv.21.1688124264789; Fri, 30 Jun 2023 04:24:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688124264; cv=none; d=google.com; s=arc-20160816; b=DdB8+fjcxoYV4eQkpfcHXj2iqQqRWhdX7vSd2NDjf88gU6s4Nz6JVSACxIOg9c5v9O LjwoDMnWae20N4LYwWerE0XD+L76HQPeTdXnrYa3HgztvwXRHezgffnox1nanFMg53ki jdEs43dpI6lNKVwPUZr5F5D57AhtkFdAfM7BupELzN3iTusfvw7yhx4tdwDez6/78KDN gaOo4n2aylU+nqsGQuLqhbbqkzvlbFChfewaXIFgDVBhr04U3sqnk7KyiKGOpxqPrjlc hcTVpOp84mn75ydN9cmS7OhKUW+AOczmEjXE/lLkpEvYzrBeXexAR5NuGdiWQQ1y7eg+ GzGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=XIBqZCpBzLJtgaJ/9XIBwuSlT4WWVBuaA/7b7B0jloI=; fh=OArF2ZhbGojyOQqjX3bgmAq2CC7CD7GTko4v/7ZF9uw=; b=hovDP+nfdfxuNXgvH2ujVw6V/Sb/J7RHSiP/Yx2TWshEHee/8+g9UqQqmM6MbvNA0F ZYUbPZV+t7Do5Gp5PBIf9fvPXjZuf2gwLHC24V6AO1Fh6A4TpO83vljH2ayYvvkIQN+b ZfnAomxGdmY8C7EwHUmtvDT3P9oy67OZ8GWDdXWp3ToY7ZEMK0VsKHSFPxlb0AGeJV/+ YBUe09kMXC2PntLsFfOw90Wk4Tm0ymCDe/DP0QCWzmip/psgFK5BIwGHAsp6PkkSqwzJ eINRsn01iJKIgtxfyH5IPig3ycQu/Kf/Q9pIxSs5pLe6W3ppiBGVjQJoWOrrOgsVyadd PSGA== 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 11-20020a630f4b000000b00553c2f85085si12569062pgp.220.2023.06.30.04.24.11; Fri, 30 Jun 2023 04:24:24 -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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232783AbjF3LM2 (ORCPT <rfc822;nicolai.engesland@gmail.com> + 99 others); Fri, 30 Jun 2023 07:12:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232316AbjF3LL7 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 30 Jun 2023 07:11:59 -0400 X-Greylist: delayed 67 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Fri, 30 Jun 2023 04:11:51 PDT Received: from us-smtp-delivery-44.mimecast.com (us-smtp-delivery-44.mimecast.com [205.139.111.44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4215E4206 for <linux-kernel@vger.kernel.org>; Fri, 30 Jun 2023 04:11:50 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-58-9uE2y5jYOzqoK4RnKsOZFA-1; Fri, 30 Jun 2023 07:10:42 -0400 X-MC-Unique: 9uE2y5jYOzqoK4RnKsOZFA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9289310504B0; Fri, 30 Jun 2023 11:10:41 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.45.226.211]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4E03F200A3AD; Fri, 30 Jun 2023 11:10:40 +0000 (UTC) From: Alexey Gladkov <legion@kernel.org> To: Alexander Viro <viro@zeniv.linux.org.uk>, Alexei Starovoitov <alexei.starovoitov@gmail.com>, Christian Brauner <brauner@kernel.org> Cc: bpf@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v1] fs: Add kfuncs to handle idmapped mounts Date: Fri, 30 Jun 2023 13:08:25 +0200 Message-Id: <c35fbb4cb0a3a9b4653f9a032698469d94ca6e9c.1688123230.git.legion@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_SOFTFAIL,T_SCC_BODY_TEXT_LINE autolearn=no 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770126588933584884?= X-GMAIL-MSGID: =?utf-8?q?1770126588933584884?= |
Series |
[v1] fs: Add kfuncs to handle idmapped mounts
|
|
Commit Message
Alexey Gladkov
June 30, 2023, 11:08 a.m. UTC
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.
Signed-off-by: Alexey Gladkov <legion@kernel.org>
---
fs/mnt_idmapping.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 69 insertions(+)
Comments
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 <legion@kernel.org> > --- > 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 <linux/mnt_idmapping.h> > #include <linux/slab.h> > #include <linux/user_namespace.h> > +#include <linux/bpf.h> > > #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);
On Tue, Jul 04, 2023 at 07:42:53PM +0800, Hou Tao wrote: > 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 <legion@kernel.org> > > --- > > 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 <linux/mnt_idmapping.h> > > #include <linux/slab.h> > > #include <linux/user_namespace.h> > > +#include <linux/bpf.h> > > > > #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); > I don't want any of these helpers as kfuncs as they are peeking deeply into implementation details that we reserve to change. Specifically in the light of: 3. kfunc lifecycle expectations part b): "Unlike with regular kernel symbols, this is expected behavior for BPF symbols, and out-of-tree BPF programs that use kfuncs should be considered relevant to discussions and decisions around modifying and removing those kfuncs. The BPF community will take an active role in participating in upstream discussions when necessary to ensure that the perspectives of such users are taken into account." That's too much stability for my taste for these helpers. The helpers here exposed have been modified multiple times and once we wean off idmapped mounts from user namespaces completely they will change again. So I'm fine if they're traceable but not as kfuncs with any - even minimal - stability guarantees.
On Tue, Jul 04, 2023 at 03:01:21PM +0200, Christian Brauner wrote: > On Tue, Jul 04, 2023 at 07:42:53PM +0800, Hou Tao wrote: > > 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 <legion@kernel.org> > > > --- > > > 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 <linux/mnt_idmapping.h> > > > #include <linux/slab.h> > > > #include <linux/user_namespace.h> > > > +#include <linux/bpf.h> > > > > > > #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); > > > > I don't want any of these helpers as kfuncs as they are peeking deeply > into implementation details that we reserve to change. Specifically in > the light of: > > 3. kfunc lifecycle expectations part b): > > "Unlike with regular kernel symbols, this is expected behavior for BPF > symbols, and out-of-tree BPF programs that use kfuncs should be considered > relevant to discussions and decisions around modifying and removing those > kfuncs. The BPF community will take an active role in participating in > upstream discussions when necessary to ensure that the perspectives of such > users are taken into account." > > That's too much stability for my taste for these helpers. The helpers > here exposed have been modified multiple times and once we wean off > idmapped mounts from user namespaces completely they will change again. > So I'm fine if they're traceable but not as kfuncs with any - even > minimal - stability guarantees. I thought that the mnt_idmap interface is already quite stable. I wanted to give a minimal interface to bpf programs. Would it be better if I hide the mnt_idmap inside the helper or are you against using kfuncs at the moment ? Like that: __bpf_kfunc uint64_t bpf_get_file_vfs_ids(struct file *file) { struct mnt_idmap *idmap = file_mnt_idmap(file); vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, file->f_inode); vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, file->f_inode); return (u64) __vfsgid_val(vfsgid) << 32 | __vfsuid_val(vfsuid); }
On Tue, Jul 04, 2023 at 07:42:53PM +0800, Hou Tao wrote: > 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 <legion@kernel.org> > > --- > > 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 <linux/mnt_idmapping.h> > > #include <linux/slab.h> > > #include <linux/user_namespace.h> > > +#include <linux/bpf.h> > > > > #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 ? I wanted to provide a minimal API for bpf programs. I thought that this interface is stable enough, but after reading Christian's answer, it looks like I was wrong. > > + > > +/** > > + * 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. This can be used not only in BPF_PROG_TYPE_TRACING but also at least for BPF_PROG_TYPE_LSM. > > +late_initcall(bpf_idmap_kfunc_init); > >
On Tue, Jul 04, 2023 at 05:11:12PM +0200, Alexey Gladkov wrote: > On Tue, Jul 04, 2023 at 07:42:53PM +0800, Hou Tao wrote: > > 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 <legion@kernel.org> > > > --- > > > 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 <linux/mnt_idmapping.h> > > > #include <linux/slab.h> > > > #include <linux/user_namespace.h> > > > +#include <linux/bpf.h> > > > > > > #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 ? > > I wanted to provide a minimal API for bpf programs. I thought that this > interface is stable enough, but after reading Christian's answer, it looks > like I was wrong. It isn't even about stability per se. It's unlikely that if we change internal details that types or arguments to these helpers change. That's why we did the work of abstracting this all away in the first place and making this an opaque type. The wider point is that according to the docs, kfuncs claim to have equivalent status to EXPORT_SYMBOL_*() with the added complexity of maybe having to take out of tree bpf programs into account. Right now, we can look at the in-kernel users of is_idmapped_mnt(), convert them and then kill this thing off if we wanted to. As soon as this is a kfunc such an endeavour becomes a measure of "f**** around and find out". That's an entirely avoidable conflict if we don't even expose it in the first place.
On Tue, Jul 04, 2023 at 05:28:13PM +0200, Christian Brauner wrote: > On Tue, Jul 04, 2023 at 05:11:12PM +0200, Alexey Gladkov wrote: > > On Tue, Jul 04, 2023 at 07:42:53PM +0800, Hou Tao wrote: > > > 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 <legion@kernel.org> > > > > --- > > > > 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 <linux/mnt_idmapping.h> > > > > #include <linux/slab.h> > > > > #include <linux/user_namespace.h> > > > > +#include <linux/bpf.h> > > > > > > > > #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 ? > > > > I wanted to provide a minimal API for bpf programs. I thought that this > > interface is stable enough, but after reading Christian's answer, it looks > > like I was wrong. > > It isn't even about stability per se. It's unlikely that if we change > internal details that types or arguments to these helpers change. That's > why we did the work of abstracting this all away in the first place and > making this an opaque type. > > The wider point is that according to the docs, kfuncs claim to have > equivalent status to EXPORT_SYMBOL_*() with the added complexity of > maybe having to take out of tree bpf programs into account. > > Right now, we can look at the in-kernel users of is_idmapped_mnt(), > convert them and then kill this thing off if we wanted to. As soon as > this is a kfunc such an endeavour becomes a measure of "f**** around and > find out". That's an entirely avoidable conflict if we don't even expose > it in the first place. > I was hoping to make it possible to use is_idmapped_mnt or its equivalent to at least be able to distinguish a file with an idmapped mount from a regular one.
On Wed, Jul 05, 2023 at 03:43:09PM +0200, Alexey Gladkov wrote: > On Tue, Jul 04, 2023 at 05:28:13PM +0200, Christian Brauner wrote: > > On Tue, Jul 04, 2023 at 05:11:12PM +0200, Alexey Gladkov wrote: > > > On Tue, Jul 04, 2023 at 07:42:53PM +0800, Hou Tao wrote: > > > > 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 <legion@kernel.org> > > > > > --- > > > > > 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 <linux/mnt_idmapping.h> > > > > > #include <linux/slab.h> > > > > > #include <linux/user_namespace.h> > > > > > +#include <linux/bpf.h> > > > > > > > > > > #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 ? > > > > > > I wanted to provide a minimal API for bpf programs. I thought that this > > > interface is stable enough, but after reading Christian's answer, it looks > > > like I was wrong. > > > > It isn't even about stability per se. It's unlikely that if we change > > internal details that types or arguments to these helpers change. That's > > why we did the work of abstracting this all away in the first place and > > making this an opaque type. > > > > The wider point is that according to the docs, kfuncs claim to have > > equivalent status to EXPORT_SYMBOL_*() with the added complexity of > > maybe having to take out of tree bpf programs into account. > > > > Right now, we can look at the in-kernel users of is_idmapped_mnt(), > > convert them and then kill this thing off if we wanted to. As soon as > > this is a kfunc such an endeavour becomes a measure of "f**** around and > > find out". That's an entirely avoidable conflict if we don't even expose > > it in the first place. > > > > I was hoping to make it possible to use is_idmapped_mnt or its equivalent > to at least be able to distinguish a file with an idmapped mount from a > regular one. Afaict, you can do this today pretty easily. For example, #!/usr/bin/env bpftrace #include <linux/mount.h> #include <linux/path.h> #include <linux/dcache.h> kfunc:do_move_mount { printf("Target path %s\n", str(args->new_path->dentry->d_name.name)); printf("Target mount idmapped %d\n", args->new_path->mnt->mnt_idmap != kaddr("nop_mnt_idmap")); } sample output: Target path console Target mount idmapped 0 Target path rootfs Target mount idmapped 1
On Wed, Jul 05, 2023 at 04:18:07PM +0200, Christian Brauner wrote: > On Wed, Jul 05, 2023 at 03:43:09PM +0200, Alexey Gladkov wrote: > > On Tue, Jul 04, 2023 at 05:28:13PM +0200, Christian Brauner wrote: > > > On Tue, Jul 04, 2023 at 05:11:12PM +0200, Alexey Gladkov wrote: > > > > On Tue, Jul 04, 2023 at 07:42:53PM +0800, Hou Tao wrote: > > > > > 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 <legion@kernel.org> > > > > > > --- > > > > > > 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 <linux/mnt_idmapping.h> > > > > > > #include <linux/slab.h> > > > > > > #include <linux/user_namespace.h> > > > > > > +#include <linux/bpf.h> > > > > > > > > > > > > #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 ? > > > > > > > > I wanted to provide a minimal API for bpf programs. I thought that this > > > > interface is stable enough, but after reading Christian's answer, it looks > > > > like I was wrong. > > > > > > It isn't even about stability per se. It's unlikely that if we change > > > internal details that types or arguments to these helpers change. That's > > > why we did the work of abstracting this all away in the first place and > > > making this an opaque type. > > > > > > The wider point is that according to the docs, kfuncs claim to have > > > equivalent status to EXPORT_SYMBOL_*() with the added complexity of > > > maybe having to take out of tree bpf programs into account. > > > > > > Right now, we can look at the in-kernel users of is_idmapped_mnt(), > > > convert them and then kill this thing off if we wanted to. As soon as > > > this is a kfunc such an endeavour becomes a measure of "f**** around and > > > find out". That's an entirely avoidable conflict if we don't even expose > > > it in the first place. > > > > > > > I was hoping to make it possible to use is_idmapped_mnt or its equivalent > > to at least be able to distinguish a file with an idmapped mount from a > > regular one. > > Afaict, you can do this today pretty easily. For example, > > #!/usr/bin/env bpftrace > > #include <linux/mount.h> > #include <linux/path.h> > #include <linux/dcache.h> > > kfunc:do_move_mount > { > printf("Target path %s\n", str(args->new_path->dentry->d_name.name)); > printf("Target mount idmapped %d\n", args->new_path->mnt->mnt_idmap != kaddr("nop_mnt_idmap")); > } > > sample output: > > Target path console > Target mount idmapped 0 > Target path rootfs > Target mount idmapped 1 > Well, it's a possible solution, but in this case we don't limit the bpf programs in hacking. But on the other hand it will be only their problem. Since this is the current strategy, this is suitable for me.
On Tue, Jul 4, 2023 at 6:01 AM Christian Brauner <brauner@kernel.org> wrote: > > > > +/** > > > + * 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); > > > +} ... > > I don't want any of these helpers as kfuncs as they are peeking deeply > into implementation details that we reserve to change. Specifically in > the light of: > > 3. kfunc lifecycle expectations part b): > > "Unlike with regular kernel symbols, this is expected behavior for BPF > symbols, and out-of-tree BPF programs that use kfuncs should be considered > relevant to discussions and decisions around modifying and removing those > kfuncs. The BPF community will take an active role in participating in > upstream discussions when necessary to ensure that the perspectives of such > users are taken into account." > > That's too much stability for my taste for these helpers. The helpers > here exposed have been modified multiple times and once we wean off > idmapped mounts from user namespaces completely they will change again. > So I'm fine if they're traceable but not as kfuncs with any - even > minimal - stability guarantees. Christian, That quote is taken out of context. In the first place the Documentation/bpf/kfuncs.rst says: " kfuncs provide a kernel <-> kernel API, and thus are not bound by any of the strict stability restrictions associated with kernel <-> user UAPIs. This means they can be thought of as similar to EXPORT_SYMBOL_GPL, and can therefore be modified or removed by a maintainer of the subsystem they're defined in when it's deemed necessary. " bpf_get_file_vfs_ids is vfs related, so you guys decide when and how to add/remove them. It's ok that you don't want this particular one for whatever reason, but that reason shouldn't be 'stability guarantees'. There are really none. The kernel kfuncs can change at any time. There are plenty of examples in git log where we added and then tweaked/removed kfuncs. The doc also says: " As described above, while sometimes a maintainer may find that a kfunc must be changed or removed immediately to accommodate some changes in their subsystem, " and git log of such cases proves the point. The quote about out-of-tree bpf progs is necessary today, since very few bpf progs are in-tree, so when maintainers of a subsystem want to remove kfunc the program authors need something in the doc to point to and explain why and how they use the kfunc otherwise maintainers will just say 'go away. you're out-of-tree'. The users need their voice to be heard. Even if the result is the same. In other words the part you quoted is needed to make kfuncs usable. Otherwise 'kfunc is 100% unstable and maintainers can rename it every release just to make life of bpf prog writers harder' becomes a real possibility in the minds of bpf users. The kfunc doc makes it 100% clear that there are no stability guarantees. So please don't say 'minimal stability'. In your other reply: > we can look at the in-kernel users of is_idmapped_mnt(), > convert them and then kill this thing off if we wanted to. you can absolutely do that even if is_idmapped_mnt() is exposed as a kfunc. You'll just delete it with zero notice if you like. Just like what you would do with a normal export_symbol. The doc is pretty clear about it and there are examples where we did such things.
On Wed, Jul 05, 2023 at 06:10:32PM -0700, Alexei Starovoitov wrote: > On Tue, Jul 4, 2023 at 6:01 AM Christian Brauner <brauner@kernel.org> wrote: > > > > > > +/** > > > > + * 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); > > > > +} > ... > > > > I don't want any of these helpers as kfuncs as they are peeking deeply > > into implementation details that we reserve to change. Specifically in > > the light of: > > > > 3. kfunc lifecycle expectations part b): > > > > "Unlike with regular kernel symbols, this is expected behavior for BPF > > symbols, and out-of-tree BPF programs that use kfuncs should be considered > > relevant to discussions and decisions around modifying and removing those > > kfuncs. The BPF community will take an active role in participating in > > upstream discussions when necessary to ensure that the perspectives of such > > users are taken into account." > > > > That's too much stability for my taste for these helpers. The helpers > > here exposed have been modified multiple times and once we wean off > > idmapped mounts from user namespaces completely they will change again. > > So I'm fine if they're traceable but not as kfuncs with any - even > > minimal - stability guarantees. > > Christian, > That quote is taken out of context. > In the first place the Documentation/bpf/kfuncs.rst says: > " > kfuncs provide a kernel <-> kernel API, and thus are not bound by any of the > strict stability restrictions associated with kernel <-> user UAPIs. This means > they can be thought of as similar to EXPORT_SYMBOL_GPL, and can therefore be > modified or removed by a maintainer of the subsystem they're defined in when > it's deemed necessary. > " > > bpf_get_file_vfs_ids is vfs related, so you guys decide when and how > to add/remove them. It's ok that you don't want this particular one > for whatever reason, but that reason shouldn't be 'stability guarantees'. > There are really none. The kernel kfuncs can change at any time. > There are plenty of examples in git log where we added and then > tweaked/removed kfuncs. > > The doc also says: > " > As described above, while sometimes a maintainer may find that a kfunc must be > changed or removed immediately to accommodate some changes in their subsystem, > " > and git log of such cases proves the point. > > The quote about out-of-tree bpf progs is necessary today, since > very few bpf progs are in-tree, so when maintainers of a subsystem > want to remove kfunc the program authors need something in the doc > to point to and explain why and how they use the kfunc otherwise > maintainers will just say 'go away. you're out-of-tree'. > The users need their voice to be heard. Even if the result is the same. > In other words the part you quoted is needed to make kfuncs usable. > Otherwise 'kfunc is 100% unstable and maintainers can rename it > every release just to make life of bpf prog writers harder' > becomes a real possibility in the minds of bpf users. > The kfunc doc makes it 100% clear that there are no stability guarantees. > So please don't say 'minimal stability'. > > In your other reply: > > > we can look at the in-kernel users of is_idmapped_mnt(), > > convert them and then kill this thing off if we wanted to. > > you can absolutely do that even if is_idmapped_mnt() is exposed as a kfunc. > You'll just delete it with zero notice if you like. > Just like what you would do with a normal export_symbol. > The doc is pretty clear about it and there are examples where we did > such things. I think I said it somewhere else: I'm not opposing your position on kfruncs in a sense I understand that's kinda the model that you have to push for. But you have to admit that this out-of-tree portion is very hard to swallow if you've been hit by out of tree modules and their complaints about removed EXPORT_SYMBOL*()s. I'm still rather hesitant about this because I find it hard to figure out how this will go down in practice. But, especially with the internal idmapped mount api. This is a very hidden and abstracted away implementation around an opaque type and I'm not yet ready to let modules or bpf programs peek into it's implementation details. I hope that's understandable.
On Tue, Jul 04, 2023 at 03:01:21PM +0200, Christian Brauner wrote: > That's too much stability for my taste for these helpers. The helpers > here exposed have been modified multiple times and once we wean off > idmapped mounts from user namespaces completely they will change again. > So I'm fine if they're traceable but not as kfuncs with any - even > minimal - stability guarantees. I fully agree. I also don't think any eBPF program has any business looking at idmapping.
On Thu, Jul 6, 2023 at 12:22 AM Christian Brauner <brauner@kernel.org> wrote: > push for. But you have to admit that this out-of-tree portion is very > hard to swallow if you've been hit by out of tree modules and their > complaints about removed EXPORT_SYMBOL*()s. I don't remember a single case where somebody complained so hard about unexport of a symbol that it was reinstated. Instead there are plenty of 'unexport foo' in every kernel release. Like commit 4bb218a65a43 ("fs: unexport buffer_check_dirty_writeback"). Surely they break some oot mods, so what? Complaining doesn't help.
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 <linux/mnt_idmapping.h> #include <linux/slab.h> #include <linux/user_namespace.h> +#include <linux/bpf.h> #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); +} + +/** + * 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); +} + +late_initcall(bpf_idmap_kfunc_init);