vishalmishra434 / rpms / openssh

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