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