Skip to contentSkip to conversation

sandbox/seunshare: fix killall() realloc and missing type comparison

PATCH In review Stephen Smalley 1 participant 2 messages May 14 – May 18 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH v2 1/1 sandbox/seunshare: fix killall() realloc and missing type comparison

The killall() realloc() can produce an integer overflow. Check and handle this correctly.

The killall() logic also only compares the MCS category set when deciding whether to kill the process. We should at least also compare the type to avoid incorrectly killing an unrelated process with the same category set (e.g. if multiple applications are independently assigning category sets on the same system). Check the type as well.

killall() still seems error prone and I couldn't find any actual users of the -k/--kill option for seunshare. Can we just drop this?

---
v2 fixes the signedness of "i" to match its usage.

 sandbox/seunshare.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
sandbox/seunshare.c +19 -7
index 2eef0e2f..e20c0f87 100644
@@ -680,8 +680,8 @@ killall (const char *execcon)
 	char *scon;
 	struct dirent *de;
 	pid_t *pid_table, pid, self;
-	int i;
-	int pids, max_pids;
+	unsigned int i;
+	unsigned int pids, max_pids;
 	int running = 0;
 	self = getpid();
 	if (!(dir = opendir(PROC_BASE))) {
@@ -701,26 +701,34 @@ killall (const char *execcon)
 		return -1;
 	}
 	const char *const mcs = context_range_get(con);
-	if (!mcs) {
+	const char *const type = context_type_get(con);
+	if (!mcs || !type) {
 		context_free(con);
 		free(pid_table);
 		(void)closedir(dir);
 		return -1;
 	}
-	printf("mcs=%s\n", mcs);
+	if (verbose)
+		printf("mcs=%s type=%s\n", mcs, type);
 	while ((de = readdir (dir)) != NULL) {
 		if (!(pid = (pid_t)atoi(de->d_name)) || pid == self)
 			continue;
 
 		if (pids == max_pids) {
-			pid_t *new_pid_table = realloc(pid_table, 2*pids*sizeof(pid_t));
+			max_pids *= 2;
+			if (max_pids <= pids)
+			{
+				free(pid_table);
+				(void)closedir(dir);
+				return -1;
+			}
+			pid_t *new_pid_table = reallocarray(pid_table, max_pids, sizeof(pid_t));
 			if (!new_pid_table) {
 				free(pid_table);
 				(void)closedir(dir);
 				return -1;
 			}
 			pid_table = new_pid_table;
-			max_pids *= 2;
 		}
 		pid_table[pids++] = pid;
 	}
@@ -734,8 +742,12 @@ killall (const char *execcon)
 
 			context_t pidcon = context_new(scon);
 			if (pidcon) {
+				const char *const pmcs = context_range_get(pidcon);
+				const char *const ptype = context_type_get(pidcon);
+
 				/* Attempt to kill remaining processes */
-				if (strcmp(context_range_get(pidcon), mcs) == 0)
+				if (pmcs && ptype && !strcmp(pmcs, mcs) &&
+					!strcmp(ptype, type))
 					kill(id, SIGKILL);
 
 				context_free(pidcon);
Signature · 1 line
2.54.0
author maintainer
#1 lore raw
Quoted 13 lines · Stephen Smalley

The killall() realloc() can produce an integer overflow. Check and handle this correctly.

The killall() logic also only compares the MCS category set when deciding whether to kill the process. We should at least also compare the type to avoid incorrectly killing an unrelated process with the same category set (e.g. if multiple applications are independently assigning category sets on the same system). Check the type as well.

killall() still seems error prone and I couldn't find any actual users of the -k/--kill option for seunshare. Can we just drop this?

Quoted 6 lines

---
v2 fixes the signedness of "i" to match its usage.

 sandbox/seunshare.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

Merged.