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