Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp938293pxb; Wed, 6 Apr 2022 04:51:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx8aMMqIXkp746ZWE5dPrxQQLPP1QlFY7EgKGoaV025SM7zejbYiMcUV5A1vKwsi3f3DaG3 X-Received: by 2002:a63:d257:0:b0:399:2df1:2fc with SMTP id t23-20020a63d257000000b003992df102fcmr6626213pgi.268.1649245873253; Wed, 06 Apr 2022 04:51:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649245873; cv=none; d=google.com; s=arc-20160816; b=gZEePRIRRcjws3046JjLg8/I9PWC2T/GJWZThWqBVhKCmNt+Lb1/H8OzrTHETbf5gx SmwleDF7wt9JuOEVSF5Im40kNsugB76HkT0QGHlSjf2u30ANDhHI4x++7S3ay31SioKx ng7K0Ydg/aWdONeelAD80YrD6x236NHY1oMIj3X+I+NAVpdAMqBm4JJbpzd7blE5TrKg RvW7V7wohpWYJDxbAeM6C9qLQVC2dKEWLurM3W5YSloihrX7FjQLhj52IBmYzuPT/3W7 /fgHu4+4i6IrBR+9MiS72xyJohwDWGt3NcQZtavc21A5Ud+xKN1kw5x+Cb0OYIJf7ryz v/cw== 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=tgS2A1q8R+jkfE0KBIkcBzb4XssMWYMrUh9YlKjIn78=; b=C5gR+nib+BNViJIPvjPec0A0H6UDCSsAuRwR6+WvPEHy5PtOu+diNlLueyHhWyIRNJ NOuUfX6kOq5VZpBroMws1yWG3LpkrU4Z9doMcBQ3PSBHqhzLwRK4Y6cu7TyaW8T3qP4K 3q/nDUh7RZ5gU+Ez3UvdCijsOvKSHwB/keaI1Ob6amtCW+YHdKEuo+sh1JU7n5CY11Fh M4hpxdtr7hfiuFlBlX7wdEpw58uVdkEqZJkOXKWkY7gynphnlRjnQfEjRVjR68PAOCoW EoVkrAV2+ww7/3v26nbuqEE2e5SY0+126BmeGdVDsApf522UirRlklSPGJpyyYgKN6jl i0cw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Ea9ZPAhR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id d15-20020a17090ac24f00b001c73b8066e0si4222954pjx.74.2022.04.06.04.51.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 04:51:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Ea9ZPAhR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3C85711867E; Wed, 6 Apr 2022 03:09:43 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1578109AbiDEXSa (ORCPT + 99 others); Tue, 5 Apr 2022 19:18:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348808AbiDEJsi (ORCPT ); Tue, 5 Apr 2022 05:48:38 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E495190FD9; Tue, 5 Apr 2022 02:35:54 -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 9762EB81B75; Tue, 5 Apr 2022 09:35:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E467FC385A2; Tue, 5 Apr 2022 09:35:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649151352; bh=9XFbWrU7lWAogCoLR4xnEVL/QMKEss/87erBKW6mK7A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ea9ZPAhRp85B2t7C9UTu4IZSQm1MoE1oxUVXvbYo3dz0CA7/jmMQbCqKR4/impty2 He7G9JTSboR6JwNdnuKOfVxansWZh5CPgmMq/uus0vcSwLx811l6HfHlct7qQot5FH gOBUCOcHrLx+0mZLwdJFB76lw0r1XqkJgLyVU6/Q= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Yafang Shao , Andrii Nakryiko , Sasha Levin Subject: [PATCH 5.15 385/913] libbpf: Fix possible NULL pointer dereference when destroying skeleton Date: Tue, 5 Apr 2022 09:24:07 +0200 Message-Id: <20220405070351.387736568@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@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=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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: Yafang Shao [ Upstream commit a32ea51a3f17ce6524c9fc19d311e708331c8b5f ] When I checked the code in skeleton header file generated with my own bpf prog, I found there may be possible NULL pointer dereference when destroying skeleton. Then I checked the in-tree bpf progs, finding that is a common issue. Let's take the generated samples/bpf/xdp_redirect_cpu.skel.h for example. Below is the generated code in xdp_redirect_cpu__create_skeleton(): xdp_redirect_cpu__create_skeleton struct bpf_object_skeleton *s; s = (struct bpf_object_skeleton *)calloc(1, sizeof(*s)); if (!s) goto error; ... error: bpf_object__destroy_skeleton(s); return -ENOMEM; After goto error, the NULL 's' will be deferenced in bpf_object__destroy_skeleton(). We can simply fix this issue by just adding a NULL check in bpf_object__destroy_skeleton(). Fixes: d66562fba1ce ("libbpf: Add BPF object skeleton support") Signed-off-by: Yafang Shao Signed-off-by: Andrii Nakryiko Link: https://lore.kernel.org/bpf/20220108134739.32541-1-laoar.shao@gmail.com Signed-off-by: Sasha Levin --- tools/lib/bpf/libbpf.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 0ad29203cbfb..693e14799fb9 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10809,6 +10809,9 @@ void bpf_object__detach_skeleton(struct bpf_object_skeleton *s) void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s) { + if (!s) + return; + if (s->progs) bpf_object__detach_skeleton(s); if (s->obj) -- 2.34.1