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