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
Petr Lautrbach 94c6f8
@@ -409,6 +409,25 @@ 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) {
bach dfa0cd
+		if (setcon(ctx) != 0)
bach dfa0cd
+			logit("%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);
Petr Lautrbach 94c6f8
@@ -1565,6 +1568,12 @@ 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
Petr Lautrbach f578f0
+		if (options.chroot_directory == NULL ||
Petr Lautrbach f578f0
+		    strcasecmp(options.chroot_directory, "none") == 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");
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
+
Petr Lautrbach 338e71
 	/* Change our root directory */
Petr Lautrbach 338e71
 	if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
Petr Lautrbach 338e71
 		fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
Petr Lautrbach 94c6f8
@@ -768,6 +772,13 @@ privsep_postauth(Authctxt *authctxt)
Petr Lautrbach 0c438f
 	do_setusercontext(authctxt->pw);
Petr Lautrbach 0c438f
 
Petr Lautrbach 0c438f
  skip:
Petr Lautrbach cd5891
+#ifdef WITH_SELINUX
Petr Lautrbach 0c438f
+	/* switch SELinux content for root too */
Petr Lautrbach 581bf3
+	if (authctxt->pw->pw_uid == 0) {
Petr Lautrbach 94c6f8
+		sshd_selinux_copy_context();
Petr Lautrbach cd5891
+	}
Petr Lautrbach cd5891
+#endif
Petr Lautrbach cd5891
+
Petr Lautrbach 0c438f
 	/* It is safe now to apply the key state */
Petr Lautrbach 0c438f
 	monitor_apply_keystate(pmonitor);
Petr Lautrbach 0c438f