vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Petr Lautrbach afd52c
diff -up openssh-6.1p1/openbsd-compat/port-linux.c.privsep-selinux openssh-6.1p1/openbsd-compat/port-linux.c
bach dfa0cd
--- openssh-6.1p1/openbsd-compat/port-linux.c.privsep-selinux	2012-11-05 14:46:39.334809203 +0100
bach dfa0cd
+++ openssh-6.1p1/openbsd-compat/port-linux.c	2012-11-05 14:54:32.614504884 +0100
bach dfa0cd
@@ -505,6 +505,25 @@ ssh_selinux_change_context(const char *n
Petr Lautrbach afd52c
 	xfree(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 afd52c
diff -up openssh-6.1p1/openbsd-compat/port-linux.h.privsep-selinux openssh-6.1p1/openbsd-compat/port-linux.h
Petr Lautrbach afd52c
--- openssh-6.1p1/openbsd-compat/port-linux.h.privsep-selinux	2011-01-25 02:16:18.000000000 +0100
bach dfa0cd
+++ openssh-6.1p1/openbsd-compat/port-linux.h	2012-11-05 14:46:39.339809234 +0100
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 581bf3
diff -up openssh-6.1p1/session.c.privsep-selinux openssh-6.1p1/session.c
Petr Lautrbach f578f0
--- openssh-6.1p1/session.c.privsep-selinux	2012-12-03 09:43:11.727505761 +0100
Petr Lautrbach f578f0
+++ openssh-6.1p1/session.c	2012-12-03 09:54:50.455688902 +0100
Petr Lautrbach f578f0
@@ -1519,6 +1519,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 f578f0
@@ -1533,6 +1536,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 f578f0
 	}
Petr Lautrbach f578f0
 
Petr Lautrbach f578f0
 	if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
Petr Lautrbach f578f0
@@ -1787,9 +1796,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 581bf3
diff -up openssh-6.1p1/sshd.c.privsep-selinux openssh-6.1p1/sshd.c
bach dfa0cd
--- openssh-6.1p1/sshd.c.privsep-selinux	2012-11-05 14:46:39.335809209 +0100
bach dfa0cd
+++ openssh-6.1p1/sshd.c	2012-11-05 14:46:39.341809247 +0100
Petr Lautrbach 581bf3
@@ -794,6 +794,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