bmh10 / rpms / openssh

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