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