Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3384046pxb; Mon, 17 Jan 2022 19:13:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJzv3pFSB/So5gp8qpPJbedjaEa2QablFjtlbwCvCmXgSdpqamIXQwZIKG+hwgT3DezoIDrI X-Received: by 2002:a63:3445:: with SMTP id b66mr21527433pga.397.1642475607696; Mon, 17 Jan 2022 19:13:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642475607; cv=none; d=google.com; s=arc-20160816; b=RAndU2YHF3pDLrDidj66xktWljIjvjDkFBsOWgnhxRTSdnMXNyazIPBLCU41WelN2a eRbdDnkyTD9W2qsXp+37FoOF9MV3lih1QUWwBsUKoHC8kOyrRj4VCa5uICj6DBd4meU+ HN1n6cMrPrQRT61/7J+9cdkSzh1uVzAQnV9DMfrvHCnpnH0vBIVSjzqdILLGwY9CyJw3 VENqMevWD8d+NJiH2x66Q8u2KcUpthpzdXY3MZSJo/MBHnyoNQ018wsTCx539eTK/+kX 1VgdpH2wR1DNV8mihtdQj5zU4aHpKeZt5Pg13N6OWtDTl6U/faKNgL83cScV07fJJpsZ KghA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=2AsGkJvdcNlb0/8qHo3+KHLnWD8psNc4XvzrFccJ0tU=; b=Ro4LWh6zLvv0opSHe8Lf08ZIkTrnazrI5LrXzZBwWnfh1qATapSZpW6OdUd6Uy2yQM shxWp6dD/KN2/8rglvlwL0DdcbXYAPvAFcKF/DgF+9VG53wcVJSk3KOEvyE7SYeRs9D4 x38tIBBs2UN91XfNysuZmvYRnsOoxGVUg8O8O2+rc+2x2EI0latUBulL6KAQLcan2fl+ 3ANg66I4QKvxs7T/xYG9imnz/wErvCENK473LaMjZeep4wQ0dapY99KRsao43IZCzi+h 93BBPUEboxuvekmRURkRRHiVepXh1fTTK9Egzd9960d3L6VCAZAep6lUHDJTks6CLFY0 Fs5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=H33AGlSR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q76si16597515pgq.691.2022.01.17.19.13.16; Mon, 17 Jan 2022 19:13:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=H33AGlSR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244533AbiARCUK (ORCPT + 99 others); Mon, 17 Jan 2022 21:20:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244313AbiARCT6 (ORCPT ); Mon, 17 Jan 2022 21:19:58 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75F3CC061574; Mon, 17 Jan 2022 18:19:58 -0800 (PST) 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 38E75B81239; Tue, 18 Jan 2022 02:19:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0F407C36AF6; Tue, 18 Jan 2022 02:19:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1642472396; bh=/VWau8D6YDf8owSqoQpXikxvFAZkjLvobIaoJQ94Rh4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=H33AGlSRdPRF92EXdrJDhswJzZDR5iPMu++wT1uE2buIFgucwYVuCjE24J0izf4Pb OuPcRA4+Hlhk44LsxlFWo7VNePr2AaHTwNfIoK4XPd8efF7pzb0rxRgk+T6GVgfksm ahHcdZFi9L5B95YOql7xuqDyh5LxyJWGhb7EnNe1y3gHhlsnodwHdTclo2B/5d0BTZ Sjv2Ok/EdpcbNWplTQbkecRPXMEKHCdhVFMwY6mwYjd62bi2DXDhy9Rh5RxFwdzemJ Nq6ecvpDZ3pEEMiQCWx/KhdM81ljbrLereDvFRm9N19Z9acbvcg61wpvman8ye0ef8 V8Hq0rVbnr7YQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Andrii Nakryiko , Alexei Starovoitov , Yonghong Song , Sasha Levin , daniel@iogearbox.net, netdev@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH AUTOSEL 5.16 009/217] libbpf: Improve sanity checking during BTF fix up Date: Mon, 17 Jan 2022 21:16:12 -0500 Message-Id: <20220118021940.1942199-9-sashal@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220118021940.1942199-1-sashal@kernel.org> References: <20220118021940.1942199-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrii Nakryiko [ Upstream commit 88918dc12dc357a06d8d722a684617b1c87a4654 ] If BTF is corrupted DATASEC's variable type ID might be incorrect. Prevent this easy to detect situation with extra NULL check. Reported by oss-fuzz project. Signed-off-by: Andrii Nakryiko Signed-off-by: Alexei Starovoitov Acked-by: Yonghong Song Link: https://lore.kernel.org/bpf/20211103173213.1376990-3-andrii@kernel.org Signed-off-by: Sasha Levin --- tools/lib/bpf/libbpf.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index b5bf1c074832e..18651e11b9ba3 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -2752,13 +2752,12 @@ static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf, for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) { t_var = btf__type_by_id(btf, vsi->type); - var = btf_var(t_var); - - if (!btf_is_var(t_var)) { + if (!t_var || !btf_is_var(t_var)) { pr_debug("Non-VAR type seen in section %s\n", name); return -EINVAL; } + var = btf_var(t_var); if (var->linkage == BTF_VAR_STATIC) continue; -- 2.34.1