8f2528
diff --git a/openbsd-compat/port-linux-sshd.c b/openbsd-compat/port-linux-sshd.c
8f2528
index c18524e..d04f4ed 100644
8f2528
--- a/openbsd-compat/port-linux-sshd.c
8f2528
+++ b/openbsd-compat/port-linux-sshd.c
8f2528
@@ -409,6 +409,25 @@ sshd_selinux_setup_exec_context(char *pwname)
8f2528
 	debug3("%s: done", __func__);
8f2528
 }
8f2528
 
8f2528
+void
8f2528
+sshd_selinux_copy_context(void)
8f2528
+{
8f2528
+	security_context_t *ctx;
8f2528
+
8f2528
+	if (!ssh_selinux_enabled())
8f2528
+		return;
8f2528
+
8f2528
+	if (getexeccon((security_context_t *)&ctx) != 0) {
8f2528
+		logit("%s: getcon failed with %s", __func__, strerror (errno));
8f2528
+		return;
8f2528
+	}
8f2528
+	if (ctx != NULL) {
8f2528
+		if (setcon(ctx) != 0)
8f2528
+			logit("%s: setcon failed with %s", __func__, strerror (errno));
8f2528
+		freecon(ctx);
8f2528
+	}
8f2528
+}
8f2528
+
8f2528
 #endif
8f2528
 #endif
8f2528
 
8f2528
diff --git a/openbsd-compat/port-linux.h b/openbsd-compat/port-linux.h
8f2528
index 8ef6cc4..b18893c 100644
8f2528
--- a/openbsd-compat/port-linux.h
8f2528
+++ b/openbsd-compat/port-linux.h
8f2528
@@ -25,6 +25,7 @@ void ssh_selinux_setup_pty(char *, const char *);
8f2528
 void ssh_selinux_change_context(const char *);
8f2528
 void ssh_selinux_setfscreatecon(const char *);
8f2528
 
8f2528
+void sshd_selinux_copy_context(void);
8f2528
 void sshd_selinux_setup_exec_context(char *);
8f2528
 #endif
8f2528
 
8f2528
diff --git a/session.c b/session.c
8f2528
index 2bcf818..b5dc144 100644
8f2528
--- a/session.c
8f2528
+++ b/session.c
8f2528
@@ -1538,6 +1538,9 @@ do_setusercontext(struct passwd *pw)
8f2528
 			    pw->pw_uid);
8f2528
 			chroot_path = percent_expand(tmp, "h", pw->pw_dir,
8f2528
 			    "u", pw->pw_name, (char *)NULL);
8f2528
+#ifdef WITH_SELINUX
8f2528
+			sshd_selinux_copy_context();
8f2528
+#endif
8f2528
 			safely_chroot(chroot_path, pw->pw_uid);
8f2528
 			free(tmp);
8f2528
 			free(chroot_path);
8f2528
@@ -1565,6 +1568,12 @@ do_setusercontext(struct passwd *pw)
8f2528
 		/* Permanently switch to the desired uid. */
8f2528
 		permanently_set_uid(pw);
8f2528
 #endif
8f2528
+
8f2528
+#ifdef WITH_SELINUX
8f2528
+		if (options.chroot_directory == NULL ||
8f2528
+		    strcasecmp(options.chroot_directory, "none") == 0)
8f2528
+			sshd_selinux_copy_context();
8f2528
+#endif
8f2528
 	} else if (options.chroot_directory != NULL &&
8f2528
 	    strcasecmp(options.chroot_directory, "none") != 0) {
8f2528
 		fatal("server lacks privileges to chroot to ChrootDirectory");
8f2528
@@ -1826,9 +1835,6 @@ do_child(Session *s, const char *command)
8f2528
 		argv[i] = NULL;
8f2528
 		optind = optreset = 1;
8f2528
 		__progname = argv[0];
8f2528
-#ifdef WITH_SELINUX
8f2528
-		ssh_selinux_change_context("sftpd_t");
8f2528
-#endif
8f2528
 		exit(sftp_server_main(i, argv, s->pw));
8f2528
 	}
8f2528
 
8f2528
diff --git a/sshd.c b/sshd.c
8f2528
index 07f9926..a97f8b7 100644
8f2528
--- a/sshd.c
8f2528
+++ b/sshd.c
8f2528
@@ -632,6 +632,10 @@ privsep_preauth_child(void)
8f2528
 	/* Demote the private keys to public keys. */
8f2528
 	demote_sensitive_data();
8f2528
 
8f2528
+#ifdef WITH_SELINUX
8f2528
+	ssh_selinux_change_context("sshd_net_t");
8f2528
+#endif
8f2528
+
8f2528
 	/* Demote the child */
8f2528
 	if (getuid() == 0 || geteuid() == 0) {
8f2528
 		/* Change our root directory */
8f2528
@@ -768,6 +772,13 @@ privsep_postauth(Authctxt *authctxt)
8f2528
 	do_setusercontext(authctxt->pw);
8f2528
 
8f2528
  skip:
8f2528
+#ifdef WITH_SELINUX
8f2528
+	/* switch SELinux content for root too */
8f2528
+	if (authctxt->pw->pw_uid == 0) {
8f2528
+		sshd_selinux_copy_context();
8f2528
+	}
8f2528
+#endif
8f2528
+
8f2528
 	/* It is safe now to apply the key state */
8f2528
 	monitor_apply_keystate(pmonitor);
8f2528