Jan F 003cb0b
diff -up openssh-5.8p1/misc.c.mls openssh-5.8p1/misc.c
Jan F 003cb0b
--- openssh-5.8p1/misc.c.mls	2011-01-13 02:21:36.000000000 +0100
Jan F 003cb0b
+++ openssh-5.8p1/misc.c	2011-02-12 15:05:06.000000000 +0100
Jan F 003cb0b
@@ -427,6 +427,7 @@ char *
82bc825
 colon(char *cp)
82bc825
 {
82bc825
 	int flag = 0;
82bc825
+	int start = 1;
82bc825
 
82bc825
 	if (*cp == ':')		/* Leading colon is part of file name. */
Jan F. Chadima 1b8a267
 		return NULL;
Jan F 003cb0b
@@ -442,6 +443,13 @@ colon(char *cp)
82bc825
 			return (cp);
Jan F. Chadima 1b8a267
 		if (*cp == '/')
Jan F. Chadima 1b8a267
 			return NULL;
82bc825
+		if (start) {
82bc825
+		/* Slash on beginning or after dots only denotes file name. */
82bc825
+			if (*cp == '/')
82bc825
+				return (0);
82bc825
+			if (*cp != '.')
82bc825
+				start = 0;
82bc825
+		}
82bc825
 	}
Jan F. Chadima 1b8a267
 	return NULL;
82bc825
 }
Jan F 003cb0b
diff -up openssh-5.8p1/openbsd-compat/port-linux.c.mls openssh-5.8p1/openbsd-compat/port-linux.c
Jan F 003cb0b
--- openssh-5.8p1/openbsd-compat/port-linux.c.mls	2011-02-12 15:05:06.000000000 +0100
Jan F 003cb0b
+++ openssh-5.8p1/openbsd-compat/port-linux.c	2011-02-12 15:09:23.000000000 +0100
Jan F 003cb0b
@@ -40,13 +40,164 @@
82bc825
 #ifdef WITH_SELINUX
82bc825
 #include <selinux/selinux.h>
82bc825
 #include <selinux/flask.h>
82bc825
+#include <selinux/context.h>
82bc825
 #include <selinux/get_context_list.h>
82bc825
+#include <selinux/get_default_type.h>
82bc825
+#include <selinux/av_permissions.h>
82bc825
+
82bc825
+#ifdef HAVE_LINUX_AUDIT
82bc825
+#include <libaudit.h>
82bc825
+#include <unistd.h>
82bc825
+#endif
82bc825
 
Jan F 003cb0b
 extern ServerOptions options;
82bc825
 extern Authctxt *the_authctxt;
Jan F 003cb0b
 extern int inetd_flag;
Jan F 003cb0b
 extern int rexeced_flag;
82bc825
 
82bc825
+/* Send audit message */
82bc825
+static int
82bc825
+send_audit_message(int success, security_context_t default_context,
82bc825
+		       security_context_t selected_context)
82bc825
+{
82bc825
+	int rc=0;
82bc825
+#ifdef HAVE_LINUX_AUDIT
82bc825
+	char *msg = NULL;
82bc825
+	int audit_fd = audit_open();
82bc825
+	security_context_t default_raw=NULL;
82bc825
+	security_context_t selected_raw=NULL;
82bc825
+	rc = -1;
82bc825
+	if (audit_fd < 0) {
82bc825
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
Jan F 003cb0b
+					errno == EAFNOSUPPORT)
Jan F 003cb0b
+				return 0; /* No audit support in kernel */
82bc825
+		error("Error connecting to audit system.");
82bc825
+		return rc;
82bc825
+	}
82bc825
+	if (selinux_trans_to_raw_context(default_context, &default_raw) < 0) {
82bc825
+		error("Error translating default context.");
82bc825
+		default_raw = NULL;
82bc825
+	}
82bc825
+	if (selinux_trans_to_raw_context(selected_context, &selected_raw) < 0) {
82bc825
+		error("Error translating selected context.");
82bc825
+		selected_raw = NULL;
82bc825
+	}
82bc825
+	if (asprintf(&msg, "sshd: default-context=%s selected-context=%s",
82bc825
+		     default_raw ? default_raw : (default_context ? default_context: "?"),
82bc825
+		     selected_context ? selected_raw : (selected_context ? selected_context :"?")) < 0) {
82bc825
+		error("Error allocating memory.");
82bc825
+		goto out;
82bc825
+	}
82bc825
+	if (audit_log_user_message(audit_fd, AUDIT_USER_ROLE_CHANGE,
82bc825
+				   msg, NULL, NULL, NULL, success) <= 0) {
82bc825
+		error("Error sending audit message.");
82bc825
+		goto out;
82bc825
+	}
82bc825
+	rc = 0;
82bc825
+      out:
82bc825
+	free(msg);
82bc825
+	freecon(default_raw);
82bc825
+	freecon(selected_raw);
82bc825
+	close(audit_fd);
82bc825
+#endif
82bc825
+	return rc;
82bc825
+}
82bc825
+
82bc825
+static int
82bc825
+mls_range_allowed(security_context_t src, security_context_t dst)
82bc825
+{
82bc825
+	struct av_decision avd;
82bc825
+	int retval;
82bc825
+	unsigned int bit = CONTEXT__CONTAINS;
82bc825
+
82bc825
+	debug("%s: src:%s dst:%s", __func__, src, dst);
82bc825
+	retval = security_compute_av(src, dst, SECCLASS_CONTEXT, bit, &avd);
82bc825
+	if (retval || ((bit & avd.allowed) != bit))
82bc825
+		return 0;
82bc825
+
82bc825
+	return 1;
82bc825
+}
82bc825
+
82bc825
+static int
82bc825
+get_user_context(const char *sename, const char *role, const char *lvl,
82bc825
+	security_context_t *sc) {
82bc825
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
82bc825
+	if (lvl == NULL || lvl[0] == '\0' || get_default_context_with_level(sename, lvl, NULL, sc) != 0) {
82bc825
+	        /* User may have requested a level completely outside of his 
82bc825
+	           allowed range. We get a context just for auditing as the
82bc825
+	           range check below will certainly fail for default context. */
82bc825
+#endif
82bc825
+		if (get_default_context(sename, NULL, sc) != 0) {
82bc825
+			*sc = NULL;
82bc825
+			return -1;
82bc825
+		}
82bc825
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
82bc825
+	}
82bc825
+#endif
82bc825
+	if (role != NULL && role[0]) {
82bc825
+		context_t con;
82bc825
+		char *type=NULL;
82bc825
+		if (get_default_type(role, &type) != 0) {
82bc825
+			error("get_default_type: failed to get default type for '%s'",
82bc825
+				role);
82bc825
+			goto out;
82bc825
+		}
82bc825
+		con = context_new(*sc);
82bc825
+		if (!con) {
82bc825
+			goto out;
82bc825
+		}
82bc825
+		context_role_set(con, role);
82bc825
+		context_type_set(con, type);
82bc825
+		freecon(*sc);
82bc825
+		*sc = strdup(context_str(con));
82bc825
+		context_free(con);
82bc825
+		if (!*sc) 
82bc825
+			return -1;
82bc825
+	}
82bc825
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
82bc825
+	if (lvl != NULL && lvl[0]) {
82bc825
+		/* verify that the requested range is obtained */
82bc825
+		context_t con;
82bc825
+		security_context_t obtained_raw;
82bc825
+		security_context_t requested_raw;
82bc825
+		con = context_new(*sc);
82bc825
+		if (!con) {
82bc825
+			goto out;
82bc825
+		}
82bc825
+		context_range_set(con, lvl);
82bc825
+		if (selinux_trans_to_raw_context(*sc, &obtained_raw) < 0) {
82bc825
+			context_free(con);
82bc825
+			goto out;
82bc825
+		}
82bc825
+		if (selinux_trans_to_raw_context(context_str(con), &requested_raw) < 0) {
82bc825
+			freecon(obtained_raw);
82bc825
+			context_free(con);
82bc825
+			goto out;
82bc825
+		}
82bc825
+
82bc825
+		debug("get_user_context: obtained context '%s' requested context '%s'",
82bc825
+			obtained_raw, requested_raw);
82bc825
+		if (strcmp(obtained_raw, requested_raw)) {
82bc825
+			/* set the context to the real requested one but fail */
82bc825
+			freecon(requested_raw);
82bc825
+			freecon(obtained_raw);
82bc825
+			freecon(*sc);
82bc825
+			*sc = strdup(context_str(con));
82bc825
+			context_free(con);
82bc825
+			return -1;
82bc825
+		}
82bc825
+		freecon(requested_raw);
82bc825
+		freecon(obtained_raw);
82bc825
+		context_free(con);
82bc825
+	}
82bc825
+#endif
82bc825
+	return 0;
82bc825
+      out:
Jan F 003cb0b
+	freecon(*sc);
Jan F 003cb0b
+	*sc = NULL;
Jan F 003cb0b
+	return -1;
82bc825
+}
82bc825
+
Jan F 003cb0b
 static void
Jan F 003cb0b
 ssh_selinux_get_role_level(char **role, const char **level)
Jan F 003cb0b
 {
Jan F 8a77a1d
@@ -65,14 +216,15 @@ ssh_selinux_get_role_level(char **role, 
Jan F 003cb0b
 }
Jan F 003cb0b
 
82bc825
 /* Return the default security context for the given username */
82bc825
-static security_context_t
82bc825
-ssh_selinux_getctxbyname(char *pwname)
82bc825
+static int
82bc825
+ssh_selinux_getctxbyname(char *pwname,
82bc825
+	security_context_t *default_sc, security_context_t *user_sc)
82bc825
 {
Jan F 8a77a1d
-	security_context_t sc = NULL;
82bc825
 	char *sename, *lvl;
Jan F 003cb0b
 	char *role;
Jan F 003cb0b
 	const char *reqlvl;
Jan F 003cb0b
 	int r = 0;
Jan F 8a77a1d
+	context_t con = NULL;
82bc825
 
Jan F 003cb0b
 	ssh_selinux_get_role_level(&role, &reqlvl);
82bc825
 #ifdef HAVE_GETSEUSERBYNAME
Jan F 003cb0b
@@ -82,38 +235,63 @@ ssh_selinux_getctxbyname(char *pwname)
82bc825
 	}
82bc825
 #else
82bc825
 	sename = pwname;
82bc825
-	lvl = NULL;
82bc825
+	lvl = "";
82bc825
 #endif
82bc825
 
82bc825
 	if (r == 0) {
82bc825
 #ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
82bc825
-		if (role != NULL && role[0])
82bc825
-			r = get_default_context_with_rolelevel(sename, role, lvl, NULL, &sc);
82bc825
-		else
82bc825
-			r = get_default_context_with_level(sename, lvl, NULL, &sc);
82bc825
+		r = get_default_context_with_level(sename, lvl, NULL, default_sc);
82bc825
 #else
82bc825
-		if (role != NULL && role[0])
82bc825
-			r = get_default_context_with_role(sename, role, NULL, &sc);
82bc825
-		else
82bc825
-			r = get_default_context(sename, NULL, &sc);
82bc825
+		r = get_default_context(sename, NULL, default_sc);
82bc825
 #endif
82bc825
 	}
82bc825
 
82bc825
-	if (r != 0) {
82bc825
-		switch (security_getenforce()) {
82bc825
-		case -1:
82bc825
-			fatal("%s: ssh_selinux_getctxbyname: "
82bc825
-			    "security_getenforce() failed", __func__);
82bc825
-		case 0:
82bc825
-			error("%s: Failed to get default SELinux security "
82bc825
-			    "context for %s", __func__, pwname);
82bc825
-			break;
82bc825
-		default:
82bc825
-			fatal("%s: Failed to get default SELinux security "
82bc825
-			    "context for %s (in enforcing mode)",
82bc825
-			    __func__, pwname);
82bc825
+	if (r == 0) {
82bc825
+		/* If launched from xinetd, we must use current level */
82bc825
+		if (inetd_flag && !rexeced_flag) {
82bc825
+			security_context_t sshdsc=NULL;
82bc825
+
82bc825
+			if (getcon_raw(&sshdsc) < 0)
82bc825
+				fatal("failed to allocate security context");
82bc825
+
82bc825
+			if ((con=context_new(sshdsc)) == NULL)
82bc825
+				fatal("failed to allocate selinux context");
82bc825
+			reqlvl = context_range_get(con);
82bc825
+			freecon(sshdsc);
82bc825
+			if (reqlvl !=NULL && lvl != NULL && strcmp(reqlvl, lvl) == 0)
82bc825
+			    /* we actually don't change level */
82bc825
+			    reqlvl = "";
82bc825
+
82bc825
+			debug("%s: current connection level '%s'", __func__, reqlvl);
82bc825
+		}
82bc825
+		
82bc825
+		if ((reqlvl != NULL && reqlvl[0]) || (role != NULL && role[0])) {
82bc825
+			r = get_user_context(sename, role, reqlvl, user_sc);
82bc825
+		
82bc825
+			if (r == 0 && reqlvl != NULL && reqlvl[0]) {
82bc825
+				security_context_t default_level_sc = *default_sc;
82bc825
+				if (role != NULL && role[0]) {
82bc825
+					if (get_user_context(sename, role, lvl, &default_level_sc) < 0)
82bc825
+						default_level_sc = *default_sc;
82bc825
+				}
82bc825
+				/* verify that the requested range is contained in the user range */
82bc825
+				if (mls_range_allowed(default_level_sc, *user_sc)) {
82bc825
+					logit("permit MLS level %s (user range %s)", reqlvl, lvl);
82bc825
+				} else {
82bc825
+					r = -1;
82bc825
+					error("deny MLS level %s (user range %s)", reqlvl, lvl);
82bc825
+				}
82bc825
+				if (default_level_sc != *default_sc)
82bc825
+					freecon(default_level_sc);
82bc825
+			}
82bc825
+		} else {
82bc825
+			*user_sc = *default_sc;
82bc825
 		}
82bc825
 	}
82bc825
+	if (r != 0) {
82bc825
+		error("%s: Failed to get default SELinux security "
82bc825
+		    "context for %s", __func__, pwname);
82bc825
+	}
82bc825
 
82bc825
 #ifdef HAVE_GETSEUSERBYNAME
82bc825
 	if (sename != NULL)
Jan F 003cb0b
@@ -121,8 +299,12 @@ ssh_selinux_getctxbyname(char *pwname)
82bc825
 	if (lvl != NULL)
82bc825
 		xfree(lvl);
82bc825
 #endif
Jan F 003cb0b
-
Jan F 003cb0b
-	return (sc);
82bc825
+	if (role != NULL)
82bc825
+		xfree(role);
82bc825
+	if (con)
82bc825
+		context_free(con);
Jan F 003cb0b
+ 
82bc825
+	return (r);
82bc825
 }
82bc825
 
Jan F 003cb0b
 /* Setup environment variables for pam_selinux */
Jan F 003cb0b
@@ -160,6 +342,8 @@ void
82bc825
 ssh_selinux_setup_exec_context(char *pwname)
82bc825
 {
Jan F 003cb0b
 	security_context_t user_ctx = NULL;
82bc825
+	int r = 0;
82bc825
+	security_context_t default_ctx = NULL;
82bc825
 
82bc825
 	if (!ssh_selinux_enabled())
Jan F 003cb0b
 		return;
Jan F 003cb0b
@@ -184,22 +368,45 @@ ssh_selinux_setup_exec_context(char *pwn
82bc825
 
82bc825
 	debug3("%s: setting execution context", __func__);
82bc825
 
82bc825
-	user_ctx = ssh_selinux_getctxbyname(pwname);
82bc825
-	if (setexeccon(user_ctx) != 0) {
82bc825
+	r = ssh_selinux_getctxbyname(pwname, &default_ctx, &user_ctx);
82bc825
+	if (r >= 0) {
82bc825
+		r = setexeccon(user_ctx);
82bc825
+		if (r < 0) {
82bc825
+			error("%s: Failed to set SELinux execution context %s for %s",
82bc825
+			    __func__, user_ctx, pwname);
82bc825
+		} 
82bc825
+#ifdef HAVE_SETKEYCREATECON
82bc825
+		else if (setkeycreatecon(user_ctx) < 0) {
82bc825
+			error("%s: Failed to set SELinux keyring creation context %s for %s",
82bc825
+			    __func__, user_ctx, pwname);
82bc825
+		}
82bc825
+#endif
82bc825
+	}
82bc825
+	if (user_ctx == NULL) {
82bc825
+		user_ctx = default_ctx;
82bc825
+	}
82bc825
+	if (r < 0 || user_ctx != default_ctx) {
82bc825
+		/* audit just the case when user changed a role or there was
82bc825
+		   a failure */
82bc825
+		send_audit_message(r >= 0, default_ctx, user_ctx);
82bc825
+	}
82bc825
+	if (r < 0) {
82bc825
 		switch (security_getenforce()) {
82bc825
 		case -1:
82bc825
 			fatal("%s: security_getenforce() failed", __func__);
82bc825
 		case 0:
82bc825
-			error("%s: Failed to set SELinux execution "
82bc825
-			    "context for %s", __func__, pwname);
82bc825
+			error("%s: SELinux failure. Continuing in permissive mode.",
82bc825
+			    __func__);
82bc825
 			break;
82bc825
 		default:
82bc825
-			fatal("%s: Failed to set SELinux execution context "
82bc825
-			    "for %s (in enforcing mode)", __func__, pwname);
82bc825
+			fatal("%s: SELinux failure. Aborting connection.",
82bc825
+			    __func__);
82bc825
 		}
82bc825
 	}
82bc825
-	if (user_ctx != NULL)
82bc825
+	if (user_ctx != NULL && user_ctx != default_ctx)
82bc825
 		freecon(user_ctx);
82bc825
+	if (default_ctx != NULL)
82bc825
+		freecon(default_ctx);
82bc825
 
82bc825
 	debug3("%s: done", __func__);
82bc825
 }
Jan F 003cb0b
@@ -217,7 +424,10 @@ ssh_selinux_setup_pty(char *pwname, cons
82bc825
 
82bc825
 	debug3("%s: setting TTY context on %s", __func__, tty);
82bc825
 
82bc825
-	user_ctx = ssh_selinux_getctxbyname(pwname);
82bc825
+	if (getexeccon(&user_ctx) < 0) {
82bc825
+		error("%s: getexeccon: %s", __func__, strerror(errno));
82bc825
+		goto out;
82bc825
+	}
82bc825
 
82bc825
 	/* XXX: should these calls fatal() upon failure in enforcing mode? */
82bc825
 
Jan F 003cb0b
diff -up openssh-5.8p1/sshd.c.mls openssh-5.8p1/sshd.c
Jan F 003cb0b
--- openssh-5.8p1/sshd.c.mls	2011-02-12 15:05:05.000000000 +0100
Jan F 003cb0b
+++ openssh-5.8p1/sshd.c	2011-02-12 15:05:06.000000000 +0100
Jan F 003cb0b
@@ -2011,6 +2011,9 @@ main(int ac, char **av)
82bc825
 		restore_uid();
82bc825
 	}
82bc825
 #endif
82bc825
+#ifdef WITH_SELINUX
82bc825
+	ssh_selinux_setup_exec_context(authctxt->pw->pw_name);
82bc825
+#endif
82bc825
 #ifdef USE_PAM
82bc825
 	if (options.use_pam) {
82bc825
 		do_pam_setcred(1);