From patchwork Tue Jun 21 20:46:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: KP Singh X-Patchwork-Id: 12889764 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 1FAE6CCA473 for ; Tue, 21 Jun 2022 20:46:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354006AbiFUUq5 (ORCPT ); Tue, 21 Jun 2022 16:46:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354156AbiFUUq4 (ORCPT ); Tue, 21 Jun 2022 16:46:56 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 206842B1A1 for ; Tue, 21 Jun 2022 13:46:56 -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 B162B6185A for ; Tue, 21 Jun 2022 20:46:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B3FC4C341CC; Tue, 21 Jun 2022 20:46:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1655844415; bh=5ZMi3z6QKbQw0Q9J7MHsWauSOb4XqeY0Org1meXj0iM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PYcDSolRNuDvcV7tchH7BXc9fiI/uOHjUySAzVmjxQ1BeP7x5xQXad63srAANXrgi AMv969IWFDhu+ZpLKiCDFOFfnrUz6oLPd6ubwe9dnlOcRidquXGpRN49hrieD0zR+h MvQBXZ+7FzZAWtNyoHbc+a42vucGtMqsbx0xOGf9ZODz2d4Ol2xWWlafZRp01gp9L+ 1YnMHBRl1U7vxWATy1QUjRzhYVQ3+ZnGm8htWWOI+xNYXUC75IP7URXwQF89h2WRQL U06ei4TwLuSSoa7UNyPHkFSuvQuO0M/waUkT6mQ/hYpgK1BLy08wVfF0rqN8npNh8g pr8IkynXM+VMw== From: KP Singh To: bpf@vger.kernel.org Cc: KP Singh , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Benjamin Tissoires , Yosry Ahmed Subject: [PATCH v3 bpf-next 4/5] bpf: Add a bpf_getxattr kfunc Date: Tue, 21 Jun 2022 20:46:41 +0000 Message-Id: <20220621204642.2891979-5-kpsingh@kernel.org> X-Mailer: git-send-email 2.37.0.rc0.104.g0611611a94-goog In-Reply-To: <20220621204642.2891979-1-kpsingh@kernel.org> References: <20220621204642.2891979-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..210e29bd0cbb 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 which will be used in BPF programs"); + +noinline __weak ssize_t bpf_getxattr(struct dentry *dentry, struct inode *inode, + const char *name, void *value, int size) +{ + return __vfs_getxattr(dentry, inode, name, value, size); +} + +__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) {