mgahagan / rpms / openssh

Forked from rpms/openssh 6 years ago
Clone
94c6f8d
diff --git a/openbsd-compat/port-linux-sshd.c b/openbsd-compat/port-linux-sshd.c
94c6f8d
index c18524e..d04f4ed 100644
94c6f8d
--- a/openbsd-compat/port-linux-sshd.c
94c6f8d
+++ b/openbsd-compat/port-linux-sshd.c
22a08c3
@@ -409,6 +409,28 @@ sshd_selinux_setup_exec_context(char *pwname)
94c6f8d
 	debug3("%s: done", __func__);
afd52c4
 }
afd52c4
 
afd52c4
+void
94c6f8d
+sshd_selinux_copy_context(void)
afd52c4
+{
bach dfa0cd5
+	security_context_t *ctx;
afd52c4
+
afd52c4
+	if (!ssh_selinux_enabled())
afd52c4
+		return;
afd52c4
+
afd52c4
+	if (getexeccon((security_context_t *)&ctx) != 0) {
afd52c4
+		logit("%s: getcon failed with %s", __func__, strerror (errno));
afd52c4
+		return;
afd52c4
+	}
bach dfa0cd5
+	if (ctx != NULL) {
22a08c3
+		/* unset exec context before we will lose this capabililty */
22a08c3
+		if (setexeccon(NULL) != 0)
22a08c3
+			fatal("%s: setexeccon failed with %s", __func__, strerror (errno));
bach dfa0cd5
+		if (setcon(ctx) != 0)
22a08c3
+			fatal("%s: setcon failed with %s", __func__, strerror (errno));
bach dfa0cd5
+		freecon(ctx);
bach dfa0cd5
+	}
afd52c4
+}
afd52c4
+
94c6f8d
 #endif
94c6f8d
 #endif
afd52c4
 
94c6f8d
diff --git a/openbsd-compat/port-linux.h b/openbsd-compat/port-linux.h
94c6f8d
index 8ef6cc4..b18893c 100644
94c6f8d
--- a/openbsd-compat/port-linux.h
94c6f8d
+++ b/openbsd-compat/port-linux.h
94c6f8d
@@ -25,6 +25,7 @@ void ssh_selinux_setup_pty(char *, const char *);
afd52c4
 void ssh_selinux_change_context(const char *);
afd52c4
 void ssh_selinux_setfscreatecon(const char *);
94c6f8d
 
94c6f8d
+void sshd_selinux_copy_context(void);
94c6f8d
 void sshd_selinux_setup_exec_context(char *);
afd52c4
 #endif
afd52c4
 
94c6f8d
diff --git a/session.c b/session.c
94c6f8d
index 2bcf818..b5dc144 100644
94c6f8d
--- a/session.c
94c6f8d
+++ b/session.c
94c6f8d
@@ -1538,6 +1538,9 @@ do_setusercontext(struct passwd *pw)
f578f0a
 			    pw->pw_uid);
f578f0a
 			chroot_path = percent_expand(tmp, "h", pw->pw_dir,
f578f0a
 			    "u", pw->pw_name, (char *)NULL);
f578f0a
+#ifdef WITH_SELINUX
94c6f8d
+			sshd_selinux_copy_context();
f578f0a
+#endif
f578f0a
 			safely_chroot(chroot_path, pw->pw_uid);
f578f0a
 			free(tmp);
f578f0a
 			free(chroot_path);
22a08c3
@@ -1565,6 +1568,11 @@ do_setusercontext(struct passwd *pw)
f578f0a
 		/* Permanently switch to the desired uid. */
f578f0a
 		permanently_set_uid(pw);
f578f0a
 #endif
581bf30
+
0c438f5
+#ifdef WITH_SELINUX
13073f8
+		if (in_chroot == 0)
94c6f8d
+			sshd_selinux_copy_context();
0c438f5
+#endif
84822b5
 	} else if (options.chroot_directory != NULL &&
84822b5
 	    strcasecmp(options.chroot_directory, "none") != 0) {
84822b5
 		fatal("server lacks privileges to chroot to ChrootDirectory");
22a08c3
@@ -1588,9 +1588,6 @@ do_pwchange(Session *s)
22a08c3
 	if (s->ttyfd != -1) {
22a08c3
 		fprintf(stderr,
22a08c3
 		    "You must change your password now and login again!\n");
22a08c3
-#ifdef WITH_SELINUX
22a08c3
-		setexeccon(NULL);
22a08c3
-#endif
22a08c3
 #ifdef PASSWD_NEEDS_USERNAME
22a08c3
 		execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
22a08c3
 		    (char *)NULL);
94c6f8d
@@ -1826,9 +1835,6 @@ do_child(Session *s, const char *command)
afd52c4
 		argv[i] = NULL;
afd52c4
 		optind = optreset = 1;
afd52c4
 		__progname = argv[0];
afd52c4
-#ifdef WITH_SELINUX
afd52c4
-		ssh_selinux_change_context("sftpd_t");
afd52c4
-#endif
afd52c4
 		exit(sftp_server_main(i, argv, s->pw));
cd5891d
 	}
cd5891d
 
94c6f8d
diff --git a/sshd.c b/sshd.c
94c6f8d
index 07f9926..a97f8b7 100644
94c6f8d
--- a/sshd.c
94c6f8d
+++ b/sshd.c
94c6f8d
@@ -632,6 +632,10 @@ privsep_preauth_child(void)
338e719
 	/* Demote the private keys to public keys. */
338e719
 	demote_sensitive_data();
338e719
 
338e719
+#ifdef WITH_SELINUX
338e719
+	ssh_selinux_change_context("sshd_net_t");
338e719
+#endif
338e719
+
13073f8
 	/* Demote the child */
13073f8
 	if (getuid() == 0 || geteuid() == 0) {
13073f8
 		/* Change our root directory */
0ebe96b
@@ -755,6 +755,9 @@ privsep_postauth(Authctxt *authctxt)
0c438f5
 
0ebe96b
 #ifdef DISABLE_FD_PASSING
0ebe96b
 	if (1) {
0ebe96b
+#elif defined(WITH_SELINUX)
0ebe96b
+	if (options.use_login) {
0ebe96b
+		/* even root user can be confined by SELinux */
0ebe96b
 #else
0ebe96b
 	if (authctxt->pw->pw_uid == 0 || options.use_login) {
0ebe96b
 #endif
b6d4dc0
diff --git a/session.c b/session.c
b6d4dc0
index 684f867..09048bc 100644
b6d4dc0
--- a/session.c
b6d4dc0
+++ b/session.c
b6d4dc0
@@ -1538,7 +1538,7 @@ do_setusercontext(struct passwd *pw)
b6d4dc0
 
b6d4dc0
 	platform_setusercontext(pw);
b6d4dc0
 
b6d4dc0
-	if (platform_privileged_uidswap()) {
4fdc3c5
+	if (platform_privileged_uidswap() && (!is_child || !use_privsep)) {
b6d4dc0
 #ifdef HAVE_LOGIN_CAP
b6d4dc0
 		if (setusercontext(lc, pw, pw->pw_uid,
b6d4dc0
 		    (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {