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

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