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