Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp9057320rwb; Thu, 24 Nov 2022 07:33:43 -0800 (PST) X-Google-Smtp-Source: AA0mqf6OWixv+mBhpIkVZIh1kkX3UpFAYoEJu/STgvLVoOm7WMOIuWQBgj0g4L+1pINCeaSnfGLJ X-Received: by 2002:a05:6402:150:b0:468:fdc3:6b44 with SMTP id s16-20020a056402015000b00468fdc36b44mr12880898edu.388.1669304023642; Thu, 24 Nov 2022 07:33:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669304023; cv=none; d=google.com; s=arc-20160816; b=ob+m+FmrlaZ7wTgJjN1h4umRxEtbm+FceyXPhJf+J9+0qEEb4juLHymq5BIOqAXV7K 8gvzu6wF79WpWZodChDc3o9ZfQ7wN1Q7Lt0k4AbUqV2xKZlEo71TZS/e9GxUxYm4kQTQ TaHmU1s/8gAqdOyyHxC8kSg+eaffs0EstNRNJ2WMo2Jm3XDptHqfXA3YqPpS9pJ1jK4b Md1MzKsrK1akkHdgTL7xTRoglMuwKiy5QIdxuAS8JnsVYUyHgRE0J2lPHJq4sdcxaXJ9 l7/VMXgg+SqdaiO7B4/e3K8+H6thBjYjjq0uREtPeoHFIpiqKNoUItkYfGFOqTSetW4I HYpw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=1gSBxJ3JD+Lhw0lQsSl13cc2SNWiLM1GIUzZ7OkQbMU=; b=l830zl3Ocu+wsZ3IBW0uR3SOtCy7oyXoj1I1T+Fx1jEhsNKQ2Y5CyBsZkGVrxCi8NO Pjv3FH/Cit5dZCTMKzQgxESX+fVEmrqT1qw41QJS0aObxFgCNqd7rDv8antYN6QauA7Q RRvmNQ6StbcepuJML/mJ8EcyzRbUF8J8w3Nnir2XwTs6vEpIYaY/vLMTpIsdrUAki+o4 ahCl9OYAJtEG9YAbXbWt8PV5yaPF6s4yyLCuijF4tGGSnPXHZy6tMN12GxYEbG8jzWmh 8AE0w9jJyefDsUjZees8Dbj7h5vsIEMv7xFTkZd/mg1SFGbWlJXV17or1qq6Sf2XvjxK KPbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=A+tYn6JY; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gt39-20020a1709072da700b0078c4a772ea7si450846ejc.11.2022.11.24.07.33.22; Thu, 24 Nov 2022 07:33:43 -0800 (PST) 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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=A+tYn6JY; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229985AbiKXPST (ORCPT + 87 others); Thu, 24 Nov 2022 10:18:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229884AbiKXPSC (ORCPT ); Thu, 24 Nov 2022 10:18:02 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CECB16E8D6 for ; Thu, 24 Nov 2022 07:16:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669302980; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1gSBxJ3JD+Lhw0lQsSl13cc2SNWiLM1GIUzZ7OkQbMU=; b=A+tYn6JYB9UApKhsM/aaoZ16zkAhLFdJvsz+4MZTla7o1Ym4e7G4iRom3zZPFscVHIVNtB MiSmWfN3g60msFKRIBxUJjPSsHYYjvtNkOVE28wLIqnKm3yG2x9MTq499IRvXqxasDo9vu Rd/M+7L1PwHE6fBybMNFgR8T9b7bPoQ= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-9-2nluelRgODCohAjC4AFX6g-1; Thu, 24 Nov 2022 10:16:14 -0500 X-MC-Unique: 2nluelRgODCohAjC4AFX6g-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 87F903C01E03; Thu, 24 Nov 2022 15:16:13 +0000 (UTC) Received: from plouf.redhat.com (unknown [10.39.193.200]) by smtp.corp.redhat.com (Postfix) with ESMTP id C7AEF40C2064; Thu, 24 Nov 2022 15:16:11 +0000 (UTC) From: Benjamin Tissoires To: Greg KH , Jiri Kosina , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Dmitry Torokhov Cc: Tero Kristo , linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Benjamin Tissoires Subject: [RFC hid v1 02/10] WIP: bpf: allow to pin programs from the kernel when bpffs is mounted Date: Thu, 24 Nov 2022 16:15:55 +0100 Message-Id: <20221124151603.807536-3-benjamin.tissoires@redhat.com> In-Reply-To: <20221124151603.807536-1-benjamin.tissoires@redhat.com> References: <20221124151603.807536-1-benjamin.tissoires@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 I want to be able to pin programs loaded by the kernel and expose them through the bpffs so userspace knows what is loaded. There are a few things missings in this WIP: - locking on bpffs_sb - ability to create a hierarchy from the kernel: I'd like to store all of my programs in /sys/fs/bpf/hid, not everything at the root of the mount - ability to store programs when bpffs is not mounted Signed-off-by: Benjamin Tissoires --- include/linux/bpf.h | 1 + kernel/bpf/inode.c | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 0566705c1d4e..f5a7dca520eb 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1806,6 +1806,7 @@ struct bpf_link *bpf_link_get_curr_or_next(u32 *id); int bpf_obj_pin_user(u32 ufd, const char __user *pathname); int bpf_obj_get_user(const char __user *pathname, int flags); +int bpf_prog_pin_kernel(const char *name, struct bpf_prog *prog); #define BPF_ITER_FUNC_PREFIX "bpf_iter_" #define DEFINE_BPF_ITER_FUNC(target, args...) \ diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c index 4f841e16779e..7be24ffad7f7 100644 --- a/kernel/bpf/inode.c +++ b/kernel/bpf/inode.c @@ -29,6 +29,9 @@ enum bpf_type { BPF_TYPE_LINK, }; + +static struct super_block *bpffs_sb; + static void *bpf_any_get(void *raw, enum bpf_type type) { switch (type) { @@ -435,6 +438,34 @@ static int bpf_iter_link_pin_kernel(struct dentry *parent, return ret; } +/* pin a program in the bpffs */ +/* TODO: handle path relative to mount point instead of plain name by recreating + * the hierarchy, like in drivers/base/devtmpfs.c + */ +int bpf_prog_pin_kernel(const char *name, struct bpf_prog *prog) +{ + struct dentry *parent; + umode_t mode = S_IFREG | S_IRUSR; + struct dentry *dentry; + int ret; + + if (!bpffs_sb) + return -ENOENT; + + parent = bpffs_sb->s_root; + + inode_lock(parent->d_inode); + dentry = lookup_one_len(name, parent, strlen(name)); + if (IS_ERR(dentry)) { + inode_unlock(parent->d_inode); + return PTR_ERR(dentry); + } + ret = bpf_mkprog(dentry, mode, prog); + dput(dentry); + inode_unlock(parent->d_inode); + return ret; +} + static int bpf_obj_do_pin(const char __user *pathname, void *raw, enum bpf_type type) { @@ -758,6 +789,8 @@ static int bpf_fill_super(struct super_block *sb, struct fs_context *fc) inode->i_mode &= ~S_IALLUGO; populate_bpffs(sb->s_root); inode->i_mode |= S_ISVTX | opts->mode; + + bpffs_sb = sb; return 0; } @@ -795,12 +828,18 @@ static int bpf_init_fs_context(struct fs_context *fc) return 0; } +static void bpf_kill_sb(struct super_block *sb) +{ + bpffs_sb = NULL; + kill_litter_super(sb); +} + static struct file_system_type bpf_fs_type = { .owner = THIS_MODULE, .name = "bpf", .init_fs_context = bpf_init_fs_context, .parameters = bpf_fs_parameters, - .kill_sb = kill_litter_super, + .kill_sb = bpf_kill_sb, }; static int __init bpf_init(void) -- 2.38.1