Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6148065iog; Thu, 23 Jun 2022 12:20:11 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tzy33BDobb7AZhoFWgK67FwH4HCDap4vN59fnKYEHFBGx1DkevkbUjEIXkeMOG4m9h6PAH X-Received: by 2002:a17:907:9490:b0:722:e9ee:fbc7 with SMTP id dm16-20020a170907949000b00722e9eefbc7mr9753174ejc.338.1656012011393; Thu, 23 Jun 2022 12:20:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656012011; cv=none; d=google.com; s=arc-20160816; b=dntdRnsBTZ6TSOF0aiMuCTKWVGx0Oe8eMAUC7aclXfM//IwpUfR5PLT43rfqIiuN42 kE95sB5EApESfwoYkjox5VevzpowRiRGWLWumnBctzaSGvTXL6Zx9ygkPYZGfG7Hh8XC RzKKWeVkjV7ei6s0wLXt64b4apYcfj+s249OJcaE+YuYWKtMRAUD7UdtmmgyrParndsg OavD7+OqH6ZJ51Agn4PIeZjdAeSNiZ0OFTguaKcMKywkJrpzugzlWfW7TB9xoVgvC4IT G9ReMMn+vYVsDK150lR3u0EhU9z5tbMqRRSg6oHFvVQN04WJwZXoowUmaTGT3nBW1J4U BoIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=R+W/zM+dj2eDjwM2NUq14gKz33qNTD4J69wbUVIc5+Q=; b=W/YPY+4dXvlbGBpIw/9XwSwYvWCRv/PnI/w3b7pbGm3C4mVsiPvujXAR6v757Hqpfp FoBVQa1+jn/02K34Inf0f4OjnTHG8jC+H4bQR1txCxObi3Wt2rwmoQfnuQEDB7i2A9ze hsrPF3npJIa2YtLGblVRoCG7cCiCw60MFil10DxkZ8gDga+Ksisvw3QCN63WixISwZha POH9gfV6ceRo3PzKh1kJfKLCueZ81YdEX7/1dmzc7STMPcaMezloXC5pYatQ3zD6kgoh cdsNgmqRPIKevtLmsiSmf2GDf/4ff10YvBg5sdK5uWnVhX7ivOAccj7RffpxTR57OhNM Eexw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=gODA1EaR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b15-20020a056402350f00b004357f76325dsi392556edd.294.2022.06.23.12.19.43; Thu, 23 Jun 2022 12:20:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=gODA1EaR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234490AbiFWSYg (ORCPT + 99 others); Thu, 23 Jun 2022 14:24:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237715AbiFWSXV (ORCPT ); Thu, 23 Jun 2022 14:23:21 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 68622C2C5F; Thu, 23 Jun 2022 10:25:50 -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 A94C2B824C1; Thu, 23 Jun 2022 17:25:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1EBF3C341C7; Thu, 23 Jun 2022 17:25:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656005147; bh=2JmOFGewJ4ZB7dxdcwRFUX2CBNPWpL4Q3bPJdpAazwk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gODA1EaRzQ5QoYnXwnoWkESemA4D4HtaLZgYnadBOfcmxa3xxd0tvo+zerWaupJr5 AOboKkizQv+l6YkJKwMGBrnRs5opP7sP5T08+xOFM/jz2Cc7kxs9muo9JzlpFCOJ6C ZxOV13NBpuaU+zu8m4uX9RKPztyJ3zFgplLGTIEM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Simon Sundberg , =?UTF-8?q?Toke=20H=C3=B8iland-J=C3=B8rgensen?= , Alexei Starovoitov Subject: [PATCH 5.18 09/11] bpf: Fix calling global functions from BPF_PROG_TYPE_EXT programs Date: Thu, 23 Jun 2022 18:45:21 +0200 Message-Id: <20220623164322.585696427@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164322.315085512@linuxfoundation.org> References: <20220623164322.315085512@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Toke Høiland-Jørgensen commit f858c2b2ca04fc7ead291821a793638ae120c11d upstream. The verifier allows programs to call global functions as long as their argument types match, using BTF to check the function arguments. One of the allowed argument types to such global functions is PTR_TO_CTX; however the check for this fails on BPF_PROG_TYPE_EXT functions because the verifier uses the wrong type to fetch the vmlinux BTF ID for the program context type. This failure is seen when an XDP program is loaded using libxdp (which loads it as BPF_PROG_TYPE_EXT and attaches it to a global XDP type program). Fix the issue by passing in the target program type instead of the BPF_PROG_TYPE_EXT type to bpf_prog_get_ctx() when checking function argument compatibility. The first Fixes tag refers to the latest commit that touched the code in question, while the second one points to the code that first introduced the global function call verification. v2: - Use resolve_prog_type() Fixes: 3363bd0cfbb8 ("bpf: Extend kfunc with PTR_TO_CTX, PTR_TO_MEM argument support") Fixes: 51c39bb1d5d1 ("bpf: Introduce function-by-function verification") Reported-by: Simon Sundberg Signed-off-by: Toke Høiland-Jørgensen Link: https://lore.kernel.org/r/20220606075253.28422-1-toke@redhat.com Signed-off-by: Alexei Starovoitov [ backport: resolve conflict due to kptr series missing ] Signed-off-by: Toke Høiland-Jørgensen Signed-off-by: Greg Kroah-Hartman --- kernel/bpf/btf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -5769,6 +5769,7 @@ static int btf_check_func_arg_match(stru struct bpf_reg_state *regs, bool ptr_to_mem_ok) { + enum bpf_prog_type prog_type = resolve_prog_type(env->prog); struct bpf_verifier_log *log = &env->log; u32 i, nargs, ref_id, ref_obj_id = 0; bool is_kfunc = btf_is_kernel(btf); @@ -5834,8 +5835,7 @@ static int btf_check_func_arg_match(stru if (ret < 0) return ret; - if (btf_get_prog_ctx_type(log, btf, t, - env->prog->type, i)) { + if (btf_get_prog_ctx_type(log, btf, t, prog_type, i)) { /* If function expects ctx type in BTF check that caller * is passing PTR_TO_CTX. */