vishalmishra434 / rpms / openssh

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