Skip to contentSkip to conversation

kconfig: fix segmentation fault in menuconfig search

PATCH In review Masahiro Yamada 3 participants 6 messages Oct 23, 2022 – Nov 2, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kconfig: fix segmentation fault in menuconfig search

Since commit d05377e184fc ("kconfig: Create links to main menu items in search"), menuconfig shows a jump key next to "Main menu" if the nearest visible parent is the rootmenu. If you press that jump key, menuconfig crashes with a segmentation fault.

For example, do this:

  $ make ARCH=arm64 allnoconfig menuconfig

Press '/' to search for the string "ACPI". Press '1' to choose "(1) Main menu". Then, menuconfig crashed with a segmentation fault.

The following code in search_conf()

    conf(targets[i]->parent, targets[i]);

results in NULL pointer dereference because targets[i] is the rootmenu, which does not have a parent.

Commit d05377e184fc tried to fix the issue of top-level items not having a jump key, but adding the "Main menu" was not the right fix.

The correct fix is to show the searched item itself. This fixes another weird behavior described in the comment block.

Fixes d05377e184fc ("kconfig: Create links to main menu items in search")

---

 scripts/kconfig/menu.c | 23 ++++-------------------
 1 file changed, 4 insertions(+), 19 deletions(-)
scripts/kconfig/menu.c +4 -19
index 62b6313f51c8..109325f31bef 100644
@@ -722,8 +722,8 @@ static void get_prompt_str(struct gstr *r, struct property *prop,
 	if (!expr_eq(prop->menu->dep, prop->visible.expr))
 		get_dep_str(r, prop->visible.expr, "  Visible if: ");
 
-	menu = prop->menu->parent;
-	for (i = 0; menu && i < 8; menu = menu->parent) {
+	menu = prop->menu;
+	for (i = 0; menu != &rootmenu && i < 8; menu = menu->parent) {
 		bool accessible = menu_is_visible(menu);
 
 		submenu[i++] = menu;
@@ -733,16 +733,7 @@ static void get_prompt_str(struct gstr *r, struct property *prop,
 	if (head && location) {
 		jump = xmalloc(sizeof(struct jump_key));
 
-		if (menu_is_visible(prop->menu)) {
-			/*
-			 * There is not enough room to put the hint at the
-			 * beginning of the "Prompt" line. Put the hint on the
-			 * last "Location" line even when it would belong on
-			 * the former.
-			 */
-			jump->target = prop->menu;
-		} else
-			jump->target = location;
+		jump->target = location;
 
 		if (list_empty(head))
 			jump->index = 0;
@@ -758,13 +749,7 @@ static void get_prompt_str(struct gstr *r, struct property *prop,
 		menu = submenu[i];
 		if (jump && menu == location)
 			jump->offset = strlen(r->s);
-
-		if (menu == &rootmenu)
-			/* The real rootmenu prompt is ugly */
-			str_printf(r, "%*cMain menu", j, ' ');
-		else
-			str_printf(r, "%*c-> %s", j, ' ', menu_get_prompt(menu));
-
+		str_printf(r, "%*c-> %s", j, ' ', menu_get_prompt(menu));
 		if (menu->sym) {
 			str_printf(r, " (%s [=%s])", menu->sym->name ?
 				menu->sym->name : "<choice>",
Signature · 1 line
2.34.1
Tested-by on v1 1/1
#1 lore raw
Quoted 11 lines · Masahiro Yamada

Since commit d05377e184fc ("kconfig: Create links to main menu items in search"), menuconfig shows a jump key next to "Main menu" if the nearest visible parent is the rootmenu. If you press that jump key, menuconfig crashes with a segmentation fault.

For example, do this:

  $ make ARCH=arm64 allnoconfig menuconfig

Press '/' to search for the string "ACPI". Press '1' to choose "(1) Main menu". Then, menuconfig crashed with a segmentation fault.

You missed the prerequisites: search EFI and press 1 to jump to CONFIG_EFI.

Quoted 11 lines

The following code in search_conf()

    conf(targets[i]->parent, targets[i]);

results in NULL pointer dereference because targets[i] is the rootmenu, which does not have a parent.

Commit d05377e184fc tried to fix the issue of top-level items not having a jump key, but adding the "Main menu" was not the right fix.

Quoted 7 lines

The correct fix is to show the searched item itself. This fixes another weird behavior described in the comment block.

Fixes d05377e184fc ("kconfig: Create links to main menu items in search")

Missing Cc: stable? The segfault (IMO) appears after v5.15, so all supported stable branches are affected.

Anyway, the segfault gone away with this patch applied. Thanks.

Signature · 1 line
An old man doll... just what I always wanted! - Clara
#2 lore raw
Quoted 9 lines · Bagas Sanjaya
Quoted 7 lines · Masahiro Yamada

Since commit d05377e184fc ("kconfig: Create links to main menu items in search"), menuconfig shows a jump key next to "Main menu" if the nearest visible parent is the rootmenu. If you press that jump key, menuconfig crashes with a segmentation fault.

For example, do this:

Quoted 7 lines
Quoted 4 lines
  $ make ARCH=arm64 allnoconfig menuconfig

Press '/' to search for the string "ACPI". Press '1' to choose "(1) Main menu". Then, menuconfig crashed with a segmentation fault.

You missed the prerequisites: search EFI and press 1 to jump to CONFIG_EFI.

Try the command in my commit description.

"allnoconfig" disables EFI.

Quoted 14 lines
Quoted 14 lines

The following code in search_conf()

    conf(targets[i]->parent, targets[i]);

results in NULL pointer dereference because targets[i] is the rootmenu, which does not have a parent.

Commit d05377e184fc tried to fix the issue of top-level items not having a jump key, but adding the "Main menu" was not the right fix.

The correct fix is to show the searched item itself. This fixes another weird behavior described in the comment block.

Quoted 7 lines
Quoted 4 lines
Fixes d05377e184fc ("kconfig: Create links to main menu items in search")

Missing Cc: stable? The segfault (IMO) appears after v5.15, so all supported stable branches are affected.

In my understanding, Fixes: is enough to automatically find which stable kernels to which the patch should be back-ported.

Quoted 7 lines

Anyway, the segfault gone away with this patch applied. Thanks.

--
An old man doll... just what I always wanted! - Clara

Signature · 2 lines
Best Regards
Masahiro Yamada
#3 lore raw
Quoted 9 lines · Masahiro Yamada
Quoted 3 lines

You missed the prerequisites: search EFI and press 1 to jump to CONFIG_EFI.

Try the command in my commit description.

"allnoconfig" disables EFI.

I can also reproduce the segfault by allyesconfig then disable CONFIG_EFI.

Signature · 1 line
An old man doll... just what I always wanted! - Clara
Tested-by on v1 1/1
#4 lore raw
In reply to Masahiro Yamada

Hi Masahiro,

Quoted 24 lines · Masahiro Yamada

Since commit d05377e184fc ("kconfig: Create links to main menu items in search"), menuconfig shows a jump key next to "Main menu" if the nearest visible parent is the rootmenu. If you press that jump key, menuconfig crashes with a segmentation fault.

For example, do this:

  $ make ARCH=arm64 allnoconfig menuconfig

Press '/' to search for the string "ACPI". Press '1' to choose "(1) Main menu". Then, menuconfig crashed with a segmentation fault.

The following code in search_conf()

    conf(targets[i]->parent, targets[i]);

results in NULL pointer dereference because targets[i] is the rootmenu,
which does not have a parent.

Commit d05377e184fc tried to fix the issue of top-level items not having
a jump key, but adding the "Main menu" was not the right fix.

Quoted 8 lines

The correct fix is to show the searched item itself. This fixes another
weird behavior described in the comment block.

Fixes d05377e184fc ("kconfig: Create links to main menu items in

search")

Sorry for the delay, I eventually found time to test your patch, and it fixes the issue I reported. Thank you for helping out, feel free to add my

Quoted 63 lines

---

 scripts/kconfig/menu.c | 23 ++++-------------------

 1 file changed, 4 insertions(+), 19 deletions(-)
scripts/kconfig/menu.c +0 -0
index 62b6313f51c8..109325f31bef 100644
@@ -722,8 +722,8 @@ static void get_prompt_str(struct gstr *r, struct
 property *prop,
         if (!expr_eq(prop->menu->dep, prop->visible.expr))
                 get_dep_str(r, prop->visible.expr, "  Visible if: ");


-       menu = prop->menu->parent;
-       for (i = 0; menu && i < 8; menu = menu->parent) { +       menu = prop->menu;
+       for (i = 0; menu != &rootmenu && i < 8; menu = menu->parent) {
                bool accessible = menu_is_visible(menu);

                submenu[i++] = menu;

scripts/kconfig/menu.c +0 -0
@@ -733,16 +733,7 @@ static void get_prompt_str(struct gstr *r,
 struct property *prop,
         if (head && location) {
                 jump = xmalloc(sizeof(struct jump_key));


-               if (menu_is_visible(prop->menu)) {
-                       /*
-                        * There is not enough room to put the hint at the
-                        * beginning of the "Prompt" line. Put the hint on the
-                        * last "Location" line even when it would belong on
-                        * the former.
-                        */
-                       jump->target = prop->menu;
-               } else
-                       jump->target = location;
+               jump->target = location;

                if (list_empty(head))
                        jump->index = 0;

scripts/kconfig/menu.c +1 -7
@@ -758,13 +749,7 @@ static void get_prompt_str(struct gstr *r,
 struct property *prop,
                 menu = submenu[i];
                 if (jump && menu == location)
                         jump->offset = strlen(r->s);
-
-               if (menu == &rootmenu)
-                       /* The real rootmenu prompt is ugly */
-                       str_printf(r, "%*cMain menu", j, ' ');
-               else
-                       str_printf(r, "%*c-> %s", j, ' ',
 menu_get_prompt(menu));
-
+               str_printf(r, "%*c-> %s", j, ' ',
 menu_get_prompt(menu));

                if (menu->sym) {
                        str_printf(r, " (%s [=%s])", menu->sym->name ?
                                menu->sym->name : "<choice>",

Signature · 4 lines
Pengutronix e.K.                | Johannes Zink                  |
Steuerwalder Str. 21            | https://www.pengutronix.de/    |
31137 Hildesheim, Germany       | Phone: +49-5121-206917-0       |
Amtsgericht Hildesheim, HRA 2686| Fax:   +49-5121-206917-5555    |
#5 lore raw
Quoted 37 lines · Johannes Zink

Hi Masahiro,

Quoted 24 lines · Masahiro Yamada

Since commit d05377e184fc ("kconfig: Create links to main menu items in search"), menuconfig shows a jump key next to "Main menu" if the nearest visible parent is the rootmenu. If you press that jump key, menuconfig crashes with a segmentation fault.

For example, do this:

  $ make ARCH=arm64 allnoconfig menuconfig

Press '/' to search for the string "ACPI". Press '1' to choose "(1) Main menu". Then, menuconfig crashed with a segmentation fault.

The following code in search_conf()

    conf(targets[i]->parent, targets[i]);

results in NULL pointer dereference because targets[i] is the rootmenu,
which does not have a parent.

Commit d05377e184fc tried to fix the issue of top-level items not having
a jump key, but adding the "Main menu" was not the right fix.

Quoted 8 lines

The correct fix is to show the searched item itself. This fixes another
weird behavior described in the comment block.

Fixes d05377e184fc ("kconfig: Create links to main menu items in

search")

Quoted 5 lines

Sorry for the delay, I eventually found time to test your patch, and it fixes the issue I reported. Thank you for helping out, feel free to add my

Applied to linux-kbuild/fixes.

Quoted 70 lines
Quoted 59 lines

---

 scripts/kconfig/menu.c | 23 ++++-------------------
 1 file changed, 4 insertions(+), 19 deletions(-)
scripts/kconfig/menu.c +2 -2
index 62b6313f51c8..109325f31bef 100644
@@ -722,8 +722,8 @@ static void get_prompt_str(struct gstr *r, struct
 property *prop,
        if (!expr_eq(prop->menu->dep, prop->visible.expr))
                get_dep_str(r, prop->visible.expr, "  Visible if: ");
 
-       menu = prop->menu->parent;
-       for (i = 0; menu && i < 8; menu = menu->parent) {
+       menu = prop->menu;
+       for (i = 0; menu != &rootmenu && i < 8; menu = menu->parent)
 {
                bool accessible = menu_is_visible(menu);
                submenu[i++] = menu;
scripts/kconfig/menu.c +0 -3
@@ -733,16 +733,7 @@ static void get_prompt_str(struct gstr *r,
 struct property *prop,
        if (head && location) {
                jump = xmalloc(sizeof(struct jump_key));
 
-               if (menu_is_visible(prop->menu)) {
-                       /*
-                        * There is not enough room to put the hint

at the
- * beginning of the "Prompt" line. Put the
hint on the
- * last "Location" line even when it would
belong on

scripts/kconfig/menu.c +2 -12
-                        * the former.
-                        */
-                       jump->target = prop->menu;
-               } else
-                       jump->target = location;
+               jump->target = location;
 
                if (list_empty(head))
                        jump->index = 0;
@@ -758,13 +749,7 @@ static void get_prompt_str(struct gstr *r,
 struct property *prop,
                menu = submenu[i];
                if (jump && menu == location)
                        jump->offset = strlen(r->s);
-
-               if (menu == &rootmenu)
-                       /* The real rootmenu prompt is ugly */
-                       str_printf(r, "%*cMain menu", j, ' ');
-               else
-                       str_printf(r, "%*c-> %s", j, ' ',
 menu_get_prompt(menu));
-
+               str_printf(r, "%*c-> %s", j, ' ',
 menu_get_prompt(menu));
Quoted 4 lines
                if (menu->sym) {
                        str_printf(r, " (%s [=%s])", menu->sym->name
?
                                menu->sym->name : "<choice>",

--

Pengutronix e.K.                | Johannes Zink                  |
Steuerwalder Str. 21            | https://www.pengutronix.de/    |
31137 Hildesheim, Germany       | Phone: +49-5121-206917-0       |
Amtsgericht Hildesheim, HRA 2686| Fax:   +49-5121-206917-5555    |
Signature · 2 lines
Best Regards
Masahiro Yamada