vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Petr Lautrbach 84822b
diff -up openssh-6.3p1/openbsd-compat/port-linux.c.privsep-selinux openssh-6.3p1/openbsd-compat/port-linux.c
Petr Lautrbach 84822b
--- openssh-6.3p1/openbsd-compat/port-linux.c.privsep-selinux	2013-10-10 14:58:20.634762245 +0200
Petr Lautrbach 84822b
+++ openssh-6.3p1/openbsd-compat/port-linux.c	2013-10-10 15:13:57.864306950 +0200
Petr Lautrbach 84822b
@@ -503,6 +503,25 @@ ssh_selinux_change_context(const char *n
Petr Lautrbach 84822b
 	free(newctx);
Petr Lautrbach afd52c
 }
Petr Lautrbach afd52c
 
Petr Lautrbach afd52c
+void
Petr Lautrbach afd52c
+ssh_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 afd52c
 #endif /* WITH_SELINUX */
Petr Lautrbach afd52c
 
Petr Lautrbach afd52c
 #ifdef LINUX_OOM_ADJUST
Petr Lautrbach 84822b
diff -up openssh-6.3p1/openbsd-compat/port-linux.h.privsep-selinux openssh-6.3p1/openbsd-compat/port-linux.h
Petr Lautrbach 84822b
--- openssh-6.3p1/openbsd-compat/port-linux.h.privsep-selinux	2011-01-25 02:16:18.000000000 +0100
Petr Lautrbach 84822b
+++ openssh-6.3p1/openbsd-compat/port-linux.h	2013-10-10 14:58:20.634762245 +0200
Petr Lautrbach afd52c
@@ -24,6 +24,7 @@ int ssh_selinux_enabled(void);
Petr Lautrbach afd52c
 void ssh_selinux_setup_pty(char *, const char *);
Petr Lautrbach afd52c
 void ssh_selinux_setup_exec_context(char *);
Petr Lautrbach afd52c
 void ssh_selinux_change_context(const char *);
Petr Lautrbach afd52c
+void ssh_selinux_copy_context(void);
Petr Lautrbach afd52c
 void ssh_selinux_setfscreatecon(const char *);
Petr Lautrbach afd52c
 #endif
Petr Lautrbach afd52c
 
Petr Lautrbach 84822b
diff -up openssh-6.3p1/session.c.privsep-selinux openssh-6.3p1/session.c
Petr Lautrbach 84822b
--- openssh-6.3p1/session.c.privsep-selinux	2013-10-10 14:58:20.617762326 +0200
Petr Lautrbach 84822b
+++ openssh-6.3p1/session.c	2013-10-10 15:13:16.520503590 +0200
Petr Lautrbach 84822b
@@ -1522,6 +1522,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 f578f0
+			ssh_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 84822b
@@ -1544,6 +1547,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 f578f0
+			ssh_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 84822b
@@ -1808,9 +1817,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 84822b
diff -up openssh-6.3p1/sshd.c.privsep-selinux openssh-6.3p1/sshd.c
Petr Lautrbach 84822b
--- openssh-6.3p1/sshd.c.privsep-selinux	2013-10-10 14:58:20.632762255 +0200
Petr Lautrbach 84822b
+++ openssh-6.3p1/sshd.c	2013-10-10 14:58:20.635762241 +0200
Petr Lautrbach 84822b
@@ -668,6 +668,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 84822b
@@ -811,6 +815,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 cd5891
+		ssh_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