Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp4429961pxb; Mon, 21 Feb 2022 21:22:08 -0800 (PST) X-Google-Smtp-Source: ABdhPJwJ4LViv9s+7/Ycm2l9fEUdJq6S0Hz7KyxtujA5aYvPABE0bTSlQjasBIn8T9YJ1wJj0CyI X-Received: by 2002:a05:6a00:b84:b0:4e1:b113:d444 with SMTP id g4-20020a056a000b8400b004e1b113d444mr23125746pfj.12.1645507328348; Mon, 21 Feb 2022 21:22:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645507328; cv=none; d=google.com; s=arc-20160816; b=HxVDokr9C4UGHcq6zMDi+QmG3TmqFsXVtgIWwR3fXbmgX5oWQeeMy91PV49E5kHxx0 UP8WqmVknHxlcpZZsnYxvaM9MQH2lL7CRjDkX22C5cKn2MXSUy8FXNSsDSFokd8Nv6cG DNzCjtqp28pDxvr2oUqifVSX75ARZOH2zREchgp7+3eheaZMwPpAchystLNu/N/Rycvm bJqPu826wMUzgVttpt6GUIg/QLl1yZwb14Y1itxNQUQDqVvt4Avd9T7VA5fIhVDyzJs5 PfXfTRCaPPTPHP51PZNkBu37PtKP9WonUdCEt3kMR0r3wVHQw9LJcvL5C8CgkhYIz9VO AGKg== 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=OcnfYVrF+dcSAMJO/QWvQZDPQu4uBw/yNsmXWVkUqyU=; b=hieRVaBhdT09w/NGOBnRY+LmeieKjb4E4podYISWmM5RqpCwKVophz4SSW1cG8Hoqv lHd4ryCBuaN7yo73izeTPWvFwddDjtz/zoxbXGkq/hy9dKhq6ZkG76BKYztrr6zx5Brq tvGXtaQFk9M91UrKNeVFYW3imeZtxccEKjUQTe/RlitxmPi+TRWWw5HUU07sdV8NRA+8 rrZTQ9PrRKrCMj0QybxZLzsCUHmWzRgieNZ7IYbDoT+9gTBhTUzsrPvfU/oN2BxeOLfx ICQD8sOSqVO0cWEaREevPUvHeVhN/+jNaAjrt4/QOu7aTShqbqogL0J1jn5tFgwNRHC0 c6zg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Zrndo4lu; 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 q2si177458plc.155.2022.02.21.21.22.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Feb 2022 21:22:08 -0800 (PST) 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=Zrndo4lu; 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 43C85F7478; Mon, 21 Feb 2022 20:53:12 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347934AbiBUJKa (ORCPT + 99 others); Mon, 21 Feb 2022 04:10:30 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:43232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347658AbiBUJF7 (ORCPT ); Mon, 21 Feb 2022 04:05:59 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CDA0625EAA; Mon, 21 Feb 2022 00:59:27 -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 dfw.source.kernel.org (Postfix) with ESMTPS id AE5D661204; Mon, 21 Feb 2022 08:59:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 96871C340E9; Mon, 21 Feb 2022 08:59:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1645433967; bh=8IUsyVO3CUV/NHmrn9iyf0MiQePqG+1lN0ZMxV/tScY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Zrndo4lug0l8Nu2hxfDA/TRbptrt2HAopmliJZKqx6ZnN6+6vZUqkHlwoTZqBje8L yS5prmK2qmkmS9NATrRNaPCFaf5rF8sVFseTYqEwHSWdvYoC9XrzaxGU8aOR3/f+Q4 WR28Ep3HHIt/7ZKnGUNrq6WM2ersnjfq0HLmCIsM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, =?UTF-8?q?Valdis=20Kl=C4=93tnieks?= , Kees Kook , "Justin M. Forbes" , Josh Poimboeuf , linux-hardening@vger.kernel.org, Arnaldo Carvalho de Melo Subject: [PATCH 5.4 45/80] libsubcmd: Fix use-after-free for realloc(..., 0) Date: Mon, 21 Feb 2022 09:49:25 +0100 Message-Id: <20220221084917.048654891@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220221084915.554151737@linuxfoundation.org> References: <20220221084915.554151737@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: Kees Cook commit 52a9dab6d892763b2a8334a568bd4e2c1a6fde66 upstream. GCC 12 correctly reports a potential use-after-free condition in the xrealloc helper. Fix the warning by avoiding an implicit "free(ptr)" when size == 0: In file included from help.c:12: In function 'xrealloc', inlined from 'add_cmdname' at help.c:24:2: subcmd-util.h:56:23: error: pointer may be used after 'realloc' [-Werror=use-after-free] 56 | ret = realloc(ptr, size); | ^~~~~~~~~~~~~~~~~~ subcmd-util.h:52:21: note: call to 'realloc' here 52 | void *ret = realloc(ptr, size); | ^~~~~~~~~~~~~~~~~~ subcmd-util.h:58:31: error: pointer may be used after 'realloc' [-Werror=use-after-free] 58 | ret = realloc(ptr, 1); | ^~~~~~~~~~~~~~~ subcmd-util.h:52:21: note: call to 'realloc' here 52 | void *ret = realloc(ptr, size); | ^~~~~~~~~~~~~~~~~~ Fixes: 2f4ce5ec1d447beb ("perf tools: Finalize subcmd independence") Reported-by: Valdis Klētnieks Signed-off-by: Kees Kook Tested-by: Valdis Klētnieks Tested-by: Justin M. Forbes Acked-by: Josh Poimboeuf Cc: linux-hardening@vger.kernel.org Cc: Valdis Klētnieks Link: http://lore.kernel.org/lkml/20220213182443.4037039-1-keescook@chromium.org Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Greg Kroah-Hartman --- tools/lib/subcmd/subcmd-util.h | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) --- a/tools/lib/subcmd/subcmd-util.h +++ b/tools/lib/subcmd/subcmd-util.h @@ -50,15 +50,8 @@ static NORETURN inline void die(const ch static inline void *xrealloc(void *ptr, size_t size) { void *ret = realloc(ptr, size); - if (!ret && !size) - ret = realloc(ptr, 1); - if (!ret) { - ret = realloc(ptr, size); - if (!ret && !size) - ret = realloc(ptr, 1); - if (!ret) - die("Out of memory, realloc failed"); - } + if (!ret) + die("Out of memory, realloc failed"); return ret; }