bmh10 / rpms / openssh

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