jonathancammack / rpms / openssh

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