vishalmishra434 / rpms / openssh

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