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