kentpeacock / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
1d31ef
diff -up openssh-7.4p1/auth2.c.role-mls openssh-7.4p1/auth2.c
1d31ef
--- openssh-7.4p1/auth2.c.role-mls	2016-12-19 05:59:41.000000000 +0100
1d31ef
+++ openssh-7.4p1/auth2.c	2017-02-08 14:08:30.271308186 +0100
1d31ef
@@ -215,6 +215,9 @@ input_userauth_request(int type, u_int32
1d31ef
 	Authctxt *authctxt = ctxt;
1d31ef
 	Authmethod *m = NULL;
1d31ef
 	char *user, *service, *method, *style = NULL;
f09e2e
+#ifdef WITH_SELINUX
1d31ef
+	char *role = NULL;
f09e2e
+#endif
1d31ef
 	int authenticated = 0;
f09e2e
 
1d31ef
 	if (authctxt == NULL)
1d31ef
@@ -226,6 +229,11 @@ input_userauth_request(int type, u_int32
1d31ef
 	debug("userauth-request for user %s service %s method %s", user, service, method);
1d31ef
 	debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
f09e2e
 
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+	if ((role = strchr(user, '/')) != NULL)
1d31ef
+		*role++ = 0;
f09e2e
+#endif
f09e2e
+
f09e2e
 	if ((style = strchr(user, ':')) != NULL)
1d31ef
 		*style++ = 0;
1d31ef
 
1d31ef
@@ -251,8 +259,15 @@ input_userauth_request(int type, u_int32
1d31ef
 		    use_privsep ? " [net]" : "");
1d31ef
 		authctxt->service = xstrdup(service);
1d31ef
 		authctxt->style = style ? xstrdup(style) : NULL;
1d31ef
-		if (use_privsep)
f09e2e
+#ifdef WITH_SELINUX
1d31ef
+		authctxt->role = role ? xstrdup(role) : NULL;
f09e2e
+#endif
1d31ef
+		if (use_privsep) {
1d31ef
 			mm_inform_authserv(service, style);
f09e2e
+#ifdef WITH_SELINUX
1d31ef
+			mm_inform_authrole(role);
f09e2e
+#endif
1d31ef
+		}
1d31ef
 		userauth_banner();
1d31ef
 		if (auth2_setup_methods_lists(authctxt) != 0)
1d31ef
 			packet_disconnect("no authentication methods enabled");
1d31ef
diff -up openssh-7.4p1/auth2-gss.c.role-mls openssh-7.4p1/auth2-gss.c
1d31ef
--- openssh-7.4p1/auth2-gss.c.role-mls	2016-12-19 05:59:41.000000000 +0100
1d31ef
+++ openssh-7.4p1/auth2-gss.c	2017-02-08 14:08:30.270308187 +0100
1d31ef
@@ -255,6 +255,7 @@ input_gssapi_mic(int type, u_int32_t ple
f09e2e
 	Authctxt *authctxt = ctxt;
f09e2e
 	Gssctxt *gssctxt;
f09e2e
 	int authenticated = 0;
f09e2e
+	char *micuser;
f09e2e
 	Buffer b;
f09e2e
 	gss_buffer_desc mic, gssbuf;
f09e2e
 	u_int len;
1d31ef
@@ -267,7 +268,13 @@ input_gssapi_mic(int type, u_int32_t ple
f09e2e
 	mic.value = packet_get_string(&len;;
f09e2e
 	mic.length = len;
f09e2e
 
f09e2e
-	ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+	if (authctxt->role && (strlen(authctxt->role) > 0))
f09e2e
+		xasprintf(&micuser, "%s/%s", authctxt->user, authctxt->role);
f09e2e
+	else
f09e2e
+#endif
f09e2e
+		micuser = authctxt->user;
f09e2e
+	ssh_gssapi_buildmic(&b, micuser, authctxt->service,
f09e2e
 	    "gssapi-with-mic");
f09e2e
 
f09e2e
 	gssbuf.value = buffer_ptr(&b);
1d31ef
@@ -279,6 +286,8 @@ input_gssapi_mic(int type, u_int32_t ple
f09e2e
 		logit("GSSAPI MIC check failed");
f09e2e
 
f09e2e
 	buffer_free(&b);
f09e2e
+	if (micuser != authctxt->user)
f09e2e
+		free(micuser);
f09e2e
 	free(mic.value);
f09e2e
 
f09e2e
 	authctxt->postponed = 0;
1d31ef
diff -up openssh-7.4p1/auth2-hostbased.c.role-mls openssh-7.4p1/auth2-hostbased.c
1d31ef
--- openssh-7.4p1/auth2-hostbased.c.role-mls	2016-12-19 05:59:41.000000000 +0100
1d31ef
+++ openssh-7.4p1/auth2-hostbased.c	2017-02-08 14:08:30.270308187 +0100
1d31ef
@@ -121,7 +121,15 @@ userauth_hostbased(Authctxt *authctxt)
f09e2e
 	buffer_put_string(&b, session_id2, session_id2_len);
f09e2e
 	/* reconstruct packet */
f09e2e
 	buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
f09e2e
-	buffer_put_cstring(&b, authctxt->user);
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+	if (authctxt->role) {
f09e2e
+		buffer_put_int(&b, strlen(authctxt->user)+strlen(authctxt->role)+1);
f09e2e
+		buffer_append(&b, authctxt->user, strlen(authctxt->user));
f09e2e
+		buffer_put_char(&b, '/');
f09e2e
+		buffer_append(&b, authctxt->role, strlen(authctxt->role));
f09e2e
+	} else 
f09e2e
+#endif
f09e2e
+		buffer_put_cstring(&b, authctxt->user);
f09e2e
 	buffer_put_cstring(&b, service);
f09e2e
 	buffer_put_cstring(&b, "hostbased");
f09e2e
 	buffer_put_string(&b, pkalg, alen);
1d31ef
diff -up openssh-7.4p1/auth2-pubkey.c.role-mls openssh-7.4p1/auth2-pubkey.c
1d31ef
--- openssh-7.4p1/auth2-pubkey.c.role-mls	2016-12-19 05:59:41.000000000 +0100
1d31ef
+++ openssh-7.4p1/auth2-pubkey.c	2017-02-08 14:08:30.270308187 +0100
1d31ef
@@ -151,9 +151,11 @@ userauth_pubkey(Authctxt *authctxt)
f09e2e
 		}
f09e2e
 		/* reconstruct packet */
f09e2e
 		buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
f09e2e
-		xasprintf(&userstyle, "%s%s%s", authctxt->user,
f09e2e
+		xasprintf(&userstyle, "%s%s%s%s%s", authctxt->user,
f09e2e
 		    authctxt->style ? ":" : "",
f09e2e
-		    authctxt->style ? authctxt->style : "");
f09e2e
+		    authctxt->style ? authctxt->style : "",
f09e2e
+		    authctxt->role ? "/" : "",
f09e2e
+		    authctxt->role ? authctxt->role : "");
f09e2e
 		buffer_put_cstring(&b, userstyle);
f09e2e
 		free(userstyle);
f09e2e
 		buffer_put_cstring(&b,
1d31ef
diff -up openssh-7.4p1/auth.h.role-mls openssh-7.4p1/auth.h
1d31ef
--- openssh-7.4p1/auth.h.role-mls	2016-12-19 05:59:41.000000000 +0100
1d31ef
+++ openssh-7.4p1/auth.h	2017-02-08 14:08:30.270308187 +0100
1d31ef
@@ -62,6 +62,9 @@ struct Authctxt {
1d31ef
 	char		*service;
1d31ef
 	struct passwd	*pw;		/* set if 'valid' */
1d31ef
 	char		*style;
f09e2e
+#ifdef WITH_SELINUX
1d31ef
+	char		*role;
f09e2e
+#endif
1d31ef
 	void		*kbdintctxt;
1d31ef
 	char		*info;		/* Extra info for next auth_log */
1d31ef
 #ifdef BSD_AUTH
1d31ef
diff -up openssh-7.4p1/auth-pam.c.role-mls openssh-7.4p1/auth-pam.c
1d31ef
--- openssh-7.4p1/auth-pam.c.role-mls	2016-12-19 05:59:41.000000000 +0100
1d31ef
+++ openssh-7.4p1/auth-pam.c	2017-02-08 14:08:30.270308187 +0100
1d31ef
@@ -1087,7 +1087,7 @@ is_pam_session_open(void)
1d31ef
  * during the ssh authentication process.
1d31ef
  */
1d31ef
 int
1d31ef
-do_pam_putenv(char *name, char *value)
1d31ef
+do_pam_putenv(char *name, const char *value)
1d31ef
 {
1d31ef
 	int ret = 1;
1d31ef
 #ifdef HAVE_PAM_PUTENV
1d31ef
diff -up openssh-7.4p1/auth-pam.h.role-mls openssh-7.4p1/auth-pam.h
1d31ef
--- openssh-7.4p1/auth-pam.h.role-mls	2017-02-08 14:08:30.270308187 +0100
1d31ef
+++ openssh-7.4p1/auth-pam.h	2017-02-08 14:09:09.711273302 +0100
1d31ef
@@ -31,7 +31,7 @@ u_int do_pam_account(void);
1d31ef
 void do_pam_session(void);
1d31ef
 void do_pam_setcred(int );
1d31ef
 void do_pam_chauthtok(void);
1d31ef
-int do_pam_putenv(char *, char *);
1d31ef
+int do_pam_putenv(char *, const char *);
1d31ef
 char ** fetch_pam_environment(void);
1d31ef
 char ** fetch_pam_child_environment(void);
1d31ef
 void free_pam_environment(char **);
1d31ef
diff -up openssh-7.4p1/misc.c.role-mls openssh-7.4p1/misc.c
1d31ef
--- openssh-7.4p1/misc.c.role-mls	2016-12-19 05:59:41.000000000 +0100
1d31ef
+++ openssh-7.4p1/misc.c	2017-02-08 14:08:30.271308186 +0100
1d31ef
@@ -432,6 +432,7 @@ char *
f09e2e
 colon(char *cp)
f09e2e
 {
f09e2e
 	int flag = 0;
f09e2e
+	int start = 1;
f09e2e
 
f09e2e
 	if (*cp == ':')		/* Leading colon is part of file name. */
f09e2e
 		return NULL;
1d31ef
@@ -447,6 +448,13 @@ colon(char *cp)
f09e2e
 			return (cp);
f09e2e
 		if (*cp == '/')
f09e2e
 			return NULL;
f09e2e
+		if (start) {
f09e2e
+		/* Slash on beginning or after dots only denotes file name. */
f09e2e
+			if (*cp == '/')
f09e2e
+				return (0);
f09e2e
+			if (*cp != '.')
f09e2e
+				start = 0;
f09e2e
+		}
f09e2e
 	}
f09e2e
 	return NULL;
f09e2e
 }
1d31ef
diff -up openssh-7.4p1/monitor.c.role-mls openssh-7.4p1/monitor.c
1d31ef
--- openssh-7.4p1/monitor.c.role-mls	2016-12-19 05:59:41.000000000 +0100
1d31ef
+++ openssh-7.4p1/monitor.c	2017-02-08 14:18:13.289928913 +0100
1d31ef
@@ -127,6 +127,9 @@ int mm_answer_sign(int, Buffer *);
f09e2e
 int mm_answer_pwnamallow(int, Buffer *);
f09e2e
 int mm_answer_auth2_read_banner(int, Buffer *);
f09e2e
 int mm_answer_authserv(int, Buffer *);
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+int mm_answer_authrole(int, Buffer *);
f09e2e
+#endif
f09e2e
 int mm_answer_authpassword(int, Buffer *);
f09e2e
 int mm_answer_bsdauthquery(int, Buffer *);
f09e2e
 int mm_answer_bsdauthrespond(int, Buffer *);
1d31ef
@@ -202,6 +205,9 @@ struct mon_table mon_dispatch_proto20[]
f09e2e
     {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
f09e2e
     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
f09e2e
     {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+    {MONITOR_REQ_AUTHROLE, MON_ONCE, mm_answer_authrole},
f09e2e
+#endif
f09e2e
     {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
f09e2e
     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
f09e2e
 #ifdef USE_PAM
1d31ef
@@ -769,6 +775,9 @@ mm_answer_pwnamallow(int sock, Buffer *m
1d31ef
 
1d31ef
 	/* Allow service/style information on the auth context */
1d31ef
 	monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
f09e2e
+#ifdef WITH_SELINUX
1d31ef
+	monitor_permit(mon_dispatch, MONITOR_REQ_AUTHROLE, 1);
f09e2e
+#endif
1d31ef
 	monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
1d31ef
 
f09e2e
 #ifdef USE_PAM
1d31ef
@@ -810,6 +819,25 @@ mm_answer_authserv(int sock, Buffer *m)
f09e2e
 	return (0);
f09e2e
 }
f09e2e
 
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+int
f09e2e
+mm_answer_authrole(int sock, Buffer *m)
f09e2e
+{
f09e2e
+	monitor_permit_authentications(1);
f09e2e
+
f09e2e
+	authctxt->role = buffer_get_string(m, NULL);
f09e2e
+	debug3("%s: role=%s",
f09e2e
+	    __func__, authctxt->role);
f09e2e
+
f09e2e
+	if (strlen(authctxt->role) == 0) {
f09e2e
+		free(authctxt->role);
f09e2e
+		authctxt->role = NULL;
f09e2e
+	}
f09e2e
+
f09e2e
+	return (0);
f09e2e
+}
f09e2e
+#endif
f09e2e
+
f09e2e
 int
f09e2e
 mm_answer_authpassword(int sock, Buffer *m)
f09e2e
 {
1d31ef
@@ -1208,7 +1236,7 @@ monitor_valid_userblob(u_char *data, u_i
f09e2e
 {
f09e2e
 	Buffer b;
1d31ef
 	u_char *p;
1d31ef
-	char *userstyle, *cp;
1d31ef
+	char *userstyle, *r, *cp;
f09e2e
 	u_int len;
f09e2e
 	int fail = 0;
f09e2e
 
1d31ef
@@ -1234,6 +1262,8 @@ monitor_valid_userblob(u_char *data, u_i
f09e2e
 	if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
f09e2e
 		fail++;
1d31ef
 	cp = buffer_get_cstring(&b, NULL);
1d31ef
+	if ((r = strchr(cp, '/')) != NULL)
f09e2e
+		*r = '\0';
f09e2e
 	xasprintf(&userstyle, "%s%s%s", authctxt->user,
f09e2e
 	    authctxt->style ? ":" : "",
f09e2e
 	    authctxt->style ? authctxt->style : "");
1d31ef
@@ -1269,7 +1299,7 @@ monitor_valid_hostbasedblob(u_char *data
f09e2e
     char *chost)
f09e2e
 {
f09e2e
 	Buffer b;
f09e2e
-	char *p, *userstyle;
f09e2e
+	char *p, *r, *userstyle;
f09e2e
 	u_int len;
f09e2e
 	int fail = 0;
f09e2e
 
1d31ef
@@ -1286,6 +1316,8 @@ monitor_valid_hostbasedblob(u_char *data
f09e2e
 	if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
f09e2e
 		fail++;
f09e2e
 	p = buffer_get_cstring(&b, NULL);
f09e2e
+	if ((r = strchr(p, '/')) != NULL)
f09e2e
+		*r = '\0';
f09e2e
 	xasprintf(&userstyle, "%s%s%s", authctxt->user,
f09e2e
 	    authctxt->style ? ":" : "",
f09e2e
 	    authctxt->style ? authctxt->style : "");
1d31ef
diff -up openssh-7.4p1/monitor.h.role-mls openssh-7.4p1/monitor.h
1d31ef
--- openssh-7.4p1/monitor.h.role-mls	2016-12-19 05:59:41.000000000 +0100
1d31ef
+++ openssh-7.4p1/monitor.h	2017-02-08 14:08:30.271308186 +0100
017ff1
@@ -57,6 +57,10 @@ enum monitor_reqtype {
017ff1
 	MONITOR_REQ_GSSCHECKMIC = 48, MONITOR_ANS_GSSCHECKMIC = 49,
017ff1
 	MONITOR_REQ_TERM = 50,
017ff1
 
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+	MONITOR_REQ_AUTHROLE = 80,
f09e2e
+#endif
017ff1
+
f09e2e
 	MONITOR_REQ_PAM_START = 100,
f09e2e
 	MONITOR_REQ_PAM_ACCOUNT = 102, MONITOR_ANS_PAM_ACCOUNT = 103,
017ff1
 	MONITOR_REQ_PAM_INIT_CTX = 104, MONITOR_ANS_PAM_INIT_CTX = 105,
1d31ef
diff -up openssh-7.4p1/monitor_wrap.c.role-mls openssh-7.4p1/monitor_wrap.c
1d31ef
--- openssh-7.4p1/monitor_wrap.c.role-mls	2016-12-19 05:59:41.000000000 +0100
1d31ef
+++ openssh-7.4p1/monitor_wrap.c	2017-02-08 14:08:30.271308186 +0100
1d31ef
@@ -345,6 +345,25 @@ mm_inform_authserv(char *service, char *
f09e2e
 	buffer_free(&m);
f09e2e
 }
f09e2e
 
f09e2e
+/* Inform the privileged process about role */
f09e2e
+
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+void
f09e2e
+mm_inform_authrole(char *role)
f09e2e
+{
f09e2e
+	Buffer m;
f09e2e
+
f09e2e
+	debug3("%s entering", __func__);
f09e2e
+
f09e2e
+	buffer_init(&m);
f09e2e
+	buffer_put_cstring(&m, role ? role : "");
f09e2e
+
f09e2e
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHROLE, &m);
f09e2e
+
f09e2e
+	buffer_free(&m);
f09e2e
+}
f09e2e
+#endif
f09e2e
+
f09e2e
 /* Do the password authentication */
f09e2e
 int
f09e2e
 mm_auth_password(Authctxt *authctxt, char *password)
1d31ef
diff -up openssh-7.4p1/monitor_wrap.h.role-mls openssh-7.4p1/monitor_wrap.h
1d31ef
--- openssh-7.4p1/monitor_wrap.h.role-mls       2016-12-19 05:59:41.000000000 +0100
1d31ef
+++ openssh-7.4p1/monitor_wrap.h        2016-12-23 12:19:58.588459376 +0100
f09e2e
@@ -42,6 +42,9 @@ int mm_is_monitor(void);
f09e2e
 DH *mm_choose_dh(int, int, int);
1d31ef
 int mm_key_sign(Key *, u_char **, u_int *, const u_char *, u_int, const char *);
f09e2e
 void mm_inform_authserv(char *, char *);
f09e2e
+#ifdef WITH_SELINUX
f09e2e
+void mm_inform_authrole(char *);
f09e2e
+#endif
f09e2e
 struct passwd *mm_getpwnamallow(const char *);
f09e2e
 char *mm_auth2_read_banner(void);
f09e2e
 int mm_auth_password(struct Authctxt *, char *);
017ff1
diff --git a/openbsd-compat/Makefile.in b/openbsd-compat/Makefile.in
017ff1
index 6ecfb93..b912dbe 100644
017ff1
--- a/openbsd-compat/Makefile.in
017ff1
+++ b/openbsd-compat/Makefile.in
017ff1
@@ -20,7 +20,7 @@ OPENBSD=base64.o basename.o bcrypt_pbkdf.o bindresvport.o blowfish.o daemon.o di
f09e2e
 
1d31ef
 COMPAT=arc4random.o bsd-asprintf.o bsd-closefrom.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o getrrsetbyname-ldns.o bsd-err.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-poll.o bsd-setres_id.o bsd-snprintf.o bsd-statvfs.o bsd-waitpid.o fake-rfc2553.o openssl-compat.o xcrypt.o kludge-fd_set.o
f09e2e
 
f09e2e
-PORTS=port-aix.o port-irix.o port-linux.o port-solaris.o port-tun.o port-uw.o
017ff1
+PORTS=port-aix.o port-irix.o port-linux.o port-linux-sshd.o port-solaris.o port-tun.o port-uw.o
f09e2e
 
f09e2e
 .c.o:
f09e2e
 	$(CC) $(CFLAGS) $(CPPFLAGS) -c $<
1d31ef
diff -up openssh-7.4p1/openbsd-compat/port-linux.c.role-mls openssh-7.4p1/openbsd-compat/port-linux.c
1d31ef
--- openssh-7.4p1/openbsd-compat/port-linux.c.role-mls	2016-12-19 05:59:41.000000000 +0100
1d31ef
+++ openssh-7.4p1/openbsd-compat/port-linux.c	2017-02-08 14:08:30.272308185 +0100
1d31ef
@@ -101,37 +101,6 @@ ssh_selinux_getctxbyname(char *pwname)
1d31ef
 	return sc;
1d31ef
 }
1d31ef
 
1d31ef
-/* Set the execution context to the default for the specified user */
1d31ef
-void
1d31ef
-ssh_selinux_setup_exec_context(char *pwname)
1d31ef
-{
1d31ef
-	security_context_t user_ctx = NULL;
1d31ef
-
1d31ef
-	if (!ssh_selinux_enabled())
1d31ef
-		return;
1d31ef
-
1d31ef
-	debug3("%s: setting execution context", __func__);
1d31ef
-
1d31ef
-	user_ctx = ssh_selinux_getctxbyname(pwname);
1d31ef
-	if (setexeccon(user_ctx) != 0) {
1d31ef
-		switch (security_getenforce()) {
1d31ef
-		case -1:
1d31ef
-			fatal("%s: security_getenforce() failed", __func__);
1d31ef
-		case 0:
1d31ef
-			error("%s: Failed to set SELinux execution "
1d31ef
-			    "context for %s", __func__, pwname);
1d31ef
-			break;
1d31ef
-		default:
1d31ef
-			fatal("%s: Failed to set SELinux execution context "
1d31ef
-			    "for %s (in enforcing mode)", __func__, pwname);
1d31ef
-		}
1d31ef
-	}
1d31ef
-	if (user_ctx != NULL)
1d31ef
-		freecon(user_ctx);
1d31ef
-
1d31ef
-	debug3("%s: done", __func__);
1d31ef
-}
1d31ef
-
1d31ef
 /* Set the TTY context for the specified user */
1d31ef
 void
1d31ef
 ssh_selinux_setup_pty(char *pwname, const char *tty)
1d31ef
diff -up openssh-7.4p1/openbsd-compat/port-linux.h.role-mls openssh-7.4p1/openbsd-compat/port-linux.h
1d31ef
--- openssh-7.4p1/openbsd-compat/port-linux.h.role-mls	2016-12-19 05:59:41.000000000 +0100
1d31ef
+++ openssh-7.4p1/openbsd-compat/port-linux.h	2017-02-08 14:08:30.272308185 +0100
1d31ef
@@ -20,9 +20,10 @@
1d31ef
 #ifdef WITH_SELINUX
1d31ef
 int ssh_selinux_enabled(void);
1d31ef
 void ssh_selinux_setup_pty(char *, const char *);
1d31ef
-void ssh_selinux_setup_exec_context(char *);
1d31ef
 void ssh_selinux_change_context(const char *);
1d31ef
 void ssh_selinux_setfscreatecon(const char *);
1d31ef
+
1d31ef
+void sshd_selinux_setup_exec_context(char *);
1d31ef
 #endif
1d31ef
 
1d31ef
 #ifdef LINUX_OOM_ADJUST
1d31ef
diff -up openssh-7.4p1/openbsd-compat/port-linux-sshd.c.role-mls openssh-7.4p1/openbsd-compat/port-linux-sshd.c
1d31ef
--- openssh-7.4p1/openbsd-compat/port-linux-sshd.c.role-mls	2017-02-08 14:08:30.272308185 +0100
1d31ef
+++ openssh-7.4p1/openbsd-compat/port-linux-sshd.c	2017-02-08 14:08:30.272308185 +0100
1d31ef
@@ -0,0 +1,415 @@
017ff1
+/*
017ff1
+ * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com>
017ff1
+ * Copyright (c) 2014 Petr Lautrbach <plautrba@redhat.com>
017ff1
+ *
017ff1
+ * Permission to use, copy, modify, and distribute this software for any
017ff1
+ * purpose with or without fee is hereby granted, provided that the above
017ff1
+ * copyright notice and this permission notice appear in all copies.
017ff1
+ *
017ff1
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
017ff1
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
017ff1
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
017ff1
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
017ff1
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
017ff1
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
017ff1
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
017ff1
+ */
017ff1
+
017ff1
+/*
017ff1
+ * Linux-specific portability code - just SELinux support for sshd at present
017ff1
+ */
017ff1
+
017ff1
+#include "includes.h"
017ff1
+
017ff1
+#if defined(WITH_SELINUX) || defined(LINUX_OOM_ADJUST)
017ff1
+#include <errno.h>
017ff1
+#include <stdarg.h>
017ff1
+#include <string.h>
017ff1
+#include <stdio.h>
017ff1
+
017ff1
+#include "log.h"
017ff1
+#include "xmalloc.h"
1d31ef
+#include "misc.h"      /* servconf.h needs misc.h for struct ForwardOptions */
f09e2e
+#include "servconf.h"
017ff1
+#include "port-linux.h"
f09e2e
+#include "key.h"
f09e2e
+#include "hostfile.h"
f09e2e
+#include "auth.h"
017ff1
+
017ff1
+#ifdef WITH_SELINUX
017ff1
+#include <selinux/selinux.h>
017ff1
+#include <selinux/flask.h>
f09e2e
+#include <selinux/context.h>
017ff1
+#include <selinux/get_context_list.h>
f09e2e
+#include <selinux/get_default_type.h>
f09e2e
+#include <selinux/av_permissions.h>
f09e2e
+
f09e2e
+#ifdef HAVE_LINUX_AUDIT
f09e2e
+#include <libaudit.h>
f09e2e
+#include <unistd.h>
f09e2e
+#endif
017ff1
+
f09e2e
+extern ServerOptions options;
f09e2e
+extern Authctxt *the_authctxt;
f09e2e
+extern int inetd_flag;
f09e2e
+extern int rexeced_flag;
f09e2e
+
f09e2e
+/* Send audit message */
f09e2e
+static int
017ff1
+sshd_selinux_send_audit_message(int success, security_context_t default_context,
f09e2e
+		       security_context_t selected_context)
017ff1
+{
f09e2e
+	int rc=0;
f09e2e
+#ifdef HAVE_LINUX_AUDIT
f09e2e
+	char *msg = NULL;
f09e2e
+	int audit_fd = audit_open();
f09e2e
+	security_context_t default_raw=NULL;
f09e2e
+	security_context_t selected_raw=NULL;
f09e2e
+	rc = -1;
f09e2e
+	if (audit_fd < 0) {
f09e2e
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
f09e2e
+					errno == EAFNOSUPPORT)
f09e2e
+				return 0; /* No audit support in kernel */
f09e2e
+		error("Error connecting to audit system.");
f09e2e
+		return rc;
f09e2e
+	}
f09e2e
+	if (selinux_trans_to_raw_context(default_context, &default_raw) < 0) {
f09e2e
+		error("Error translating default context.");
f09e2e
+		default_raw = NULL;
f09e2e
+	}
f09e2e
+	if (selinux_trans_to_raw_context(selected_context, &selected_raw) < 0) {
f09e2e
+		error("Error translating selected context.");
f09e2e
+		selected_raw = NULL;
f09e2e
+	}
f09e2e
+	if (asprintf(&msg, "sshd: default-context=%s selected-context=%s",
f09e2e
+		     default_raw ? default_raw : (default_context ? default_context: "?"),
f09e2e
+		     selected_context ? selected_raw : (selected_context ? selected_context :"?")) < 0) {
f09e2e
+		error("Error allocating memory.");
f09e2e
+		goto out;
f09e2e
+	}
f09e2e
+	if (audit_log_user_message(audit_fd, AUDIT_USER_ROLE_CHANGE,
f09e2e
+				   msg, NULL, NULL, NULL, success) <= 0) {
f09e2e
+		error("Error sending audit message.");
f09e2e
+		goto out;
f09e2e
+	}
f09e2e
+	rc = 0;
f09e2e
+      out:
f09e2e
+	free(msg);
f09e2e
+	freecon(default_raw);
f09e2e
+	freecon(selected_raw);
f09e2e
+	close(audit_fd);
f09e2e
+#endif
f09e2e
+	return rc;
f09e2e
+}
f09e2e
+
f09e2e
+static int
f09e2e
+mls_range_allowed(security_context_t src, security_context_t dst)
f09e2e
+{
f09e2e
+	struct av_decision avd;
f09e2e
+	int retval;
f09e2e
+	unsigned int bit = CONTEXT__CONTAINS;
f09e2e
+
f09e2e
+	debug("%s: src:%s dst:%s", __func__, src, dst);
f09e2e
+	retval = security_compute_av(src, dst, SECCLASS_CONTEXT, bit, &avd);
f09e2e
+	if (retval || ((bit & avd.allowed) != bit))
f09e2e
+		return 0;
f09e2e
+
f09e2e
+	return 1;
f09e2e
+}
f09e2e
+
f09e2e
+static int
f09e2e
+get_user_context(const char *sename, const char *role, const char *lvl,
f09e2e
+	security_context_t *sc) {
f09e2e
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
f09e2e
+	if (lvl == NULL || lvl[0] == '\0' || get_default_context_with_level(sename, lvl, NULL, sc) != 0) {
f09e2e
+	        /* User may have requested a level completely outside of his 
f09e2e
+	           allowed range. We get a context just for auditing as the
f09e2e
+	           range check below will certainly fail for default context. */
f09e2e
+#endif
f09e2e
+		if (get_default_context(sename, NULL, sc) != 0) {
f09e2e
+			*sc = NULL;
f09e2e
+			return -1;
f09e2e
+		}
f09e2e
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
f09e2e
+	}
f09e2e
+#endif
f09e2e
+	if (role != NULL && role[0]) {
f09e2e
+		context_t con;
f09e2e
+		char *type=NULL;
f09e2e
+		if (get_default_type(role, &type) != 0) {
f09e2e
+			error("get_default_type: failed to get default type for '%s'",
f09e2e
+				role);
f09e2e
+			goto out;
f09e2e
+		}
f09e2e
+		con = context_new(*sc);
f09e2e
+		if (!con) {
f09e2e
+			goto out;
f09e2e
+		}
f09e2e
+		context_role_set(con, role);
f09e2e
+		context_type_set(con, type);
f09e2e
+		freecon(*sc);
f09e2e
+		*sc = strdup(context_str(con));
f09e2e
+		context_free(con);
017ff1
+		if (!*sc)
f09e2e
+			return -1;
f09e2e
+	}
f09e2e
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
f09e2e
+	if (lvl != NULL && lvl[0]) {
f09e2e
+		/* verify that the requested range is obtained */
f09e2e
+		context_t con;
f09e2e
+		security_context_t obtained_raw;
f09e2e
+		security_context_t requested_raw;
f09e2e
+		con = context_new(*sc);
f09e2e
+		if (!con) {
f09e2e
+			goto out;
f09e2e
+		}
f09e2e
+		context_range_set(con, lvl);
f09e2e
+		if (selinux_trans_to_raw_context(*sc, &obtained_raw) < 0) {
f09e2e
+			context_free(con);
f09e2e
+			goto out;
f09e2e
+		}
f09e2e
+		if (selinux_trans_to_raw_context(context_str(con), &requested_raw) < 0) {
f09e2e
+			freecon(obtained_raw);
f09e2e
+			context_free(con);
f09e2e
+			goto out;
f09e2e
+		}
017ff1
+
f09e2e
+		debug("get_user_context: obtained context '%s' requested context '%s'",
f09e2e
+			obtained_raw, requested_raw);
f09e2e
+		if (strcmp(obtained_raw, requested_raw)) {
f09e2e
+			/* set the context to the real requested one but fail */
f09e2e
+			freecon(requested_raw);
f09e2e
+			freecon(obtained_raw);
f09e2e
+			freecon(*sc);
f09e2e
+			*sc = strdup(context_str(con));
f09e2e
+			context_free(con);
f09e2e
+			return -1;
f09e2e
+		}
f09e2e
+		freecon(requested_raw);
f09e2e
+		freecon(obtained_raw);
f09e2e
+		context_free(con);
017ff1
+	}
f09e2e
+#endif
f09e2e
+	return 0;
f09e2e
+      out:
f09e2e
+	freecon(*sc);
f09e2e
+	*sc = NULL;
f09e2e
+	return -1;
f09e2e
+}
017ff1
+
f09e2e
+static void
f09e2e
+ssh_selinux_get_role_level(char **role, const char **level)
f09e2e
+{
f09e2e
+	*role = NULL;
f09e2e
+	*level = NULL;
f09e2e
+	if (the_authctxt) {
f09e2e
+		if (the_authctxt->role != NULL) {
f09e2e
+			char *slash;
f09e2e
+			*role = xstrdup(the_authctxt->role);
f09e2e
+			if ((slash = strchr(*role, '/')) != NULL) {
f09e2e
+				*slash = '\0';
f09e2e
+				*level = slash + 1;
f09e2e
+			}
f09e2e
+		}
f09e2e
+	}
017ff1
+}
017ff1
+
017ff1
+/* Return the default security context for the given username */
017ff1
+static int
017ff1
+sshd_selinux_getctxbyname(char *pwname,
f09e2e
+	security_context_t *default_sc, security_context_t *user_sc)
017ff1
+{
f09e2e
+	char *sename, *lvl;
f09e2e
+	char *role;
f09e2e
+	const char *reqlvl;
f09e2e
+	int r = 0;
f09e2e
+	context_t con = NULL;
017ff1
+
f09e2e
+	ssh_selinux_get_role_level(&role, &reqlvl);
017ff1
+
017ff1
+#ifdef HAVE_GETSEUSERBYNAME
f09e2e
+	if ((r=getseuserbyname(pwname, &sename, &lvl)) != 0) {
f09e2e
+		sename = NULL;
f09e2e
+		lvl = NULL;
f09e2e
+	}
017ff1
+#else
017ff1
+	sename = pwname;
f09e2e
+	lvl = "";
017ff1
+#endif
017ff1
+
f09e2e
+	if (r == 0) {
017ff1
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
f09e2e
+		r = get_default_context_with_level(sename, lvl, NULL, default_sc);
017ff1
+#else
f09e2e
+		r = get_default_context(sename, NULL, default_sc);
017ff1
+#endif
f09e2e
+	}
f09e2e
+
f09e2e
+	if (r == 0) {
f09e2e
+		/* If launched from xinetd, we must use current level */
f09e2e
+		if (inetd_flag && !rexeced_flag) {
f09e2e
+			security_context_t sshdsc=NULL;
f09e2e
+
f09e2e
+			if (getcon_raw(&sshdsc) < 0)
f09e2e
+				fatal("failed to allocate security context");
f09e2e
+
f09e2e
+			if ((con=context_new(sshdsc)) == NULL)
f09e2e
+				fatal("failed to allocate selinux context");
f09e2e
+			reqlvl = context_range_get(con);
f09e2e
+			freecon(sshdsc);
f09e2e
+			if (reqlvl !=NULL && lvl != NULL && strcmp(reqlvl, lvl) == 0)
f09e2e
+			    /* we actually don't change level */
f09e2e
+			    reqlvl = "";
f09e2e
+
f09e2e
+			debug("%s: current connection level '%s'", __func__, reqlvl);
017ff1
+
017ff1
+		}
017ff1
+
f09e2e
+		if ((reqlvl != NULL && reqlvl[0]) || (role != NULL && role[0])) {
f09e2e
+			r = get_user_context(sename, role, reqlvl, user_sc);
017ff1
+
f09e2e
+			if (r == 0 && reqlvl != NULL && reqlvl[0]) {
f09e2e
+				security_context_t default_level_sc = *default_sc;
f09e2e
+				if (role != NULL && role[0]) {
f09e2e
+					if (get_user_context(sename, role, lvl, &default_level_sc) < 0)
f09e2e
+						default_level_sc = *default_sc;
f09e2e
+				}
f09e2e
+				/* verify that the requested range is contained in the user range */
f09e2e
+				if (mls_range_allowed(default_level_sc, *user_sc)) {
f09e2e
+					logit("permit MLS level %s (user range %s)", reqlvl, lvl);
f09e2e
+				} else {
f09e2e
+					r = -1;
f09e2e
+					error("deny MLS level %s (user range %s)", reqlvl, lvl);
f09e2e
+				}
f09e2e
+				if (default_level_sc != *default_sc)
f09e2e
+					freecon(default_level_sc);
f09e2e
+			}
f09e2e
+		} else {
f09e2e
+			*user_sc = *default_sc;
f09e2e
+		}
f09e2e
+	}
f09e2e
+	if (r != 0) {
f09e2e
+		error("%s: Failed to get default SELinux security "
f09e2e
+		    "context for %s", __func__, pwname);
017ff1
+	}
017ff1
+
017ff1
+#ifdef HAVE_GETSEUSERBYNAME
017ff1
+	free(sename);
017ff1
+	free(lvl);
017ff1
+#endif
017ff1
+
f09e2e
+	if (role != NULL)
f09e2e
+		free(role);
f09e2e
+	if (con)
f09e2e
+		context_free(con);
f09e2e
+
f09e2e
+	return (r);
f09e2e
+}
f09e2e
+
f09e2e
+/* Setup environment variables for pam_selinux */
f09e2e
+static int
017ff1
+sshd_selinux_setup_pam_variables(void)
f09e2e
+{
f09e2e
+	const char *reqlvl;
f09e2e
+	char *role;
f09e2e
+	char *use_current;
f09e2e
+	int rv;
f09e2e
+
f09e2e
+	debug3("%s: setting execution context", __func__);
f09e2e
+
f09e2e
+	ssh_selinux_get_role_level(&role, &reqlvl);
f09e2e
+
f09e2e
+	rv = do_pam_putenv("SELINUX_ROLE_REQUESTED", role ? role : "");
f09e2e
+
f09e2e
+	if (inetd_flag && !rexeced_flag) {
f09e2e
+		use_current = "1";
f09e2e
+	} else {
f09e2e
+		use_current = "";
f09e2e
+		rv = rv || do_pam_putenv("SELINUX_LEVEL_REQUESTED", reqlvl ? reqlvl: "");
f09e2e
+	}
f09e2e
+
f09e2e
+	rv = rv || do_pam_putenv("SELINUX_USE_CURRENT_RANGE", use_current);
f09e2e
+
f09e2e
+	if (role != NULL)
f09e2e
+		free(role);
f09e2e
+
f09e2e
+	return rv;
017ff1
+}
017ff1
+
017ff1
+/* Set the execution context to the default for the specified user */
017ff1
+void
017ff1
+sshd_selinux_setup_exec_context(char *pwname)
017ff1
+{
017ff1
+	security_context_t user_ctx = NULL;
f09e2e
+	int r = 0;
f09e2e
+	security_context_t default_ctx = NULL;
017ff1
+
017ff1
+	if (!ssh_selinux_enabled())
017ff1
+		return;
017ff1
+
f09e2e
+	if (options.use_pam) {
f09e2e
+		/* do not compute context, just setup environment for pam_selinux */
017ff1
+		if (sshd_selinux_setup_pam_variables()) {
f09e2e
+			switch (security_getenforce()) {
f09e2e
+			case -1:
f09e2e
+				fatal("%s: security_getenforce() failed", __func__);
f09e2e
+			case 0:
f09e2e
+				error("%s: SELinux PAM variable setup failure. Continuing in permissive mode.",
f09e2e
+				    __func__);
f09e2e
+			break;
f09e2e
+			default:
f09e2e
+				fatal("%s: SELinux PAM variable setup failure. Aborting connection.",
f09e2e
+				    __func__);
f09e2e
+			}
f09e2e
+		}
f09e2e
+		return;
f09e2e
+	}
f09e2e
+
017ff1
+	debug3("%s: setting execution context", __func__);
017ff1
+
017ff1
+	r = sshd_selinux_getctxbyname(pwname, &default_ctx, &user_ctx);
f09e2e
+	if (r >= 0) {
f09e2e
+		r = setexeccon(user_ctx);
f09e2e
+		if (r < 0) {
f09e2e
+			error("%s: Failed to set SELinux execution context %s for %s",
f09e2e
+			    __func__, user_ctx, pwname);
017ff1
+		}
f09e2e
+#ifdef HAVE_SETKEYCREATECON
f09e2e
+		else if (setkeycreatecon(user_ctx) < 0) {
f09e2e
+			error("%s: Failed to set SELinux keyring creation context %s for %s",
f09e2e
+			    __func__, user_ctx, pwname);
f09e2e
+		}
f09e2e
+#endif
f09e2e
+	}
f09e2e
+	if (user_ctx == NULL) {
f09e2e
+		user_ctx = default_ctx;
f09e2e
+	}
f09e2e
+	if (r < 0 || user_ctx != default_ctx) {
f09e2e
+		/* audit just the case when user changed a role or there was
f09e2e
+		   a failure */
017ff1
+		sshd_selinux_send_audit_message(r >= 0, default_ctx, user_ctx);
f09e2e
+	}
f09e2e
+	if (r < 0) {
017ff1
+		switch (security_getenforce()) {
017ff1
+		case -1:
017ff1
+			fatal("%s: security_getenforce() failed", __func__);
017ff1
+		case 0:
f09e2e
+			error("%s: SELinux failure. Continuing in permissive mode.",
f09e2e
+			    __func__);
017ff1
+			break;
017ff1
+		default:
f09e2e
+			fatal("%s: SELinux failure. Aborting connection.",
f09e2e
+			    __func__);
017ff1
+		}
017ff1
+	}
f09e2e
+	if (user_ctx != NULL && user_ctx != default_ctx)
017ff1
+		freecon(user_ctx);
f09e2e
+	if (default_ctx != NULL)
f09e2e
+		freecon(default_ctx);
017ff1
+
017ff1
+	debug3("%s: done", __func__);
017ff1
+}
017ff1
+
017ff1
+#endif
017ff1
+#endif
017ff1
+
1d31ef
diff -up openssh-7.4p1/platform.c.role-mls openssh-7.4p1/platform.c
1d31ef
--- openssh-7.4p1/platform.c.role-mls	2016-12-19 05:59:41.000000000 +0100
1d31ef
+++ openssh-7.4p1/platform.c	2017-02-08 14:08:30.272308185 +0100
1d31ef
@@ -184,7 +184,7 @@ platform_setusercontext_post_groups(stru
017ff1
 	}
017ff1
 #endif /* HAVE_SETPCRED */
017ff1
 #ifdef WITH_SELINUX
017ff1
-	ssh_selinux_setup_exec_context(pw->pw_name);
017ff1
+	sshd_selinux_setup_exec_context(pw->pw_name);
017ff1
 #endif
017ff1
 }
017ff1
 
1d31ef
diff -up openssh-7.4p1/sshd.c.role-mls openssh-7.4p1/sshd.c
1d31ef
--- openssh-7.4p1/sshd.c.role-mls	2016-12-19 05:59:41.000000000 +0100
1d31ef
+++ openssh-7.4p1/sshd.c	2017-02-08 14:08:30.273308184 +0100
1d31ef
@@ -2053,6 +2053,9 @@ main(int ac, char **av)
f09e2e
 		restore_uid();
f09e2e
 	}
f09e2e
 #endif
f09e2e
+#ifdef WITH_SELINUX
017ff1
+	sshd_selinux_setup_exec_context(authctxt->pw->pw_name);
f09e2e
+#endif
f09e2e
 #ifdef USE_PAM
f09e2e
 	if (options.use_pam) {
f09e2e
 		do_pam_setcred(1);