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 B545E69951; Wed, 21 Feb 2024 13:39:51 +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=1708522791; cv=none; b=Yee0hWVYoiE8igI75AFEroMQQJMjvoH/nRtPv005RVoEszZoggkmqh/KXTvfdCAdCPZ7W6J9DJBQ1cJ9Ly2mHjo16T8IhkvGhCUQXeIxEZrR9o/thjfOFgpU3LozkvUXg9LBDlXJUEtysvkCjzLkyP67BM9jayzQT5EZpUxPTOs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708522791; c=relaxed/simple; bh=Z8PU23GgJ7aW5N09zd3dwbh6tKp2X9+eaAk8er3ntj8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HkW8BkzwS7L3fPuhJHfeaCY9jEe2CBoCSobO4OL49faUI+JQOvAHYPo6wyE0fkCgio7ir/Xjp5aptlzjHGhnvGu65y7UnaW+S/BOK5b2m/s7+PXfQZQeXI2YwD4u0QPjO0dRkDwkY35Sw0rOTOej4z81wLwmmj1n4S87vBh0hKU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=Fs67Knxi; 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="Fs67Knxi" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E5A68C43390; Wed, 21 Feb 2024 13:39:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1708522791; bh=Z8PU23GgJ7aW5N09zd3dwbh6tKp2X9+eaAk8er3ntj8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Fs67Knxi+1QEulf7wZuNtX8odtBZOPeWdPcf2zS5FlyyofWAxzROPOyI9bZUQeqFo vBnxXFxLIKw/QyIXWWfmfuBCGOh3UxU5FGKq88JonJWt/xpOcrFvP81Wzi4Z+3ih1d PzWNHoBHe7S4/X08XDfYbvGOpYxGdD8FLpGRDBNg= 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 5.15 259/476] libsubcmd: Fix memory leak in uniq() Date: Wed, 21 Feb 2024 14:05:10 +0100 Message-ID: <20240221130017.421715500@linuxfoundation.org> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240221130007.738356493@linuxfoundation.org> References: <20240221130007.738356493@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.15-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 bf02d62a3b2b..42f57b640f11 100644 --- a/tools/lib/subcmd/help.c +++ b/tools/lib/subcmd/help.c @@ -50,11 +50,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