LKML Archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] lib subcmd: Avoid use-after-free when no commands are excluded
@ 2023-06-26 22:22 Ian Rogers
  2023-07-01 18:37 ` Namhyung Kim
  0 siblings, 1 reply; 2+ messages in thread
From: Ian Rogers @ 2023-06-26 22:22 UTC (permalink / raw
  To: Ian Rogers, Namhyung Kim, Arnaldo Carvalho de Melo, Chenyuan Mi,
	linux-kernel

The array shortening may be an unnecessary array copy. Before commit
657a3efee43a ("lib subcmd: Avoid memory leak in exclude_cmds") this
was benign, but afterwards this could lead to a segv.

Fixes: 657a3efee43a ("lib subcmd: Avoid memory leak in exclude_cmds")
Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/lib/subcmd/help.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/tools/lib/subcmd/help.c b/tools/lib/subcmd/help.c
index 67a8d6b740ea..b59ca17e406d 100644
--- a/tools/lib/subcmd/help.c
+++ b/tools/lib/subcmd/help.c
@@ -77,10 +77,11 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
 			ei++;
 		}
 	}
-
-	while (ci < cmds->cnt) {
-		zfree(&cmds->names[cj]);
-		cmds->names[cj++] = cmds->names[ci++];
+	if (ci != cj) {
+		while (ci < cmds->cnt) {
+			zfree(&cmds->names[cj]);
+			cmds->names[cj++] = cmds->names[ci++];
+		}
 	}
 	for (ci = cj; ci < cmds->cnt; ci++)
 		zfree(&cmds->names[ci]);
-- 
2.41.0.162.gfafddb0af9-goog


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v1] lib subcmd: Avoid use-after-free when no commands are excluded
  2023-06-26 22:22 [PATCH v1] lib subcmd: Avoid use-after-free when no commands are excluded Ian Rogers
@ 2023-07-01 18:37 ` Namhyung Kim
  0 siblings, 0 replies; 2+ messages in thread
From: Namhyung Kim @ 2023-07-01 18:37 UTC (permalink / raw
  To: Ian Rogers; +Cc: Arnaldo Carvalho de Melo, Chenyuan Mi, linux-kernel

On Mon, Jun 26, 2023 at 3:22 PM Ian Rogers <irogers@google.com> wrote:
>
> The array shortening may be an unnecessary array copy. Before commit
> 657a3efee43a ("lib subcmd: Avoid memory leak in exclude_cmds") this
> was benign, but afterwards this could lead to a segv.
>
> Fixes: 657a3efee43a ("lib subcmd: Avoid memory leak in exclude_cmds")
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/lib/subcmd/help.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/tools/lib/subcmd/help.c b/tools/lib/subcmd/help.c
> index 67a8d6b740ea..b59ca17e406d 100644
> --- a/tools/lib/subcmd/help.c
> +++ b/tools/lib/subcmd/help.c
> @@ -77,10 +77,11 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
>                         ei++;
>                 }
>         }
> -
> -       while (ci < cmds->cnt) {
> -               zfree(&cmds->names[cj]);
> -               cmds->names[cj++] = cmds->names[ci++];
> +       if (ci != cj) {
> +               while (ci < cmds->cnt) {
> +                       zfree(&cmds->names[cj]);
> +                       cmds->names[cj++] = cmds->names[ci++];

I'm not sure I'm following the logic in this function well
but it seems we also need to check the same thing
in the above loop - if (cmp < 0).

Thanks,
Namhyung


> +               }
>         }
>         for (ci = cj; ci < cmds->cnt; ci++)
>                 zfree(&cmds->names[ci]);
> --
> 2.41.0.162.gfafddb0af9-goog
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-07-01 18:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-26 22:22 [PATCH v1] lib subcmd: Avoid use-after-free when no commands are excluded Ian Rogers
2023-07-01 18:37 ` Namhyung Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).