bmh10 / rpms / openssh

Forked from rpms/openssh 2 days ago
Clone

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

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