9070b3
diff -up openssh/auth2-pubkey.c.refactor openssh/auth2-pubkey.c
9070b3
--- openssh/auth2-pubkey.c.refactor	2019-04-04 13:19:12.188821236 +0200
9070b3
+++ openssh/auth2-pubkey.c	2019-04-04 13:19:12.276822078 +0200
9070b3
@@ -72,6 +72,9 @@
9070b3
 
9070b3
 /* import */
9070b3
 extern ServerOptions options;
9070b3
+extern int inetd_flag;
9070b3
+extern int rexeced_flag;
9070b3
+extern Authctxt *the_authctxt;
9070b3
 
9070b3
 static char *
9070b3
 format_key(const struct sshkey *key)
9070b3
@@ -511,7 +514,8 @@ match_principals_command(struct ssh *ssh
9070b3
 	if ((pid = subprocess("AuthorizedPrincipalsCommand", command,
9070b3
 	    ac, av, &f,
9070b3
 	    SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD,
9070b3
-	    runas_pw, temporarily_use_uid, restore_uid)) == 0)
9070b3
+	    runas_pw, temporarily_use_uid, restore_uid,
9070b3
+	    (inetd_flag && !rexeced_flag), the_authctxt)) == 0)
9070b3
 		goto out;
9070b3
 
9070b3
 	uid_swapped = 1;
9070b3
@@ -981,7 +985,8 @@ user_key_command_allowed2(struct ssh *ss
9070b3
 	if ((pid = subprocess("AuthorizedKeysCommand", command,
9070b3
 	    ac, av, &f,
9070b3
	    SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD,
9070b3
-	    runas_pw, temporarily_use_uid, restore_uid)) == 0)
9070b3
+	    runas_pw, temporarily_use_uid, restore_uid,
9070b3
+	    (inetd_flag && !rexeced_flag), the_authctxt)) == 0)
9070b3
 		goto out;
9070b3
 
9070b3
 	uid_swapped = 1;
9070b3
diff -up openssh/misc.c.refactor openssh/misc.c
9070b3
--- openssh/misc.c.refactor	2019-04-04 13:19:12.235821686 +0200
9070b3
+++ openssh/misc.c	2019-04-04 13:19:12.276822078 +0200
9070b3
@@ -756,7 +756,8 @@ auth_get_canonical_hostname(struct ssh *
9070b3
 pid_t
9070b3
 subprocess(const char *tag, const char *command,
9070b3
     int ac, char **av, FILE **child, u_int flags,
9070b3
-    struct passwd *pw, privdrop_fn *drop_privs, privrestore_fn *restore_privs)
9070b3
+    struct passwd *pw, privdrop_fn *drop_privs,
9070b3
+    privrestore_fn *restore_privs, int inetd, void *the_authctxt)
9070b3
 {
9070b3
 	FILE *f = NULL;
9070b3
 	struct stat st;
9070b3
@@ -872,7 +873,7 @@ subprocess(const char *tag, struct passw
9070b3
 			_exit(1);
9070b3
 		}
9070b3
 #ifdef WITH_SELINUX
9070b3
-		if (sshd_selinux_setup_env_variables() < 0) {
9070b3
+		if (sshd_selinux_setup_env_variables(inetd, the_authctxt) < 0) {
9070b3
 			error ("failed to copy environment:  %s",
9070b3
 			    strerror(errno));
9070b3
 			_exit(127);
9070b3
diff -up openssh/misc.h.refactor openssh/misc.h
9070b3
--- openssh/misc.h.refactor	2019-04-04 13:19:12.251821839 +0200
9070b3
+++ openssh/misc.h	2019-04-04 13:19:12.276822078 +0200
9070b3
@@ -235,7 +235,7 @@ struct passwd *fakepw(void);
9070b3
 #define	SSH_SUBPROCESS_UNSAFE_PATH	(1<<3)	/* Don't check for safe cmd */
9070b3
 #define	SSH_SUBPROCESS_PRESERVE_ENV	(1<<4)	/* Keep parent environment */
9070b3
 pid_t subprocess(const char *, const char *, int, char **, FILE **, u_int,
9070b3
-    struct passwd *, privdrop_fn *, privrestore_fn *);
9070b3
+    struct passwd *, privdrop_fn *, privrestore_fn *, int, void *);
9070b3
 
9070b3
 typedef struct arglist arglist;
9070b3
 struct arglist {
9070b3
diff -up openssh/openbsd-compat/port-linux.h.refactor openssh/openbsd-compat/port-linux.h
9070b3
--- openssh/openbsd-compat/port-linux.h.refactor	2019-04-04 13:19:12.256821887 +0200
9070b3
+++ openssh/openbsd-compat/port-linux.h	2019-04-04 13:19:12.276822078 +0200
9070b3
@@ -26,8 +26,8 @@ void ssh_selinux_setfscreatecon(const ch
9070b3
 
9070b3
 int sshd_selinux_enabled(void);
9070b3
 void sshd_selinux_copy_context(void);
9070b3
-void sshd_selinux_setup_exec_context(char *);
9070b3
-int sshd_selinux_setup_env_variables(void);
9070b3
+void sshd_selinux_setup_exec_context(char *, int, int(char *, const char *), void *, int);
9070b3
+int sshd_selinux_setup_env_variables(int inetd, void *);
9070b3
 void sshd_selinux_change_privsep_preauth_context(void);
9070b3
 #endif
9070b3
 
9070b3
diff -up openssh/openbsd-compat/port-linux-sshd.c.refactor openssh/openbsd-compat/port-linux-sshd.c
9070b3
--- openssh/openbsd-compat/port-linux-sshd.c.refactor	2019-04-04 13:19:12.256821887 +0200
9070b3
+++ openssh/openbsd-compat/port-linux-sshd.c	2019-04-04 13:19:12.276822078 +0200
9070b3
@@ -49,11 +49,6 @@
9070b3
 #include <unistd.h>
9070b3
 #endif
9070b3
 
9070b3
-extern ServerOptions options;
9070b3
-extern Authctxt *the_authctxt;
9070b3
-extern int inetd_flag;
9070b3
-extern int rexeced_flag;
9070b3
-
9070b3
 /* Wrapper around is_selinux_enabled() to log its return value once only */
9070b3
 int
9070b3
 sshd_selinux_enabled(void)
9070b3
@@ -223,7 +218,8 @@ get_user_context(const char *sename, con
9070b3
 }
9070b3
 
9070b3
 static void
9070b3
-ssh_selinux_get_role_level(char **role, const char **level)
9070b3
+ssh_selinux_get_role_level(char **role, const char **level,
9070b3
+    Authctxt *the_authctxt)
9070b3
 {
9070b3
 	*role = NULL;
9070b3
 	*level = NULL;
9070b3
@@ -241,8 +237,8 @@ ssh_selinux_get_role_level(char **role,
9070b3
 
9070b3
 /* Return the default security context for the given username */
9070b3
 static int
9070b3
-sshd_selinux_getctxbyname(char *pwname,
9070b3
-	security_context_t *default_sc, security_context_t *user_sc)
9070b3
+sshd_selinux_getctxbyname(char *pwname, security_context_t *default_sc,
9070b3
+    security_context_t *user_sc, int inetd, Authctxt *the_authctxt)
9070b3
 {
9070b3
 	char *sename, *lvl;
9070b3
 	char *role;
9070b3
@@ -250,7 +246,7 @@ sshd_selinux_getctxbyname(char *pwname,
9070b3
 	int r = 0;
9070b3
 	context_t con = NULL;
9070b3
 
9070b3
-	ssh_selinux_get_role_level(&role, &reqlvl);
9070b3
+	ssh_selinux_get_role_level(&role, &reqlvl, the_authctxt);
9070b3
 
9070b3
 #ifdef HAVE_GETSEUSERBYNAME
9070b3
 	if ((r=getseuserbyname(pwname, &sename, &lvl)) != 0) {
9070b3
@@ -272,7 +268,7 @@ sshd_selinux_getctxbyname(char *pwname,
9070b3
 
9070b3
 	if (r == 0) {
9070b3
 		/* If launched from xinetd, we must use current level */
9070b3
-		if (inetd_flag && !rexeced_flag) {
9070b3
+		if (inetd) {
9070b3
 			security_context_t sshdsc=NULL;
9070b3
 
9070b3
 			if (getcon_raw(&sshdsc) < 0)
9070b3
@@ -333,7 +329,8 @@ sshd_selinux_getctxbyname(char *pwname,
9070b3
 
9070b3
 /* Setup environment variables for pam_selinux */
9070b3
 static int
9070b3
-sshd_selinux_setup_variables(int(*set_it)(char *, const char *))
9070b3
+sshd_selinux_setup_variables(int(*set_it)(char *, const char *), int inetd,
9070b3
+    Authctxt *the_authctxt)
9070b3
 {
9070b3
 	const char *reqlvl;
9070b3
 	char *role;
9070b3
@@ -342,11 +339,11 @@ sshd_selinux_setup_variables(int(*set_it
9070b3
 
9070b3
 	debug3_f("setting execution context");
9070b3
 
9070b3
-	ssh_selinux_get_role_level(&role, &reqlvl);
9070b3
+	ssh_selinux_get_role_level(&role, &reqlvl, the_authctxt);
9070b3
 
9070b3
 	rv = set_it("SELINUX_ROLE_REQUESTED", role ? role : "");
9070b3
 
9070b3
-	if (inetd_flag && !rexeced_flag) {
9070b3
+	if (inetd) {
9070b3
 		use_current = "1";
9070b3
 	} else {
9070b3
 		use_current = "";
9070b3
@@ -362,9 +359,10 @@ sshd_selinux_setup_variables(int(*set_it
9070b3
 }
9070b3
 
9070b3
 static int
9070b3
-sshd_selinux_setup_pam_variables(void)
9070b3
+sshd_selinux_setup_pam_variables(int inetd,
9070b3
+    int(pam_setenv)(char *, const char *), Authctxt *the_authctxt)
9070b3
 {
9070b3
-	return sshd_selinux_setup_variables(do_pam_putenv);
9070b3
+	return sshd_selinux_setup_variables(pam_setenv, inetd, the_authctxt);
9070b3
 }
9070b3
 
9070b3
 static int
9070b3
@@ -374,25 +372,28 @@ do_setenv(char *name, const char *value)
9070b3
 }
9070b3
 
9070b3
 int
9070b3
-sshd_selinux_setup_env_variables(void)
9070b3
+sshd_selinux_setup_env_variables(int inetd, void *the_authctxt)
9070b3
 {
9070b3
-	return sshd_selinux_setup_variables(do_setenv);
9070b3
+	Authctxt *authctxt = (Authctxt *) the_authctxt;
9070b3
+	return sshd_selinux_setup_variables(do_setenv, inetd, authctxt);
9070b3
 }
9070b3
 
9070b3
 /* Set the execution context to the default for the specified user */
9070b3
 void
9070b3
-sshd_selinux_setup_exec_context(char *pwname)
9070b3
+sshd_selinux_setup_exec_context(char *pwname, int inetd,
9070b3
+    int(pam_setenv)(char *, const char *), void *the_authctxt, int use_pam)
9070b3
 {
9070b3
 	security_context_t user_ctx = NULL;
9070b3
 	int r = 0;
9070b3
 	security_context_t default_ctx = NULL;
9070b3
+	Authctxt *authctxt = (Authctxt *) the_authctxt;
9070b3
 
9070b3
 	if (!sshd_selinux_enabled())
9070b3
 		return;
9070b3
 
9070b3
-	if (options.use_pam) {
9070b3
+	if (use_pam) {
9070b3
 		/* do not compute context, just setup environment for pam_selinux */
9070b3
-		if (sshd_selinux_setup_pam_variables()) {
9070b3
+		if (sshd_selinux_setup_pam_variables(inetd, pam_setenv, authctxt)) {
9070b3
 			switch (security_getenforce()) {
9070b3
 			case -1:
9070b3
 				fatal_f("security_getenforce() failed");
9070b3
@@ -410,7 +411,7 @@ sshd_selinux_setup_exec_context(char *pw
9070b3
 
9070b3
 	debug3_f("setting execution context");
9070b3
 
9070b3
-	r = sshd_selinux_getctxbyname(pwname, &default_ctx, &user_ctx);
9070b3
+	r = sshd_selinux_getctxbyname(pwname, &default_ctx, &user_ctx, inetd, authctxt);
9070b3
 	if (r >= 0) {
9070b3
 		r = setexeccon(user_ctx);
9070b3
 		if (r < 0) {
9070b3
diff -up openssh/platform.c.refactor openssh/platform.c
9070b3
--- openssh/platform.c.refactor	2019-04-04 13:19:12.204821389 +0200
9070b3
+++ openssh/platform.c	2019-04-04 13:19:12.277822088 +0200
9070b3
@@ -32,6 +32,9 @@
9070b3
 
9070b3
 extern int use_privsep;
9070b3
 extern ServerOptions options;
9070b3
+extern int inetd_flag;
9070b3
+extern int rexeced_flag;
9070b3
+extern Authctxt *the_authctxt;
9070b3
 
9070b3
 void
9070b3
 platform_pre_listen(void)
9070b3
@@ -183,7 +186,9 @@ platform_setusercontext_post_groups(stru
9070b3
 	}
9070b3
 #endif /* HAVE_SETPCRED */
9070b3
 #ifdef WITH_SELINUX
9070b3
-	sshd_selinux_setup_exec_context(pw->pw_name);
9070b3
+	sshd_selinux_setup_exec_context(pw->pw_name,
9070b3
+	    (inetd_flag && !rexeced_flag), do_pam_putenv, the_authctxt,
9070b3
+	    options.use_pam);
9070b3
 #endif
9070b3
 }
9070b3
 
9070b3
diff -up openssh/sshd.c.refactor openssh/sshd.c
9070b3
--- openssh/sshd.c.refactor	2019-04-04 13:19:12.275822068 +0200
9070b3
+++ openssh/sshd.c	2019-04-04 13:19:51.270195262 +0200
9070b3
@@ -158,7 +158,7 @@ int debug_flag = 0;
9070b3
 static int test_flag = 0;
9070b3
 
9070b3
 /* Flag indicating that the daemon is being started from inetd. */
9070b3
-static int inetd_flag = 0;
9070b3
+int inetd_flag = 0;
9070b3
 
9070b3
 /* Flag indicating that sshd should not detach and become a daemon. */
9070b3
 static int no_daemon_flag = 0;
9070b3
@@ -171,7 +171,7 @@ static char **saved_argv;
9070b3
 static int saved_argc;
9070b3
 
9070b3
 /* re-exec */
9070b3
-static int rexeced_flag = 0;
9070b3
+int rexeced_flag = 0;
9070b3
 static int rexec_flag = 1;
9070b3
 static int rexec_argc = 0;
9070b3
 static char **rexec_argv;
9070b3
@@ -2192,7 +2192,9 @@ main(int ac, char **av)
9070b3
 	}
9070b3
 #endif
9070b3
 #ifdef WITH_SELINUX
9070b3
-	sshd_selinux_setup_exec_context(authctxt->pw->pw_name);
9070b3
+	sshd_selinux_setup_exec_context(authctxt->pw->pw_name,
9070b3
+	    (inetd_flag && !rexeced_flag), do_pam_putenv, the_authctxt,
9070b3
+	    options.use_pam);
9070b3
 #endif
9070b3
 #ifdef USE_PAM
9070b3
 	if (options.use_pam) {
9070b3
diff -up openssh/sshconnect.c.refactor openssh/sshconnect.c
9070b3
--- openssh/sshconnect.c.refactor	2021-02-24 00:12:03.065325046 +0100
9070b3
+++ openssh/sshconnect.c	2021-02-24 00:12:12.126449544 +0100
9070b3
@@ -892,7 +892,7 @@ load_hostkeys_command(struct hostkeys *h
9070b3
 
9070b3
 	if ((pid = subprocess(tag, command, ac, av, &f,
9070b3
 	    SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_UNSAFE_PATH|
9070b3
-	    SSH_SUBPROCESS_PRESERVE_ENV, NULL, NULL, NULL)) == 0)
9070b3
+	    SSH_SUBPROCESS_PRESERVE_ENV, NULL, NULL, NULL, 0, NULL)) == 0)
9070b3
 		goto out;
9070b3
 
9070b3
 	load_hostkeys_file(hostkeys, hostfile_hostname, tag, f, 1);