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