Blame SOURCES/shadow-4.6-orig-context.patch

9f13c6
diff -up shadow-4.6/lib/commonio.c.orig-context shadow-4.6/lib/commonio.c
9f13c6
--- shadow-4.6/lib/commonio.c.orig-context	2018-04-29 18:42:37.000000000 +0200
9f13c6
+++ shadow-4.6/lib/commonio.c	2018-05-28 14:56:37.287929667 +0200
9f13c6
@@ -961,7 +961,7 @@ int commonio_close (struct commonio_db *
9f13c6
 		snprintf (buf, sizeof buf, "%s-", db->filename);
9f13c6
 
9f13c6
 #ifdef WITH_SELINUX
9f13c6
-		if (set_selinux_file_context (buf) != 0) {
9f13c6
+		if (set_selinux_file_context (buf, db->filename) != 0) {
9f13c6
 			errors++;
9f13c6
 		}
9f13c6
 #endif
9f13c6
@@ -994,7 +994,7 @@ int commonio_close (struct commonio_db *
9f13c6
 	snprintf (buf, sizeof buf, "%s+", db->filename);
9f13c6
 
9f13c6
 #ifdef WITH_SELINUX
9f13c6
-	if (set_selinux_file_context (buf) != 0) {
9f13c6
+	if (set_selinux_file_context (buf, db->filename) != 0) {
9f13c6
 		errors++;
9f13c6
 	}
9f13c6
 #endif
9f13c6
diff -up shadow-4.6/libmisc/copydir.c.orig-context shadow-4.6/libmisc/copydir.c
9f13c6
--- shadow-4.6/libmisc/copydir.c.orig-context	2018-04-29 18:42:37.000000000 +0200
9f13c6
+++ shadow-4.6/libmisc/copydir.c	2018-05-28 14:56:37.287929667 +0200
9f13c6
@@ -484,7 +484,7 @@ static int copy_dir (const char *src, co
9f13c6
 	 */
9f13c6
 
9f13c6
 #ifdef WITH_SELINUX
9f13c6
-	if (set_selinux_file_context (dst) != 0) {
9f13c6
+	if (set_selinux_file_context (dst, NULL) != 0) {
9f13c6
 		return -1;
9f13c6
 	}
9f13c6
 #endif				/* WITH_SELINUX */
9f13c6
@@ -605,7 +605,7 @@ static int copy_symlink (const char *src
9f13c6
 	}
9f13c6
 
9f13c6
 #ifdef WITH_SELINUX
9f13c6
-	if (set_selinux_file_context (dst) != 0) {
9f13c6
+	if (set_selinux_file_context (dst, NULL) != 0) {
9f13c6
 		free (oldlink);
9f13c6
 		return -1;
9f13c6
 	}
9f13c6
@@ -684,7 +684,7 @@ static int copy_special (const char *src
9f13c6
 	int err = 0;
9f13c6
 
9f13c6
 #ifdef WITH_SELINUX
9f13c6
-	if (set_selinux_file_context (dst) != 0) {
9f13c6
+	if (set_selinux_file_context (dst, NULL) != 0) {
9f13c6
 		return -1;
9f13c6
 	}
9f13c6
 #endif				/* WITH_SELINUX */
9f13c6
@@ -744,7 +744,7 @@ static int copy_file (const char *src, c
9f13c6
 		return -1;
9f13c6
 	}
9f13c6
 #ifdef WITH_SELINUX
9f13c6
-	if (set_selinux_file_context (dst) != 0) {
9f13c6
+	if (set_selinux_file_context (dst, NULL) != 0) {
9f13c6
 		return -1;
9f13c6
 	}
9f13c6
 #endif				/* WITH_SELINUX */
9f13c6
diff -up shadow-4.6/lib/prototypes.h.orig-context shadow-4.6/lib/prototypes.h
9f13c6
--- shadow-4.6/lib/prototypes.h.orig-context	2018-04-29 18:42:37.000000000 +0200
9f13c6
+++ shadow-4.6/lib/prototypes.h	2018-05-28 14:56:37.287929667 +0200
9f13c6
@@ -326,7 +326,7 @@ extern /*@observer@*/const char *crypt_m
9f13c6
 
9f13c6
 /* selinux.c */
9f13c6
 #ifdef WITH_SELINUX
9f13c6
-extern int set_selinux_file_context (const char *dst_name);
9f13c6
+extern int set_selinux_file_context (const char *dst_name, const char *orig_name);
9f13c6
 extern int reset_selinux_file_context (void);
9f13c6
 #endif
9f13c6
 
9f13c6
diff -up shadow-4.6/lib/selinux.c.orig-context shadow-4.6/lib/selinux.c
9f13c6
--- shadow-4.6/lib/selinux.c.orig-context	2018-04-29 18:42:37.000000000 +0200
9f13c6
+++ shadow-4.6/lib/selinux.c	2018-05-28 14:56:37.287929667 +0200
9f13c6
@@ -50,7 +50,7 @@ static bool selinux_enabled;
9f13c6
  *	Callers may have to Reset SELinux to create files with default
9f13c6
  *	contexts with reset_selinux_file_context
9f13c6
  */
9f13c6
-int set_selinux_file_context (const char *dst_name)
9f13c6
+int set_selinux_file_context (const char *dst_name, const char *orig_name)
9f13c6
 {
9f13c6
 	/*@null@*/security_context_t scontext = NULL;
9f13c6
 
9f13c6
@@ -62,19 +62,23 @@ int set_selinux_file_context (const char
9f13c6
 	if (selinux_enabled) {
9f13c6
 		/* Get the default security context for this file */
9f13c6
 		if (matchpathcon (dst_name, 0, &scontext) < 0) {
9f13c6
-			if (security_getenforce () != 0) {
9f13c6
-				return 1;
9f13c6
-			}
9f13c6
+			/* We could not get the default, copy the original */
9f13c6
+			if (orig_name == NULL)
9f13c6
+				goto error;
9f13c6
+			if (getfilecon (orig_name, &scontext) < 0)
9f13c6
+				goto error;
9f13c6
 		}
9f13c6
 		/* Set the security context for the next created file */
9f13c6
-		if (setfscreatecon (scontext) < 0) {
9f13c6
-			if (security_getenforce () != 0) {
9f13c6
-				return 1;
9f13c6
-			}
9f13c6
-		}
9f13c6
+		if (setfscreatecon (scontext) < 0)
9f13c6
+			goto error;
9f13c6
 		freecon (scontext);
9f13c6
 	}
9f13c6
 	return 0;
9f13c6
+    error:
9f13c6
+	if (security_getenforce () != 0) {
9f13c6
+		return 1;
9f13c6
+	}
9f13c6
+	return 0;
9f13c6
 }
9f13c6
 
9f13c6
 /*
9f13c6
diff -up shadow-4.6/src/useradd.c.orig-context shadow-4.6/src/useradd.c
9f13c6
--- shadow-4.6/src/useradd.c.orig-context	2018-05-28 14:56:37.288929688 +0200
9f13c6
+++ shadow-4.6/src/useradd.c	2018-05-28 14:58:02.242730903 +0200
9f13c6
@@ -2020,7 +2020,7 @@ static void create_home (void)
9f13c6
 {
9f13c6
 	if (access (prefix_user_home, F_OK) != 0) {
9f13c6
 #ifdef WITH_SELINUX
9f13c6
-		if (set_selinux_file_context (prefix_user_home) != 0) {
9f13c6
+		if (set_selinux_file_context (prefix_user_home, NULL) != 0) {
9f13c6
 			fprintf (stderr,
9f13c6
 			         _("%s: cannot set SELinux context for home directory %s\n"),
9f13c6
 			         Prog, user_home);