File openssh-6.6p1-privsep-selinux.patch of Package openssh-askpass-gnome

Index: openssh-8.4p1/openbsd-compat/port-linux.h
===================================================================
--- openssh-8.4p1.orig/openbsd-compat/port-linux.h
+++ openssh-8.4p1/openbsd-compat/port-linux.h
@@ -23,6 +23,7 @@ void ssh_selinux_setup_pty(char *, const
 void ssh_selinux_change_context(const char *);
 void ssh_selinux_setfscreatecon(const char *);
 
+void sshd_selinux_copy_context(void);
 void sshd_selinux_setup_exec_context(char *);
 #endif
 
Index: openssh-8.4p1/openbsd-compat/port-linux-sshd.c
===================================================================
--- openssh-8.4p1.orig/openbsd-compat/port-linux-sshd.c
+++ openssh-8.4p1/openbsd-compat/port-linux-sshd.c
@@ -420,6 +420,28 @@ sshd_selinux_setup_exec_context(char *pw
 	debug3("%s: done", __func__);
 }
 
+void
+sshd_selinux_copy_context(void)
+{
+	security_context_t *ctx;
+
+	if (!ssh_selinux_enabled())
+		return;
+
+	if (getexeccon((security_context_t *)&ctx) != 0) {
+		logit("%s: getexeccon failed with %s", __func__, strerror(errno));
+		return;
+	}
+	if (ctx != NULL) {
+		/* unset exec context before we will lose this capabililty */
+		if (setexeccon(NULL) != 0)
+			fatal("%s: setexeccon failed with %s", __func__, strerror(errno));
+		if (setcon(ctx) != 0)
+			fatal("%s: setcon failed with %s", __func__, strerror(errno));
+		freecon(ctx);
+	}
+}
+
 #endif
 #endif
 
Index: openssh-8.4p1/session.c
===================================================================
--- openssh-8.4p1.orig/session.c
+++ openssh-8.4p1/session.c
@@ -1412,7 +1412,7 @@ do_setusercontext(struct passwd *pw)
 
 	platform_setusercontext(pw);
 
-	if (platform_privileged_uidswap()) {
+	if (platform_privileged_uidswap() && (!is_child || !use_privsep)) {
 #ifdef HAVE_LOGIN_CAP
 		if (setusercontext(lc, pw, pw->pw_uid,
 		    (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETUSER))) < 0) {
@@ -1444,6 +1444,9 @@ do_setusercontext(struct passwd *pw)
 			    (unsigned long long)pw->pw_uid);
 			chroot_path = percent_expand(tmp, "h", pw->pw_dir,
 			    "u", pw->pw_name, "U", uidstr, (char *)NULL);
+#ifdef WITH_SELINUX
+			sshd_selinux_copy_context();
+#endif
 			safely_chroot(chroot_path, pw->pw_uid);
 			free(tmp);
 			free(chroot_path);
@@ -1479,6 +1482,11 @@ do_setusercontext(struct passwd *pw)
 		/* Permanently switch to the desired uid. */
 		permanently_set_uid(pw);
 #endif
+
+#ifdef WITH_SELINUX
+		if (in_chroot == 0)
+			sshd_selinux_copy_context();
+#endif
 	} else if (options.chroot_directory != NULL &&
 	    strcasecmp(options.chroot_directory, "none") != 0) {
 		fatal("server lacks privileges to chroot to ChrootDirectory");
@@ -1496,9 +1504,6 @@ do_pwchange(Session *s)
 	if (s->ttyfd != -1) {
 		fprintf(stderr,
 		    "You must change your password now and login again!\n");
-#ifdef WITH_SELINUX
-		setexeccon(NULL);
-#endif
 #ifdef PASSWD_NEEDS_USERNAME
 		execl(_PATH_PASSWD_PROG, "passwd", s->pw->pw_name,
 		    (char *)NULL);
@@ -1732,9 +1737,6 @@ do_child(struct ssh *ssh, Session *s, co
 		argv[i] = NULL;
 		optind = optreset = 1;
 		__progname = argv[0];
-#ifdef WITH_SELINUX
-		ssh_selinux_change_context("sftpd_t");
-#endif
 		exit(sftp_server_main(i, argv, s->pw));
 	}
 
Index: openssh-8.4p1/sshd.c
===================================================================
--- openssh-8.4p1.orig/sshd.c
+++ openssh-8.4p1/sshd.c
@@ -539,6 +539,10 @@ privsep_preauth_child(struct ssh *ssh)
 	/* Demote the private keys to public keys. */
 	demote_sensitive_data(ssh);
 
+#ifdef WITH_SELINUX
+	ssh_selinux_change_context("sshd_net_t");
+#endif
+
 	/* Demote the child */
 	if (privsep_chroot) {
 		/* Change our root directory */
@@ -634,6 +638,9 @@ privsep_postauth(struct ssh *ssh, Authct
 
 #ifdef DISABLE_FD_PASSING
 	if (1) {
+#elif defined(WITH_SELINUX)
+	if (0) {
+		/* even root user can be confined by SELinux */
 #else
 	if (authctxt->pw->pw_uid == 0) {
 #endif
openSUSE Build Service is sponsored by