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