kentpeacock / rpms / openssh

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