rcolebaugh / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone

Blame SOURCES/openssh-6.6p1-privsep-selinux.patch

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