Skip to contentSkip to conversation

samples/landlock: Fix possible NULL dereference in parse_path()

PATCH In review Gax-c 2 participants 2 messages Nov 28, 2024 – Jan 10, 2025

Subsystems (3)
lore
Share & tools
lore

Gax-c

author
#0 lore raw

PATCH v2 1/1 samples/landlock: Fix possible NULL dereference in parse_path()

From: Zichen Xie <[email protected]>

malloc() may return NULL, leading to NULL dereference. Add a NULL check.

Fixes ba84b0bf5a16 ("samples/landlock: Add a sandbox manager example")

---
v2: Modify logic & Add Fixes tag.

---
 samples/landlock/sandboxer.c | 8 ++++++++
 1 file changed, 8 insertions(+)
samples/landlock/sandboxer.c +8 -0
index 57565dfd74a2..ef2a34173d84 100644
@@ -91,6 +91,9 @@ static int parse_path(char *env_path, const char ***const path_list)
 		}
 	}
 	*path_list = malloc(num_paths * sizeof(**path_list));
+	if (*path_list == NULL)
+		return -1;
+
 	for (i = 0; i < num_paths; i++)
 		(*path_list)[i] = strsep(&env_path, ENV_DELIMITER);
 
@@ -127,6 +130,11 @@ static int populate_ruleset_fs(const char *const env_var, const int ruleset_fd,
 	env_path_name = strdup(env_path_name);
 	unsetenv(env_var);
 	num_paths = parse_path(env_path_name, &path_list);
+	if (num_paths == -1) {
+		fprintf(stderr, "Failed to allocate memory\n");
+		ret = 1;
+		goto out_free_name;
+	}
 	if (num_paths == 1 && path_list[0][0] == '\0') {
 		/*
 		 * Allows to not use all possible restrictions (e.g. use
Signature · 1 line
2.34.1
maintainer
#1 lore raw

Thanks! I've simplified a bit your patch and pushed it to my next tree.

Quoted 44 lines · Gax-c

From: Zichen Xie <[email protected]>

malloc() may return NULL, leading to NULL dereference. Add a NULL check.

Fixes ba84b0bf5a16 ("samples/landlock: Add a sandbox manager example")

---
v2: Modify logic & Add Fixes tag.

---
 samples/landlock/sandboxer.c | 8 ++++++++
 1 file changed, 8 insertions(+)
samples/landlock/sandboxer.c +8 -0
index 57565dfd74a2..ef2a34173d84 100644
@@ -91,6 +91,9 @@ static int parse_path(char *env_path, const char ***const path_list)
 		}
 	}
 	*path_list = malloc(num_paths * sizeof(**path_list));
+	if (*path_list == NULL)
+		return -1;
+
 	for (i = 0; i < num_paths; i++)
 		(*path_list)[i] = strsep(&env_path, ENV_DELIMITER);
 
@@ -127,6 +130,11 @@ static int populate_ruleset_fs(const char *const env_var, const int ruleset_fd,
 	env_path_name = strdup(env_path_name);
 	unsetenv(env_var);
 	num_paths = parse_path(env_path_name, &path_list);
+	if (num_paths == -1) {
+		fprintf(stderr, "Failed to allocate memory\n");
+		ret = 1;
+		goto out_free_name;
+	}
 	if (num_paths == 1 && path_list[0][0] == '\0') {
 		/*
 		 * Allows to not use all possible restrictions (e.g. use
Signature · 1 line
2.34.1