vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Jan F. Chadima 82bc82
diff -up openssh-5.4p1/configure.ac.mls openssh-5.4p1/configure.ac
Jan F. Chadima 82bc82
--- openssh-5.4p1/configure.ac.mls	2010-03-01 15:24:27.000000000 +0100
Jan F. Chadima 82bc82
+++ openssh-5.4p1/configure.ac	2010-03-01 15:24:28.000000000 +0100
Jan F. Chadima 82bc82
@@ -3360,6 +3360,7 @@ AC_ARG_WITH(selinux,
Jan F. Chadima 82bc82
 		SSHDLIBS="$SSHDLIBS $LIBSELINUX"
Jan F. Chadima 82bc82
 		LIBS="$LIBS $LIBSELINUX"
Jan F. Chadima 82bc82
 		AC_CHECK_FUNCS(getseuserbyname get_default_context_with_level)
Jan F. Chadima 82bc82
+		AC_CHECK_FUNCS(setkeycreatecon)
Jan F. Chadima 82bc82
 		LIBS="$save_LIBS"
Jan F. Chadima 82bc82
 	fi ]
Jan F. Chadima 82bc82
 )
Jan F. Chadima 82bc82
diff -up openssh-5.4p1/misc.c.mls openssh-5.4p1/misc.c
Jan F. Chadima 82bc82
--- openssh-5.4p1/misc.c.mls	2010-01-10 00:31:12.000000000 +0100
Jan F. Chadima 82bc82
+++ openssh-5.4p1/misc.c	2010-03-01 15:24:28.000000000 +0100
Jan F. Chadima 82bc82
@@ -423,6 +423,7 @@ char *
Jan F. Chadima 82bc82
 colon(char *cp)
Jan F. Chadima 82bc82
 {
Jan F. Chadima 82bc82
 	int flag = 0;
Jan F. Chadima 82bc82
+	int start = 1;
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
 	if (*cp == ':')		/* Leading colon is part of file name. */
Jan F. Chadima 82bc82
 		return (0);
Jan F. Chadima 82bc82
@@ -436,8 +437,13 @@ colon(char *cp)
Jan F. Chadima 82bc82
 			return (cp+1);
Jan F. Chadima 82bc82
 		if (*cp == ':' && !flag)
Jan F. Chadima 82bc82
 			return (cp);
Jan F. Chadima 82bc82
-		if (*cp == '/')
Jan F. Chadima 82bc82
-			return (0);
Jan F. Chadima 82bc82
+		if (start) {
Jan F. Chadima 82bc82
+		/* Slash on beginning or after dots only denotes file name. */
Jan F. Chadima 82bc82
+			if (*cp == '/')
Jan F. Chadima 82bc82
+				return (0);
Jan F. Chadima 82bc82
+			if (*cp != '.')
Jan F. Chadima 82bc82
+				start = 0;
Jan F. Chadima 82bc82
+		}
Jan F. Chadima 82bc82
 	}
Jan F. Chadima 82bc82
 	return (0);
Jan F. Chadima 82bc82
 }
Jan F. Chadima 82bc82
diff -up openssh-5.4p1/openbsd-compat/port-linux.c.mls openssh-5.4p1/openbsd-compat/port-linux.c
Jan F. Chadima 82bc82
--- openssh-5.4p1/openbsd-compat/port-linux.c.mls	2010-03-01 15:24:27.000000000 +0100
Jan F. Chadima 82bc82
+++ openssh-5.4p1/openbsd-compat/port-linux.c	2010-03-01 15:25:50.000000000 +0100
Jan F. Chadima 82bc82
@@ -35,13 +35,24 @@
Jan F. Chadima 82bc82
 #include "key.h"
Jan F. Chadima 82bc82
 #include "hostfile.h"
Jan F. Chadima 82bc82
 #include "auth.h"
Jan F. Chadima 82bc82
+#include "xmalloc.h"
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
 #ifdef WITH_SELINUX
Jan F. Chadima 82bc82
 #include <selinux/selinux.h>
Jan F. Chadima 82bc82
 #include <selinux/flask.h>
Jan F. Chadima 82bc82
+#include <selinux/context.h>
Jan F. Chadima 82bc82
 #include <selinux/get_context_list.h>
Jan F. Chadima 82bc82
+#include <selinux/get_default_type.h>
Jan F. Chadima 82bc82
+#include <selinux/av_permissions.h>
Jan F. Chadima 82bc82
+
Jan F. Chadima 82bc82
+#ifdef HAVE_LINUX_AUDIT
Jan F. Chadima 82bc82
+#include <libaudit.h>
Jan F. Chadima 82bc82
+#include <unistd.h>
Jan F. Chadima 82bc82
+#endif
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
 extern Authctxt *the_authctxt;
Jan F. Chadima 82bc82
+extern int inetd_flag;
Jan F. Chadima 82bc82
+extern int rexeced_flag;
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
 /* Wrapper around is_selinux_enabled() to log its return value once only */
Jan F. Chadima 82bc82
 int
Jan F. Chadima 82bc82
@@ -57,17 +68,173 @@ ssh_selinux_enabled(void)
Jan F. Chadima 82bc82
 	return (enabled);
Jan F. Chadima 82bc82
 }
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
+/* Send audit message */
Jan F. Chadima 82bc82
+static int
Jan F. Chadima 82bc82
+send_audit_message(int success, security_context_t default_context,
Jan F. Chadima 82bc82
+		       security_context_t selected_context)
Jan F. Chadima 82bc82
+{
Jan F. Chadima 82bc82
+	int rc=0;
Jan F. Chadima 82bc82
+#ifdef HAVE_LINUX_AUDIT
Jan F. Chadima 82bc82
+	char *msg = NULL;
Jan F. Chadima 82bc82
+	int audit_fd = audit_open();
Jan F. Chadima 82bc82
+	security_context_t default_raw=NULL;
Jan F. Chadima 82bc82
+	security_context_t selected_raw=NULL;
Jan F. Chadima 82bc82
+	rc = -1;
Jan F. Chadima 82bc82
+	if (audit_fd < 0) {
Jan F. Chadima 82bc82
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
Jan F. Chadima 82bc82
+                                        errno == EAFNOSUPPORT)
Jan F. Chadima 82bc82
+                        return 0; /* No audit support in kernel */
Jan F. Chadima 82bc82
+		error("Error connecting to audit system.");
Jan F. Chadima 82bc82
+		return rc;
Jan F. Chadima 82bc82
+	}
Jan F. Chadima 82bc82
+	if (selinux_trans_to_raw_context(default_context, &default_raw) < 0) {
Jan F. Chadima 82bc82
+		error("Error translating default context.");
Jan F. Chadima 82bc82
+		default_raw = NULL;
Jan F. Chadima 82bc82
+	}
Jan F. Chadima 82bc82
+	if (selinux_trans_to_raw_context(selected_context, &selected_raw) < 0) {
Jan F. Chadima 82bc82
+		error("Error translating selected context.");
Jan F. Chadima 82bc82
+		selected_raw = NULL;
Jan F. Chadima 82bc82
+	}
Jan F. Chadima 82bc82
+	if (asprintf(&msg, "sshd: default-context=%s selected-context=%s",
Jan F. Chadima 82bc82
+		     default_raw ? default_raw : (default_context ? default_context: "?"),
Jan F. Chadima 82bc82
+		     selected_context ? selected_raw : (selected_context ? selected_context :"?")) < 0) {
Jan F. Chadima 82bc82
+		error("Error allocating memory.");
Jan F. Chadima 82bc82
+		goto out;
Jan F. Chadima 82bc82
+	}
Jan F. Chadima 82bc82
+	if (audit_log_user_message(audit_fd, AUDIT_USER_ROLE_CHANGE,
Jan F. Chadima 82bc82
+				   msg, NULL, NULL, NULL, success) <= 0) {
Jan F. Chadima 82bc82
+		error("Error sending audit message.");
Jan F. Chadima 82bc82
+		goto out;
Jan F. Chadima 82bc82
+	}
Jan F. Chadima 82bc82
+	rc = 0;
Jan F. Chadima 82bc82
+      out:
Jan F. Chadima 82bc82
+	free(msg);
Jan F. Chadima 82bc82
+	freecon(default_raw);
Jan F. Chadima 82bc82
+	freecon(selected_raw);
Jan F. Chadima 82bc82
+	close(audit_fd);
Jan F. Chadima 82bc82
+#endif
Jan F. Chadima 82bc82
+	return rc;
Jan F. Chadima 82bc82
+}
Jan F. Chadima 82bc82
+
Jan F. Chadima 82bc82
+static int
Jan F. Chadima 82bc82
+mls_range_allowed(security_context_t src, security_context_t dst)
Jan F. Chadima 82bc82
+{
Jan F. Chadima 82bc82
+	struct av_decision avd;
Jan F. Chadima 82bc82
+	int retval;
Jan F. Chadima 82bc82
+	unsigned int bit = CONTEXT__CONTAINS;
Jan F. Chadima 82bc82
+
Jan F. Chadima 82bc82
+	debug("%s: src:%s dst:%s", __func__, src, dst);
Jan F. Chadima 82bc82
+	retval = security_compute_av(src, dst, SECCLASS_CONTEXT, bit, &avd);
Jan F. Chadima 82bc82
+	if (retval || ((bit & avd.allowed) != bit))
Jan F. Chadima 82bc82
+		return 0;
Jan F. Chadima 82bc82
+
Jan F. Chadima 82bc82
+	return 1;
Jan F. Chadima 82bc82
+}
Jan F. Chadima 82bc82
+
Jan F. Chadima 82bc82
+static int
Jan F. Chadima 82bc82
+get_user_context(const char *sename, const char *role, const char *lvl,
Jan F. Chadima 82bc82
+	security_context_t *sc) {
Jan F. Chadima 82bc82
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
Jan F. Chadima 82bc82
+	if (lvl == NULL || lvl[0] == '\0' || get_default_context_with_level(sename, lvl, NULL, sc) != 0) {
Jan F. Chadima 82bc82
+	        /* User may have requested a level completely outside of his 
Jan F. Chadima 82bc82
+	           allowed range. We get a context just for auditing as the
Jan F. Chadima 82bc82
+	           range check below will certainly fail for default context. */
Jan F. Chadima 82bc82
+#endif
Jan F. Chadima 82bc82
+		if (get_default_context(sename, NULL, sc) != 0) {
Jan F. Chadima 82bc82
+			*sc = NULL;
Jan F. Chadima 82bc82
+			return -1;
Jan F. Chadima 82bc82
+		}
Jan F. Chadima 82bc82
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
Jan F. Chadima 82bc82
+	}
Jan F. Chadima 82bc82
+#endif
Jan F. Chadima 82bc82
+	if (role != NULL && role[0]) {
Jan F. Chadima 82bc82
+		context_t con;
Jan F. Chadima 82bc82
+		char *type=NULL;
Jan F. Chadima 82bc82
+		if (get_default_type(role, &type) != 0) {
Jan F. Chadima 82bc82
+			error("get_default_type: failed to get default type for '%s'",
Jan F. Chadima 82bc82
+				role);
Jan F. Chadima 82bc82
+			goto out;
Jan F. Chadima 82bc82
+		}
Jan F. Chadima 82bc82
+		con = context_new(*sc);
Jan F. Chadima 82bc82
+		if (!con) {
Jan F. Chadima 82bc82
+			goto out;
Jan F. Chadima 82bc82
+		}
Jan F. Chadima 82bc82
+		context_role_set(con, role);
Jan F. Chadima 82bc82
+		context_type_set(con, type);
Jan F. Chadima 82bc82
+		freecon(*sc);
Jan F. Chadima 82bc82
+		*sc = strdup(context_str(con));
Jan F. Chadima 82bc82
+		context_free(con);
Jan F. Chadima 82bc82
+		if (!*sc) 
Jan F. Chadima 82bc82
+			return -1;
Jan F. Chadima 82bc82
+	}
Jan F. Chadima 82bc82
+#ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
Jan F. Chadima 82bc82
+	if (lvl != NULL && lvl[0]) {
Jan F. Chadima 82bc82
+		/* verify that the requested range is obtained */
Jan F. Chadima 82bc82
+		context_t con;
Jan F. Chadima 82bc82
+		security_context_t obtained_raw;
Jan F. Chadima 82bc82
+		security_context_t requested_raw;
Jan F. Chadima 82bc82
+		con = context_new(*sc);
Jan F. Chadima 82bc82
+		if (!con) {
Jan F. Chadima 82bc82
+			goto out;
Jan F. Chadima 82bc82
+		}
Jan F. Chadima 82bc82
+		context_range_set(con, lvl);
Jan F. Chadima 82bc82
+		if (selinux_trans_to_raw_context(*sc, &obtained_raw) < 0) {
Jan F. Chadima 82bc82
+			context_free(con);
Jan F. Chadima 82bc82
+			goto out;
Jan F. Chadima 82bc82
+		}
Jan F. Chadima 82bc82
+		if (selinux_trans_to_raw_context(context_str(con), &requested_raw) < 0) {
Jan F. Chadima 82bc82
+			freecon(obtained_raw);
Jan F. Chadima 82bc82
+			context_free(con);
Jan F. Chadima 82bc82
+			goto out;
Jan F. Chadima 82bc82
+		}
Jan F. Chadima 82bc82
+
Jan F. Chadima 82bc82
+		debug("get_user_context: obtained context '%s' requested context '%s'",
Jan F. Chadima 82bc82
+			obtained_raw, requested_raw);
Jan F. Chadima 82bc82
+		if (strcmp(obtained_raw, requested_raw)) {
Jan F. Chadima 82bc82
+			/* set the context to the real requested one but fail */
Jan F. Chadima 82bc82
+			freecon(requested_raw);
Jan F. Chadima 82bc82
+			freecon(obtained_raw);
Jan F. Chadima 82bc82
+			freecon(*sc);
Jan F. Chadima 82bc82
+			*sc = strdup(context_str(con));
Jan F. Chadima 82bc82
+			context_free(con);
Jan F. Chadima 82bc82
+			return -1;
Jan F. Chadima 82bc82
+		}
Jan F. Chadima 82bc82
+		freecon(requested_raw);
Jan F. Chadima 82bc82
+		freecon(obtained_raw);
Jan F. Chadima 82bc82
+		context_free(con);
Jan F. Chadima 82bc82
+	}
Jan F. Chadima 82bc82
+#endif
Jan F. Chadima 82bc82
+	return 0;
Jan F. Chadima 82bc82
+      out:
Jan F. Chadima 82bc82
+        freecon(*sc);
Jan F. Chadima 82bc82
+        *sc = NULL;
Jan F. Chadima 82bc82
+        return -1;
Jan F. Chadima 82bc82
+}
Jan F. Chadima 82bc82
+
Jan F. Chadima 82bc82
 /* Return the default security context for the given username */
Jan F. Chadima 82bc82
-static security_context_t
Jan F. Chadima 82bc82
-ssh_selinux_getctxbyname(char *pwname)
Jan F. Chadima 82bc82
+static int
Jan F. Chadima 82bc82
+ssh_selinux_getctxbyname(char *pwname,
Jan F. Chadima 82bc82
+	security_context_t *default_sc, security_context_t *user_sc)
Jan F. Chadima 82bc82
 {
Jan F. Chadima 82bc82
-	security_context_t sc = NULL;
Jan F. Chadima 82bc82
 	char *sename, *lvl;
Jan F. Chadima 82bc82
+	const char *reqlvl = NULL;
Jan F. Chadima 82bc82
 	char *role = NULL;
Jan F. Chadima 82bc82
-	int r = 0;
Jan F. Chadima 82bc82
+	int r = -1;
Jan F. Chadima 82bc82
+	context_t con = NULL;
Jan F. Chadima 82bc82
+
Jan F. Chadima 82bc82
+	*default_sc = NULL;
Jan F. Chadima 82bc82
+	*user_sc = NULL;
Jan F. Chadima 82bc82
+	if (the_authctxt) {
Jan F. Chadima 82bc82
+		if (the_authctxt->role != NULL) {
Jan F. Chadima 82bc82
+			char *slash;
Jan F. Chadima 82bc82
+			role = xstrdup(the_authctxt->role);
Jan F. Chadima 82bc82
+			if ((slash = strchr(role, '/')) != NULL) {
Jan F. Chadima 82bc82
+				*slash = '\0';
Jan F. Chadima 82bc82
+				reqlvl = slash + 1;
Jan F. Chadima 82bc82
+			}
Jan F. Chadima 82bc82
+		}
Jan F. Chadima 82bc82
+	}
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
-	if (the_authctxt) 
Jan F. Chadima 82bc82
-		role=the_authctxt->role;
Jan F. Chadima 82bc82
 #ifdef HAVE_GETSEUSERBYNAME
Jan F. Chadima 82bc82
 	if ((r=getseuserbyname(pwname, &sename, &lvl)) != 0) {
Jan F. Chadima 82bc82
 		sename = NULL;
Jan F. Chadima 82bc82
@@ -75,38 +242,63 @@ ssh_selinux_getctxbyname(char *pwname)
Jan F. Chadima 82bc82
 	}
Jan F. Chadima 82bc82
 #else
Jan F. Chadima 82bc82
 	sename = pwname;
Jan F. Chadima 82bc82
-	lvl = NULL;
Jan F. Chadima 82bc82
+	lvl = "";
Jan F. Chadima 82bc82
 #endif
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
 	if (r == 0) {
Jan F. Chadima 82bc82
 #ifdef HAVE_GET_DEFAULT_CONTEXT_WITH_LEVEL
Jan F. Chadima 82bc82
-		if (role != NULL && role[0])
Jan F. Chadima 82bc82
-			r = get_default_context_with_rolelevel(sename, role, lvl, NULL, &sc);
Jan F. Chadima 82bc82
-		else
Jan F. Chadima 82bc82
-			r = get_default_context_with_level(sename, lvl, NULL, &sc);
Jan F. Chadima 82bc82
+		r = get_default_context_with_level(sename, lvl, NULL, default_sc);
Jan F. Chadima 82bc82
 #else
Jan F. Chadima 82bc82
-		if (role != NULL && role[0])
Jan F. Chadima 82bc82
-			r = get_default_context_with_role(sename, role, NULL, &sc);
Jan F. Chadima 82bc82
-		else
Jan F. Chadima 82bc82
-			r = get_default_context(sename, NULL, &sc);
Jan F. Chadima 82bc82
+		r = get_default_context(sename, NULL, default_sc);
Jan F. Chadima 82bc82
 #endif
Jan F. Chadima 82bc82
 	}
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
-	if (r != 0) {
Jan F. Chadima 82bc82
-		switch (security_getenforce()) {
Jan F. Chadima 82bc82
-		case -1:
Jan F. Chadima 82bc82
-			fatal("%s: ssh_selinux_getctxbyname: "
Jan F. Chadima 82bc82
-			    "security_getenforce() failed", __func__);
Jan F. Chadima 82bc82
-		case 0:
Jan F. Chadima 82bc82
-			error("%s: Failed to get default SELinux security "
Jan F. Chadima 82bc82
-			    "context for %s", __func__, pwname);
Jan F. Chadima 82bc82
-			break;
Jan F. Chadima 82bc82
-		default:
Jan F. Chadima 82bc82
-			fatal("%s: Failed to get default SELinux security "
Jan F. Chadima 82bc82
-			    "context for %s (in enforcing mode)",
Jan F. Chadima 82bc82
-			    __func__, pwname);
Jan F. Chadima 82bc82
+	if (r == 0) {
Jan F. Chadima 82bc82
+		/* If launched from xinetd, we must use current level */
Jan F. Chadima 82bc82
+		if (inetd_flag && !rexeced_flag) {
Jan F. Chadima 82bc82
+			security_context_t sshdsc=NULL;
Jan F. Chadima 82bc82
+
Jan F. Chadima 82bc82
+			if (getcon_raw(&sshdsc) < 0)
Jan F. Chadima 82bc82
+				fatal("failed to allocate security context");
Jan F. Chadima 82bc82
+
Jan F. Chadima 82bc82
+			if ((con=context_new(sshdsc)) == NULL)
Jan F. Chadima 82bc82
+				fatal("failed to allocate selinux context");
Jan F. Chadima 82bc82
+			reqlvl = context_range_get(con);
Jan F. Chadima 82bc82
+			freecon(sshdsc);
Jan F. Chadima 82bc82
+			if (reqlvl !=NULL && lvl != NULL && strcmp(reqlvl, lvl) == 0)
Jan F. Chadima 82bc82
+			    /* we actually don't change level */
Jan F. Chadima 82bc82
+			    reqlvl = "";
Jan F. Chadima 82bc82
+
Jan F. Chadima 82bc82
+			debug("%s: current connection level '%s'", __func__, reqlvl);
Jan F. Chadima 82bc82
+		}
Jan F. Chadima 82bc82
+		
Jan F. Chadima 82bc82
+		if ((reqlvl != NULL && reqlvl[0]) || (role != NULL && role[0])) {
Jan F. Chadima 82bc82
+			r = get_user_context(sename, role, reqlvl, user_sc);
Jan F. Chadima 82bc82
+		
Jan F. Chadima 82bc82
+			if (r == 0 && reqlvl != NULL && reqlvl[0]) {
Jan F. Chadima 82bc82
+				security_context_t default_level_sc = *default_sc;
Jan F. Chadima 82bc82
+				if (role != NULL && role[0]) {
Jan F. Chadima 82bc82
+					if (get_user_context(sename, role, lvl, &default_level_sc) < 0)
Jan F. Chadima 82bc82
+						default_level_sc = *default_sc;
Jan F. Chadima 82bc82
+				}
Jan F. Chadima 82bc82
+				/* verify that the requested range is contained in the user range */
Jan F. Chadima 82bc82
+				if (mls_range_allowed(default_level_sc, *user_sc)) {
Jan F. Chadima 82bc82
+					logit("permit MLS level %s (user range %s)", reqlvl, lvl);
Jan F. Chadima 82bc82
+				} else {
Jan F. Chadima 82bc82
+					r = -1;
Jan F. Chadima 82bc82
+					error("deny MLS level %s (user range %s)", reqlvl, lvl);
Jan F. Chadima 82bc82
+				}
Jan F. Chadima 82bc82
+				if (default_level_sc != *default_sc)
Jan F. Chadima 82bc82
+					freecon(default_level_sc);
Jan F. Chadima 82bc82
+			}
Jan F. Chadima 82bc82
+		} else {
Jan F. Chadima 82bc82
+			*user_sc = *default_sc;
Jan F. Chadima 82bc82
 		}
Jan F. Chadima 82bc82
 	}
Jan F. Chadima 82bc82
+	if (r != 0) {
Jan F. Chadima 82bc82
+		error("%s: Failed to get default SELinux security "
Jan F. Chadima 82bc82
+		    "context for %s", __func__, pwname);
Jan F. Chadima 82bc82
+	}
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
 #ifdef HAVE_GETSEUSERBYNAME
Jan F. Chadima 82bc82
 	if (sename != NULL)
Jan F. Chadima 82bc82
@@ -114,14 +306,20 @@ ssh_selinux_getctxbyname(char *pwname)
Jan F. Chadima 82bc82
 	if (lvl != NULL)
Jan F. Chadima 82bc82
 		xfree(lvl);
Jan F. Chadima 82bc82
 #endif
Jan F. Chadima 82bc82
+	if (role != NULL)
Jan F. Chadima 82bc82
+		xfree(role);
Jan F. Chadima 82bc82
+	if (con)
Jan F. Chadima 82bc82
+		context_free(con);
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
-	return (sc);
Jan F. Chadima 82bc82
+	return (r);
Jan F. Chadima 82bc82
 }
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
 /* Set the execution context to the default for the specified user */
Jan F. Chadima 82bc82
 void
Jan F. Chadima 82bc82
 ssh_selinux_setup_exec_context(char *pwname)
Jan F. Chadima 82bc82
 {
Jan F. Chadima 82bc82
+	int r = 0;
Jan F. Chadima 82bc82
+	security_context_t default_ctx = NULL;
Jan F. Chadima 82bc82
 	security_context_t user_ctx = NULL;
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
 	if (!ssh_selinux_enabled())
Jan F. Chadima 82bc82
@@ -129,22 +327,45 @@ ssh_selinux_setup_exec_context(char *pwn
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
 	debug3("%s: setting execution context", __func__);
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
-	user_ctx = ssh_selinux_getctxbyname(pwname);
Jan F. Chadima 82bc82
-	if (setexeccon(user_ctx) != 0) {
Jan F. Chadima 82bc82
+	r = ssh_selinux_getctxbyname(pwname, &default_ctx, &user_ctx);
Jan F. Chadima 82bc82
+	if (r >= 0) {
Jan F. Chadima 82bc82
+		r = setexeccon(user_ctx);
Jan F. Chadima 82bc82
+		if (r < 0) {
Jan F. Chadima 82bc82
+			error("%s: Failed to set SELinux execution context %s for %s",
Jan F. Chadima 82bc82
+			    __func__, user_ctx, pwname);
Jan F. Chadima 82bc82
+		} 
Jan F. Chadima 82bc82
+#ifdef HAVE_SETKEYCREATECON
Jan F. Chadima 82bc82
+		else if (setkeycreatecon(user_ctx) < 0) {
Jan F. Chadima 82bc82
+			error("%s: Failed to set SELinux keyring creation context %s for %s",
Jan F. Chadima 82bc82
+			    __func__, user_ctx, pwname);
Jan F. Chadima 82bc82
+		}
Jan F. Chadima 82bc82
+#endif
Jan F. Chadima 82bc82
+	}
Jan F. Chadima 82bc82
+	if (user_ctx == NULL) {
Jan F. Chadima 82bc82
+		user_ctx = default_ctx;
Jan F. Chadima 82bc82
+	}
Jan F. Chadima 82bc82
+	if (r < 0 || user_ctx != default_ctx) {
Jan F. Chadima 82bc82
+		/* audit just the case when user changed a role or there was
Jan F. Chadima 82bc82
+		   a failure */
Jan F. Chadima 82bc82
+		send_audit_message(r >= 0, default_ctx, user_ctx);
Jan F. Chadima 82bc82
+	}
Jan F. Chadima 82bc82
+	if (r < 0) {
Jan F. Chadima 82bc82
 		switch (security_getenforce()) {
Jan F. Chadima 82bc82
 		case -1:
Jan F. Chadima 82bc82
 			fatal("%s: security_getenforce() failed", __func__);
Jan F. Chadima 82bc82
 		case 0:
Jan F. Chadima 82bc82
-			error("%s: Failed to set SELinux execution "
Jan F. Chadima 82bc82
-			    "context for %s", __func__, pwname);
Jan F. Chadima 82bc82
+			error("%s: SELinux failure. Continuing in permissive mode.",
Jan F. Chadima 82bc82
+			    __func__);
Jan F. Chadima 82bc82
 			break;
Jan F. Chadima 82bc82
 		default:
Jan F. Chadima 82bc82
-			fatal("%s: Failed to set SELinux execution context "
Jan F. Chadima 82bc82
-			    "for %s (in enforcing mode)", __func__, pwname);
Jan F. Chadima 82bc82
+			fatal("%s: SELinux failure. Aborting connection.",
Jan F. Chadima 82bc82
+			    __func__);
Jan F. Chadima 82bc82
 		}
Jan F. Chadima 82bc82
 	}
Jan F. Chadima 82bc82
-	if (user_ctx != NULL)
Jan F. Chadima 82bc82
+	if (user_ctx != NULL && user_ctx != default_ctx)
Jan F. Chadima 82bc82
 		freecon(user_ctx);
Jan F. Chadima 82bc82
+	if (default_ctx != NULL)
Jan F. Chadima 82bc82
+		freecon(default_ctx);
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
 	debug3("%s: done", __func__);
Jan F. Chadima 82bc82
 }
Jan F. Chadima 82bc82
@@ -162,7 +383,10 @@ ssh_selinux_setup_pty(char *pwname, cons
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
 	debug3("%s: setting TTY context on %s", __func__, tty);
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
-	user_ctx = ssh_selinux_getctxbyname(pwname);
Jan F. Chadima 82bc82
+	if (getexeccon(&user_ctx) < 0) {
Jan F. Chadima 82bc82
+		error("%s: getexeccon: %s", __func__, strerror(errno));
Jan F. Chadima 82bc82
+		goto out;
Jan F. Chadima 82bc82
+	}
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
 	/* XXX: should these calls fatal() upon failure in enforcing mode? */
Jan F. Chadima 82bc82
 
Jan F. Chadima 82bc82
diff -up openssh-5.4p1/sshd.c.mls openssh-5.4p1/sshd.c
Jan F. Chadima 82bc82
--- openssh-5.4p1/sshd.c.mls	2010-03-01 15:24:27.000000000 +0100
Jan F. Chadima 82bc82
+++ openssh-5.4p1/sshd.c	2010-03-01 15:24:28.000000000 +0100
Jan F. Chadima 82bc82
@@ -1987,6 +1987,9 @@ main(int ac, char **av)
Jan F. Chadima 82bc82
 		restore_uid();
Jan F. Chadima 82bc82
 	}
Jan F. Chadima 82bc82
 #endif
Jan F. Chadima 82bc82
+#ifdef WITH_SELINUX
Jan F. Chadima 82bc82
+	ssh_selinux_setup_exec_context(authctxt->pw->pw_name);
Jan F. Chadima 82bc82
+#endif
Jan F. Chadima 82bc82
 #ifdef USE_PAM
Jan F. Chadima 82bc82
 	if (options.use_pam) {
Jan F. Chadima 82bc82
 		do_pam_setcred(1);