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