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