From nobody Sat Apr 11 05:44:19 2026 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 CE4A6C6FA8E for ; Thu, 2 Mar 2023 12:34:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229675AbjCBMe4 (ORCPT ); Thu, 2 Mar 2023 07:34:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229523AbjCBMey (ORCPT ); Thu, 2 Mar 2023 07:34:54 -0500 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7997C196A1 for ; Thu, 2 Mar 2023 04:34:52 -0800 (PST) Received: by mail-wm1-x32c.google.com with SMTP id l7-20020a05600c4f0700b003e79fa98ce1so1401464wmq.2 for ; Thu, 02 Mar 2023 04:34:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=HrezzsKX3CNQQh1aE1R/Oh3up/yQLahHBMvgdcVIB/M=; b=AALLbwxufAgi1qtXiuvonhy/sYJA3HoGvt25pwS8TrTVps+hst1+ocicdb+t5OvuPy wT2x0Pnl3uybxDiJF5n01gNFTyFV5Ddp2hSznvcjlhTA/1NTkKPlTBBryGoBToDhJ1Zx Ez9a+0ZTSFWCXu2jOfy31PeYy/VNtsgSdPyHpdgqrZt8hjuGLoXn+LHY/6x+JWoJia/j WBHShey5zPQYf4vDh2/6jPWGHQxvlapzyWZo+uezkgovkzuEoWXR0fa+m1ZRRA8/QwTj h88E2yulX5lLocRAhUerUeiKtk207gN81IsDzcnh6/P8fA0JR03DWil1DaNqGS2FvZ5u HzgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=HrezzsKX3CNQQh1aE1R/Oh3up/yQLahHBMvgdcVIB/M=; b=J+w2XjL3SRG9NzHFpzW6CjpeFSKve8NU+uFxpR3CRFLK9VN8GxyNEWOb3S6EqGUYn3 RjCX9moZG3nmhjfgIqPvrlblW8Nn1ZsntQ2gY5tjGNymvwu7Gx3Wr5/fo+MluZbhp+Fv xyjlUQwj4gcPPkEX1WTlc8fYPuvrsM0Y6cyPiKnQy5/yU0sO59V/SvOZD/fIan15cM5w 4co4y2n2B9aj03x4N5YP7PBF/58BqjRikxZuN7SDwp8kkr+bo9qQ8liqXNJEPiTYz1Ui hhdEoYfCToOKMVOHhpV3xnLNfaLWat1kcsOKfO6lYiq+JFka/5KPhj7tGX3LlAB69E5B YW8A== X-Gm-Message-State: AO0yUKUYC6Ds+5RjnELhQLchzyVmwhxyqkpLp6e2/Y/oU2pA3Wl8ZM// lgFuINnYcnneq30VVwHDk8zYiQ== X-Google-Smtp-Source: AK7set9911P/JcoyltopCJ0H3lCKo+BZQ5jwLQSudjBZx3m7H3U1K/WJCuJ88IPxd32N7KNrD1AJVg== X-Received: by 2002:a05:600c:5252:b0:3ea:8ed9:8f03 with SMTP id fc18-20020a05600c525200b003ea8ed98f03mr7520533wmb.24.1677760490953; Thu, 02 Mar 2023 04:34:50 -0800 (PST) Received: from tpx1.lan (f.c.7.0.0.0.0.0.0.0.0.0.0.0.0.0.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff::7cf]) by smtp.gmail.com with ESMTPSA id s2-20020adff802000000b002c706c754fesm15232575wrp.32.2023.03.02.04.34.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Mar 2023 04:34:50 -0800 (PST) From: Lorenz Bauer X-Google-Original-From: Lorenz Bauer To: Martin KaFai Lau , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Cc: Lorenz Bauer , Martin KaFai Lau , bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] btf: fix resolving BTF_KIND_VAR after ARRAY, STRUCT, UNION, PTR Date: Thu, 2 Mar 2023 12:34:40 +0000 Message-Id: <20230302123440.1193507-1-lmb@isovalent.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" btf_datasec_resolve contains a bug that causes the following BTF to fail loading: [1] DATASEC a size=3D2 vlen=3D2 type_id=3D4 offset=3D0 size=3D1 type_id=3D7 offset=3D1 size=3D1 [2] INT (anon) size=3D1 bits_offset=3D0 nr_bits=3D8 encoding=3D(none) [3] PTR (anon) type_id=3D2 [4] VAR a type_id=3D3 linkage=3D0 [5] INT (anon) size=3D1 bits_offset=3D0 nr_bits=3D8 encoding=3D(none) [6] TYPEDEF td type_id=3D5 [7] VAR b type_id=3D6 linkage=3D0 This error message is printed during btf_check_all_types: [1] DATASEC a size=3D2 vlen=3D2 type_id=3D7 offset=3D1 size=3D1 Invalid type By tracing btf_*_resolve we can pinpoint the problem: btf_datasec_resolve(depth: 1, type_id: 1, mode: RESOLVE_TBD) =3D 0 btf_var_resolve(depth: 2, type_id: 4, mode: RESOLVE_TBD) =3D 0 btf_ptr_resolve(depth: 3, type_id: 3, mode: RESOLVE_PTR) =3D 0 btf_var_resolve(depth: 2, type_id: 4, mode: RESOLVE_PTR) =3D 0 btf_datasec_resolve(depth: 1, type_id: 1, mode: RESOLVE_PTR) =3D -22 The last invocation of btf_datasec_resolve should invoke btf_var_resolve by means of env_stack_push, instead it returns EINVAL. The reason is that env_stack_push is never executed for the second VAR. if (!env_type_is_resolve_sink(env, var_type) && !env_type_is_resolved(env, var_type_id)) { env_stack_set_next_member(env, i + 1); return env_stack_push(env, var_type, var_type_id); } env_type_is_resolve_sink() changes its behaviour based on resolve_mode. For RESOLVE_PTR, we can simplify the if condition to the following: (btf_type_is_modifier() || btf_type_is_ptr) && !env_type_is_resolved() Since we're dealing with a VAR the clause evaluates to false. This is not sufficient to trigger the bug however. The log output and EINVAL are only generated if btf_type_id_size() fails. if (!btf_type_id_size(btf, &type_id, &type_size)) { btf_verifier_log_vsi(env, v->t, vsi, "Invalid type"); return -EINVAL; } Most types are sized, so for example a VAR referring to an INT is not a problem. The bug is only triggered if a VAR points at a modifier. Since we skipped btf_var_resolve that modifier was also never resolved, which means that btf_resolved_type_id returns 0 aka VOID for the modifier. This in turn causes btf_type_id_size to return NULL, triggering EINVAL. To summarise, the following conditions are necessary: - VAR pointing at PTR, STRUCT, UNION or ARRAY - Followed by a VAR pointing at TYPEDEF, VOLATILE, CONST, RESTRICT or TYPE_TAG The fix is to reset resolve_mode to RESOLVE_TBD before attempting to resolve a VAR from a DATASEC. Fixes: 1dc92851849c ("bpf: kernel side support for BTF Var and DataSec") Signed-off-by: Lorenz Bauer --- kernel/bpf/btf.c | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index fa22ec79ac0e..91145298c238 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -4579,6 +4579,7 @@ static int btf_datasec_resolve(struct btf_verifier_en= v *env, return -EINVAL; } =20 + env->resolve_mode =3D RESOLVE_TBD; if (!env_type_is_resolve_sink(env, var_type) && !env_type_is_resolved(env, var_type_id)) { env_stack_set_next_member(env, i + 1); --=20 2.39.2