6995654
diff -up openssh-7.4p1/openbsd-compat/port-linux.h.privsep-selinux openssh-7.4p1/openbsd-compat/port-linux.h
6995654
--- openssh-7.4p1/openbsd-compat/port-linux.h.privsep-selinux	2016-12-23 18:58:52.972122201 +0100
6995654
+++ openssh-7.4p1/openbsd-compat/port-linux.h	2016-12-23 18:58:52.974122201 +0100
6995654
@@ -23,6 +23,7 @@ void ssh_selinux_setup_pty(char *, const
6995654
 void ssh_selinux_change_context(const char *);
6995654
 void ssh_selinux_setfscreatecon(const char *);
6995654
 
6995654
+void sshd_selinux_copy_context(void);
6995654
 void sshd_selinux_setup_exec_context(char *);
6995654
 #endif
6995654
 
6995654
diff -up openssh-7.4p1/openbsd-compat/port-linux-sshd.c.privsep-selinux openssh-7.4p1/openbsd-compat/port-linux-sshd.c
6995654
--- openssh-7.4p1/openbsd-compat/port-linux-sshd.c.privsep-selinux	2016-12-23 18:58:52.973122201 +0100
6995654
+++ openssh-7.4p1/openbsd-compat/port-linux-sshd.c	2016-12-23 18:58:52.974122201 +0100
6995654
@@ -419,6 +419,28 @@ sshd_selinux_setup_exec_context(char *pw
d482358
 	debug3_f("done");
2d54875
 }
2d54875
 
2d54875
+void
0ae19bf
+sshd_selinux_copy_context(void)
2d54875
+{
9dd0663
+	security_context_t *ctx;
2d54875
+
2d54875
+	if (!ssh_selinux_enabled())
2d54875
+		return;
2d54875
+
2d54875
+	if (getexeccon((security_context_t *)&ctx) != 0) {
d482358
+		logit_f("getexeccon failed with %s", strerror(errno));
2d54875
+		return;
2d54875
+	}
9dd0663
+	if (ctx != NULL) {
adf76a3
+		/* unset exec context before we will lose this capabililty */
adf76a3
+		if (setexeccon(NULL) != 0)
d482358
+			fatal_f("setexeccon failed with %s", strerror(errno));
9dd0663
+		if (setcon(ctx) != 0)
d482358
+			fatal_f("setcon failed with %s", strerror(errno));
9dd0663
+		freecon(ctx);
9dd0663
+	}
2d54875
+}
2d54875
+
0ae19bf
 #endif
0ae19bf
 #endif
2d54875
 
6995654
diff -up openssh-7.4p1/session.c.privsep-selinux openssh-7.4p1/session.c
6995654
--- openssh-7.4p1/session.c.privsep-selinux	2016-12-19 05:59:41.000000000 +0100
6995654
+++ openssh-7.4p1/session.c	2016-12-23 18:58:52.974122201 +0100
6995654
@@ -1331,7 +1331,7 @@ do_setusercontext(struct passwd *pw)
0ae19bf
 
6995654
 	platform_setusercontext(pw);
2d54875
 
6995654
-	if (platform_privileged_uidswap()) {
6995654
+	if (platform_privileged_uidswap() && (!is_child || !use_privsep)) {
6995654
 #ifdef HAVE_LOGIN_CAP
6995654
 		if (setusercontext(lc, pw, pw->pw_uid,
6995654
 		    (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
6995654
@@ -1361,6 +1361,9 @@ do_setusercontext(struct passwd *pw)
5923b7b
			    (unsigned long long)pw->pw_uid);
5923b7b
			chroot_path = percent_expand(tmp, "h", pw->pw_dir,
5923b7b
			    "u", pw->pw_name, "U", uidstr, (char *)NULL);
9dd0663
+#ifdef WITH_SELINUX
0ae19bf
+			sshd_selinux_copy_context();
9dd0663
+#endif
9dd0663
 			safely_chroot(chroot_path, pw->pw_uid);
9dd0663
 			free(tmp);
9dd0663
 			free(chroot_path);
6995654
@@ -1396,6 +1399,11 @@ do_setusercontext(struct passwd *pw)
9dd0663
 		/* Permanently switch to the desired uid. */
9dd0663
 		permanently_set_uid(pw);
9dd0663
 #endif
edfa524
+
325c7f3
+#ifdef WITH_SELINUX
845bd87
+		if (in_chroot == 0)
0ae19bf
+			sshd_selinux_copy_context();
325c7f3
+#endif
aaf34e5
 	} else if (options.chroot_directory != NULL &&
aaf34e5
 	    strcasecmp(options.chroot_directory, "none") != 0) {
aaf34e5
 		fatal("server lacks privileges to chroot to ChrootDirectory");
6995654
@@ -1413,9 +1421,6 @@ do_pwchange(Session *s)
adf76a3
 	if (s->ttyfd != -1) {
adf76a3
 		fprintf(stderr,
adf76a3
 		    "You must change your password now and login again!\n");
adf76a3
-#ifdef WITH_SELINUX
adf76a3
-		setexeccon(NULL);
adf76a3
-#endif
adf76a3
 #ifdef PASSWD_NEEDS_USERNAME
adf76a3
 		execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
adf76a3
 		    (char *)NULL);
6995654
@@ -1625,9 +1630,6 @@ do_child(Session *s, const char *command
2d54875
 		argv[i] = NULL;
2d54875
 		optind = optreset = 1;
2d54875
 		__progname = argv[0];
2d54875
-#ifdef WITH_SELINUX
2d54875
-		ssh_selinux_change_context("sftpd_t");
2d54875
-#endif
2d54875
 		exit(sftp_server_main(i, argv, s->pw));
d9cd0da
 	}
d9cd0da
 
6995654
diff -up openssh-7.4p1/sshd.c.privsep-selinux openssh-7.4p1/sshd.c
6995654
--- openssh-7.4p1/sshd.c.privsep-selinux	2016-12-23 18:58:52.973122201 +0100
6995654
+++ openssh-7.4p1/sshd.c	2016-12-23 18:59:13.808124269 +0100
6995654
@@ -540,6 +540,10 @@ privsep_preauth_child(void)
6ef9c48
 	/* Demote the private keys to public keys. */
6ef9c48
 	demote_sensitive_data();
6ef9c48
 
6ef9c48
+#ifdef WITH_SELINUX
6ef9c48
+	ssh_selinux_change_context("sshd_net_t");
6ef9c48
+#endif
6ef9c48
+
845bd87
 	/* Demote the child */
c7d42e7
 	if (privsep_chroot) {
845bd87
 		/* Change our root directory */
6995654
@@ -633,6 +637,9 @@ privsep_postauth(Authctxt *authctxt)
6995654
 {
adf76a3
 #ifdef DISABLE_FD_PASSING
adf76a3
 	if (1) {
adf76a3
+#elif defined(WITH_SELINUX)
6995654
+	if (0) {
adf76a3
+		/* even root user can be confined by SELinux */
adf76a3
 #else
6995654
 	if (authctxt->pw->pw_uid == 0) {
adf76a3
 #endif