Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp900139rwi; Wed, 19 Oct 2022 04:30:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7tU2IqqwM5oExMRXUyqHcjN5AUgrKLQYahmYUeQQz5tFrx4r7bjXMf1muuQg3AO6jrAqjh X-Received: by 2002:a17:90a:3f86:b0:20a:e7dc:340a with SMTP id m6-20020a17090a3f8600b0020ae7dc340amr9414435pjc.157.1666179040208; Wed, 19 Oct 2022 04:30:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666179040; cv=none; d=google.com; s=arc-20160816; b=uxIvbgkNmpMe67cJEVK2UyoBOjON2OO4+AG6ss7IAPmrIbvlCV7TW2/UoAL1Q9ZW5/ alZTbYTu1u13AGrxI8VJOltH7qheIfLWra1honSoukoNLsAWwGCwhjm8G9KGurD7KA/w 9UDydjl5qbDgGwXmYxUFwD6YepzDz2JwHQyrw3gxNm3U956exPnBBER0HomvJbiJOBen c2cTdfmlQzyI6bp9el28Xn1Isx+jyey/cYEscepCQDmnnmDDGqIj4GW0+RLB6HrZ2dr2 wDYsySuB/VgCPUmTwisSfkfN6xNbNt2Gwpnt+rAtGUv5Ces8byjg8U53NXMZfW7yOAgI Mk5w== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t5-20020a17090aba8500b0020dbb78e5c6si16283871pjr.73.2022.10.19.04.30.27; Wed, 19 Oct 2022 04:30:40 -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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 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;