psss / rpms / libsemanage

Forked from rpms/libsemanage 5 years ago
Clone
0e04653
diff --git libsemanage-2.8/src/semanage_store.c libsemanage-2.8/src/semanage_store.c
0e04653
index f1984c5..58dded6 100644
0e04653
--- libsemanage-2.8/src/semanage_store.c
0e04653
+++ libsemanage-2.8/src/semanage_store.c
0e04653
@@ -541,14 +541,18 @@ int semanage_create_store(semanage_handle_t * sh, int create)
0e04653
 	struct stat sb;
0e04653
 	const char *path = semanage_files[SEMANAGE_ROOT];
0e04653
 	int fd;
0e04653
+	mode_t mask;
0e04653
 
0e04653
 	if (stat(path, &sb) == -1) {
0e04653
 		if (errno == ENOENT && create) {
0e04653
+			mask = umask(0077);
0e04653
 			if (mkdir(path, S_IRWXU) == -1) {
0e04653
+				umask(mask);
0e04653
 				ERR(sh, "Could not create module store at %s.",
0e04653
 				    path);
0e04653
 				return -2;
0e04653
 			}
0e04653
+			umask(mask);
0e04653
 		} else {
0e04653
 			if (create)
0e04653
 				ERR(sh,
0e04653
@@ -567,12 +571,15 @@ int semanage_create_store(semanage_handle_t * sh, int create)
0e04653
 	path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_TOPLEVEL);
0e04653
 	if (stat(path, &sb) == -1) {
0e04653
 		if (errno == ENOENT && create) {
0e04653
+			mask = umask(0077);
0e04653
 			if (mkdir(path, S_IRWXU) == -1) {
0e04653
+				umask(mask);
0e04653
 				ERR(sh,
0e04653
 				    "Could not create module store, active subdirectory at %s.",
0e04653
 				    path);
0e04653
 				return -2;
0e04653
 			}
0e04653
+			umask(mask);
0e04653
 		} else {
0e04653
 			ERR(sh,
0e04653
 			    "Could not read from module store, active subdirectory at %s.",
0e04653
@@ -590,12 +597,15 @@ int semanage_create_store(semanage_handle_t * sh, int create)
0e04653
 	path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_MODULES);
0e04653
 	if (stat(path, &sb) == -1) {
0e04653
 		if (errno == ENOENT && create) {
0e04653
+			mask = umask(0077);
0e04653
 			if (mkdir(path, S_IRWXU) == -1) {
0e04653
+				umask(mask);
0e04653
 				ERR(sh,
0e04653
 				    "Could not create module store, active modules subdirectory at %s.",
0e04653
 				    path);
0e04653
 				return -2;
0e04653
 			}
0e04653
+			umask(mask);
0e04653
 		} else {
0e04653
 			ERR(sh,
0e04653
 			    "Could not read from module store, active modules subdirectory at %s.",
0e04653
@@ -613,11 +623,14 @@ int semanage_create_store(semanage_handle_t * sh, int create)
0e04653
 	path = semanage_files[SEMANAGE_READ_LOCK];
0e04653
 	if (stat(path, &sb) == -1) {
0e04653
 		if (errno == ENOENT && create) {
0e04653
+			mask = umask(0077);
0e04653
 			if ((fd = creat(path, S_IRUSR | S_IWUSR)) == -1) {
0e04653
+				umask(mask);
0e04653
 				ERR(sh, "Could not create lock file at %s.",
0e04653
 				    path);
0e04653
 				return -2;
0e04653
 			}
0e04653
+			umask(mask);
0e04653
 			close(fd);
0e04653
 		} else {
0e04653
 			ERR(sh, "Could not read lock file at %s.", path);
0e04653
@@ -763,6 +776,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
0e04653
 	struct stat sb;
0e04653
 	struct dirent **names = NULL;
0e04653
 	char path[PATH_MAX], path2[PATH_MAX];
0e04653
+	mode_t mask;
0e04653
 
0e04653
 	if ((len = scandir(src, &names, semanage_filename_select, NULL)) == -1) {
0e04653
 		fprintf(stderr, "Could not read the contents of %s: %s\n", src, strerror(errno));
0e04653
@@ -770,10 +784,13 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
0e04653
 	}
0e04653
 
0e04653
 	if (stat(dst, &sb) != 0) {
0e04653
+		mask = umask(0077);
0e04653
 		if (mkdir(dst, S_IRWXU) != 0) {
0e04653
+			umask(mask);
0e04653
 			fprintf(stderr, "Could not create %s: %s\n", dst, strerror(errno));
0e04653
 			goto cleanup;
0e04653
 		}
0e04653
+		umask(mask);
0e04653
 	}
0e04653
 
0e04653
 	for (i = 0; i < len; i++) {
0e04653
@@ -785,14 +802,20 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
0e04653
 		}
0e04653
 		snprintf(path2, sizeof(path2), "%s/%s", dst, names[i]->d_name);
0e04653
 		if (S_ISDIR(sb.st_mode)) {
0e04653
+			mask = umask(0077);
0e04653
 			if (mkdir(path2, 0700) == -1 ||
0e04653
 			    semanage_copy_dir_flags(path, path2, flag) == -1) {
0e04653
+				umask(mask);
0e04653
 				goto cleanup;
0e04653
 			}
0e04653
+			umask(mask);
0e04653
 		} else if (S_ISREG(sb.st_mode) && flag == 1) {
0e04653
+			mask = umask(0077);
0e04653
 			if (semanage_copy_file(path, path2, sb.st_mode) < 0) {
0e04653
+				umask(mask);
0e04653
 				goto cleanup;
0e04653
 			}
0e04653
+			umask(mask);
0e04653
 		}
0e04653
 	}
0e04653
 	retval = 0;
0e04653
@@ -872,16 +895,20 @@ int semanage_mkdir(semanage_handle_t *sh, const char *path)
0e04653
 {
0e04653
 	int status = 0;
0e04653
 	struct stat sb;
0e04653
+	mode_t mask;
0e04653
 
0e04653
 	/* check if directory already exists */
0e04653
 	if (stat(path, &sb) != 0) {
0e04653
 		/* make the modules directory */
0e04653
+		mask = umask(0077);
0e04653
 		if (mkdir(path, S_IRWXU) != 0) {
0e04653
+			umask(mask);
0e04653
 			ERR(sh, "Cannot make directory at %s", path);
0e04653
 			status = -1;
0e04653
 			goto cleanup;
0e04653
 
0e04653
 		}
0e04653
+		umask(mask);
0e04653
 	}
0e04653
 	else {
0e04653
 		/* check that it really is a directory */
0e04653
@@ -906,6 +933,7 @@ int semanage_make_sandbox(semanage_handle_t * sh)
0e04653
 	const char *sandbox = semanage_path(SEMANAGE_TMP, SEMANAGE_TOPLEVEL);
0e04653
 	struct stat buf;
0e04653
 	int errsv;
0e04653
+	mode_t mask;
0e04653
 
0e04653
 	if (stat(sandbox, &buf) == -1) {
0e04653
 		if (errno != ENOENT) {
0e04653
@@ -922,12 +950,15 @@ int semanage_make_sandbox(semanage_handle_t * sh)
0e04653
 		}
0e04653
 	}
0e04653
 
0e04653
+	mask = umask(0077);
0e04653
 	if (mkdir(sandbox, S_IRWXU) == -1 ||
0e04653
 	    semanage_copy_dir(semanage_path(SEMANAGE_ACTIVE, SEMANAGE_TOPLEVEL),
0e04653
 			      sandbox) == -1) {
0e04653
+		umask(mask);
0e04653
 		ERR(sh, "Could not copy files to sandbox %s.", sandbox);
0e04653
 		goto cleanup;
0e04653
 	}
0e04653
+	umask(mask);
0e04653
 	return 0;
0e04653
 
0e04653
       cleanup: