kentpeacock / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
017ff1
diff --git a/openbsd-compat/port-linux-sshd.c b/openbsd-compat/port-linux-sshd.c
017ff1
index c18524e..d04f4ed 100644
017ff1
--- a/openbsd-compat/port-linux-sshd.c
017ff1
+++ b/openbsd-compat/port-linux-sshd.c
017ff1
@@ -409,6 +409,25 @@ sshd_selinux_setup_exec_context(char *pwname)
017ff1
 	debug3("%s: done", __func__);
f09e2e
 }
f09e2e
 
f09e2e
+void
017ff1
+sshd_selinux_copy_context(void)
f09e2e
+{
f09e2e
+	security_context_t *ctx;
f09e2e
+
f09e2e
+	if (!ssh_selinux_enabled())
f09e2e
+		return;
f09e2e
+
f09e2e
+	if (getexeccon((security_context_t *)&ctx) != 0) {
f09e2e
+		logit("%s: getcon failed with %s", __func__, strerror (errno));
f09e2e
+		return;
f09e2e
+	}
f09e2e
+	if (ctx != NULL) {
f09e2e
+		if (setcon(ctx) != 0)
f09e2e
+			logit("%s: setcon failed with %s", __func__, strerror (errno));
f09e2e
+		freecon(ctx);
f09e2e
+	}
f09e2e
+}
f09e2e
+
017ff1
 #endif
017ff1
 #endif
f09e2e
 
017ff1
diff --git a/openbsd-compat/port-linux.h b/openbsd-compat/port-linux.h
017ff1
index 8ef6cc4..b18893c 100644
017ff1
--- a/openbsd-compat/port-linux.h
017ff1
+++ b/openbsd-compat/port-linux.h
017ff1
@@ -25,6 +25,7 @@ void ssh_selinux_setup_pty(char *, const char *);
f09e2e
 void ssh_selinux_change_context(const char *);
f09e2e
 void ssh_selinux_setfscreatecon(const char *);
017ff1
 
017ff1
+void sshd_selinux_copy_context(void);
017ff1
 void sshd_selinux_setup_exec_context(char *);
f09e2e
 #endif
f09e2e
 
017ff1
diff --git a/session.c b/session.c
017ff1
index 2bcf818..b5dc144 100644
017ff1
--- a/session.c
017ff1
+++ b/session.c
017ff1
@@ -1538,6 +1538,9 @@ do_setusercontext(struct passwd *pw)
f09e2e
 			    pw->pw_uid);
f09e2e
 			chroot_path = percent_expand(tmp, "h", pw->pw_dir,
f09e2e
 			    "u", pw->pw_name, (char *)NULL);
f09e2e
+#ifdef WITH_SELINUX
017ff1
+			sshd_selinux_copy_context();
f09e2e
+#endif
f09e2e
 			safely_chroot(chroot_path, pw->pw_uid);
f09e2e
 			free(tmp);
f09e2e
 			free(chroot_path);
017ff1
@@ -1565,6 +1568,12 @@ do_setusercontext(struct passwd *pw)
f09e2e
 		/* Permanently switch to the desired uid. */
f09e2e
 		permanently_set_uid(pw);
f09e2e
 #endif
f09e2e
+
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+		if (options.chroot_directory == NULL ||
f09e2e
+		    strcasecmp(options.chroot_directory, "none") == 0)
017ff1
+			sshd_selinux_copy_context();
f09e2e
+#endif
f09e2e
 	} else if (options.chroot_directory != NULL &&
f09e2e
 	    strcasecmp(options.chroot_directory, "none") != 0) {
f09e2e
 		fatal("server lacks privileges to chroot to ChrootDirectory");
017ff1
@@ -1826,9 +1835,6 @@ do_child(Session *s, const char *command)
f09e2e
 		argv[i] = NULL;
f09e2e
 		optind = optreset = 1;
f09e2e
 		__progname = argv[0];
f09e2e
-#ifdef WITH_SELINUX
f09e2e
-		ssh_selinux_change_context("sftpd_t");
f09e2e
-#endif
f09e2e
 		exit(sftp_server_main(i, argv, s->pw));
f09e2e
 	}
f09e2e
 
017ff1
diff --git a/sshd.c b/sshd.c
017ff1
index 07f9926..a97f8b7 100644
017ff1
--- a/sshd.c
017ff1
+++ b/sshd.c
017ff1
@@ -632,6 +632,10 @@ privsep_preauth_child(void)
f09e2e
 	/* Demote the private keys to public keys. */
f09e2e
 	demote_sensitive_data();
f09e2e
 
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+	ssh_selinux_change_context("sshd_net_t");
f09e2e
+#endif
f09e2e
+
f09e2e
 	/* Change our root directory */
f09e2e
 	if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
f09e2e
 		fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
017ff1
@@ -768,6 +772,13 @@ privsep_postauth(Authctxt *authctxt)
f09e2e
 	do_setusercontext(authctxt->pw);
f09e2e
 
f09e2e
  skip:
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+	/* switch SELinux content for root too */
f09e2e
+	if (authctxt->pw->pw_uid == 0) {
017ff1
+		sshd_selinux_copy_context();
f09e2e
+	}
f09e2e
+#endif
f09e2e
+
f09e2e
 	/* It is safe now to apply the key state */
f09e2e
 	monitor_apply_keystate(pmonitor);
f09e2e