rcolebaugh / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone

Blame SOURCES/openssh-6.6.1p1-selinux-contexts.patch

aedd00
diff --git a/openbsd-compat/port-linux-sshd.c b/openbsd-compat/port-linux-sshd.c
aedd00
index 8f32464..18a2ca4 100644
aedd00
--- a/openbsd-compat/port-linux-sshd.c
aedd00
+++ b/openbsd-compat/port-linux-sshd.c
aedd00
@@ -32,6 +32,7 @@
aedd00
 #include "misc.h"      /* servconf.h needs misc.h for struct ForwardOptions */
aedd00
 #include "servconf.h"
aedd00
 #include "port-linux.h"
aedd00
+#include "misc.h"
aedd00
 #include "sshkey.h"
aedd00
 #include "hostfile.h"
aedd00
 #include "auth.h"
aedd00
@@ -445,7 +446,7 @@ sshd_selinux_setup_exec_context(char *pwname)
aedd00
 void
aedd00
 sshd_selinux_copy_context(void)
aedd00
 {
aedd00
-	security_context_t *ctx;
aedd00
+	char *ctx;
aedd00
 
aedd00
 	if (!sshd_selinux_enabled())
aedd00
 		return;
aedd00
@@ -461,6 +462,72 @@ sshd_selinux_copy_context(void)
aedd00
 	}
aedd00
 }
aedd00
 
aedd00
+void
aedd00
+sshd_selinux_change_privsep_preauth_context(void)
aedd00
+{
aedd00
+	int len;
aedd00
+	char line[1024], *preauth_context = NULL, *cp, *arg;
aedd00
+	const char *contexts_path;
aedd00
+	FILE *contexts_file;
aedd00
+	struct stat sb;
aedd00
+
aedd00
+	contexts_path = selinux_openssh_contexts_path();
aedd00
+	if (contexts_path == NULL) {
aedd00
+		debug3("%s: Failed to get the path to SELinux context", __func__);
aedd00
+		return;
aedd00
+	}
aedd00
+
aedd00
+	if ((contexts_file = fopen(contexts_path, "r")) == NULL) {
aedd00
+		debug("%s: Failed to open SELinux context file", __func__);
aedd00
+		return;
aedd00
+	}
aedd00
+
aedd00
+	if (fstat(fileno(contexts_file), &sb) != 0 ||
aedd00
+	    sb.st_uid != 0 || (sb.st_mode & 022) != 0) {
aedd00
+		logit("%s: SELinux context file needs to be owned by root"
aedd00
+		    " and not writable by anyone else", __func__);
aedd00
+		fclose(contexts_file);
aedd00
+		return;
aedd00
+	}
aedd00
+
aedd00
+	while (fgets(line, sizeof(line), contexts_file)) {
aedd00
+		/* Strip trailing whitespace */
aedd00
+		for (len = strlen(line) - 1; len > 0; len--) {
aedd00
+			if (strchr(" \t\r\n", line[len]) == NULL)
aedd00
+				break;
aedd00
+			line[len] = '\0';
aedd00
+		}
aedd00
+
aedd00
+		if (line[0] == '\0')
aedd00
+			continue;
aedd00
+
aedd00
+		cp = line;
aedd00
+		arg = strdelim(&cp;;
aedd00
+		if (arg && *arg == '\0')
aedd00
+			arg = strdelim(&cp;;
aedd00
+
aedd00
+		if (arg && strcmp(arg, "privsep_preauth") == 0) {
aedd00
+			arg = strdelim(&cp;;
aedd00
+			if (!arg || *arg == '\0') {
aedd00
+				debug("%s: privsep_preauth is empty", __func__);
aedd00
+				fclose(contexts_file);
aedd00
+				return;
aedd00
+			}
aedd00
+			preauth_context = xstrdup(arg);
aedd00
+		}
aedd00
+	}
aedd00
+	fclose(contexts_file);
aedd00
+
aedd00
+	if (preauth_context == NULL) {
aedd00
+		debug("%s: Unable to find 'privsep_preauth' option in"
aedd00
+		    " SELinux context file", __func__);
aedd00
+		return;
aedd00
+	}
aedd00
+
aedd00
+	ssh_selinux_change_context(preauth_context);
aedd00
+	free(preauth_context);
aedd00
+}
aedd00
+
aedd00
 #endif
aedd00
 #endif
aedd00
 
aedd00
diff --git a/openbsd-compat/port-linux.c b/openbsd-compat/port-linux.c
aedd00
index 22ea8ef..1fc963d 100644
aedd00
--- a/openbsd-compat/port-linux.c
aedd00
+++ b/openbsd-compat/port-linux.c
aedd00
@@ -179,7 +179,7 @@ ssh_selinux_change_context(const char *newname)
aedd00
 	strlcpy(newctx + len, newname, newlen - len);
aedd00
 	if ((cx = index(cx + 1, ':')))
aedd00
 		strlcat(newctx, cx, newlen);
aedd00
-	debug3("%s: setting context from '%s' to '%s'", __func__,
aedd00
+	debug("%s: setting context from '%s' to '%s'", __func__,
aedd00
 	    oldctx, newctx);
aedd00
 	if (setcon(newctx) < 0)
aedd00
 		switchlog("%s: setcon %s from %s failed with %s", __func__,
aedd00
diff --git a/openbsd-compat/port-linux.h b/openbsd-compat/port-linux.h
aedd00
index cb51f99..8b7cda2 100644
aedd00
--- a/openbsd-compat/port-linux.h
aedd00
+++ b/openbsd-compat/port-linux.h
aedd00
@@ -29,6 +29,7 @@ int sshd_selinux_enabled(void);
aedd00
 void sshd_selinux_copy_context(void);
aedd00
 void sshd_selinux_setup_exec_context(char *);
aedd00
 int sshd_selinux_setup_env_variables(void);
aedd00
+void sshd_selinux_change_privsep_preauth_context(void);
aedd00
 #endif
aedd00
 
aedd00
 #ifdef LINUX_OOM_ADJUST
aedd00
diff --git a/sshd.c b/sshd.c
aedd00
index 2871fe9..39b9c08 100644
aedd00
--- a/sshd.c
aedd00
+++ b/sshd.c
aedd00
@@ -629,7 +629,7 @@ privsep_preauth_child(void)
aedd00
 	demote_sensitive_data();
aedd00
 
aedd00
 #ifdef WITH_SELINUX
aedd00
-	ssh_selinux_change_context("sshd_net_t");
aedd00
+	sshd_selinux_change_privsep_preauth_context();
aedd00
 #endif
aedd00
 
aedd00
 	/* Demote the child */