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