From patchwork Wed Oct 19 08:26:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 5036 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp246838wrs; Wed, 19 Oct 2022 03:39:17 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4FlcmzMoSVhOC7IzoA0znUfbGATocTOXK2D8NySBnaZiIApn265jxwKue4W89l2Lf6R0UA X-Received: by 2002:a17:906:4fcd:b0:78d:4b28:9d57 with SMTP id i13-20020a1709064fcd00b0078d4b289d57mr6133377ejw.303.1666175956994; Wed, 19 Oct 2022 03:39:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666175956; cv=none; d=google.com; s=arc-20160816; b=GAUJwnKsg2zfLhyTKbwZfi8cMUKSp3t9WTn+XyUqKM6+W5I5Jvbw3I+95aMpY99VTQ nQ8Sf+zOReD/x6sr39NqgasH0FKILvqvrR2UqexKovpw2PqhRTpT4C8vSbtVw76TqwGX K269seSCuCDRywpLsr9Ku2RLu4vmwhct8SCs6RbITfoN0W6DawOylfh7TDc+t3xMtMCw VQcthbrQPy9BxF7DuJWOLEwvEG3Wm8KvaEeq0hQ/PBC+r7JMjjIWvi0hAYmWfiAZeTud Y+GZ6c+iVy4q8dE9Pd+nv5UzTKwvEuHIu+wx5Pce4DQGb6sIlS1oVjcOiEGMJIsKQMSA 9goA== 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=bjksqVOXQBfaH9ZWHXkPPOm4GgaCwe7HdZj9vT9HmKY=; b=k9ruVxfBs4o4l28T+fuwWBJMjJvc5a15RM0qDOjwG9R3z6FOxHW2Jx7YSxD1rRVzGP 2TDVnNTZ0DXeLaZM/7vfoY7W5D4Hv9Wba+DJWwS8B2Jsqm2E7r3vm/7XIeo1Qm39tQJB qnKzT+GBfyzKKmwj4mpgFl7BJIaNf5uNUDgcANW8cGk8G7m9vj6maklBfdMp1j/3mUdT BcsFQddFN4r3wLUQWrwpVFlbe/NRYFglYjc5D6uWswha3/lpf/ONmKcBKcnIiV4okN8s zshexQWvgCi4E9cwh0IilYaVp+GBry5/szAQva+z1Ud2sfGR/ws+W1UffSlY1mZwPnFB CkEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=xKjD8QTL; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cb25-20020a170906a45900b0078e1d213812si11102688ejb.184.2022.10.19.03.38.51; Wed, 19 Oct 2022 03:39:16 -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=xKjD8QTL; 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 S231883AbiJSKiF (ORCPT + 99 others); Wed, 19 Oct 2022 06:38:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230429AbiJSKhb (ORCPT ); Wed, 19 Oct 2022 06:37:31 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17B2B108254; Wed, 19 Oct 2022 03:16:10 -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 7D0C5B8238A; Wed, 19 Oct 2022 08:51:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EBE0BC433D6; Wed, 19 Oct 2022 08:51:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666169493; bh=cSXhoQQnS0sgrIV1JzC+sZBN08NptzZPX3j/Utqtc7Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xKjD8QTLm/a+WfkIty0SGGd/vBQn+xADypRod55e0jZl1FYDODChzbeIXb3ELnTrr /qOW162DDPuYSapBV7B4tME2mRifdMVRK+kZGGIluhFNodWdYToBZHBu9TFq/pIduC pXJrK3UsAKf5OBzfjMwEs2ZRbfdYX4NdSrvLnh6Q= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andrii Nakryiko , Daniel Borkmann , Sasha Levin Subject: [PATCH 6.0 296/862] libbpf: Fix crash if SEC("freplace") programs dont have attach_prog_fd set Date: Wed, 19 Oct 2022 10:26:23 +0200 Message-Id: <20221019083303.081773927@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221019083249.951566199@linuxfoundation.org> References: <20221019083249.951566199@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.4 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747112119953940098?= X-GMAIL-MSGID: =?utf-8?q?1747112119953940098?= From: Andrii Nakryiko [ Upstream commit 749c202cb6ea40f4d7ac95c4a1217a7b506f43a8 ] Fix SIGSEGV caused by libbpf trying to find attach type in vmlinux BTF for freplace programs. It's wrong to search in vmlinux BTF and libbpf doesn't even mark vmlinux BTF as required for freplace programs. So trying to search anything in obj->vmlinux_btf might cause NULL dereference if nothing else in BPF object requires vmlinux BTF. Instead, error out if freplace (EXT) program doesn't specify attach_prog_fd during at the load time. Fixes: 91abb4a6d79d ("libbpf: Support attachment of BPF tracing programs to kernel modules") Signed-off-by: Andrii Nakryiko Signed-off-by: Daniel Borkmann Link: https://lore.kernel.org/bpf/20220909193053.577111-3-andrii@kernel.org Signed-off-by: Sasha Levin --- tools/lib/bpf/libbpf.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -9056,11 +9056,15 @@ static int libbpf_find_attach_btf_id(str int err = 0; /* BPF program's BTF ID */ - if (attach_prog_fd) { + if (prog->type == BPF_PROG_TYPE_EXT || attach_prog_fd) { + if (!attach_prog_fd) { + pr_warn("prog '%s': attach program FD is not set\n", prog->name); + return -EINVAL; + } err = libbpf_find_prog_btf_id(attach_name, attach_prog_fd); if (err < 0) { - pr_warn("failed to find BPF program (FD %d) BTF ID for '%s': %d\n", - attach_prog_fd, attach_name, err); + pr_warn("prog '%s': failed to find BPF program (FD %d) BTF ID for '%s': %d\n", + prog->name, attach_prog_fd, attach_name, err); return err; } *btf_obj_fd = 0; @@ -9077,7 +9081,8 @@ static int libbpf_find_attach_btf_id(str err = find_kernel_btf_id(prog->obj, attach_name, attach_type, btf_obj_fd, btf_type_id); } if (err) { - pr_warn("failed to find kernel BTF type ID of '%s': %d\n", attach_name, err); + pr_warn("prog '%s': failed to find kernel BTF type ID of '%s': %d\n", + prog->name, attach_name, err); return err; } return 0;