Skip to contentSkip to conversation

scripts: kconfig: Fix potential NULL pointer dereference in

PATCH In review Anton Moryakov 2 participants 2 messages Feb 12, 2025 – Feb 13, 2025

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

PATCH 1/1 scripts: kconfig: Fix potential NULL pointer dereference in

The function prop_get_symbol may return NULL, which was not checked before dereferencing the pointer in menu_finalize. This could lead to undefined behavior or crashes.

This commit adds a NULL check before accessing es->rev_dep.expr and es->implied.expr. If es is NULL, a warning is logged, and the operation is skipped.

Triggers found by static analyzer Svace.

---
 scripts/kconfig/menu.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
scripts/kconfig/menu.c +10 -4
index 0fe7f3255a..3fb3ab4637 100644
@@ -400,12 +400,18 @@ void menu_finalize(struct menu *parent)
 				 */
 				if (prop->type == P_SELECT) {
 					struct symbol *es = prop_get_symbol(prop);
-					es->rev_dep.expr = expr_alloc_or(es->rev_dep.expr,
-							expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
+					if (es) {
+						es->rev_dep.expr = expr_alloc_or(es->rev_dep.expr,
+								expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
+					} else
+						menu_warn(menu, "select property has no symbol");
 				} else if (prop->type == P_IMPLY) {
 					struct symbol *es = prop_get_symbol(prop);
-					es->implied.expr = expr_alloc_or(es->implied.expr,
-							expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
+					if (es) {
+						es->implied.expr = expr_alloc_or(es->implied.expr,
+								expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
+					} else
+						menu_warn(menu, "imply property has no symbol");
 				}
 			}
 		}
Signature · 1 line
2.30.2
#1 lore raw
Quoted 4 lines · Anton Moryakov

The function prop_get_symbol may return NULL, which was not checked before dereferencing the pointer in menu_finalize. This could lead to undefined behavior or crashes.

Please attach a simple test case that leads to a crash.

Quoted 6 lines

This commit adds a NULL check before accessing es->rev_dep.expr and es->implied.expr. If es is NULL, a warning is logged, and the operation is skipped.

Triggers found by static analyzer Svace.

In my understanding, P_SELECT always has a non-NULL ->expr if you look at the code in scripts/kconfig/parser.y

  menu_add_symbol(P_SELECT, $2, $3);

$2 is the selected symbol, and passed to expr_alloc_symbol()

Quoted 37 lines
---
 scripts/kconfig/menu.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
scripts/kconfig/menu.c +10 -5
index 0fe7f3255a..3fb3ab4637 100644
@@ -400,12 +400,18 @@ void menu_finalize(struct menu *parent)
                                 */
                                if (prop->type == P_SELECT) {
                                        struct symbol *es = prop_get_symbol(prop);
-                                       es->rev_dep.expr = expr_alloc_or(es->rev_dep.expr,
-                                                       expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
+                                       if (es) {
+                                               es->rev_dep.expr = expr_alloc_or(es->rev_dep.expr,
+                                                               expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
+                                       } else
+                                               menu_warn(menu, "select property has no symbol");
                                } else if (prop->type == P_IMPLY) {
                                        struct symbol *es = prop_get_symbol(prop);
-                                       es->implied.expr = expr_alloc_or(es->implied.expr,
-                                                       expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
+                                       if (es) {
+                                               es->implied.expr = expr_alloc_or(es->implied.expr,
+                                                               expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
+                                       } else
+                                               menu_warn(menu, "imply property has no symbol");
                                }
                        }
                }
--

2.30.2

Signature · 2 lines
Best Regards
Masahiro Yamada