jonathancammack / rpms / openssh

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