diff mbox series

BUG: segv in Lua getroot if root dev is not found

Message ID 20241002143255.230942-1-stefano.babic@swupdate.org
State Accepted
Headers show
Series BUG: segv in Lua getroot if root dev is not found | expand

Commit Message

Stefano Babic Oct. 2, 2024, 2:32 p.m. UTC
In some special cases, rootdev cannot be found and a missing check
causes a SEGV when getroot is calkled from Lua.

Signed-off-by: Stefano Babic <stefano.babic@swupdate.org>
---
 corelib/lua_interface.c | 35 +++++++++++++++++++++--------------
 include/lua_util.h      |  3 ++-
 2 files changed, 23 insertions(+), 15 deletions(-)

--
2.34.1
diff mbox series

Patch

diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index 1460b5ec..77e4aef5 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -847,21 +847,27 @@  static int l_getroot(lua_State *L) {
 	root_dev_type type = ROOT_DEV_PATH;
 	char **root = NULL;
 	char *value = rootdev;
-	char* dev_path = getroot_dev_path((char*)"", rootdev);
-
-	root = string_split(rootdev, '=');
-	if (count_string_array((const char **)root) > 1) {
-		if (!strncmp(root[0], "UUID", strlen("UUID"))) {
-			type = ROOT_DEV_UUID;
-			dev_path = getroot_dev_path((char*)"/dev/disk/by-uuid/", root[1]);
-		} else if (!strncmp(root[0], "PARTUUID", strlen("PARTUUID"))) {
-			type = ROOT_DEV_PARTUUID;
-			dev_path = getroot_dev_path((char*)"/dev/disk/by-partuuid/", root[1]);
-		} else if (!strncmp(root[0], "PARTLABEL", strlen("PARTLABEL"))) {
-			type = ROOT_DEV_PARTLABEL;
-			dev_path = getroot_dev_path((char*)"/dev/disk/by-partlabel/", root[1]);
+	char* dev_path = NULL;
+
+	if (rootdev) {
+		dev_path = getroot_dev_path((char*)"", rootdev);
+		root = string_split(rootdev, '=');
+		if (count_string_array((const char **)root) > 1) {
+			if (!strncmp(root[0], "UUID", strlen("UUID"))) {
+				type = ROOT_DEV_UUID;
+				dev_path = getroot_dev_path((char*)"/dev/disk/by-uuid/", root[1]);
+			} else if (!strncmp(root[0], "PARTUUID", strlen("PARTUUID"))) {
+				type = ROOT_DEV_PARTUUID;
+				dev_path = getroot_dev_path((char*)"/dev/disk/by-partuuid/", root[1]);
+			} else if (!strncmp(root[0], "PARTLABEL", strlen("PARTLABEL"))) {
+				type = ROOT_DEV_PARTLABEL;
+				dev_path = getroot_dev_path((char*)"/dev/disk/by-partlabel/", root[1]);
+			}
+			value = root[1];
 		}
-		value = root[1];
+	} else {
+		type = ROOT_DEV_UNKNOWN;
+		value = (char *)"";
 	}
 	lua_newtable (L);
 	if (dev_path) {
@@ -1258,6 +1264,7 @@  static int luaopen_swupdate(lua_State *L)
 	lua_push_enum(L, "UUID", ROOT_DEV_UUID);
 	lua_push_enum(L, "PARTUUID", ROOT_DEV_PARTUUID);
 	lua_push_enum(L, "PARTLABEL", ROOT_DEV_PARTLABEL);
+	lua_push_enum(L, "PARTUNKNOWN", ROOT_DEV_UNKNOWN);
 	lua_settable(L, -3);

 	if (is_type(L, LUA_TYPE_HANDLER)) {
diff --git a/include/lua_util.h b/include/lua_util.h
index 8eb5cef7..0bd904aa 100644
--- a/include/lua_util.h
+++ b/include/lua_util.h
@@ -15,7 +15,8 @@  typedef enum {
 	ROOT_DEV_PATH,
 	ROOT_DEV_UUID,
 	ROOT_DEV_PARTUUID,
-	ROOT_DEV_PARTLABEL
+	ROOT_DEV_PARTLABEL,
+	ROOT_DEV_UNKNOWN
 } root_dev_type;

 void LUAstackDump (lua_State *L);