psss / rpms / libselinux

Forked from rpms/libselinux 5 years ago
Clone
e7c97c5
--- libselinux-1.21.10/src/query_user_context.c.ud	2005-02-17 11:22:46.000000000 -0800
e7c97c5
+++ libselinux-1.21.10/src/query_user_context.c	2005-02-20 12:04:50.001377520 -0800
e7c97c5
@@ -23,7 +23,8 @@
e7c97c5
     {
e7c97c5
         printf ("Enter number of choice: ");
e7c97c5
         fflush (stdin);
e7c97c5
-        fgets (response, sizeof (response), stdin);
e7c97c5
+        if (fgets (response, sizeof (response), stdin) == NULL)
e7c97c5
+		continue;
e7c97c5
         fflush (stdin);
e7c97c5
         choice = strtol (response, NULL, 10);
e7c97c5
     }
e7c97c5
@@ -50,7 +51,8 @@
e7c97c5
     if (list[1]) {
e7c97c5
             printf ("Do you want to choose a different one? [n]");
e7c97c5
             fflush (stdin);
e7c97c5
-            fgets (response, sizeof (response), stdin);
e7c97c5
+            if (fgets (response, sizeof (response), stdin) == NULL)
e7c97c5
+		return -1;
e7c97c5
             fflush (stdin);
e7c97c5
 
e7c97c5
             if ((response[0] == 'y') || (response[0] == 'Y'))
e7c97c5
@@ -86,9 +88,11 @@
e7c97c5
     {
e7c97c5
         printf ("\tEnter %s ", fieldstr);
e7c97c5
         fflush (stdin);
e7c97c5
-        fgets (newfield, newfieldlen, stdin);
e7c97c5
+        if (fgets (newfield, newfieldlen, stdin) == NULL)
e7c97c5
+	    continue;
e7c97c5
         fflush (stdin);
e7c97c5
-        newfield[strlen(newfield)-1] = '\0';
e7c97c5
+	if (newfield[strlen(newfield)-1] == '\n')
e7c97c5
+	    newfield[strlen(newfield)-1] = '\0';
e7c97c5
  
e7c97c5
         if (strlen(newfield) == 0)  
e7c97c5
         {
e7c97c5
@@ -137,8 +141,8 @@
e7c97c5
     while (!done)
e7c97c5
     {
e7c97c5
         printf ("Would you like to enter a security context? [y]");
e7c97c5
-        fgets (response, sizeof(response), stdin);
e7c97c5
-        if ((response[0] == 'n') || (response[0] == 'N')) {
e7c97c5
+        if (fgets (response, sizeof(response), stdin) == NULL
e7c97c5
+            || (response[0] == 'n') || (response[0] == 'N')) {
e7c97c5
             context_free(new_context);
e7c97c5
             return -1;
e7c97c5
 	}
e7c97c5
--- libselinux-1.21.10/src/matchpathcon.c.ud	2005-02-17 11:22:46.000000000 -0800
e7c97c5
+++ libselinux-1.21.10/src/matchpathcon.c	2005-02-20 12:19:39.883094936 -0800
e7c97c5
@@ -4,6 +4,7 @@
e7c97c5
 #include <string.h>
e7c97c5
 #include "selinux_internal.h"
e7c97c5
 #include <stdio.h>
e7c97c5
+#include <stdio_ext.h>
e7c97c5
 #include <stdlib.h>
e7c97c5
 #include <ctype.h>
e7c97c5
 #include <errno.h>
e7c97c5
@@ -401,11 +402,8 @@
31e19c1
 	char *regex, *type, *context;
31e19c1
 	char *anchored_regex;
31e19c1
 	len = strlen(line_buf);
31e19c1
-	if (line_buf[len - 1] != '\n') {
e7c97c5
-		myprintf("%s:  line %d is too long, would be truncated, skipping\n", path, lineno); 
e7c97c5
-		return 0;
e7c97c5
-	}
e7c97c5
-	line_buf[len - 1] = 0;
e7c97c5
+	if (line_buf[len - 1] == '\n')
e7c97c5
+		line_buf[len - 1] = 0;
e7c97c5
 	buf_p = line_buf;
e7c97c5
 	while (isspace(*buf_p))
e7c97c5
 		buf_p++;
e7c97c5
@@ -522,7 +520,8 @@
e7c97c5
 	FILE *homedirfp;
e7c97c5
 	char local_path[PATH_MAX + 1];
e7c97c5
 	char homedir_path[PATH_MAX + 1];
e7c97c5
-	char line_buf[BUFSIZ + 1];
e7c97c5
+	char *line_buf = NULL;
e7c97c5
+	size_t line_len = 0;
e7c97c5
 	unsigned int lineno, pass, i, j, maxnspec;
e7c97c5
 	spec_t *spec_copy;
e7c97c5
 	int status=-1;
e7c97c5
@@ -532,12 +531,17 @@
e7c97c5
 		path = selinux_file_context_path();
e7c97c5
 	if ((fp = fopen(path, "r")) == NULL)
e7c97c5
 		return -1;
e7c97c5
+	__fsetlocking(fp, FSETLOCKING_BYCALLER);
e7c97c5
 
e7c97c5
 	snprintf(homedir_path, sizeof(homedir_path), "%s.homedirs", path);
e7c97c5
 	homedirfp = fopen(homedir_path, "r");
e7c97c5
+	if (homedirfp != NULL)
e7c97c5
+		__fsetlocking(homedirfp, FSETLOCKING_BYCALLER);
e7c97c5
 
e7c97c5
 	snprintf(local_path, sizeof(local_path), "%s.local", path);
e7c97c5
 	localfp = fopen(local_path, "r");
e7c97c5
+	if (localfp != NULL)
e7c97c5
+		__fsetlocking(localfp, FSETLOCKING_BYCALLER);
e7c97c5
 
e7c97c5
 	/* 
e7c97c5
 	 * Perform two passes over the specification file.
e7c97c5
@@ -551,19 +555,19 @@
e7c97c5
 	for (pass = 0; pass < 2; pass++) {
e7c97c5
 		lineno = 0;
e7c97c5
 		nspec = 0;
e7c97c5
-		while (fgets_unlocked(line_buf, sizeof line_buf, fp) && nspec < maxnspec) {
e7c97c5
+		while (getline(&line_buf, &line_len, fp) > 0 && nspec < maxnspec) {
e7c97c5
 			if (process_line(path, line_buf, pass, ++lineno) != 0)
e7c97c5
 				goto finish;
e7c97c5
 		}
e7c97c5
 		if (homedirfp) 
e7c97c5
-			while (fgets_unlocked(line_buf, sizeof line_buf, homedirfp) && nspec < maxnspec) {
e7c97c5
+			while (getline(&line_buf, &line_len, homedirfp) > 0 && nspec < maxnspec) {
e7c97c5
 				if (process_line(homedir_path, line_buf, pass, ++lineno) != 0)
e7c97c5
 					goto finish;
e7c97c5
 			}
e7c97c5
 
e7c97c5
 
e7c97c5
 		if (localfp) 
e7c97c5
-			while (fgets_unlocked(line_buf, sizeof line_buf, localfp) && nspec < maxnspec) {
e7c97c5
+			while (getline(&line_buf, &line_len, localfp) > 0 && nspec < maxnspec) {
e7c97c5
 				if (process_line(local_path, line_buf, pass, ++lineno) != 0)
e7c97c5
 					goto finish;
e7c97c5
 			}
e7c97c5
@@ -583,6 +587,7 @@
e7c97c5
 			if (localfp) rewind(localfp);
e7c97c5
 		}
e7c97c5
 	}
e7c97c5
+	free(line_buf);
e7c97c5
 
e7c97c5
 	/* Move exact pathname specifications to the end. */
e7c97c5
 	spec_copy = malloc(sizeof(spec_t) * nspec);
e7c97c5
--- libselinux-1.21.10/utils/setsebool.c.ud	2005-02-17 11:22:47.000000000 -0800
e7c97c5
+++ libselinux-1.21.10/utils/setsebool.c	2005-02-20 12:04:50.001377520 -0800
e7c97c5
@@ -122,6 +122,7 @@
e7c97c5
 	if (permanent) {
e7c97c5
 		char **names;
e7c97c5
 		const char *bool_file;
e7c97c5
+		char *tmp_bool_file;
e7c97c5
 		int rc, len, fd, j;
e7c97c5
 
e7c97c5
 		rc = security_get_boolean_names(&names, &len;;
e7c97c5
@@ -143,8 +144,9 @@
e7c97c5
 
e7c97c5
 		/* Open file */
e7c97c5
 		bool_file = selinux_booleans_path();
e7c97c5
-		fd = open(bool_file, O_CREAT | O_TRUNC | O_WRONLY, 
e7c97c5
-							S_IRUSR | S_IWUSR);
e7c97c5
+		tmp_bool_file = (char *) alloca (strlen(bool_file) + 8);
e7c97c5
+		strcpy(stpcpy(tmp_bool_file, bool_file), ".XXXXXX");
e7c97c5
+		fd = mkstemp(tmp_bool_file);
e7c97c5
 		if (fd < 0) {
e7c97c5
 			fprintf(stderr, 
e7c97c5
 				"Error creating boolean file %s\n", 
e7c97c5
@@ -157,13 +159,25 @@
e7c97c5
 		/* Walk the list in pending memory, writing each to the file */
e7c97c5
 		for (j=0; j
e7c97c5
 			char val_str[72];
e7c97c5
+			int len;
e7c97c5
 			int pending = security_get_boolean_pending(names[j]);
e7c97c5
-			snprintf(val_str, sizeof(val_str), "%s=%d\n", 
e7c97c5
+			len = snprintf(val_str, sizeof(val_str), "%s=%d\n", 
e7c97c5
 							names[j], pending);
e7c97c5
-			write(fd, val_str, strlen(val_str));
e7c97c5
+			if (write(fd, val_str, len) != len) {
e7c97c5
+			close_remove_fail:
e7c97c5
+				close(fd);
e7c97c5
+			remove_fail:
e7c97c5
+				unlink(tmp_bool_file);
e7c97c5
+				rollback(list, start, i);
e7c97c5
+				return 8;
e7c97c5
+			}
e7c97c5
 		}
e7c97c5
 
e7c97c5
+		if (fchmod(fd, S_IRUSR | S_IWUSR) != 0)
e7c97c5
+			goto close_remove_fail;
e7c97c5
 		close(fd);
e7c97c5
+		if (rename(tmp_bool_file, bool_file) != 0)
e7c97c5
+			goto remove_fail;
e7c97c5
 		syslog(LOG_NOTICE, "%s has been updated.", bool_file);
03d51ea
 	}
e7c97c5