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