summaryrefslogtreecommitdiff
path: root/backport-openssh-6.6.1p1-selinux-contexts.patch
blob: b5347ae86bf078e037e0b38b67479464a9846554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
diff --git a/openbsd-compat/port-linux-sshd.c b/openbsd-compat/port-linux-sshd.c
index 8f32464..18a2ca4 100644
--- a/openbsd-compat/port-linux-sshd.c
+++ b/openbsd-compat/port-linux-sshd.c
Reference:https://src.fedoraproject.org/rpms/openssh/blob/rawhide/f/backport-openssh-6.6.1p1-selinux-contexts.patch
@@ -32,6 +32,7 @@
 #include "misc.h"      /* servconf.h needs misc.h for struct ForwardOptions */
 #include "servconf.h"
 #include "port-linux.h"
+#include "misc.h"
 #include "sshkey.h"
 #include "hostfile.h"
 #include "auth.h"
@@ -445,7 +446,7 @@ sshd_selinux_setup_exec_context(char *pwname)
 void
 sshd_selinux_copy_context(void)
 {
-	security_context_t *ctx;
+	char *ctx;
 
 	if (!sshd_selinux_enabled())
 		return;
@@ -461,6 +462,72 @@ sshd_selinux_copy_context(void)
 	}
 }
 
+void
+sshd_selinux_change_privsep_preauth_context(void)
+{
+	int len;
+	char line[1024], *preauth_context = NULL, *cp, *arg;
+	const char *contexts_path;
+	FILE *contexts_file;
+	struct stat sb;
+
+	contexts_path = selinux_openssh_contexts_path();
+	if (contexts_path == NULL) {
+		debug3_f("Failed to get the path to SELinux context");
+		return;
+	}
+
+	if ((contexts_file = fopen(contexts_path, "r")) == NULL) {
+		debug_f("Failed to open SELinux context file");
+		return;
+	}
+
+	if (fstat(fileno(contexts_file), &sb) != 0 ||
+	    sb.st_uid != 0 || (sb.st_mode & 022) != 0) {
+		logit_f("SELinux context file needs to be owned by root"
+		    " and not writable by anyone else");
+		fclose(contexts_file);
+		return;
+	}
+
+	while (fgets(line, sizeof(line), contexts_file)) {
+		/* Strip trailing whitespace */
+		for (len = strlen(line) - 1; len > 0; len--) {
+			if (strchr(" \t\r\n", line[len]) == NULL)
+				break;
+			line[len] = '\0';
+		}
+
+		if (line[0] == '\0')
+			continue;
+
+		cp = line;
+		arg = strdelim(&cp);
+		if (arg && *arg == '\0')
+			arg = strdelim(&cp);
+
+		if (arg && strcmp(arg, "privsep_preauth") == 0) {
+			arg = strdelim(&cp);
+			if (!arg || *arg == '\0') {
+				debug_f("privsep_preauth is empty");
+				fclose(contexts_file);
+				return;
+			}
+			preauth_context = xstrdup(arg);
+		}
+	}
+	fclose(contexts_file);
+
+	if (preauth_context == NULL) {
+		debug_f("Unable to find 'privsep_preauth' option in"
+		    " SELinux context file");
+		return;
+	}
+
+	ssh_selinux_change_context(preauth_context);
+	free(preauth_context);
+}
+
 #endif
 #endif
 
diff --git a/openbsd-compat/port-linux.c b/openbsd-compat/port-linux.c
index 22ea8ef..1fc963d 100644
--- a/openbsd-compat/port-linux.c
+++ b/openbsd-compat/port-linux.c
Reference:https://src.fedoraproject.org/rpms/openssh/blob/rawhide/f/backport-openssh-6.6.1p1-selinux-contexts.patch
@@ -179,7 +179,7 @@ ssh_selinux_change_context(const char *newname)
 	strlcpy(newctx + len, newname, newlen - len);
 	if ((cx = index(cx + 1, ':')))
 		strlcat(newctx, cx, newlen);
-	debug3("%s: setting context from '%s' to '%s'", __func__,
+	debug_f("setting context from '%s' to '%s'",
 	    oldctx, newctx);
 	if (setcon(newctx) < 0)
 		do_log2(log_level, "%s: setcon %s from %s failed with %s",
		    __func__, newctx, oldctx, strerror(errno));
diff --git a/openbsd-compat/port-linux.h b/openbsd-compat/port-linux.h
index cb51f99..8b7cda2 100644
--- a/openbsd-compat/port-linux.h
+++ b/openbsd-compat/port-linux.h
Reference:https://src.fedoraproject.org/rpms/openssh/blob/rawhide/f/backport-openssh-6.6.1p1-selinux-contexts.patch
@@ -29,6 +29,7 @@ int sshd_selinux_enabled(void);
 void sshd_selinux_copy_context(void);
 void sshd_selinux_setup_exec_context(char *);
 int sshd_selinux_setup_env_variables(void);
+void sshd_selinux_change_privsep_preauth_context(void);
 #endif
 
 #ifdef LINUX_OOM_ADJUST
diff --git a/sshd.c b/sshd.c
index 2871fe9..39b9c08 100644
--- a/sshd.c
+++ b/sshd.c
Reference:https://src.fedoraproject.org/rpms/openssh/blob/rawhide/f/backport-openssh-6.6.1p1-selinux-contexts.patch
@@ -629,7 +629,7 @@ privsep_preauth_child(void)
 	demote_sensitive_data();
 
 #ifdef WITH_SELINUX
-	ssh_selinux_change_context("sshd_net_t");
+	sshd_selinux_change_privsep_preauth_context();
 #endif
 
 	/* Demote the child */