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"
bbf61da
 #include "sshkey.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;
3339efd
@@ -461,6 +462,72 @@ 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;
3339efd
+	struct stat sb;
5296a79
+
5296a79
+	contexts_path = selinux_openssh_contexts_path();
3339efd
+	if (contexts_path == NULL) {
25c16c6
+		debug3_f("Failed to get the path to SELinux context");
3339efd
+		return;
3339efd
+	}
5296a79
+
3339efd
+	if ((contexts_file = fopen(contexts_path, "r")) == NULL) {
25c16c6
+		debug_f("Failed to open SELinux context file");
3339efd
+		return;
3339efd
+	}
5296a79
+
3339efd
+	if (fstat(fileno(contexts_file), &sb) != 0 ||
3339efd
+	    sb.st_uid != 0 || (sb.st_mode & 022) != 0) {
25c16c6
+		logit_f("SELinux context file needs to be owned by root"
25c16c6
+		    " and not writable by anyone else");
3339efd
+		fclose(contexts_file);
3339efd
+		return;
3339efd
+	}
5296a79
+
3339efd
+	while (fgets(line, sizeof(line), contexts_file)) {
3339efd
+		/* Strip trailing whitespace */
3339efd
+		for (len = strlen(line) - 1; len > 0; len--) {
3339efd
+			if (strchr(" \t\r\n", line[len]) == NULL)
3339efd
+				break;
3339efd
+			line[len] = '\0';
3339efd
+		}
5296a79
+
3339efd
+		if (line[0] == '\0')
3339efd
+			continue;
3339efd
+
3339efd
+		cp = line;
3339efd
+		arg = strdelim(&cp;;
3339efd
+		if (arg && *arg == '\0')
3339efd
+			arg = strdelim(&cp;;
3339efd
+
3339efd
+		if (arg && strcmp(arg, "privsep_preauth") == 0) {
3339efd
+			arg = strdelim(&cp;;
3339efd
+			if (!arg || *arg == '\0') {
25c16c6
+				debug_f("privsep_preauth is empty");
3339efd
+				fclose(contexts_file);
3339efd
+				return;
5296a79
+			}
3339efd
+			preauth_context = xstrdup(arg);
5296a79
+		}
5296a79
+	}
3339efd
+	fclose(contexts_file);
5296a79
+
3339efd
+	if (preauth_context == NULL) {
25c16c6
+		debug_f("Unable to find 'privsep_preauth' option in"
25c16c6
+		    " SELinux context file");
3339efd
+		return;
3339efd
+	}
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__,
25c16c6
+	debug_f("setting context from '%s' to '%s'",
5296a79
 	    oldctx, newctx);
5296a79
 	if (setcon(newctx) < 0)
25c16c6
 		do_log2(log_level, "%s: setcon %s from %s failed with %s",
25c16c6
		    __func__, newctx, oldctx, strerror(errno));
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 */