kentpeacock / rpms / openssh

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