From patchwork Tue Jun 28 16:19:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: KP Singh X-Patchwork-Id: 12898522 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 6393DCCA487 for ; Tue, 28 Jun 2022 16:30:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234154AbiF1QaS (ORCPT ); Tue, 28 Jun 2022 12:30:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347466AbiF1Q3J (ORCPT ); Tue, 28 Jun 2022 12:29:09 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 581603A182; Tue, 28 Jun 2022 09:19:58 -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 ams.source.kernel.org (Postfix) with ESMTPS id 16A83B81F15; Tue, 28 Jun 2022 16:19:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1614EC341CD; Tue, 28 Jun 2022 16:19:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1656433195; bh=uZ0a5aDXW8MZkIEq0XmbcRjUTh8YkwmgzJs6XG0ENok=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qCz6rDAbKU0oO71+34+lZjqNMXkCbW6WI+5/itTKjwMCcKz6Ukno9pgLnu0CrrQdQ YbGOa/8PMeu2M8Nw+G3b7CGJ9Pp71MEMY/lJw/O/yNuL1FjP/8D3aX5ooyUx+Qgxbf 1Z/aOr0tbvoOwvEzuaN3ViIWWLBloc+fRcDinX58eTrzolVw6LS5ac3XVX8pi6JQXg Mb/puUcabKhLRL22bKcJU6JlqPv0Vo4T9mZqmHB2v4LhQQ4la68DtBV5FdKe772o6y UQ4lrA4OM796Ud5f4deH88uo6gKU1LKnjFAvdSBtvCHassLxWROhMpodfqUqA+eH5X G9c2WfOI3xFgA== From: KP Singh To: bpf@vger.kernel.org, linux-security-module@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: KP Singh , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Benjamin Tissoires , Yosry Ahmed Subject: [PATCH v5 bpf-next 1/5] btf: Add a new kfunc set which allows to mark a function to be sleepable Date: Tue, 28 Jun 2022 16:19:44 +0000 Message-Id: <20220628161948.475097-2-kpsingh@kernel.org> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog In-Reply-To: <20220628161948.475097-1-kpsingh@kernel.org> References: <20220628161948.475097-1-kpsingh@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: From: Benjamin Tissoires This allows to declare a kfunc as sleepable and prevents its use in a non sleepable program. Acked-by: KP Singh Signed-off-by: Benjamin Tissoires Signed-off-by: KP Singh --- include/linux/btf.h | 2 ++ kernel/bpf/btf.c | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/linux/btf.h b/include/linux/btf.h index 1bfed7fa0428..6e7517573d9e 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -18,6 +18,7 @@ enum btf_kfunc_type { BTF_KFUNC_TYPE_RELEASE, BTF_KFUNC_TYPE_RET_NULL, BTF_KFUNC_TYPE_KPTR_ACQUIRE, + BTF_KFUNC_TYPE_SLEEPABLE, BTF_KFUNC_TYPE_MAX, }; @@ -37,6 +38,7 @@ struct btf_kfunc_id_set { struct btf_id_set *release_set; struct btf_id_set *ret_null_set; struct btf_id_set *kptr_acquire_set; + struct btf_id_set *sleepable_set; }; struct btf_id_set *sets[BTF_KFUNC_TYPE_MAX]; }; diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 2e2066d6af94..37bc77b3b499 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -6171,7 +6171,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, struct bpf_verifier_log *log = &env->log; u32 i, nargs, ref_id, ref_obj_id = 0; bool is_kfunc = btf_is_kernel(btf); - bool rel = false, kptr_get = false; + bool rel = false, kptr_get = false, sleepable = false; const char *func_name, *ref_tname; const struct btf_type *t, *ref_t; const struct btf_param *args; @@ -6202,9 +6202,10 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, } if (is_kfunc) { - /* Only kfunc can be release func */ rel = btf_kfunc_id_set_contains(btf, resolve_prog_type(env->prog), BTF_KFUNC_TYPE_RELEASE, func_id); + sleepable = btf_kfunc_id_set_contains(btf, resolve_prog_type(env->prog), + BTF_KFUNC_TYPE_SLEEPABLE, func_id); kptr_get = btf_kfunc_id_set_contains(btf, resolve_prog_type(env->prog), BTF_KFUNC_TYPE_KPTR_ACQUIRE, func_id); } @@ -6404,6 +6405,13 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, func_name); return -EINVAL; } + + if (sleepable && !env->prog->aux->sleepable) { + bpf_log(log, "kernel function %s is sleepable but the program is not\n", + func_name); + return -EINVAL; + } + /* returns argument register number > 0 in case of reference release kfunc */ return rel ? ref_regno : 0; } From patchwork Tue Jun 28 16:19:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: KP Singh X-Patchwork-Id: 12898524 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 83DDDCCA47E for ; Tue, 28 Jun 2022 16:30:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233634AbiF1QaR (ORCPT ); Tue, 28 Jun 2022 12:30:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347545AbiF1Q3K (ORCPT ); Tue, 28 Jun 2022 12:29:10 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63C773A189; Tue, 28 Jun 2022 09:20:00 -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 ams.source.kernel.org (Postfix) with ESMTPS id 21BC1B81EA4; Tue, 28 Jun 2022 16:19:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 32244C341C8; Tue, 28 Jun 2022 16:19:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1656433197; bh=AcNuOCR5+c8FlyRF2gJBdkVGbfQprs6DCiPiX5v7zyE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I1+HS8ck/uWSgFRzYFxgi/pCmJ8Iw4cyxDroJADV6VlP5A1GC0zuoW9E3JCRW9zOe YfUX9tOYkXu8Y/c40yHp0GgxkysGir9KPhFBAEAb3mY80pZQBxrJaC1Ut/Vv4ebAzk qzOPnfBiJM6z3ZnhDqBa5oBG1pHlthZe3jSJ8zI38mxjocxYwBisBtJVpUhhLdNmzQ w0TKQcZsnjV14Z0sRPT2G3KCd/aYPwQJZco9Smp4JYxOSP0NKvIii09UPveB3X3xhw irzm32K0ZU4rcJSBOk1amAl0I10t5k4i9DOZ3GNDqenV6F52mQNPCTMzf2HZL4fZHV WCzVR/a6CkALw== From: KP Singh To: bpf@vger.kernel.org, linux-security-module@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: KP Singh , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Benjamin Tissoires , Yosry Ahmed Subject: [PATCH v5 bpf-next 2/5] bpf: kfunc support for ARG_PTR_TO_CONST_STR Date: Tue, 28 Jun 2022 16:19:45 +0000 Message-Id: <20220628161948.475097-3-kpsingh@kernel.org> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog In-Reply-To: <20220628161948.475097-1-kpsingh@kernel.org> References: <20220628161948.475097-1-kpsingh@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: kfuncs can handle pointers to memory when the next argument is the size of the memory that can be read and verify these as ARG_CONST_SIZE_OR_ZERO Similarly add support for string constants (const char *) and verify it similar to ARG_PTR_TO_CONST_STR. Signed-off-by: KP Singh --- include/linux/bpf_verifier.h | 2 + kernel/bpf/btf.c | 30 ++++++++++++ kernel/bpf/verifier.c | 89 +++++++++++++++++++++--------------- 3 files changed, 83 insertions(+), 38 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 81b19669efba..f6d8898270d5 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -560,6 +560,8 @@ int check_kfunc_mem_size_reg(struct bpf_verifier_env *env, struct bpf_reg_state u32 regno); int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, u32 regno, u32 mem_size); +int check_const_str(struct bpf_verifier_env *env, + const struct bpf_reg_state *reg, int regno); /* this lives here instead of in bpf.h because it needs to dereference tgt_prog */ static inline u64 bpf_trampoline_compute_key(const struct bpf_prog *tgt_prog, diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 37bc77b3b499..9b9d6117deae 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -6162,6 +6162,27 @@ static bool is_kfunc_arg_mem_size(const struct btf *btf, return true; } +static bool btf_param_is_const_str_ptr(const struct btf *btf, + const struct btf_param *param) +{ + const struct btf_type *t; + bool is_const = false; + + t = btf_type_by_id(btf, param->type); + if (!btf_type_is_ptr(t)) + return false; + + t = btf_type_by_id(btf, t->type); + while (btf_type_is_modifier(t)) { + if (BTF_INFO_KIND(t->info) == BTF_KIND_CONST) + is_const = true; + t = btf_type_by_id(btf, t->type); + } + + return (is_const && + !strcmp(btf_name_by_offset(btf, t->name_off), "char")); +} + static int btf_check_func_arg_match(struct bpf_verifier_env *env, const struct btf *btf, u32 func_id, struct bpf_reg_state *regs, @@ -6344,10 +6365,19 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, } else if (ptr_to_mem_ok) { const struct btf_type *resolve_ret; u32 type_size; + int err; if (is_kfunc) { bool arg_mem_size = i + 1 < nargs && is_kfunc_arg_mem_size(btf, &args[i + 1], ®s[regno + 1]); + + if (btf_param_is_const_str_ptr(btf, &args[i])) { + err = check_const_str(env, reg, regno); + if (err < 0) + return err; + continue; + } + /* Permit pointer to mem, but only when argument * type is pointer to scalar, or struct composed * (recursively) of scalars. diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 4938477912cd..8ce5d2f86c1e 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5840,6 +5840,56 @@ static u32 stack_slot_get_id(struct bpf_verifier_env *env, struct bpf_reg_state return state->stack[spi].spilled_ptr.id; } +int check_const_str(struct bpf_verifier_env *env, + const struct bpf_reg_state *reg, int regno) +{ + struct bpf_map *map; + int map_off; + u64 map_addr; + char *str_ptr; + int err; + + if (reg->type != PTR_TO_MAP_VALUE) + return -EACCES; + + map = reg->map_ptr; + if (!bpf_map_is_rdonly(map)) { + verbose(env, "R%d does not point to a readonly map'\n", regno); + return -EACCES; + } + + if (!tnum_is_const(reg->var_off)) { + verbose(env, "R%d is not a constant address'\n", regno); + return -EACCES; + } + + if (!map->ops->map_direct_value_addr) { + verbose(env, + "no direct value access support for this map type\n"); + return -EACCES; + } + + err = check_map_access(env, regno, reg->off, map->value_size - reg->off, + false, ACCESS_HELPER); + if (err) + return err; + + map_off = reg->off + reg->var_off.value; + err = map->ops->map_direct_value_addr(map, &map_addr, map_off); + if (err) { + verbose(env, "direct value access on string failed\n"); + return err; + } + + str_ptr = (char *)(long)(map_addr); + if (!strnchr(str_ptr + map_off, map->value_size - map_off, 0)) { + verbose(env, "string is not zero-terminated\n"); + return -EINVAL; + } + + return 0; +} + static int check_func_arg(struct bpf_verifier_env *env, u32 arg, struct bpf_call_arg_meta *meta, const struct bpf_func_proto *fn) @@ -6074,44 +6124,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, return err; err = check_ptr_alignment(env, reg, 0, size, true); } else if (arg_type == ARG_PTR_TO_CONST_STR) { - struct bpf_map *map = reg->map_ptr; - int map_off; - u64 map_addr; - char *str_ptr; - - if (!bpf_map_is_rdonly(map)) { - verbose(env, "R%d does not point to a readonly map'\n", regno); - return -EACCES; - } - - if (!tnum_is_const(reg->var_off)) { - verbose(env, "R%d is not a constant address'\n", regno); - return -EACCES; - } - - if (!map->ops->map_direct_value_addr) { - verbose(env, "no direct value access support for this map type\n"); - return -EACCES; - } - - err = check_map_access(env, regno, reg->off, - map->value_size - reg->off, false, - ACCESS_HELPER); - if (err) - return err; - - map_off = reg->off + reg->var_off.value; - err = map->ops->map_direct_value_addr(map, &map_addr, map_off); - if (err) { - verbose(env, "direct value access on string failed\n"); - return err; - } - - str_ptr = (char *)(long)(map_addr); - if (!strnchr(str_ptr + map_off, map->value_size - map_off, 0)) { - verbose(env, "string is not zero-terminated\n"); - return -EINVAL; - } + err = check_const_str(env, reg, regno); } else if (arg_type == ARG_PTR_TO_KPTR) { if (process_kptr_func(env, regno, meta)) return -EACCES; From patchwork Tue Jun 28 16:19:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: KP Singh X-Patchwork-Id: 12898525 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 260DEC433EF for ; Tue, 28 Jun 2022 16:30:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235067AbiF1QaT (ORCPT ); Tue, 28 Jun 2022 12:30:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347591AbiF1Q3L (ORCPT ); Tue, 28 Jun 2022 12:29:11 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9877F37A82; Tue, 28 Jun 2022 09:20:02 -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 ams.source.kernel.org (Postfix) with ESMTPS id 4672BB81EF1; Tue, 28 Jun 2022 16:20:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4E765C341CE; Tue, 28 Jun 2022 16:19:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1656433200; bh=AFbYjOXviL5rmJow0VE7Z2Ll+TzqAJN36EvtLVEyoBQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RBzJ3f2FDgtelVRVNO08rnuOooIsTtouQkRpgXCpCNlrB/uhh3U3M0Y7Gaklc4Be6 K9jU1JlvI1U4vS1cR8t1RidM2DBfbQOSXbyVsTL/cqajjsJJgbh5CPp3f64W0kfaMa k1YnSbjkc6SNfm5JmCBc5JMshXme5LnHNvDzh9Z5UVvVnoecyX+xIc7esJBJYz8REh buTSXg17tbE0JcunijA8JsxSd5xejcCkFA+G4n7NLJv5Wzbp3BL2YAJZx6ppNLxR+i yilmppsQn6I8+Qxk1dabrbAZ7rnjLYqvi/MsbyFr60IVpV9VgbWIl8m1+yxJK/Ea8W xLHQd/m0Znfxg== From: KP Singh To: bpf@vger.kernel.org, linux-security-module@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: KP Singh , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Benjamin Tissoires , Yosry Ahmed Subject: [PATCH v5 bpf-next 3/5] bpf: Allow kfuncs to be used in LSM programs Date: Tue, 28 Jun 2022 16:19:46 +0000 Message-Id: <20220628161948.475097-4-kpsingh@kernel.org> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog In-Reply-To: <20220628161948.475097-1-kpsingh@kernel.org> References: <20220628161948.475097-1-kpsingh@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: In preparation for the addition of bpf_getxattr kfunc. Signed-off-by: KP Singh --- kernel/bpf/btf.c | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 9b9d6117deae..0331caa98726 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -7265,6 +7265,7 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type) case BPF_PROG_TYPE_STRUCT_OPS: return BTF_KFUNC_HOOK_STRUCT_OPS; case BPF_PROG_TYPE_TRACING: + case BPF_PROG_TYPE_LSM: return BTF_KFUNC_HOOK_TRACING; case BPF_PROG_TYPE_SYSCALL: return BTF_KFUNC_HOOK_SYSCALL; From patchwork Tue Jun 28 16:19:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: KP Singh X-Patchwork-Id: 12898520 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 C4159CCA483 for ; Tue, 28 Jun 2022 16:30:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231987AbiF1QaL (ORCPT ); Tue, 28 Jun 2022 12:30:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347635AbiF1Q3L (ORCPT ); Tue, 28 Jun 2022 12:29:11 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C4FF3A191; Tue, 28 Jun 2022 09:20:03 -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 BD946617CB; Tue, 28 Jun 2022 16:20:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 748AFC341CD; Tue, 28 Jun 2022 16:20:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1656433202; bh=uBHpOboY6y9w+HwMF5AuSrCFXcv/uOgB4BTVB2oGngo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cnvuNN2i97ReGHjMPkFMfNtPeC9XLQ+9qYt9/BMZH9jj3tbFvd15y6pnVHVmS+Gnd swrPLE6ii2PsVrcsKhuJDmM+BVf24MKm2HXptqOk38QMgkT7IZZdU3xzjCBiLErZqE Gs/u2PZT342n388LeTUOi3nUcKnrMLgMxWqj5zOyaZ3diE+Oexnz9zusWPNQeprYt8 0+w+p2FNodbJhhNxFwaMNkpIFXZ8sHPIf9krSQ4/w6nj9sgQP+yCnK/K964dJVX5PD vAtQe/sewg5GrDmYG1MJ7hZxZKAugAV11pKqcVnFpc7OnrSinfmtGiI/Ji82tsXj3z rELaR4IyoM+zg== From: KP Singh To: bpf@vger.kernel.org, linux-security-module@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: KP Singh , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Benjamin Tissoires , Yosry Ahmed Subject: [PATCH v5 bpf-next 4/5] bpf: Add a bpf_getxattr kfunc Date: Tue, 28 Jun 2022 16:19:47 +0000 Message-Id: <20220628161948.475097-5-kpsingh@kernel.org> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog In-Reply-To: <20220628161948.475097-1-kpsingh@kernel.org> References: <20220628161948.475097-1-kpsingh@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: 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) { From patchwork Tue Jun 28 16:19:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: KP Singh X-Patchwork-Id: 12898523 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 F185FCCA486 for ; Tue, 28 Jun 2022 16:30:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234688AbiF1QaT (ORCPT ); Tue, 28 Jun 2022 12:30:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347724AbiF1Q3O (ORCPT ); Tue, 28 Jun 2022 12:29:14 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CC3E23A1A0; Tue, 28 Jun 2022 09:20:06 -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 ams.source.kernel.org (Postfix) with ESMTPS id 7BAF7B81EF1; Tue, 28 Jun 2022 16:20:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8FF58C385A5; Tue, 28 Jun 2022 16:20:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1656433204; bh=9pchgL8YFP3Hx5Q02p0k2JNpsUBkX+zukfrJ3q2OcKI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=O5fRq5RidSwRGjSy3tClru4NFzOywFU0UFX7DH9LUOrUQsivUbrDr2uT4moov+Kkx KWZvUES4e4R03/T5Uy4dCylqjBtpiBzNRvEuK3KI8e9039f9K97/lLdPsSTtbqexx/ 4+vPwqQ8gSnHBApZIsgu3qVvau8NRIi9Dn5/9x1L2cqqh/+N5OzZ4MnZe/hvKo5Jv9 qJfFZPtGkGKXjZTtjlGTgGxgS2at0UEhleWIItaps/NFm4ZKWBpkMWJkh6ILYBQKj1 cj1c//KDlTwBWma3mZ1YGIZK2Z4YMIh+TGEvzh4Fm6w8CWj5s1amo8A1Wtt0Rdx8yb 4oJYwcD/CGTTA== From: KP Singh To: bpf@vger.kernel.org, linux-security-module@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: KP Singh , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Benjamin Tissoires , Yosry Ahmed Subject: [PATCH v5 bpf-next 5/5] bpf/selftests: Add a selftest for bpf_getxattr Date: Tue, 28 Jun 2022 16:19:48 +0000 Message-Id: <20220628161948.475097-6-kpsingh@kernel.org> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog In-Reply-To: <20220628161948.475097-1-kpsingh@kernel.org> References: <20220628161948.475097-1-kpsingh@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: A simple test that adds an xattr on a copied /bin/ls and reads it back when the copied ls is executed. Signed-off-by: KP Singh --- .../testing/selftests/bpf/prog_tests/xattr.c | 54 +++++++++++++++++++ tools/testing/selftests/bpf/progs/xattr.c | 37 +++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/xattr.c create mode 100644 tools/testing/selftests/bpf/progs/xattr.c diff --git a/tools/testing/selftests/bpf/prog_tests/xattr.c b/tools/testing/selftests/bpf/prog_tests/xattr.c new file mode 100644 index 000000000000..ef07fa8a1763 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/xattr.c @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright 2022 Google LLC. + */ + +#include +#include +#include "xattr.skel.h" + +#define XATTR_NAME "security.bpf" +#define XATTR_VALUE "test_progs" + +void test_xattr(void) +{ + struct xattr *skel = NULL; + char tmp_dir_path[] = "/tmp/xattrXXXXXX"; + char tmp_exec_path[64]; + char cmd[256]; + int err; + + if (CHECK_FAIL(!mkdtemp(tmp_dir_path))) + goto close_prog; + + snprintf(tmp_exec_path, sizeof(tmp_exec_path), "%s/copy_of_ls", + tmp_dir_path); + snprintf(cmd, sizeof(cmd), "cp /bin/ls %s", tmp_exec_path); + if (CHECK_FAIL(system(cmd))) + goto close_prog_rmdir; + + if (CHECK_FAIL(setxattr(tmp_exec_path, XATTR_NAME, XATTR_VALUE, + sizeof(XATTR_VALUE), 0))) + goto close_prog_rmdir; + + skel = xattr__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_load")) + goto close_prog_rmdir; + + err = xattr__attach(skel); + if (!ASSERT_OK(err, "xattr__attach failed")) + goto close_prog_rmdir; + + snprintf(cmd, sizeof(cmd), "%s -l", tmp_exec_path); + if (CHECK_FAIL(system(cmd))) + goto close_prog_rmdir; + + ASSERT_EQ(skel->bss->result, 1, "xattr result"); + +close_prog_rmdir: + snprintf(cmd, sizeof(cmd), "rm -rf %s", tmp_dir_path); + system(cmd); +close_prog: + xattr__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/xattr.c b/tools/testing/selftests/bpf/progs/xattr.c new file mode 100644 index 000000000000..ccc078fb8ebd --- /dev/null +++ b/tools/testing/selftests/bpf/progs/xattr.c @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright 2022 Google LLC. + */ + +#include "vmlinux.h" +#include +#include + +char _license[] SEC("license") = "GPL"; + +#define XATTR_NAME "security.bpf" +#define XATTR_VALUE "test_progs" + +__u64 result = 0; + +extern ssize_t bpf_getxattr(struct dentry *dentry, struct inode *inode, + const char *name, void *value, int size) __ksym; + +SEC("lsm.s/bprm_committed_creds") +void BPF_PROG(bprm_cc, struct linux_binprm *bprm) +{ + struct task_struct *current = bpf_get_current_task_btf(); + char dir_xattr_value[64] = {0}; + int xattr_sz = 0; + + xattr_sz = bpf_getxattr(bprm->file->f_path.dentry, + bprm->file->f_path.dentry->d_inode, XATTR_NAME, + dir_xattr_value, 64); + + if (xattr_sz <= 0) + return; + + if (!bpf_strncmp(dir_xattr_value, sizeof(XATTR_VALUE), XATTR_VALUE)) + result = 1; +}