From patchwork Sun Jul 9 02:59:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13305733 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1D7F317C6 for ; Sun, 9 Jul 2023 02:59:28 +0000 (UTC) Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B80FEE45 for ; Sat, 8 Jul 2023 19:59:26 -0700 (PDT) Received: by mail-pg1-x52c.google.com with SMTP id 41be03b00d2f7-55ae51a45deso1630296a12.3 for ; Sat, 08 Jul 2023 19:59:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1688871566; x=1691463566; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Fb4Q5082Tgrw2hcUXrT55P3pFjx3xVG0YEjEsl93YPA=; b=bSDaiZzfWw5/Wu/Y9y83KnLJglne1CyblWKlXkwTyuDiov58zbN/qhRnYsScptaF/C b9/3mXjZ0z437jl4lDMm491+PWMUOH3oQ6GLWf92lKuryZkcXrL9bniROkafnnHh4bCv xGLQx7S21KAfYWd/3z3pXh0QWa8H4lOiGbWJoRqvE9XjXswg8qLMXEp60LPk5Tm14by3 Lk2FTaKeodVBB0deufbbopUbaZr27NO0kalYJ1zhuHXt5M1cI+Clu5cv/ffCfMRbOdgu tYVAcwVGW5wJZ7LS5M5oT8Jcjda37rLeWhfmglnYFKWSAsxOWzUoO8bAhURIlZBTyf0F JIWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688871566; x=1691463566; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Fb4Q5082Tgrw2hcUXrT55P3pFjx3xVG0YEjEsl93YPA=; b=fPxPfuqK5EZ5//pM2CINUuelf+beLSGWIpKphXY7TMKb1kjn1TxUUaLwL4gLafcw4s 8wlZRgDOzuhbjhKJfg96TPuj7Imc8lyMOYMFOaag6QcC8QzKq3HvAn+3G21fBCdszmvF tRxDkm0Hvd8r4A/E4daKb5YaViUM0PSeVdokGDsRoQSBvo1j09kT7FAgZRMBMOSZqnfb ++qu4bCxNJ8UZdNlq4AWiel1Wr5iDoWhFlVXVF4AcFYcbvj4GEGU91ueM6OqFd/NN6cW bF7KWNZ7djykXSkGVwi4/VauD6wBDie44p+hztVlYjsvrIJOcsy7/hc4yko5LXUATSGV wvpQ== X-Gm-Message-State: ABy/qLbUIbvm0/68T82KJ7tsF/dmP47baob65jc6IzrbcxuCzoK7vj2x mub/rCc0btvwn7Mr782DisE= X-Google-Smtp-Source: APBJJlEEBp748nO2V+WBCHqEQUBReP6mcNzfSkmnQZcB4QD6NWoI9TnkS+itbiEOxnmY9oUhyOkfRg== X-Received: by 2002:a17:90a:f407:b0:265:7719:b849 with SMTP id ch7-20020a17090af40700b002657719b849mr4659538pjb.41.1688871566121; Sat, 08 Jul 2023 19:59:26 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:14bb:5400:4ff:fe80:41df]) by smtp.gmail.com with ESMTPSA id q9-20020a17090a68c900b0024e4f169931sm3670659pjj.2.2023.07.08.19.59.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Jul 2023 19:59:25 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next 1/3] bpf: Introduce BTF_TYPE_SAFE_TRUSTED_UNION Date: Sun, 9 Jul 2023 02:59:10 +0000 Message-Id: <20230709025912.3837-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230709025912.3837-1-laoar.shao@gmail.com> References: <20230709025912.3837-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net When we are verifying a field in a union, we may unexpectedly verify another field which has the same offset in this union. So in such case, we should annotate that field as PTR_UNTRUSTED. However, in some cases we are sure some fields in a union is safe and then we can add them into BTF_TYPE_SAFE_TRUSTED_UNION allow list. Signed-off-by: Yafang Shao --- kernel/bpf/btf.c | 20 +++++++++----------- kernel/bpf/verifier.c | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 3dd47451f097..fae6fc24a845 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -6133,7 +6133,6 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf, const char *tname, *mname, *tag_value; u32 vlen, elem_id, mid; - *flag = 0; again: if (btf_type_is_modifier(t)) t = btf_type_skip_modifiers(btf, t->type, NULL); @@ -6144,6 +6143,14 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf, } vlen = btf_type_vlen(t); + if (BTF_INFO_KIND(t->info) == BTF_KIND_UNION && vlen != 1 && !(*flag & PTR_UNTRUSTED)) + /* + * walking unions yields untrusted pointers + * with exception of __bpf_md_ptr and other + * unions with a single member + */ + *flag |= PTR_UNTRUSTED; + if (off + size > t->size) { /* If the last element is a variable size array, we may * need to relax the rule. @@ -6304,15 +6311,6 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf, * of this field or inside of this struct */ if (btf_type_is_struct(mtype)) { - if (BTF_INFO_KIND(mtype->info) == BTF_KIND_UNION && - btf_type_vlen(mtype) != 1) - /* - * walking unions yields untrusted pointers - * with exception of __bpf_md_ptr and other - * unions with a single member - */ - *flag |= PTR_UNTRUSTED; - /* our field must be inside that union or struct */ t = mtype; @@ -6478,7 +6476,7 @@ bool btf_struct_ids_match(struct bpf_verifier_log *log, bool strict) { const struct btf_type *type; - enum bpf_type_flag flag; + enum bpf_type_flag flag = 0; int err; /* Are we already done? */ diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 11e54dd8b6dd..1fb0a64f5bce 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5847,6 +5847,7 @@ static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) #define BTF_TYPE_SAFE_RCU(__type) __PASTE(__type, __safe_rcu) #define BTF_TYPE_SAFE_RCU_OR_NULL(__type) __PASTE(__type, __safe_rcu_or_null) #define BTF_TYPE_SAFE_TRUSTED(__type) __PASTE(__type, __safe_trusted) +#define BTF_TYPE_SAFE_TRUSTED_UNION(__type) __PASTE(__type, __safe_trusted_union) /* * Allow list few fields as RCU trusted or full trusted. @@ -5914,6 +5915,11 @@ BTF_TYPE_SAFE_TRUSTED(struct socket) { struct sock *sk; }; +/* union trusted: these fields are trusted even in a uion */ +BTF_TYPE_SAFE_TRUSTED_UNION(struct sk_buff) { + struct sock *sk; +}; + static bool type_is_rcu(struct bpf_verifier_env *env, struct bpf_reg_state *reg, const char *field_name, u32 btf_id) @@ -5950,6 +5956,17 @@ static bool type_is_trusted(struct bpf_verifier_env *env, return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id, "__safe_trusted"); } + +static bool type_is_trusted_union(struct bpf_verifier_env *env, + struct bpf_reg_state *reg, + const char *field_name, u32 btf_id) +{ + BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_UNION(struct sk_buff)); + + return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id, + "__safe_trusted_union"); +} + static int check_ptr_to_btf_access(struct bpf_verifier_env *env, struct bpf_reg_state *regs, int regno, int off, int size, @@ -6087,6 +6104,10 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env, clear_trusted_flags(&flag); } + /* Clear the PTR_UNTRUSTED for the fields which are in the allow list */ + if (type_is_trusted_union(env, reg, field_name, btf_id)) + flag &= ~PTR_UNTRUSTED; + if (atype == BPF_READ && value_regno >= 0) mark_btf_ld_reg(env, regs, value_regno, ret, reg->btf, btf_id, flag); From patchwork Sun Jul 9 02:59:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13305734 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E116185D for ; Sun, 9 Jul 2023 02:59:28 +0000 (UTC) Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F21F8E48 for ; Sat, 8 Jul 2023 19:59:27 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id 98e67ed59e1d1-262e89a3ee2so1652118a91.1 for ; Sat, 08 Jul 2023 19:59:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1688871567; x=1691463567; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/qKQNmDgEbsmo5qrDDGhDZO25ZZ7r2Gtzsmn/56tImQ=; b=Hcwc1olL/i08BfZbNG09dq6a8skklHMpD9uRtAxu5TI+V0y5r4GPrTBI6vuLGG9ZZM 0QQIwdXaEIacRGiqC4fqhSrT7Uk0T+YTDt68iTxRmztrr/3Q1jxbRa3a7Xq7UZm9jGg5 UAzEnqfTgna9g+6RF4eEKCoqA8J0XqCw1Oi6Fmco9vXBcWZ33SSrKCabN6xAzKVVjF0X glHOUoatwTwQ4s2dmhfIWw8byID+5MGomZy3ZwSt0P7T+pnBhBb90/zR4wJe/Gip75ca Up2/cf+JOMbX76bLSNIkF4YsejxDb4veSmuFBFjqX6STOhOMhAnRawWLUbpm/ooVIDJV ZNsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688871567; x=1691463567; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/qKQNmDgEbsmo5qrDDGhDZO25ZZ7r2Gtzsmn/56tImQ=; b=KQw7NIyZID3y4TEihX45xeRK4Od9bQTF1thgq4y2MFM7ro7MXaNeHqTrqiejgTHors jVs3FuphKDGUzD/9/yGt20YlGHgvuYd2s7djxTyhyXzli1GJdqBuNzQK91zQ+N1yDqnQ 6J9EgUWWTMVm1H7rerfKV85L8akk+70VrybcPBwmZQaP8rYy8P1X8wHX+JsmbgaIOCc9 NLDj8OpYGZEXpQ9lugg3dj19OHj7BAiao3kIFrEiPrA/eqamirP1fjJT4qRC7uQjGmJU Dip63RDY2Wv6njYIdyLiNKTvCfnlVvuDJRDebdiHjeDrtO/IPR+0pAKn8wXDqSEgqJb5 lPQA== X-Gm-Message-State: ABy/qLYwBrRhasolVcevOIeTiTtfYzbBvveNJA0AQx4/WhfWCc00ARm+ G6Yb6gzeAUVti8uLk5iieoo= X-Google-Smtp-Source: APBJJlEzqrQD4AFg5nbAczXSXLN6EIin+sMM19P1aRT0nSn22t4oIfN402ibt9nFL0RTIN430I7IUw== X-Received: by 2002:a17:90a:5b0e:b0:263:4e41:bdb4 with SMTP id o14-20020a17090a5b0e00b002634e41bdb4mr6787757pji.33.1688871567389; Sat, 08 Jul 2023 19:59:27 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:14bb:5400:4ff:fe80:41df]) by smtp.gmail.com with ESMTPSA id q9-20020a17090a68c900b0024e4f169931sm3670659pjj.2.2023.07.08.19.59.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Jul 2023 19:59:27 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next 2/3] selftests/bpf: Add selftests for BTF_TYPE_SAFE_TRUSTED_UNION Date: Sun, 9 Jul 2023 02:59:11 +0000 Message-Id: <20230709025912.3837-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230709025912.3837-1-laoar.shao@gmail.com> References: <20230709025912.3837-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net Add selftests for BTF_TYPE_SAFE_TRUSTED_UNION, the result as follows: #141/1 nested_trust/test_read_cpumask:OK #141/2 nested_trust/test_skb_field:OK <<<< #141/3 nested_trust/test_invalid_nested_user_cpus:OK #141/4 nested_trust/test_invalid_nested_offset:OK #141/5 nested_trust/test_invalid_skb_field:OK <<<< #141 nested_trust:OK Signed-off-by: Yafang Shao --- .../selftests/bpf/progs/nested_trust_failure.c | 16 ++++++++++++++++ .../selftests/bpf/progs/nested_trust_success.c | 15 +++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/nested_trust_failure.c b/tools/testing/selftests/bpf/progs/nested_trust_failure.c index 0d1aa6bbace4..ea39497f11ed 100644 --- a/tools/testing/selftests/bpf/progs/nested_trust_failure.c +++ b/tools/testing/selftests/bpf/progs/nested_trust_failure.c @@ -10,6 +10,13 @@ char _license[] SEC("license") = "GPL"; +struct { + __uint(type, BPF_MAP_TYPE_SK_STORAGE); + __uint(map_flags, BPF_F_NO_PREALLOC); + __type(key, int); + __type(value, u64); +} sk_storage_map SEC(".maps"); + /* Prototype for all of the program trace events below: * * TRACE_EVENT(task_newtask, @@ -31,3 +38,12 @@ int BPF_PROG(test_invalid_nested_offset, struct task_struct *task, u64 clone_fla bpf_cpumask_first_zero(&task->cpus_mask); return 0; } + +/* Although R2 is of type sk_buff but sock_common is expected, we will hit untrusted ptr first. */ +SEC("tp_btf/tcp_probe") +__failure __msg("R2 type=untrusted_ptr_ expected=ptr_, trusted_ptr_, rcu_ptr_") +int BPF_PROG(test_invalid_skb_field, struct sock *sk, struct sk_buff *skb) +{ + bpf_sk_storage_get(&sk_storage_map, skb->next, 0, 0); + return 0; +} diff --git a/tools/testing/selftests/bpf/progs/nested_trust_success.c b/tools/testing/selftests/bpf/progs/nested_trust_success.c index 886ade4aa99d..833840bffd3b 100644 --- a/tools/testing/selftests/bpf/progs/nested_trust_success.c +++ b/tools/testing/selftests/bpf/progs/nested_trust_success.c @@ -10,6 +10,13 @@ char _license[] SEC("license") = "GPL"; +struct { + __uint(type, BPF_MAP_TYPE_SK_STORAGE); + __uint(map_flags, BPF_F_NO_PREALLOC); + __type(key, int); + __type(value, u64); +} sk_storage_map SEC(".maps"); + SEC("tp_btf/task_newtask") __success int BPF_PROG(test_read_cpumask, struct task_struct *task, u64 clone_flags) @@ -17,3 +24,11 @@ int BPF_PROG(test_read_cpumask, struct task_struct *task, u64 clone_flags) bpf_cpumask_test_cpu(0, task->cpus_ptr); return 0; } + +SEC("tp_btf/tcp_probe") +__success +int BPF_PROG(test_skb_field, struct sock *sk, struct sk_buff *skb) +{ + bpf_sk_storage_get(&sk_storage_map, skb->sk, 0, 0); + return 0; +} From patchwork Sun Jul 9 02:59:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13305735 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 39500185D for ; Sun, 9 Jul 2023 02:59:30 +0000 (UTC) Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49332E45 for ; Sat, 8 Jul 2023 19:59:29 -0700 (PDT) Received: by mail-pg1-x52b.google.com with SMTP id 41be03b00d2f7-53482b44007so1629346a12.2 for ; Sat, 08 Jul 2023 19:59:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1688871569; x=1691463569; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7q7c8LyHLAJMt2B4YkHxHBPcsxHYOmrQVVgSh5G9AYg=; b=czOgEjcvY/3lQOE1MNYRNHkkPiy1qCqa3iBtQQjalx5UVFGMvwdRfNg/WFP8Ob8NYh 8eimR/hoIzKbPTJFHnuzcAiZLZCZL4emKLXhQA5FC9JX1C4ledJhlVEpirGZfg4RV1WC ZNPCQJX9Gb2Xw1itPkUF5QK90MeyMI+FgERYJS+508B/k2BNhvvc+Kcudr+LPYUwKDR3 NFA3RBG+0I7t5HIsB5e5Qsof44MhT9xY2FKyO7rg0zEssMSdrObx/jnOVBhDZCGNWE1x jhKPytsTCvPuyr6/e7EBSzEwzx4gy65CLormS1lusFPVhbpQDFqln/NsVOTiAKMQrYvj IXQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688871569; x=1691463569; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7q7c8LyHLAJMt2B4YkHxHBPcsxHYOmrQVVgSh5G9AYg=; b=NBRnxEgwRHWRcupkZUt+ZSg0pmov7WPikqxjp1kenGNTTfW8poXzLewaAyVwXrR6m0 t+BSP/vUn/0QiSmeSQ54idfg90yCOwKL8/xhk9UHUMkTsmCiM20+GJF+g3WPDjEg4jyb HaisnAYim2gewpLwOZyTMa0LF/cL1p0X7c82NyVCMNBG4wCVX12b/gSKSDe7gNDmNabb U6MH4mUUfW/55CVpk6QTVEJtAahxm2yaDdrLoPyU1O5QTTQgFdbItfrOUGZdxo7jR/Dm dtiPdx4llxrQBeecosXSOhloXkErDiO18JRXQXN6NAG6Wv76jhZW27sUpeBYomdc09RO VFEQ== X-Gm-Message-State: ABy/qLYQAjIaAxJhBmCcz20yOcPRfXpwYzPWMPT/2cAF/WR2SIejqz+3 aFdpVidbCz8uRbj5rvEaCb8= X-Google-Smtp-Source: APBJJlHW3RnqHD6eonFnqVgfxr7rtmKrfWdNNda3YblhAZUpLMqIWlbSI6OfiNHbcjVPYCXTd8TuEA== X-Received: by 2002:a17:90b:1b4b:b0:25b:b2ba:2ff4 with SMTP id nv11-20020a17090b1b4b00b0025bb2ba2ff4mr7085181pjb.17.1688871568712; Sat, 08 Jul 2023 19:59:28 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:14bb:5400:4ff:fe80:41df]) by smtp.gmail.com with ESMTPSA id q9-20020a17090a68c900b0024e4f169931sm3670659pjj.2.2023.07.08.19.59.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Jul 2023 19:59:28 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next 3/3] bpf: Fix an error in verifying a field in a union Date: Sun, 9 Jul 2023 02:59:12 +0000 Message-Id: <20230709025912.3837-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230709025912.3837-1-laoar.shao@gmail.com> References: <20230709025912.3837-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net We are utilizing BPF LSM to monitor BPF operations within our container environment. When we add support for raw_tracepoint, it hits below error. ; (const void *)attr->raw_tracepoint.name); 27: (79) r3 = *(u64 *)(r2 +0) access beyond the end of member map_type (mend:4) in struct (anon) with off 0 size 8 It can be reproduced with below BPF prog. SEC("lsm/bpf") int BPF_PROG(bpf_audit, int cmd, union bpf_attr *attr, unsigned int size) { switch (cmd) { case BPF_RAW_TRACEPOINT_OPEN: bpf_printk("raw_tracepoint is %s", attr->raw_tracepoint.name); break; default: break; } return 0; } The reason is that when accessing a field in a union, such as bpf_attr, if the field is located within a nested struct that is not the first member of the union, it can result in incorrect field verification. union bpf_attr { struct { __u32 map_type; <<<< Actually it will find that field. __u32 key_size; __u32 value_size; ... }; ... struct { __u64 name; <<<< We want to verify this field. __u32 prog_fd; } raw_tracepoint; }; Considering the potential deep nesting levels, finding a perfect solution to address this issue has proven challenging. Therefore, I propose a solution where we simply skip the verification process if the field in question is located within a union. Fixes: 7e3617a72df3 ("bpf: Add array support to btf_struct_access") Signed-off-by: Yafang Shao --- kernel/bpf/btf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index fae6fc24a845..a542760c807a 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -6368,7 +6368,7 @@ static int btf_struct_walk(struct bpf_verifier_log *log, const struct btf *btf, * that also allows using an array of int as a scratch * space. e.g. skb->cb[]. */ - if (off + size > mtrue_end) { + if (off + size > mtrue_end && !(*flag & PTR_UNTRUSTED)) { bpf_log(log, "access beyond the end of member %s (mend:%u) in struct %s with off %u size %u\n", mname, mtrue_end, tname, off, size);