rcolebaugh / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
9070b3
diff -up openssh/auth2.c.role-mls openssh/auth2.c
9070b3
--- openssh/auth2.c.role-mls	2018-08-20 07:57:29.000000000 +0200
9070b3
+++ openssh/auth2.c	2018-08-22 11:14:56.815430916 +0200
9070b3
@@ -256,6 +256,9 @@ input_userauth_request(int type, u_int32
9070b3
 	Authctxt *authctxt = ssh->authctxt;
9070b3
 	Authmethod *m = NULL;
9070b3
 	char *user = NULL, *service = NULL, *method = NULL, *style = NULL;
9070b3
+#ifdef WITH_SELINUX
9070b3
+	char *role = NULL;
9070b3
+#endif
9070b3
 	int r, authenticated = 0;
9070b3
 	double tstart = monotime_double();
9070b3
 
9070b3
@@ -268,6 +271,11 @@ input_userauth_request(int type, u_int32
9070b3
 	debug("userauth-request for user %s service %s method %s", user, service, method);
9070b3
 	debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
9070b3
 
9070b3
+#ifdef WITH_SELINUX
9070b3
+	if ((role = strchr(user, '/')) != NULL)
9070b3
+		*role++ = 0;
9070b3
+#endif
9070b3
+
9070b3
 	if ((style = strchr(user, ':')) != NULL)
9070b3
 		*style++ = 0;
9070b3
 
9070b3
@@ -296,8 +304,15 @@ input_userauth_request(int type, u_int32
9070b3
 		    use_privsep ? " [net]" : "");
9070b3
 		authctxt->service = xstrdup(service);
9070b3
 		authctxt->style = style ? xstrdup(style) : NULL;
9070b3
-		if (use_privsep)
9070b3
+#ifdef WITH_SELINUX
9070b3
+		authctxt->role = role ? xstrdup(role) : NULL;
9070b3
+#endif
9070b3
+		if (use_privsep) {
9070b3
 			mm_inform_authserv(service, style);
9070b3
+#ifdef WITH_SELINUX
9070b3
+			mm_inform_authrole(role);
9070b3
+#endif
9070b3
+		}
9070b3
 		userauth_banner(ssh);
9070b3
 		if (auth2_setup_methods_lists(authctxt) != 0)
9070b3
 			ssh_packet_disconnect(ssh,
9070b3
diff -up openssh/auth2-gss.c.role-mls openssh/auth2-gss.c
9070b3
--- openssh/auth2-gss.c.role-mls	2018-08-20 07:57:29.000000000 +0200
9070b3
+++ openssh/auth2-gss.c	2018-08-22 11:15:42.459799171 +0200
9070b3
@@ -281,6 +281,7 @@ input_gssapi_mic(int type, u_int32_t ple
9070b3
 	Authctxt *authctxt = ssh->authctxt;
9070b3
 	Gssctxt *gssctxt;
9070b3
 	int r, authenticated = 0;
9070b3
+	char *micuser;
9070b3
 	struct sshbuf *b;
9070b3
 	gss_buffer_desc mic, gssbuf;
9070b3
 	const char *displayname;
9070b3
@@ -298,7 +299,13 @@ input_gssapi_mic(int type, u_int32_t ple
9070b3
 		fatal_f("sshbuf_new failed");
9070b3
 	mic.value = p;
9070b3
 	mic.length = len;
9070b3
-	ssh_gssapi_buildmic(b, authctxt->user, authctxt->service,
9070b3
+#ifdef WITH_SELINUX
9070b3
+	if (authctxt->role && authctxt->role[0] != 0)
9070b3
+		xasprintf(&micuser, "%s/%s", authctxt->user, authctxt->role);
9070b3
+	else
9070b3
+#endif
9070b3
+		micuser = authctxt->user;
9070b3
+	ssh_gssapi_buildmic(b, micuser, authctxt->service,
9070b3
 	    "gssapi-with-mic", ssh->kex->session_id);
9070b3
 
9070b3
 	if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
9070b3
@@ -311,6 +318,8 @@ input_gssapi_mic(int type, u_int32_t ple
9070b3
 		logit("GSSAPI MIC check failed");
9070b3
 
9070b3
 	sshbuf_free(b);
9070b3
+	if (micuser != authctxt->user)
9070b3
+		free(micuser);
9070b3
 	free(mic.value);
9070b3
 
9070b3
 	if ((!use_privsep || mm_is_monitor()) &&
9070b3
diff -up openssh/auth2-hostbased.c.role-mls openssh/auth2-hostbased.c
9070b3
--- openssh/auth2-hostbased.c.role-mls	2018-08-20 07:57:29.000000000 +0200
9070b3
+++ openssh/auth2-hostbased.c	2018-08-22 11:14:56.816430924 +0200
9070b3
@@ -123,7 +123,16 @@ userauth_hostbased(struct ssh *ssh)
9070b3
 	/* reconstruct packet */
9070b3
 	if ((r = sshbuf_put_stringb(b, ssh->kex->session_id)) != 0 ||
9070b3
 	    (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
9070b3
+#ifdef WITH_SELINUX
9070b3
+	    (authctxt->role
9070b3
+	    ? ( (r = sshbuf_put_u32(b, strlen(authctxt->user)+strlen(authctxt->role)+1)) != 0 ||
9070b3
+	        (r = sshbuf_put(b, authctxt->user, strlen(authctxt->user))) != 0 ||
9070b3
+	        (r = sshbuf_put_u8(b, '/') != 0) ||
9070b3
+	        (r = sshbuf_put(b, authctxt->role, strlen(authctxt->role))) != 0)
9070b3
+	    : (r = sshbuf_put_cstring(b, authctxt->user)) != 0) ||
9070b3
+#else
9070b3
 	    (r = sshbuf_put_cstring(b, authctxt->user)) != 0 ||
9070b3
+#endif
9070b3
 	    (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
9070b3
 	    (r = sshbuf_put_cstring(b, "hostbased")) != 0 ||
9070b3
 	    (r = sshbuf_put_string(b, pkalg, alen)) != 0 ||
9070b3
diff -up openssh/auth2-pubkey.c.role-mls openssh/auth2-pubkey.c
9070b3
--- openssh/auth2-pubkey.c.role-mls	2018-08-22 11:14:56.816430924 +0200
9070b3
+++ openssh/auth2-pubkey.c	2018-08-22 11:17:07.331483958 +0200
9070b3
@@ -169,9 +169,16 @@ userauth_pubkey(struct ssh *ssh)
9070b3
 			goto done;
9070b3
 		}
9070b3
 		/* reconstruct packet */
9070b3
-		xasprintf(&userstyle, "%s%s%s", authctxt->user,
9070b3
+		xasprintf(&userstyle, "%s%s%s%s%s", authctxt->user,
9070b3
 		    authctxt->style ? ":" : "",
9070b3
-		    authctxt->style ? authctxt->style : "");
9070b3
+		    authctxt->style ? authctxt->style : "",
9070b3
+#ifdef WITH_SELINUX
9070b3
+		    authctxt->role ? "/" : "",
9070b3
+		    authctxt->role ? authctxt->role : ""
9070b3
+#else
9070b3
+		    "", ""
9070b3
+#endif
9070b3
+		    );
9070b3
 		if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
9070b3
 		    (r = sshbuf_put_cstring(b, userstyle)) != 0 ||
9070b3
 		    (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
9070b3
diff -up openssh/auth.h.role-mls openssh/auth.h
9070b3
--- openssh/auth.h.role-mls	2018-08-20 07:57:29.000000000 +0200
9070b3
+++ openssh/auth.h	2018-08-22 11:14:56.816430924 +0200
9070b3
@@ -65,6 +65,9 @@ struct Authctxt {
9070b3
 	char		*service;
9070b3
 	struct passwd	*pw;		/* set if 'valid' */
9070b3
 	char		*style;
9070b3
+#ifdef WITH_SELINUX
9070b3
+	char		*role;
9070b3
+#endif
9070b3
 
9070b3
 	/* Method lists for multiple authentication */
9070b3
 	char		**auth_methods;	/* modified from server config */
9070b3
diff -up openssh/auth-pam.c.role-mls openssh/auth-pam.c
9070b3
--- openssh/auth-pam.c.role-mls	2018-08-20 07:57:29.000000000 +0200
9070b3
+++ openssh/auth-pam.c	2018-08-22 11:14:56.816430924 +0200
9070b3
@@ -1172,7 +1172,7 @@ is_pam_session_open(void)
9070b3
  * during the ssh authentication process.
9070b3
  */
9070b3
 int
9070b3
-do_pam_putenv(char *name, char *value)
9070b3
+do_pam_putenv(char *name, const char *value)
9070b3
 {
9070b3
 	int ret = 1;
9070b3
 	char *compound;
9070b3
diff -up openssh/auth-pam.h.role-mls openssh/auth-pam.h
9070b3
--- openssh/auth-pam.h.role-mls	2018-08-20 07:57:29.000000000 +0200
9070b3
+++ openssh/auth-pam.h	2018-08-22 11:14:56.817430932 +0200
9070b3
@@ -33,7 +33,7 @@ u_int do_pam_account(void);
9070b3
 void do_pam_session(struct ssh *);
9070b3
 void do_pam_setcred(int );
9070b3
 void do_pam_chauthtok(void);
9070b3
-int do_pam_putenv(char *, char *);
9070b3
+int do_pam_putenv(char *, const char *);
9070b3
 char ** fetch_pam_environment(void);
9070b3
 char ** fetch_pam_child_environment(void);
9070b3
 void free_pam_environment(char **);
9070b3
diff -up openssh/misc.c.role-mls openssh/misc.c
9070b3
--- openssh/misc.c.role-mls	2018-08-20 07:57:29.000000000 +0200
9070b3
+++ openssh/misc.c	2018-08-22 11:14:56.817430932 +0200
9070b3
@@ -542,6 +542,7 @@ char *
9070b3
 colon(char *cp)
9070b3
 {
9070b3
 	int flag = 0;
9070b3
+	int start = 1;
9070b3
 
9070b3
 	if (*cp == ':')		/* Leading colon is part of file name. */
9070b3
 		return NULL;
9070b3
@@ -557,6 +558,13 @@ colon(char *cp)
9070b3
 			return (cp);
9070b3
 		if (*cp == '/')
9070b3
 			return NULL;
9070b3
+		if (start) {
9070b3
+		/* Slash on beginning or after dots only denotes file name. */
9070b3
+			if (*cp == '/')
9070b3
+				return (0);
9070b3
+			if (*cp != '.')
9070b3
+				start = 0;
9070b3
+		}
9070b3
 	}
9070b3
 	return NULL;
9070b3
 }
9070b3
diff -up openssh-8.6p1/monitor.c.role-mls openssh-8.6p1/monitor.c
9070b3
--- openssh-8.6p1/monitor.c.role-mls	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/monitor.c	2021-05-21 14:21:56.719414087 +0200
9070b3
@@ -117,6 +117,9 @@ int mm_answer_sign(struct ssh *, int, st
9070b3
 int mm_answer_pwnamallow(struct ssh *, int, struct sshbuf *);
9070b3
 int mm_answer_auth2_read_banner(struct ssh *, int, struct sshbuf *);
9070b3
 int mm_answer_authserv(struct ssh *, int, struct sshbuf *);
9070b3
+#ifdef WITH_SELINUX
9070b3
+int mm_answer_authrole(struct ssh *, int, struct sshbuf *);
9070b3
+#endif
9070b3
 int mm_answer_authpassword(struct ssh *, int, struct sshbuf *);
9070b3
 int mm_answer_bsdauthquery(struct ssh *, int, struct sshbuf *);
9070b3
 int mm_answer_bsdauthrespond(struct ssh *, int, struct sshbuf *);
9070b3
@@ -195,6 +198,9 @@ struct mon_table mon_dispatch_proto20[]
9070b3
     {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
9070b3
     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
9070b3
     {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
9070b3
+#ifdef WITH_SELINUX
9070b3
+    {MONITOR_REQ_AUTHROLE, MON_ONCE, mm_answer_authrole},
9070b3
+#endif
9070b3
     {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
9070b3
     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
9070b3
 #ifdef USE_PAM
9070b3
@@ -803,6 +809,9 @@ mm_answer_pwnamallow(struct ssh *ssh, in
9070b3
 
9070b3
 	/* Allow service/style information on the auth context */
9070b3
 	monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
9070b3
+#ifdef WITH_SELINUX
9070b3
+	monitor_permit(mon_dispatch, MONITOR_REQ_AUTHROLE, 1);
9070b3
+#endif
9070b3
 	monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
9070b3
 
9070b3
 #ifdef USE_PAM
9070b3
@@ -877,6 +886,26 @@ key_base_type_match(const char *method,
9070b3
 	return found;
9070b3
 }
9070b3
 
9070b3
+#ifdef WITH_SELINUX
9070b3
+int
9070b3
+mm_answer_authrole(struct ssh *ssh, int sock, struct sshbuf *m)
9070b3
+{
9070b3
+	int r;
9070b3
+	monitor_permit_authentications(1);
9070b3
+
9070b3
+	if ((r = sshbuf_get_cstring(m, &authctxt->role, NULL)) != 0)
9070b3
+		fatal_f("buffer error: %s", ssh_err(r));
9070b3
+	debug3_f("role=%s", authctxt->role);
9070b3
+
9070b3
+	if (strlen(authctxt->role) == 0) {
9070b3
+		free(authctxt->role);
9070b3
+		authctxt->role = NULL;
9070b3
+	}
9070b3
+
9070b3
+	return (0);
9070b3
+}
9070b3
+#endif
9070b3
+
9070b3
 int
9070b3
 mm_answer_authpassword(struct ssh *ssh, int sock, struct sshbuf *m)
9070b3
 {
9070b3
@@ -1251,7 +1280,7 @@ monitor_valid_userblob(struct ssh *ssh,
9070b3
 {
9070b3
 	struct sshbuf *b;
9070b3
 	const u_char *p;
9070b3
-	char *userstyle, *cp;
9070b3
+	char *userstyle, *s, *cp;
9070b3
 	size_t len;
9070b3
 	u_char type;
9070b3
 	int r, fail = 0;
9070b3
@@ -1282,6 +1311,8 @@ monitor_valid_userblob(struct ssh *ssh,
9070b3
 		fail++;
9070b3
 	if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
9070b3
 		fatal_fr(r, "parse userstyle");
9070b3
+	if ((s = strchr(cp, '/')) != NULL)
9070b3
+		*s = '\0';
9070b3
 	xasprintf(&userstyle, "%s%s%s", authctxt->user,
9070b3
 	    authctxt->style ? ":" : "",
9070b3
 	    authctxt->style ? authctxt->style : "");
9070b3
@@ -1317,7 +1348,7 @@ monitor_valid_hostbasedblob(const u_char
9070b3
 {
9070b3
 	struct sshbuf *b;
9070b3
 	const u_char *p;
9070b3
-	char *cp, *userstyle;
9070b3
+	char *cp, *s, *userstyle;
9070b3
 	size_t len;
9070b3
 	int r, fail = 0;
9070b3
 	u_char type;
9070b3
@@ -1338,6 +1370,8 @@ monitor_valid_hostbasedblob(const u_char
9070b3
 		fail++;
9070b3
 	if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
9070b3
 		fatal_fr(r, "parse userstyle");
9070b3
+	if ((s = strchr(cp, '/')) != NULL)
9070b3
+		*s = '\0';
9070b3
 	xasprintf(&userstyle, "%s%s%s", authctxt->user,
9070b3
 	    authctxt->style ? ":" : "",
9070b3
 	    authctxt->style ? authctxt->style : "");
9070b3
diff -up openssh/monitor.h.role-mls openssh/monitor.h
9070b3
--- openssh/monitor.h.role-mls	2018-08-20 07:57:29.000000000 +0200
9070b3
+++ openssh/monitor.h	2018-08-22 11:14:56.818430941 +0200
9070b3
@@ -55,6 +55,10 @@ enum monitor_reqtype {
9070b3
 	MONITOR_REQ_GSSCHECKMIC = 48, MONITOR_ANS_GSSCHECKMIC = 49,
9070b3
 	MONITOR_REQ_TERM = 50,
9070b3
 
9070b3
+#ifdef WITH_SELINUX
9070b3
+	MONITOR_REQ_AUTHROLE = 80,
9070b3
+#endif
9070b3
+
9070b3
 	MONITOR_REQ_PAM_START = 100,
9070b3
 	MONITOR_REQ_PAM_ACCOUNT = 102, MONITOR_ANS_PAM_ACCOUNT = 103,
9070b3
 	MONITOR_REQ_PAM_INIT_CTX = 104, MONITOR_ANS_PAM_INIT_CTX = 105,
9070b3
diff -up openssh/monitor_wrap.c.role-mls openssh/monitor_wrap.c
9070b3
--- openssh/monitor_wrap.c.role-mls	2018-08-22 11:14:56.818430941 +0200
9070b3
+++ openssh/monitor_wrap.c	2018-08-22 11:21:47.938747968 +0200
9070b3
@@ -390,6 +390,27 @@ mm_inform_authserv(char *service, char *
9070b3
 	sshbuf_free(m);
9070b3
 }
9070b3
 
9070b3
+/* Inform the privileged process about role */
9070b3
+
9070b3
+#ifdef WITH_SELINUX
9070b3
+void
9070b3
+mm_inform_authrole(char *role)
9070b3
+{
9070b3
+	int r;
9070b3
+	struct sshbuf *m;
9070b3
+
9070b3
+	debug3_f("entering");
9070b3
+
9070b3
+	if ((m = sshbuf_new()) == NULL)
9070b3
+		fatal_f("sshbuf_new failed");
9070b3
+	if ((r = sshbuf_put_cstring(m, role ? role : "")) != 0)
9070b3
+		fatal_f("buffer error: %s", ssh_err(r));
9070b3
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHROLE, m);
9070b3
+
9070b3
+	sshbuf_free(m);
9070b3
+}
9070b3
+#endif
9070b3
+
9070b3
 /* Do the password authentication */
9070b3
 int
9070b3
 mm_auth_password(struct ssh *ssh, char *password)
9070b3
diff -up openssh/monitor_wrap.h.role-mls openssh/monitor_wrap.h
9070b3
--- openssh/monitor_wrap.h.role-mls	2018-08-22 11:14:56.818430941 +0200
9070b3
+++ openssh/monitor_wrap.h	2018-08-22 11:22:10.439929513 +0200
9070b3
@@ -44,6 +44,9 @@ DH *mm_choose_dh(int, int, int);
9070b3
     const u_char *, size_t, const char *, const char *,
9070b3
     const char *, u_int compat);
9070b3
 void mm_inform_authserv(char *, char *);
9070b3
+#ifdef WITH_SELINUX
9070b3
+void mm_inform_authrole(char *);
9070b3
+#endif
9070b3
 struct passwd *mm_getpwnamallow(struct ssh *, const char *);
9070b3
 char *mm_auth2_read_banner(void);
9070b3
 int mm_auth_password(struct ssh *, char *);
9070b3
diff -up openssh/openbsd-compat/Makefile.in.role-mls openssh/openbsd-compat/Makefile.in
9070b3
--- openssh/openbsd-compat/Makefile.in.role-mls	2018-08-20 07:57:29.000000000 +0200
9070b3
+++ openssh/openbsd-compat/Makefile.in	2018-08-22 11:14:56.819430949 +0200
9070b3
@@ -92,7 +92,8 @@ PORTS=	port-aix.o \
9070b3
 	port-prngd.o \
9070b3
 	port-solaris.o \
9070b3
 	port-net.o \
9070b3
-	port-uw.o
9070b3
+	port-uw.o \
9070b3
+	port-linux-sshd.o
9070b3
 
9070b3
 .c.o:
9070b3
 	$(CC) $(CFLAGS_NOPIE) $(PICFLAG) $(CPPFLAGS) -c $<
9070b3
diff -up openssh/openbsd-compat/port-linux.c.role-mls openssh/openbsd-compat/port-linux.c
9070b3
--- openssh/openbsd-compat/port-linux.c.role-mls	2018-08-20 07:57:29.000000000 +0200
9070b3
+++ openssh/openbsd-compat/port-linux.c	2018-08-22 11:14:56.819430949 +0200
9070b3
@@ -100,37 +100,6 @@ ssh_selinux_getctxbyname(char *pwname)
9070b3
 	return sc;
9070b3
 }
9070b3
 
9070b3
-/* Set the execution context to the default for the specified user */
9070b3
-void
9070b3
-ssh_selinux_setup_exec_context(char *pwname)
9070b3
-{
9070b3
-	char *user_ctx = NULL;
9070b3
-
9070b3
-	if (!ssh_selinux_enabled())
9070b3
-		return;
9070b3
-
9070b3
-	debug3("%s: setting execution context", __func__);
9070b3
-
9070b3
-	user_ctx = ssh_selinux_getctxbyname(pwname);
9070b3
-	if (setexeccon(user_ctx) != 0) {
9070b3
-		switch (security_getenforce()) {
9070b3
-		case -1:
9070b3
-			fatal("%s: security_getenforce() failed", __func__);
9070b3
-		case 0:
9070b3
-			error("%s: Failed to set SELinux execution "
9070b3
-			    "context for %s", __func__, pwname);
9070b3
-			break;
9070b3
-		default:
9070b3
-			fatal("%s: Failed to set SELinux execution context "
9070b3
-			    "for %s (in enforcing mode)", __func__, pwname);
9070b3
-		}
9070b3
-	}
9070b3
-	if (user_ctx != NULL)
9070b3
-		freecon(user_ctx);
9070b3
-
9070b3
-	debug3("%s: done", __func__);
9070b3
-}
9070b3
-
9070b3
 /* Set the TTY context for the specified user */
9070b3
 void
9070b3
 ssh_selinux_setup_pty(char *pwname, const char *tty)
9070b3
@@ -145,7 +114,11 @@ ssh_selinux_setup_pty(char *pwname, cons
9070b3
 
9070b3
 	debug3("%s: setting TTY context on %s", __func__, tty);
9070b3
 
9070b3
-	user_ctx = ssh_selinux_getctxbyname(pwname);
9070b3
+	if (getexeccon(&user_ctx) != 0) {
9070b3
+		error_f("getexeccon: %s", strerror(errno));
9070b3
+		goto out;
9070b3
+	}
9070b3
+
9070b3
 
9070b3
 	/* XXX: should these calls fatal() upon failure in enforcing mode? */
9070b3
 
9070b3
diff -up openssh/openbsd-compat/port-linux.h.role-mls openssh/openbsd-compat/port-linux.h
9070b3
--- openssh/openbsd-compat/port-linux.h.role-mls	2018-08-20 07:57:29.000000000 +0200
9070b3
+++ openssh/openbsd-compat/port-linux.h	2018-08-22 11:14:56.819430949 +0200
9070b3
@@ -20,9 +20,10 @@
9070b3
 #ifdef WITH_SELINUX
9070b3
 int ssh_selinux_enabled(void);
9070b3
 void ssh_selinux_setup_pty(char *, const char *);
9070b3
-void ssh_selinux_setup_exec_context(char *);
9070b3
 void ssh_selinux_change_context(const char *);
9070b3
 void ssh_selinux_setfscreatecon(const char *);
9070b3
+
9070b3
+void sshd_selinux_setup_exec_context(char *);
9070b3
 #endif
9070b3
 
9070b3
 #ifdef LINUX_OOM_ADJUST
9070b3
diff -up openssh/openbsd-compat/port-linux-sshd.c.role-mls openssh/openbsd-compat/port-linux-sshd.c
9070b3
--- openssh/openbsd-compat/port-linux-sshd.c.role-mls	2018-08-22 11:14:56.819430949 +0200
9070b3
+++ openssh/openbsd-compat/port-linux-sshd.c	2018-08-22 11:14:56.819430949 +0200
9070b3
@@ -0,0 +1,421 @@
9070b3
+/*
9070b3
+ * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com>
9070b3
+ * Copyright (c) 2014 Petr Lautrbach <plautrba@redhat.com>
9070b3
+ *
9070b3
+ * Permission to use, copy, modify, and distribute this software for any
9070b3
+ * purpose with or without fee is hereby granted, provided that the above
9070b3
+ * copyright notice and this permission notice appear in all copies.
9070b3
+ *
9070b3
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9070b3
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9070b3
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
9070b3
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
9070b3
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
9070b3
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
9070b3
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
9070b3
+ */
9070b3
+
9070b3
+/*
9070b3
+ * Linux-specific portability code - just SELinux support for sshd at present
9070b3
+ */
9070b3
+
9070b3
+#include "includes.h"
9070b3
+
9070b3
+#if defined(WITH_SELINUX) || defined(LINUX_OOM_ADJUST)
9070b3
+#include <errno.h>
9070b3
+#include <stdarg.h>
9070b3
+#include <string.h>
9070b3
+#include <stdio.h>
9070b3
+#include <stdlib.h>
9070b3
+
9070b3
+#include "log.h"
9070b3
+#include "xmalloc.h"
9070b3
+#include "misc.h"      /* servconf.h needs misc.h for struct ForwardOptions */
9070b3
+#include "servconf.h"
9070b3
+#include "port-linux.h"
9070b3
+#include "sshkey.h"
9070b3
+#include "hostfile.h"
9070b3
+#include "auth.h"
9070b3
+
9070b3
+#ifdef WITH_SELINUX
9070b3
+#include <selinux/selinux.h>
9070b3
+#include <selinux/context.h>
9070b3
+#include <selinux/get_context_list.h>
9070b3
+#include <selinux/get_default_type.h>
9070b3
+
9070b3
+#ifdef HAVE_LINUX_AUDIT
9070b3
+#include <libaudit.h>
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
+/* Send audit message */
9070b3
+static int
9070b3
+sshd_selinux_send_audit_message(int success, security_context_t default_context,
9070b3
+		       security_context_t selected_context)
9070b3
+{
9070b3
+	int rc=0;
9070b3
+#ifdef HAVE_LINUX_AUDIT
9070b3
+	char *msg = NULL;
9070b3
+	int audit_fd = audit_open();
9070b3
+	security_context_t default_raw=NULL;
9070b3
+	security_context_t selected_raw=NULL;
9070b3
+	rc = -1;
9070b3
+	if (audit_fd < 0) {
9070b3
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
9070b3
+					errno == EAFNOSUPPORT)
9070b3
+				return 0; /* No audit support in kernel */
9070b3
+		error("Error connecting to audit system.");
9070b3
+		return rc;
9070b3
+	}
9070b3
+	if (selinux_trans_to_raw_context(default_context, &default_raw) < 0) {
9070b3
+		error("Error translating default context.");
9070b3
+		default_raw = NULL;
9070b3
+	}
9070b3
+	if (selinux_trans_to_raw_context(selected_context, &selected_raw) < 0) {
9070b3
+		error("Error translating selected context.");
9070b3
+		selected_raw = NULL;
9070b3
+	}
9070b3
+	if (asprintf(&msg, "sshd: default-context=%s selected-context=%s",
9070b3
+		     default_raw ? default_raw : (default_context ? default_context: "?"),
9070b3
+		     selected_context ? selected_raw : (selected_context ? selected_context :"?")) < 0) {
9070b3
+		error("Error allocating memory.");
9070b3
+		goto out;
9070b3
+	}
9070b3
+	if (audit_log_user_message(audit_fd, AUDIT_USER_ROLE_CHANGE,
9070b3
+				   msg, NULL, NULL, NULL, success) <= 0) {
9070b3
+		error("Error sending audit message.");
9070b3
+		goto out;
9070b3
+	}
9070b3
+	rc = 0;
9070b3
+      out:
9070b3
+	free(msg);
9070b3
+	freecon(default_raw);
9070b3
+	freecon(selected_raw);
9070b3
+	close(audit_fd);
9070b3
+#endif
9070b3
+	return rc;
9070b3
+}
9070b3
+
9070b3
+static int
9070b3
+mls_range_allowed(security_context_t src, security_context_t dst)
9070b3
+{
9070b3
+	struct av_decision avd;
9070b3
+	int retval;
9070b3
+	access_vector_t bit;
9070b3
+	security_class_t class;
9070b3
+
9070b3
+	debug_f("src:%s dst:%s", src, dst);
9070b3
+	class = string_to_security_class("context");
9070b3
+	if (!class) {
9070b3
+		error("string_to_security_class failed to translate security class context");
9070b3
+		return 1;
9070b3
+	}
9070b3
+	bit = string_to_av_perm(class, "contains");
9070b3
+	if (!bit) {
9070b3
+		error("string_to_av_perm failed to translate av perm contains");
9070b3
+		return 1;
9070b3
+	}
9070b3
+	retval = security_compute_av(src, dst, class, bit, &avd);
9070b3
+	if (retval || ((bit & avd.allowed) != bit))
9070b3
+		return 0;
9070b3
+
9070b3
+	return 1;
9070b3
+}
9070b3
+
9070b3
+static int
9070b3
+get_user_context(const char *sename, const char *role, const char *lvl,
9070b3
+	security_context_t *sc) {
9070b3
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
9070b3
+	if (lvl == NULL || lvl[0] == '\0' || get_default_context_with_level(sename, lvl, NULL, sc) != 0) {
9070b3
+	        /* User may have requested a level completely outside of his 
9070b3
+	           allowed range. We get a context just for auditing as the
9070b3
+	           range check below will certainly fail for default context. */
9070b3
+#endif
9070b3
+		if (get_default_context(sename, NULL, sc) != 0) {
9070b3
+			*sc = NULL;
9070b3
+			return -1;
9070b3
+		}
9070b3
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
9070b3
+	}
9070b3
+#endif
9070b3
+	if (role != NULL && role[0]) {
9070b3
+		context_t con;
9070b3
+		char *type=NULL;
9070b3
+		if (get_default_type(role, &type) != 0) {
9070b3
+			error("get_default_type: failed to get default type for '%s'",
9070b3
+				role);
9070b3
+			goto out;
9070b3
+		}
9070b3
+		con = context_new(*sc);
9070b3
+		if (!con) {
9070b3
+			goto out;
9070b3
+		}
9070b3
+		context_role_set(con, role);
9070b3
+		context_type_set(con, type);
9070b3
+		freecon(*sc);
9070b3
+		*sc = strdup(context_str(con));
9070b3
+		context_free(con);
9070b3
+		if (!*sc)
9070b3
+			return -1;
9070b3
+	}
9070b3
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
9070b3
+	if (lvl != NULL && lvl[0]) {
9070b3
+		/* verify that the requested range is obtained */
9070b3
+		context_t con;
9070b3
+		security_context_t obtained_raw;
9070b3
+		security_context_t requested_raw;
9070b3
+		con = context_new(*sc);
9070b3
+		if (!con) {
9070b3
+			goto out;
9070b3
+		}
9070b3
+		context_range_set(con, lvl);
9070b3
+		if (selinux_trans_to_raw_context(*sc, &obtained_raw) < 0) {
9070b3
+			context_free(con);
9070b3
+			goto out;
9070b3
+		}
9070b3
+		if (selinux_trans_to_raw_context(context_str(con), &requested_raw) < 0) {
9070b3
+			freecon(obtained_raw);
9070b3
+			context_free(con);
9070b3
+			goto out;
9070b3
+		}
9070b3
+
9070b3
+		debug("get_user_context: obtained context '%s' requested context '%s'",
9070b3
+			obtained_raw, requested_raw);
9070b3
+		if (strcmp(obtained_raw, requested_raw)) {
9070b3
+			/* set the context to the real requested one but fail */
9070b3
+			freecon(requested_raw);
9070b3
+			freecon(obtained_raw);
9070b3
+			freecon(*sc);
9070b3
+			*sc = strdup(context_str(con));
9070b3
+			context_free(con);
9070b3
+			return -1;
9070b3
+		}
9070b3
+		freecon(requested_raw);
9070b3
+		freecon(obtained_raw);
9070b3
+		context_free(con);
9070b3
+	}
9070b3
+#endif
9070b3
+	return 0;
9070b3
+      out:
9070b3
+	freecon(*sc);
9070b3
+	*sc = NULL;
9070b3
+	return -1;
9070b3
+}
9070b3
+
9070b3
+static void
9070b3
+ssh_selinux_get_role_level(char **role, const char **level)
9070b3
+{
9070b3
+	*role = NULL;
9070b3
+	*level = NULL;
9070b3
+	if (the_authctxt) {
9070b3
+		if (the_authctxt->role != NULL) {
9070b3
+			char *slash;
9070b3
+			*role = xstrdup(the_authctxt->role);
9070b3
+			if ((slash = strchr(*role, '/')) != NULL) {
9070b3
+				*slash = '\0';
9070b3
+				*level = slash + 1;
9070b3
+			}
9070b3
+		}
9070b3
+	}
9070b3
+}
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
+{
9070b3
+	char *sename, *lvl;
9070b3
+	char *role;
9070b3
+	const char *reqlvl;
9070b3
+	int r = 0;
9070b3
+	context_t con = NULL;
9070b3
+
9070b3
+	ssh_selinux_get_role_level(&role, &reqlvl);
9070b3
+
9070b3
+#ifdef HAVE_GETSEUSERBYNAME
9070b3
+	if ((r=getseuserbyname(pwname, &sename, &lvl)) != 0) {
9070b3
+		sename = NULL;
9070b3
+		lvl = NULL;
9070b3
+	}
9070b3
+#else
9070b3
+	sename = pwname;
9070b3
+	lvl = "";
9070b3
+#endif
9070b3
+
9070b3
+	if (r == 0) {
9070b3
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
9070b3
+		r = get_default_context_with_level(sename, lvl, NULL, default_sc);
9070b3
+#else
9070b3
+		r = get_default_context(sename, NULL, default_sc);
9070b3
+#endif
9070b3
+	}
9070b3
+
9070b3
+	if (r == 0) {
9070b3
+		/* If launched from xinetd, we must use current level */
9070b3
+		if (inetd_flag && !rexeced_flag) {
9070b3
+			security_context_t sshdsc=NULL;
9070b3
+
9070b3
+			if (getcon_raw(&sshdsc) < 0)
9070b3
+				fatal("failed to allocate security context");
9070b3
+
9070b3
+			if ((con=context_new(sshdsc)) == NULL)
9070b3
+				fatal("failed to allocate selinux context");
9070b3
+			reqlvl = context_range_get(con);
9070b3
+			freecon(sshdsc);
9070b3
+			if (reqlvl !=NULL && lvl != NULL && strcmp(reqlvl, lvl) == 0)
9070b3
+			    /* we actually don't change level */
9070b3
+			    reqlvl = "";
9070b3
+
9070b3
+			debug_f("current connection level '%s'", reqlvl);
9070b3
+
9070b3
+		}
9070b3
+
9070b3
+		if ((reqlvl != NULL && reqlvl[0]) || (role != NULL && role[0])) {
9070b3
+			r = get_user_context(sename, role, reqlvl, user_sc);
9070b3
+
9070b3
+			if (r == 0 && reqlvl != NULL && reqlvl[0]) {
9070b3
+				security_context_t default_level_sc = *default_sc;
9070b3
+				if (role != NULL && role[0]) {
9070b3
+					if (get_user_context(sename, role, lvl, &default_level_sc) < 0)
9070b3
+						default_level_sc = *default_sc;
9070b3
+				}
9070b3
+				/* verify that the requested range is contained in the user range */
9070b3
+				if (mls_range_allowed(default_level_sc, *user_sc)) {
9070b3
+					logit("permit MLS level %s (user range %s)", reqlvl, lvl);
9070b3
+				} else {
9070b3
+					r = -1;
9070b3
+					error("deny MLS level %s (user range %s)", reqlvl, lvl);
9070b3
+				}
9070b3
+				if (default_level_sc != *default_sc)
9070b3
+					freecon(default_level_sc);
9070b3
+			}
9070b3
+		} else {
9070b3
+			*user_sc = *default_sc;
9070b3
+		}
9070b3
+	}
9070b3
+	if (r != 0) {
9070b3
+		error_f("Failed to get default SELinux security "
9070b3
+		    "context for %s", pwname);
9070b3
+	}
9070b3
+
9070b3
+#ifdef HAVE_GETSEUSERBYNAME
9070b3
+	free(sename);
9070b3
+	free(lvl);
9070b3
+#endif
9070b3
+
9070b3
+	if (role != NULL)
9070b3
+		free(role);
9070b3
+	if (con)
9070b3
+		context_free(con);
9070b3
+
9070b3
+	return (r);
9070b3
+}
9070b3
+
9070b3
+/* Setup environment variables for pam_selinux */
9070b3
+static int
9070b3
+sshd_selinux_setup_pam_variables(void)
9070b3
+{
9070b3
+	const char *reqlvl;
9070b3
+	char *role;
9070b3
+	char *use_current;
9070b3
+	int rv;
9070b3
+
9070b3
+	debug3_f("setting execution context");
9070b3
+
9070b3
+	ssh_selinux_get_role_level(&role, &reqlvl);
9070b3
+
9070b3
+	rv = do_pam_putenv("SELINUX_ROLE_REQUESTED", role ? role : "");
9070b3
+
9070b3
+	if (inetd_flag && !rexeced_flag) {
9070b3
+		use_current = "1";
9070b3
+	} else {
9070b3
+		use_current = "";
9070b3
+		rv = rv || do_pam_putenv("SELINUX_LEVEL_REQUESTED", reqlvl ? reqlvl: "");
9070b3
+	}
9070b3
+
9070b3
+	rv = rv || do_pam_putenv("SELINUX_USE_CURRENT_RANGE", use_current);
9070b3
+
9070b3
+	if (role != NULL)
9070b3
+		free(role);
9070b3
+
9070b3
+	return rv;
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
+{
9070b3
+	security_context_t user_ctx = NULL;
9070b3
+	int r = 0;
9070b3
+	security_context_t default_ctx = NULL;
9070b3
+
9070b3
+	if (!ssh_selinux_enabled())
9070b3
+		return;
9070b3
+
9070b3
+	if (options.use_pam) {
9070b3
+		/* do not compute context, just setup environment for pam_selinux */
9070b3
+		if (sshd_selinux_setup_pam_variables()) {
9070b3
+			switch (security_getenforce()) {
9070b3
+			case -1:
9070b3
+				fatal_f("security_getenforce() failed");
9070b3
+			case 0:
9070b3
+				error_f("SELinux PAM variable setup failure. Continuing in permissive mode.");
9070b3
+			break;
9070b3
+			default:
9070b3
+				fatal_f("SELinux PAM variable setup failure. Aborting connection.");
9070b3
+			}
9070b3
+		}
9070b3
+		return;
9070b3
+	}
9070b3
+
9070b3
+	debug3_f("setting execution context");
9070b3
+
9070b3
+	r = sshd_selinux_getctxbyname(pwname, &default_ctx, &user_ctx);
9070b3
+	if (r >= 0) {
9070b3
+		r = setexeccon(user_ctx);
9070b3
+		if (r < 0) {
9070b3
+			error_f("Failed to set SELinux execution context %s for %s",
9070b3
+			    user_ctx, pwname);
9070b3
+		}
9070b3
+#ifdef HAVE_SETKEYCREATECON
9070b3
+		else if (setkeycreatecon(user_ctx) < 0) {
9070b3
+			error_f("Failed to set SELinux keyring creation context %s for %s",
9070b3
+			    user_ctx, pwname);
9070b3
+		}
9070b3
+#endif
9070b3
+	}
9070b3
+	if (user_ctx == NULL) {
9070b3
+		user_ctx = default_ctx;
9070b3
+	}
9070b3
+	if (r < 0 || user_ctx != default_ctx) {
9070b3
+		/* audit just the case when user changed a role or there was
9070b3
+		   a failure */
9070b3
+		sshd_selinux_send_audit_message(r >= 0, default_ctx, user_ctx);
9070b3
+	}
9070b3
+	if (r < 0) {
9070b3
+		switch (security_getenforce()) {
9070b3
+		case -1:
9070b3
+			fatal_f("security_getenforce() failed");
9070b3
+		case 0:
9070b3
+			error_f("ELinux failure. Continuing in permissive mode.");
9070b3
+			break;
9070b3
+		default:
9070b3
+			fatal_f("SELinux failure. Aborting connection.");
9070b3
+		}
9070b3
+	}
9070b3
+	if (user_ctx != NULL && user_ctx != default_ctx)
9070b3
+		freecon(user_ctx);
9070b3
+	if (default_ctx != NULL)
9070b3
+		freecon(default_ctx);
9070b3
+
9070b3
+	debug3_f("done");
9070b3
+}
9070b3
+
9070b3
+#endif
9070b3
+#endif
9070b3
+
9070b3
diff -up openssh/platform.c.role-mls openssh/platform.c
9070b3
--- openssh/platform.c.role-mls	2018-08-20 07:57:29.000000000 +0200
9070b3
+++ openssh/platform.c	2018-08-22 11:14:56.819430949 +0200
9070b3
@@ -183,7 +183,7 @@ platform_setusercontext_post_groups(stru
9070b3
 	}
9070b3
 #endif /* HAVE_SETPCRED */
9070b3
 #ifdef WITH_SELINUX
9070b3
-	ssh_selinux_setup_exec_context(pw->pw_name);
9070b3
+	sshd_selinux_setup_exec_context(pw->pw_name);
9070b3
 #endif
9070b3
 }
9070b3
 
9070b3
diff -up openssh/sshd.c.role-mls openssh/sshd.c
9070b3
--- openssh/sshd.c.role-mls	2018-08-20 07:57:29.000000000 +0200
9070b3
+++ openssh/sshd.c	2018-08-22 11:14:56.820430957 +0200
9070b3
@@ -2186,6 +2186,9 @@ main(int ac, char **av)
9070b3
 		restore_uid();
9070b3
 	}
9070b3
 #endif
9070b3
+#ifdef WITH_SELINUX
9070b3
+	sshd_selinux_setup_exec_context(authctxt->pw->pw_name);
9070b3
+#endif
9070b3
 #ifdef USE_PAM
9070b3
 	if (options.use_pam) {
9070b3
 		do_pam_setcred(1);