From patchwork Fri Jun 24 04:56:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: KP Singh X-Patchwork-Id: 12893844 X-Patchwork-Delegate: bpf@iogearbox.net 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 C0B4EC43334 for ; Fri, 24 Jun 2022 04:56:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229948AbiFXE4x (ORCPT ); Fri, 24 Jun 2022 00:56:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229689AbiFXE4w (ORCPT ); Fri, 24 Jun 2022 00:56:52 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02736D5B for ; Thu, 23 Jun 2022 21:56:52 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 8248B60F59 for ; Fri, 24 Jun 2022 04:56:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 647AAC341CD; Fri, 24 Jun 2022 04:56:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1656046610; bh=ANUE1U9zF5NeSbOnZEGzieCHVmc1/uVbH677R+iGK2Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UVAK4ediU03ME/xlsWlM+hHGPRmPWZafnCvPwAp9REM1RSuzwIyO1/CZcj1tKfaBY 5eJTbrbOOOASDyKWTHnWR7n+nSbg9EpASslq+1c/PbO1H7eQ56Nw50IsPgNLF0bzb4 ddVeowz2HKg/3NpLzd8EiSzZp3d6G+uXykgk0gFSN3Visl+kEdg1vxBzi+nbqnd0dr SqZaHNPFZDHssupIDgCaBjQCTghoEV3GKu3sq6uHJB1NPg4lHXzyVdbWeJMXO1OuM6 mukiso06Sxhu5X9GzawJoQDi8UfjylG9tGK0NHrjy1mXZXX1AgQ80JeKt8EukF09mM 8918BdKxga8dg== From: KP Singh To: bpf@vger.kernel.org Cc: KP Singh , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Benjamin Tissoires , Yosry Ahmed Subject: [PATCH v4 bpf-next 4/5] bpf: Add a bpf_getxattr kfunc Date: Fri, 24 Jun 2022 04:56:35 +0000 Message-Id: <20220624045636.3668195-5-kpsingh@kernel.org> X-Mailer: git-send-email 2.37.0.rc0.104.g0611611a94-goog In-Reply-To: <20220624045636.3668195-1-kpsingh@kernel.org> References: <20220624045636.3668195-1-kpsingh@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net LSMs like SELinux store security state in xattrs. bpf_getxattr enables BPF LSM to implement similar functionality. In combination with bpf_local_storage, xattrs can be used to develop more complex security policies. This kfunc wraps around __vfs_getxattr which can sleep and is, therefore, limited to sleepable programs using the newly added sleepable_set for kfuncs. Signed-off-by: KP Singh --- kernel/trace/bpf_trace.c | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 4be976cf7d63..87496d57b099 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -20,6 +20,7 @@ #include #include #include +#include #include @@ -1181,6 +1182,47 @@ static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = { .arg1_type = ARG_PTR_TO_CTX, }; +__diag_push(); +__diag_ignore_all("-Wmissing-prototypes", + "kfuncs that are used in tracing/LSM BPF programs"); + +ssize_t bpf_getxattr(struct dentry *dentry, struct inode *inode, + const char *name, void *value, int value__sz) +{ + return __vfs_getxattr(dentry, inode, name, value, value__sz); +} + +__diag_pop(); + +BTF_SET_START(bpf_trace_kfunc_ids) +BTF_ID(func, bpf_getxattr) +BTF_SET_END(bpf_trace_kfunc_ids) + +BTF_SET_START(bpf_trace_sleepable_kfunc_ids) +BTF_ID(func, bpf_getxattr) +BTF_SET_END(bpf_trace_sleepable_kfunc_ids) + +static const struct btf_kfunc_id_set bpf_trace_kfunc_set = { + .owner = THIS_MODULE, + .check_set = &bpf_trace_kfunc_ids, + .sleepable_set = &bpf_trace_sleepable_kfunc_ids, +}; + +static int __init bpf_trace_kfunc_init(void) +{ + int ret; + + ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, + &bpf_trace_kfunc_set); + if (!ret) + return ret; + + return register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM, + &bpf_trace_kfunc_set); + +} +late_initcall(bpf_trace_kfunc_init); + static const struct bpf_func_proto * bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) {