bmh10 / rpms / openssh

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