Skip to contentSkip to conversation

seunshare: guard fallible function calls by checking retval

PATCH In review Rahul Sandhu 3 participants 3 messages Apr 14 – May 13 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 seunshare: guard fallible function calls by checking retval

seunshare currently segfaults is passed --kill and an invalid context via -Z.

---
 sandbox/seunshare.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
sandbox/seunshare.c +19 -7
index 2512a18c..e89a9fec 100644
@@ -679,9 +679,19 @@ killall (const char *execcon)
 		return -1;
 	}
 	pids = 0;
-	context_t con;
-	con = context_new(execcon);
-	const char *mcs = context_range_get(con);
+	context_t con = context_new(execcon);
+	if (!con) {
+		free(pid_table);
+		(void)closedir(dir);
+		return -1;
+	}
+	const char *const mcs = context_range_get(con);
+	if (!mcs) {
+		context_free(con);
+		free(pid_table);
+		(void)closedir(dir);
+		return -1;
+	}
 	printf("mcs=%s\n", mcs);
 	while ((de = readdir (dir)) != NULL) {
 		if (!(pid = (pid_t)atoi(de->d_name)) || pid == self)
@@ -708,11 +718,13 @@ killall (const char *execcon)
 		if (getpidcon(id, &scon) == 0) {
 
 			context_t pidcon = context_new(scon);
-			/* Attempt to kill remaining processes */
-			if (strcmp(context_range_get(pidcon), mcs) == 0)
-				kill(id, SIGKILL);
+			if (pidcon) {
+				/* Attempt to kill remaining processes */
+				if (strcmp(context_range_get(pidcon), mcs) == 0)
+					kill(id, SIGKILL);
 
-			context_free(pidcon);
+				context_free(pidcon);
+			}
 			freecon(scon);
 		}
 		running++;
Signature · 1 line
2.53.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Rahul Sandhu

seunshare currently segfaults is passed --kill and an invalid context via -Z.

Quoted 53 lines
---
 sandbox/seunshare.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
sandbox/seunshare.c +19 -8
index 2512a18c..e89a9fec 100644
@@ -679,9 +679,19 @@ killall (const char *execcon)
                return -1;
        }
        pids = 0;
-       context_t con;
-       con = context_new(execcon);
-       const char *mcs = context_range_get(con);
+       context_t con = context_new(execcon);
+       if (!con) {
+               free(pid_table);
+               (void)closedir(dir);
+               return -1;
+       }
+       const char *const mcs = context_range_get(con);
+       if (!mcs) {
+               context_free(con);
+               free(pid_table);
+               (void)closedir(dir);
+               return -1;
+       }
        printf("mcs=%s\n", mcs);
        while ((de = readdir (dir)) != NULL) {
                if (!(pid = (pid_t)atoi(de->d_name)) || pid == self)
@@ -708,11 +718,13 @@ killall (const char *execcon)
                if (getpidcon(id, &scon) == 0) {
 
                        context_t pidcon = context_new(scon);
-                       /* Attempt to kill remaining processes */
-                       if (strcmp(context_range_get(pidcon), mcs) == 0)
-                               kill(id, SIGKILL);
+                       if (pidcon) {
+                               /* Attempt to kill remaining processes */
+                               if (strcmp(context_range_get(pidcon), mcs) == 0)
+                                       kill(id, SIGKILL);
 
-                       context_free(pidcon);
+                               context_free(pidcon);
+                       }
                        freecon(scon);
                }
                running++;
--

2.53.0

#2 lore raw
Quoted 10 lines · Stephen Smalley
Quoted 5 lines · Rahul Sandhu

seunshare currently segfaults is passed --kill and an invalid context via -Z.

Merged.
Thanks,
Jim

Quoted 54 lines
Quoted 53 lines
---
 sandbox/seunshare.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
sandbox/seunshare.c +19 -8
index 2512a18c..e89a9fec 100644
@@ -679,9 +679,19 @@ killall (const char *execcon)
                return -1;
        }
        pids = 0;
-       context_t con;
-       con = context_new(execcon);
-       const char *mcs = context_range_get(con);
+       context_t con = context_new(execcon);
+       if (!con) {
+               free(pid_table);
+               (void)closedir(dir);
+               return -1;
+       }
+       const char *const mcs = context_range_get(con);
+       if (!mcs) {
+               context_free(con);
+               free(pid_table);
+               (void)closedir(dir);
+               return -1;
+       }
        printf("mcs=%s\n", mcs);
        while ((de = readdir (dir)) != NULL) {
                if (!(pid = (pid_t)atoi(de->d_name)) || pid == self)
@@ -708,11 +718,13 @@ killall (const char *execcon)
                if (getpidcon(id, &scon) == 0) {
 
                        context_t pidcon = context_new(scon);
-                       /* Attempt to kill remaining processes */
-                       if (strcmp(context_range_get(pidcon), mcs) == 0)
-                               kill(id, SIGKILL);
+                       if (pidcon) {
+                               /* Attempt to kill remaining processes */
+                               if (strcmp(context_range_get(pidcon), mcs) == 0)
+                                       kill(id, SIGKILL);
 
-                       context_free(pidcon);
+                               context_free(pidcon);
+                       }
                        freecon(scon);
                }
                running++;
--

2.53.0