Skip to contentSkip to conversation

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

PATCH Superseded Stephen Smalley 1 participant 1 message May 13 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 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?

---
 sandbox/seunshare.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
sandbox/seunshare.c +18 -6
index 2eef0e2f..5ed690eb 100644
@@ -681,7 +681,7 @@ killall (const char *execcon)
 	struct dirent *de;
 	pid_t *pid_table, pid, self;
 	int i;
-	int pids, max_pids;
+	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.53.0