jonathancammack / rpms / openssh

Forked from rpms/openssh 8 months ago
Clone

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

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