kentpeacock / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
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;
4369a3
@@ -461,6 +462,72 @@ 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;
4369a3
+	struct stat sb;
f5835d
+
f5835d
+	contexts_path = selinux_openssh_contexts_path();
4369a3
+	if (contexts_path == NULL) {
4369a3
+		debug3("%s: Failed to get the path to SELinux context", __func__);
4369a3
+		return;
4369a3
+	}
4369a3
+
4369a3
+	if ((contexts_file = fopen(contexts_path, "r")) == NULL) {
4369a3
+		debug("%s: Failed to open SELinux context file", __func__);
4369a3
+		return;
4369a3
+	}
4369a3
+
4369a3
+	if (fstat(fileno(contexts_file), &sb) != 0 ||
4369a3
+	    sb.st_uid != 0 || (sb.st_mode & 022) != 0) {
4369a3
+		logit("%s: SELinux context file needs to be owned by root"
4369a3
+		    " and not writable by anyone else", __func__);
4369a3
+		fclose(contexts_file);
4369a3
+		return;
4369a3
+	}
f5835d
+
4369a3
+	while (fgets(line, sizeof(line), contexts_file)) {
4369a3
+		/* Strip trailing whitespace */
4369a3
+		for (len = strlen(line) - 1; len > 0; len--) {
4369a3
+			if (strchr(" \t\r\n", line[len]) == NULL)
4369a3
+				break;
4369a3
+			line[len] = '\0';
4369a3
+		}
f5835d
+
4369a3
+		if (line[0] == '\0')
4369a3
+			continue;
f5835d
+
4369a3
+		cp = line;
4369a3
+		arg = strdelim(&cp;;
4369a3
+		if (arg && *arg == '\0')
4369a3
+			arg = strdelim(&cp;;
f5835d
+
4369a3
+		if (arg && strcmp(arg, "privsep_preauth") == 0) {
4369a3
+			arg = strdelim(&cp;;
4369a3
+			if (!arg || *arg == '\0') {
4369a3
+				debug("%s: privsep_preauth is empty", __func__);
4369a3
+				fclose(contexts_file);
4369a3
+				return;
f5835d
+			}
4369a3
+			preauth_context = xstrdup(arg);
f5835d
+		}
f5835d
+	}
4369a3
+	fclose(contexts_file);
f5835d
+
4369a3
+	if (preauth_context == NULL) {
4369a3
+		debug("%s: Unable to find 'privsep_preauth' option in"
4369a3
+		    " SELinux context file", __func__);
4369a3
+		return;
4369a3
+	}
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
@@ -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 */