Skip to contentSkip to conversation

lib subcmd: Avoid segv/use-after-free when commands aren't excluded

PATCH Applied Ian Rogers 3 participants 3 messages Jul 7, 2023 – Jul 11, 2023

lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH v2 1/1 lib subcmd: Avoid segv/use-after-free when commands aren't excluded

The array shortening may perform unnecessary array copies. 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")
---
 tools/lib/subcmd/help.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
tools/lib/subcmd/help.c +12 -6
index 67a8d6b740ea..adfbae27dc36 100644
@@ -68,8 +68,13 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
 	while (ci < cmds->cnt && ei < excludes->cnt) {
 		cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
 		if (cmp < 0) {
-			zfree(&cmds->names[cj]);
-			cmds->names[cj++] = cmds->names[ci++];
+			if (ci == cj) {
+				ci++;
+				cj++;
+			} else {
+				zfree(&cmds->names[cj]);
+				cmds->names[cj++] = cmds->names[ci++];
+			}
 		} else if (cmp == 0) {
 			ci++;
 			ei++;
@@ -77,10 +82,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]);
Signature · 1 line
2.41.0.390.g38632f3daf-goog
Acked-by on v2 1/1
#1 lore raw
Quoted 7 lines · Ian Rogers

The array shortening may perform unnecessary array copies. 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")

Thanks,
Namhyung

Quoted 43 lines
---
 tools/lib/subcmd/help.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
tools/lib/subcmd/help.c +12 -7
index 67a8d6b740ea..adfbae27dc36 100644
@@ -68,8 +68,13 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
        while (ci < cmds->cnt && ei < excludes->cnt) {
                cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
                if (cmp < 0) {
-                       zfree(&cmds->names[cj]);
-                       cmds->names[cj++] = cmds->names[ci++];
+                       if (ci == cj) {
+                               ci++;
+                               cj++;
+                       } else {
+                               zfree(&cmds->names[cj]);
+                               cmds->names[cj++] = cmds->names[ci++];
+                       }
                } else if (cmp == 0) {
                        ci++;
                        ei++;
@@ -77,10 +82,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.390.g38632f3daf-goog

#2 lore raw

Em Tue, Jul 11, 2023 at 10:36:59AM -0700, Namhyung Kim escreveu:

Quoted 10 lines · Ian Rogers
Quoted 7 lines

The array shortening may perform unnecessary array copies. 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")

Thanks, applied to perf-tools.

- Arnaldo

Quoted 47 lines

Thanks,
Namhyung

Quoted 43 lines
---
 tools/lib/subcmd/help.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
tools/lib/subcmd/help.c +12 -7
index 67a8d6b740ea..adfbae27dc36 100644
@@ -68,8 +68,13 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
        while (ci < cmds->cnt && ei < excludes->cnt) {
                cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
                if (cmp < 0) {
-                       zfree(&cmds->names[cj]);
-                       cmds->names[cj++] = cmds->names[ci++];
+                       if (ci == cj) {
+                               ci++;
+                               cj++;
+                       } else {
+                               zfree(&cmds->names[cj]);
+                               cmds->names[cj++] = cmds->names[ci++];
+                       }
                } else if (cmp == 0) {
                        ci++;
                        ei++;
@@ -77,10 +82,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.390.g38632f3daf-goog

Signature · 1 line
- Arnaldo