c9833c9
diff -up openssh-4.7p1/misc.c.mls openssh-4.7p1/misc.c
c9833c9
--- openssh-4.7p1/misc.c.mls	2007-01-05 06:24:48.000000000 +0100
c9833c9
+++ openssh-4.7p1/misc.c	2007-09-06 17:39:28.000000000 +0200
c9833c9
@@ -418,6 +418,7 @@ char *
c9833c9
 colon(char *cp)
c9833c9
 {
c9833c9
 	int flag = 0;
c9833c9
+	int start = 1;
c9833c9
 
c9833c9
 	if (*cp == ':')		/* Leading colon is part of file name. */
c9833c9
 		return (0);
c9833c9
@@ -431,8 +432,13 @@ colon(char *cp)
c9833c9
 			return (cp+1);
c9833c9
 		if (*cp == ':' && !flag)
c9833c9
 			return (cp);
c9833c9
-		if (*cp == '/')
c9833c9
-			return (0);
c9833c9
+		if (start) {
c9833c9
+		/* Slash on beginning or after dots only denotes file name. */
c9833c9
+			if (*cp == '/')
c9833c9
+				return (0);
c9833c9
+			if (*cp != '.')
c9833c9
+				start = 0;
c9833c9
+		}
c9833c9
 	}
c9833c9
 	return (0);
c9833c9
 }
c9833c9
diff -up openssh-4.7p1/session.c.mls openssh-4.7p1/session.c
c9833c9
--- openssh-4.7p1/session.c.mls	2007-09-06 17:39:28.000000000 +0200
c9833c9
+++ openssh-4.7p1/session.c	2007-09-06 17:39:28.000000000 +0200
c9833c9
@@ -1347,10 +1347,6 @@ do_setusercontext(struct passwd *pw)
c9833c9
 #endif
c9833c9
 	if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)
c9833c9
 		fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);
c9833c9
-
c9833c9
-#ifdef WITH_SELINUX
c9833c9
-	ssh_selinux_setup_exec_context(pw->pw_name);
c9833c9
-#endif
c9833c9
 }
c9833c9
 
c9833c9
 static void
c9833c9
diff -up openssh-4.7p1/openbsd-compat/port-linux.c.mls openssh-4.7p1/openbsd-compat/port-linux.c
c9833c9
--- openssh-4.7p1/openbsd-compat/port-linux.c.mls	2007-09-06 17:39:28.000000000 +0200
c9833c9
+++ openssh-4.7p1/openbsd-compat/port-linux.c	2007-08-07 17:38:18.000000000 +0200
c9833c9
@@ -1,4 +1,4 @@
c9833c9
-/* $Id: port-linux.c,v 1.4 2007/06/27 22:48:03 djm Exp $ */
c9833c9
+/* $Id: port-linux.c,v 1.3 2006/09/01 05:38:41 djm Exp $ */
c9833c9
 
c9833c9
 /*
c9833c9
  * Copyright (c) 2005 Daniel Walsh <dwalsh@redhat.com>
546fdd9
@@ -33,12 +33,23 @@
c2b35d0
 #include "key.h"
c2b35d0
 #include "hostfile.h"
c2b35d0
 #include "auth.h"
c2b35d0
+#include "xmalloc.h"
c2b35d0
 
c2b35d0
 #include <selinux/selinux.h>
c2b35d0
 #include <selinux/flask.h>
c2b35d0
+#include <selinux/context.h>
c2b35d0
 #include <selinux/get_context_list.h>
546fdd9
+#include <selinux/get_default_type.h>
c2b35d0
+#include <selinux/av_permissions.h>
c2b35d0
+
c2b35d0
+#ifdef HAVE_LINUX_AUDIT
c2b35d0
+#include <libaudit.h>
73a8557
+#include <unistd.h>
c2b35d0
+#endif
c2b35d0
 
c2b35d0
 extern Authctxt *the_authctxt;
c2b35d0
+extern int inetd_flag;
c2b35d0
+extern int rexeced_flag;
c2b35d0
 
c2b35d0
 /* Wrapper around is_selinux_enabled() to log its return value once only */
c2b35d0
 static int
c9833c9
@@ -54,17 +65,173 @@ ssh_selinux_enabled(void)
c2b35d0
 	return (enabled);
c2b35d0
 }
c2b35d0
 
c2b35d0
+/* Send audit message */
c2b35d0
+static int
c2b35d0
+send_audit_message(int success, security_context_t default_context,
c2b35d0
+		       security_context_t selected_context)
c2b35d0
+{
c2b35d0
+	int rc=0;
c2b35d0
+#ifdef HAVE_LINUX_AUDIT
c2b35d0
+	char *msg = NULL;
c2b35d0
+	int audit_fd = audit_open();
c2b35d0
+	security_context_t default_raw=NULL;
c2b35d0
+	security_context_t selected_raw=NULL;
c2b35d0
+	rc = -1;
c2b35d0
+	if (audit_fd < 0) {
c2b35d0
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
c2b35d0
+                                        errno == EAFNOSUPPORT)
c2b35d0
+                        return 0; /* No audit support in kernel */
c2b35d0
+		error("Error connecting to audit system.");
c2b35d0
+		return rc;
c2b35d0
+	}
c2b35d0
+	if (selinux_trans_to_raw_context(default_context, &default_raw) < 0) {
c2b35d0
+		error("Error translating default context.");
c3274cc
+		default_raw = NULL;
c2b35d0
+	}
c2b35d0
+	if (selinux_trans_to_raw_context(selected_context, &selected_raw) < 0) {
c2b35d0
+		error("Error translating selected context.");
c3274cc
+		selected_raw = NULL;
c2b35d0
+	}
c2b35d0
+	if (asprintf(&msg, "sshd: default-context=%s selected-context=%s",
c3274cc
+		     default_raw ? default_raw : (default_context ? default_context: "?"),
c3274cc
+		     selected_context ? selected_raw : (selected_context ? selected_context :"?")) < 0) {
c2b35d0
+		error("Error allocating memory.");
c2b35d0
+		goto out;
c2b35d0
+	}
c2b35d0
+	if (audit_log_user_message(audit_fd, AUDIT_USER_ROLE_CHANGE,
c2b35d0
+				   msg, NULL, NULL, NULL, success) <= 0) {
c2b35d0
+		error("Error sending audit message.");
c2b35d0
+		goto out;
c2b35d0
+	}
c2b35d0
+	rc = 0;
c2b35d0
+      out:
c2b35d0
+	free(msg);
c2b35d0
+	freecon(default_raw);
c2b35d0
+	freecon(selected_raw);
c2b35d0
+	close(audit_fd);
c2b35d0
+#endif
c2b35d0
+	return rc;
c2b35d0
+}
c2b35d0
+
c2b35d0
+static int
c2b35d0
+mls_range_allowed(security_context_t src, security_context_t dst)
c2b35d0
+{
c2b35d0
+	struct av_decision avd;
c2b35d0
+	int retval;
c2b35d0
+	unsigned int bit = CONTEXT__CONTAINS;
c2b35d0
+
7210c01
+	debug("%s: src:%s dst:%s", __func__, src, dst);
c2b35d0
+	retval = security_compute_av(src, dst, SECCLASS_CONTEXT, bit, &avd);
c2b35d0
+	if (retval || ((bit & avd.allowed) != bit))
c2b35d0
+		return 0;
c2b35d0
+
c2b35d0
+	return 1;
c2b35d0
+}
c2b35d0
+
c2b35d0
+static int
c2b35d0
+get_user_context(const char *sename, const char *role, const char *lvl,
c2b35d0
+	security_context_t *sc) {
c2b35d0
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
c3274cc
+	if (lvl == NULL || lvl[0] == '\0' || get_default_context_with_level(sename, lvl, NULL, sc) != 0) {
546fdd9
+	        /* User may have requested a level completely outside of his 
546fdd9
+	           allowed range. We get a context just for auditing as the
546fdd9
+	           range check below will certainly fail for default context. */
c2b35d0
+#endif
546fdd9
+		if (get_default_context(sename, NULL, sc) != 0) {
546fdd9
+			*sc = NULL;
546fdd9
+			return -1;
546fdd9
+		}
546fdd9
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
546fdd9
+	}
546fdd9
+#endif
546fdd9
+	if (role != NULL && role[0]) {
546fdd9
+		context_t con;
546fdd9
+		char *type=NULL;
546fdd9
+		if (get_default_type(role, &type) != 0) {
546fdd9
+			error("get_default_type: failed to get default type for '%s'",
546fdd9
+				role);
546fdd9
+			goto out;
546fdd9
+		}
546fdd9
+		con = context_new(*sc);
546fdd9
+		if (!con) {
546fdd9
+			goto out;
546fdd9
+		}
546fdd9
+		context_role_set(con, role);
546fdd9
+		context_type_set(con, type);
546fdd9
+		freecon(*sc);
546fdd9
+		*sc = strdup(context_str(con));
546fdd9
+		context_free(con);
546fdd9
+		if (!*sc) 
546fdd9
+			return -1;
546fdd9
+	}
546fdd9
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
546fdd9
+	if (lvl != NULL && lvl[0]) {
546fdd9
+		/* verify that the requested range is obtained */
546fdd9
+		context_t con;
546fdd9
+		security_context_t obtained_raw;
546fdd9
+		security_context_t requested_raw;
546fdd9
+		con = context_new(*sc);
546fdd9
+		if (!con) {
546fdd9
+			goto out;
546fdd9
+		}
546fdd9
+		context_range_set(con, lvl);
546fdd9
+		if (selinux_trans_to_raw_context(*sc, &obtained_raw) < 0) {
546fdd9
+			context_free(con);
546fdd9
+			goto out;
546fdd9
+		}
546fdd9
+		if (selinux_trans_to_raw_context(context_str(con), &requested_raw) < 0) {
546fdd9
+			freecon(obtained_raw);
546fdd9
+			context_free(con);
546fdd9
+			goto out;
546fdd9
+		}
546fdd9
+
546fdd9
+		debug("get_user_context: obtained context '%s' requested context '%s'",
546fdd9
+			obtained_raw, requested_raw);
546fdd9
+		if (strcmp(obtained_raw, requested_raw)) {
546fdd9
+			/* set the context to the real requested one but fail */
546fdd9
+			freecon(requested_raw);
546fdd9
+			freecon(obtained_raw);
546fdd9
+			freecon(*sc);
546fdd9
+			*sc = strdup(context_str(con));
546fdd9
+			context_free(con);
546fdd9
+			return -1;
546fdd9
+		}
546fdd9
+		freecon(requested_raw);
546fdd9
+		freecon(obtained_raw);
546fdd9
+		context_free(con);
546fdd9
+	}
546fdd9
+#endif
546fdd9
+	return 0;
546fdd9
+      out:
546fdd9
+        freecon(*sc);
546fdd9
+        *sc = NULL;
546fdd9
+        return -1;
c2b35d0
+}
c2b35d0
+
c2b35d0
 /* Return the default security context for the given username */
546fdd9
-static security_context_t
546fdd9
-ssh_selinux_getctxbyname(char *pwname)
546fdd9
+static int
546fdd9
+ssh_selinux_getctxbyname(char *pwname,
546fdd9
+	security_context_t *default_sc, security_context_t *user_sc)
c2b35d0
 {
546fdd9
-	security_context_t sc = NULL;
c2b35d0
 	char *sename, *lvl;
73a8557
+	const char *reqlvl = NULL;
c2b35d0
 	char *role = NULL;
546fdd9
-	int r = 0;
546fdd9
+	int r = -1;
c2b35d0
+	context_t con = NULL;
c2b35d0
+
546fdd9
+	*default_sc = NULL;
546fdd9
+	*user_sc = NULL;
c2b35d0
+	if (the_authctxt) {
c2b35d0
+		if (the_authctxt->role != NULL) {
c2b35d0
+			char *slash;
c2b35d0
+			role = xstrdup(the_authctxt->role);
c2b35d0
+			if ((slash = strchr(role, '/')) != NULL) {
c2b35d0
+				*slash = '\0';
c2b35d0
+				reqlvl = slash + 1;
c2b35d0
+			}
c2b35d0
+		}
c2b35d0
+	}
c2b35d0
 
c2b35d0
-	if (the_authctxt) 
c2b35d0
-		role=the_authctxt->role;
c2b35d0
 #ifdef HAVE_GETSEUSERBYNAME
73a8557
 	if ((r=getseuserbyname(pwname, &sename, &lvl)) != 0) {
c2b35d0
 		sename = NULL;
c9833c9
@@ -72,37 +239,62 @@ ssh_selinux_getctxbyname(char *pwname)
c2b35d0
 	}
c2b35d0
 #else
c2b35d0
 	sename = pwname;
c2b35d0
-	lvl = NULL;
c2b35d0
+	lvl = "";
c2b35d0
 #endif
c2b35d0
 
c2b35d0
 	if (r == 0) {
546fdd9
 #ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
c2b35d0
-		if (role != NULL && role[0])
c2b35d0
-			r = get_default_context_with_rolelevel(sename, role, lvl, NULL, &sc);
c2b35d0
-		else
c2b35d0
-			r = get_default_context_with_level(sename, lvl, NULL, &sc);
546fdd9
+		r = get_default_context_with_level(sename, lvl, NULL, default_sc);
546fdd9
 #else
c2b35d0
-		if (role != NULL && role[0])
c2b35d0
-			r = get_default_context_with_role(sename, role, NULL, &sc);
c2b35d0
-		else
c2b35d0
-			r = get_default_context(sename, NULL, &sc);
546fdd9
+		r = get_default_context(sename, NULL, default_sc);
546fdd9
 #endif
c2b35d0
 	}
c2b35d0
 
546fdd9
-	if (r != 0) {
546fdd9
-		switch (security_getenforce()) {
546fdd9
-		case -1:
546fdd9
-			fatal("%s: ssh_selinux_getctxbyname: "
546fdd9
-			    "security_getenforce() failed", __func__);
546fdd9
-		case 0:
546fdd9
-			error("%s: Failed to get default SELinux security "
546fdd9
-			    "context for %s", __func__, pwname);
c9833c9
-			break;
546fdd9
-		default:
546fdd9
-			fatal("%s: Failed to get default SELinux security "
546fdd9
-			    "context for %s (in enforcing mode)",
546fdd9
-			    __func__, pwname);
c2b35d0
+	if (r == 0) {
c2b35d0
+		/* If launched from xinetd, we must use current level */
c2b35d0
+		if (inetd_flag && !rexeced_flag) {
c2b35d0
+			security_context_t sshdsc=NULL;
c2b35d0
+
546fdd9
+			if (getcon_raw(&sshdsc) < 0)
c2b35d0
+				fatal("failed to allocate security context");
c2b35d0
+
c2b35d0
+			if ((con=context_new(sshdsc)) == NULL)
c2b35d0
+				fatal("failed to allocate selinux context");
c2b35d0
+			reqlvl = context_range_get(con);
c2b35d0
+			freecon(sshdsc);
546fdd9
+			if (reqlvl !=NULL && lvl != NULL && strcmp(reqlvl, lvl) == 0)
546fdd9
+			    /* we actually don't change level */
546fdd9
+			    reqlvl = "";
c2b35d0
+
c2b35d0
+			debug("%s: current connection level '%s'", __func__, reqlvl);
c9833c9
 		}
c2b35d0
+		
546fdd9
+		if ((reqlvl != NULL && reqlvl[0]) || (role != NULL && role[0])) {
546fdd9
+			r = get_user_context(sename, role, reqlvl, user_sc);
c2b35d0
+		
546fdd9
+			if (r == 0 && reqlvl != NULL && reqlvl[0]) {
7210c01
+				security_context_t default_level_sc = *default_sc;
7210c01
+				if (role != NULL && role[0]) {
7210c01
+					if (get_user_context(sename, role, lvl, &default_level_sc) < 0)
7210c01
+						default_level_sc = *default_sc;
7210c01
+				}
546fdd9
+				/* verify that the requested range is contained in the user range */
7210c01
+				if (mls_range_allowed(default_level_sc, *user_sc)) {
c2b35d0
+					logit("permit MLS level %s (user range %s)", reqlvl, lvl);
c2b35d0
+				} else {
546fdd9
+					r = -1;
546fdd9
+					error("deny MLS level %s (user range %s)", reqlvl, lvl);
c2b35d0
+				}
7210c01
+				if (default_level_sc != *default_sc)
7210c01
+					freecon(default_level_sc);
c2b35d0
+			}
c2b35d0
+		} else {
546fdd9
+			*user_sc = *default_sc;
c9833c9
+		}
c9833c9
+	}
546fdd9
+	if (r != 0) {
546fdd9
+		error("%s: Failed to get default SELinux security "
546fdd9
+		    "context for %s", __func__, pwname);
c9833c9
 	}
546fdd9
 
546fdd9
 #ifdef HAVE_GETSEUSERBYNAME
c9833c9
@@ -111,14 +303,20 @@ ssh_selinux_getctxbyname(char *pwname)
c2b35d0
 	if (lvl != NULL)
c2b35d0
 		xfree(lvl);
c2b35d0
 #endif
c2b35d0
+	if (role != NULL)
c2b35d0
+		xfree(role);
c2b35d0
+	if (con)
c2b35d0
+		context_free(con);
c2b35d0
 
546fdd9
-	return (sc);
546fdd9
+	return (r);
546fdd9
 }
546fdd9
 
546fdd9
 /* Set the execution context to the default for the specified user */
546fdd9
 void
546fdd9
 ssh_selinux_setup_exec_context(char *pwname)
546fdd9
 {
546fdd9
+	int r = 0;
546fdd9
+	security_context_t default_ctx = NULL;
546fdd9
 	security_context_t user_ctx = NULL;
546fdd9
 
546fdd9
 	if (!ssh_selinux_enabled())
c9833c9
@@ -126,22 +324,39 @@ ssh_selinux_setup_exec_context(char *pwn
546fdd9
 
546fdd9
 	debug3("%s: setting execution context", __func__);
546fdd9
 
546fdd9
-	user_ctx = ssh_selinux_getctxbyname(pwname);
546fdd9
-	if (setexeccon(user_ctx) != 0) {
546fdd9
+	r = ssh_selinux_getctxbyname(pwname, &default_ctx, &user_ctx);
546fdd9
+	if (r >= 0) {
546fdd9
+		r = setexeccon(user_ctx);
546fdd9
+		if (r < 0) {
546fdd9
+			error("%s: Failed to set SELinux execution context %s for %s",
546fdd9
+			    __func__, user_ctx, pwname);
546fdd9
+		}
546fdd9
+	}
546fdd9
+	if (user_ctx == NULL) {
546fdd9
+		user_ctx = default_ctx;
546fdd9
+	}
546fdd9
+	if (r < 0 || user_ctx != default_ctx) {
546fdd9
+		/* audit just the case when user changed a role or there was
546fdd9
+		   a failure */
546fdd9
+		send_audit_message(r >= 0, default_ctx, user_ctx);
546fdd9
+	}
546fdd9
+	if (r < 0) {
546fdd9
 		switch (security_getenforce()) {
546fdd9
 		case -1:
546fdd9
 			fatal("%s: security_getenforce() failed", __func__);
546fdd9
 		case 0:
546fdd9
-			error("%s: Failed to set SELinux execution "
546fdd9
-			    "context for %s", __func__, pwname);
546fdd9
+			error("%s: SELinux failure. Continuing in permissive mode.",
546fdd9
+			    __func__);
c9833c9
 			break;
546fdd9
 		default:
546fdd9
-			fatal("%s: Failed to set SELinux execution context "
546fdd9
-			    "for %s (in enforcing mode)", __func__, pwname);
546fdd9
+			fatal("%s: SELinux failure. Aborting connection.",
546fdd9
+			    __func__);
546fdd9
 		}
546fdd9
 	}
546fdd9
-	if (user_ctx != NULL)
546fdd9
+	if (user_ctx != NULL && user_ctx != default_ctx)
546fdd9
 		freecon(user_ctx);
546fdd9
+	if (default_ctx != NULL)
546fdd9
+		freecon(default_ctx);
546fdd9
 
546fdd9
 	debug3("%s: done", __func__);
c2b35d0
 }
c9833c9
@@ -159,7 +374,10 @@ ssh_selinux_setup_pty(char *pwname, cons
c2b35d0
 
c2b35d0
 	debug3("%s: setting TTY context on %s", __func__, tty);
c2b35d0
 
c2b35d0
-	user_ctx = ssh_selinux_getctxbyname(pwname);
73a8557
+	if (getexeccon(&user_ctx) < 0) {
c2b35d0
+		error("%s: getexeccon: %s", __func__, strerror(errno));
c2b35d0
+		goto out;
c2b35d0
+	}
c2b35d0
 
c2b35d0
 	/* XXX: should these calls fatal() upon failure in enforcing mode? */
c2b35d0
 
c9833c9
diff -up openssh-4.7p1/sshd.c.mls openssh-4.7p1/sshd.c
c9833c9
--- openssh-4.7p1/sshd.c.mls	2007-09-06 17:39:28.000000000 +0200
c9833c9
+++ openssh-4.7p1/sshd.c	2007-09-06 17:39:28.000000000 +0200
c9833c9
@@ -1838,6 +1838,9 @@ main(int ac, char **av)
c2b35d0
 		restore_uid();
c2b35d0
 	}
c2b35d0
 #endif
c2b35d0
+#ifdef WITH_SELINUX
c2b35d0
+	ssh_selinux_setup_exec_context(authctxt->pw->pw_name);
c2b35d0
+#endif
c2b35d0
 #ifdef USE_PAM
c2b35d0
 	if (options.use_pam) {
c2b35d0
 		do_pam_setcred(1);