rcolebaugh / rpms / openssh

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