f7459a
diff -up openssh-5.9p0/openbsd-compat/port-linux.c.sftp-chroot openssh-5.9p0/openbsd-compat/port-linux.c
f7459a
--- openssh-5.9p0/openbsd-compat/port-linux.c.sftp-chroot	2011-09-01 04:12:22.743024608 +0200
f7459a
+++ openssh-5.9p0/openbsd-compat/port-linux.c	2011-09-01 04:12:23.069088065 +0200
f7459a
@@ -503,6 +503,23 @@ ssh_selinux_change_context(const char *n
f7459a
 	xfree(newctx);
f7459a
 }
f7459a
 
f7459a
+void
f7459a
+ssh_selinux_copy_context(void)
f7459a
+{
f7459a
+	char *ctx;
f7459a
+
f7459a
+	if (!ssh_selinux_enabled())
f7459a
+		return;
f7459a
+
f7459a
+	if (getexeccon((security_context_t *)&ctx) < 0) {
f7459a
+		logit("%s: getcon failed with %s", __func__, strerror (errno));
f7459a
+		return;
f7459a
+	}
f7459a
+	if (setcon(ctx) < 0)
f7459a
+		logit("%s: setcon failed with %s", __func__, strerror (errno));
f7459a
+	xfree(ctx);
f7459a
+}
f7459a
+
f7459a
 #endif /* WITH_SELINUX */
f7459a
 
f7459a
 #ifdef LINUX_OOM_ADJUST
f7459a
diff -up openssh-5.9p0/openbsd-compat/port-linux.h.sftp-chroot openssh-5.9p0/openbsd-compat/port-linux.h
f7459a
--- openssh-5.9p0/openbsd-compat/port-linux.h.sftp-chroot	2011-01-25 02:16:18.000000000 +0100
f7459a
+++ openssh-5.9p0/openbsd-compat/port-linux.h	2011-09-01 04:12:23.163088777 +0200
f7459a
@@ -24,6 +24,7 @@ int ssh_selinux_enabled(void);
f7459a
 void ssh_selinux_setup_pty(char *, const char *);
f7459a
 void ssh_selinux_setup_exec_context(char *);
f7459a
 void ssh_selinux_change_context(const char *);
f7459a
+void ssh_selinux_chopy_context(void);
f7459a
 void ssh_selinux_setfscreatecon(const char *);
f7459a
 #endif
f7459a
 
f7459a
diff -up openssh-5.9p0/session.c.sftp-chroot openssh-5.9p0/session.c
f7459a
--- openssh-5.9p0/session.c.sftp-chroot	2011-09-01 04:12:19.698049195 +0200
f7459a
+++ openssh-5.9p0/session.c	2011-09-01 04:40:03.598148719 +0200
f7459a
@@ -1519,6 +1519,9 @@ do_setusercontext(struct passwd *pw)
f7459a
 			    pw->pw_uid);
f7459a
 			chroot_path = percent_expand(tmp, "h", pw->pw_dir,
f7459a
 			    "u", pw->pw_name, (char *)NULL);
f7459a
+#ifdef WITH_SELINUX
f7459a
+			ssh_selinux_change_context("chroot_user_t");
f7459a
+#endif
f7459a
 			safely_chroot(chroot_path, pw->pw_uid);
f7459a
 			free(tmp);
f7459a
 			free(chroot_path);
f7459a
@@ -1788,7 +1791,10 @@ do_child(Session *s, const char *command
f7459a
 		optind = optreset = 1;
f7459a
 		__progname = argv[0];
f7459a
 #ifdef WITH_SELINUX
f7459a
-		ssh_selinux_change_context("sftpd_t");
f7459a
+		if (options.chroot_directory == NULL ||
f7459a
+		    strcasecmp(options.chroot_directory, "none") == 0) {
f7459a
+			ssh_selinux_copy_context();
f7459a
+		}
f7459a
 #endif
f7459a
 		exit(sftp_server_main(i, argv, s->pw));
f7459a
 	}