From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BE63D12B89; Sat, 3 Feb 2024 04:14:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706933657; cv=none; b=CCHhWNaATOIs3KNLv7b2mkS7b7t9SXiOWGw22zx4trlnBY2mralJJzx14TSVTXl649UmxlhmUp5KLnTKCI5LsRIub7YFJEz81nMQR7xl8quamAUJnxZ451amtSTElYS0q1SDHPF2pKESk14GM7t2H/ghfShlk/D6y/wGPuB/AZQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706933657; c=relaxed/simple; bh=fBogkLbrBsauhpd7dZTMa/u9WMKkZT22HBNmJ9R/UUA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Y/JBZkH3NynMj5cQ9H2lF61+g5Roe140xO3RV7olfGzgVN18A2sf2dXF9CIzplq7jocYphLUXXLvlEJHX5HvQhT9D53OO1zkOgwO3w3EejNfas0Baw+I6PSBvlSf345CPTwoEtXFyHhCP57M3BiEEC3Nkhul0yD/dsWthrmbAPo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=QwJfXWrh; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="QwJfXWrh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 77982C433F1; Sat, 3 Feb 2024 04:14:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1706933657; bh=fBogkLbrBsauhpd7dZTMa/u9WMKkZT22HBNmJ9R/UUA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QwJfXWrhVo0IccvG3Dzf3z67Up7Wz2MHwOdMVwpILZWANdfWopfQocREfcDC4D1MJ zYWtN02keWU8FCgjgFvQLGR7tl786dMGYoHZ6SdABuUVFmUuzM9VeyIaowPu9dERb3 F9qEZFUms0v+1IfDRjLSnSuGsnIGCeTUCBgR3FtM= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Ian Rogers , Adrian Hunter , Alexander Shishkin , Chenyuan Mi , Ingo Molnar , Jiri Olsa , Mark Rutland , Namhyung Kim , Peter Zijlstra , Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH 6.6 245/322] libsubcmd: Fix memory leak in uniq() Date: Fri, 2 Feb 2024 20:05:42 -0800 Message-ID: <20240203035407.094247154@linuxfoundation.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240203035359.041730947@linuxfoundation.org> References: <20240203035359.041730947@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Ian Rogers [ Upstream commit ad30469a841b50dbb541df4d6971d891f703c297 ] uniq() will write one command name over another causing the overwritten string to be leaked. Fix by doing a pass that removes duplicates and a second that removes the holes. Signed-off-by: Ian Rogers Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Chenyuan Mi Cc: Ingo Molnar Cc: Jiri Olsa Cc: Mark Rutland Cc: Namhyung Kim Cc: Peter Zijlstra Link: https://lore.kernel.org/r/20231208000515.1693746-1-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/lib/subcmd/help.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tools/lib/subcmd/help.c b/tools/lib/subcmd/help.c index adfbae27dc36..8561b0f01a24 100644 --- a/tools/lib/subcmd/help.c +++ b/tools/lib/subcmd/help.c @@ -52,11 +52,21 @@ void uniq(struct cmdnames *cmds) if (!cmds->cnt) return; - for (i = j = 1; i < cmds->cnt; i++) - if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name)) - cmds->names[j++] = cmds->names[i]; - + for (i = 1; i < cmds->cnt; i++) { + if (!strcmp(cmds->names[i]->name, cmds->names[i-1]->name)) + zfree(&cmds->names[i - 1]); + } + for (i = 0, j = 0; i < cmds->cnt; i++) { + if (cmds->names[i]) { + if (i == j) + j++; + else + cmds->names[j++] = cmds->names[i]; + } + } cmds->cnt = j; + while (j < i) + cmds->names[j++] = NULL; } void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes) -- 2.43.0