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