kentpeacock / rpms / openssh

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