File: SELinux-support.patch

package info (click to toggle)
cron 3.0pl1-198
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,824 kB
  • sloc: ansic: 54,879; xml: 1,600; perl: 733; sh: 495; makefile: 446; python: 43
file content (365 lines) | stat: -rw-r--r-- 9,650 bytes parent folder | download | duplicates (2)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
From: Christian Kastner <ckk@kvr.at>
Date: Thu, 7 Jan 2016 23:17:45 +0100
Subject: SELinux support

Add SELinux support.

Patch contributed by Manoj Srivastava <srivasta@debian.org>, with additional
fixes by Russell Coker <russell@coker.com.au> and Laurent Bigonville
<bigon@debian.org>.
 
Bug-Debian: https://bugs.debian.org/264320
Bug-Debian: https://bugs.debian.org/315509
Bug-Debian: https://bugs.debian.org/324017
Bug-Debian: https://bugs.debian.org/383857
Bug-Debian: https://bugs.debian.org/857662
Bug-Debian: https://bugs.debian.org/924716
Forwarded: no
Last-Update: 2019-09-15
---
 Makefile     |   4 +-
 cron.8       |   2 +
 cron.h       |  12 ++++-
 database.c   |   5 +-
 do_command.c |  23 +++++++++
 user.c       | 158 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 6 files changed, 195 insertions(+), 9 deletions(-)

diff --git a/Makefile b/Makefile
index c8dd2f4..ce16df8 100644
--- a/Makefile
+++ b/Makefile
@@ -55,7 +55,7 @@ DESTMAN		=	$(DESTROOT)/share/man
 INCLUDE		=	-I.
 #INCLUDE	=
 #<<need getopt()>>
-LIBS		= $(PAM_LIBS)
+LIBS		= $(PAM_LIBS) $(SELINUX_LIBS)
 #<<optimize or debug?>>
 #OPTIM		=	-O
 OPTIM		=	-g
@@ -73,7 +73,7 @@ LINTFLAGS	=	-hbxa $(INCLUDE) $(COMPAT) $(DEBUGGING)
 #<<manifest defines>>
 # Allow override from command line
 DEBUG_DEFS	?= -DDEBUGGING=0
-DEFS		= $(DEBUG_DEFS) $(PAM_DEFS)
+DEFS		= $(DEBUG_DEFS) $(PAM_DEFS) $(SELINUX_DEFS)
 #(SGI IRIX systems need this)
 #DEFS		=	-D_BSD_SIGNALS -Dconst=
 #<<the name of the BSD-like install program>>
diff --git a/cron.8 b/cron.8
index 9b2c01d..dd40322 100644
--- a/cron.8
+++ b/cron.8
@@ -118,6 +118,8 @@ Support for /etc/cron.{hourly,daily,weekly,monthly} via /etc/crontab,
 .IP \(em
 PAM support,
 .IP \(em
+SELinux support,
+.IP \(em
 Debian-specific file locations and commands,
 .IP \(em
 Debian-specific configuration (/etc/default/cron),
diff --git a/cron.h b/cron.h
index 2120367..86e7a74 100644
--- a/cron.h
+++ b/cron.h
@@ -40,6 +40,13 @@
 #include "config.h"
 #include "externs.h"
 
+#ifdef WITH_SELINUX
+#include <selinux/selinux.h>
+#endif
+
+#define SYSUSERNAME "root"
+
+
 	/* these are really immutable, and are
 	 *   defined for symbolic convenience only
 	 * TRUE, FALSE, and ERR must be distinct
@@ -178,6 +185,9 @@ typedef	struct _user {
 	char		*name;
 	time_t		mtime;		/* last modtime of crontab */
 	entry		*crontab;	/* this person's crontab */
+#ifdef WITH_SELINUX
+	security_context_t scontext;    /* SELinux security context */
+#endif
 } user;
 
 typedef	struct _cron_db {
@@ -224,7 +234,7 @@ char		*env_get __P((char *, char **)),
 		**env_copy __P((char **)),
 		**env_set __P((char **, char *));
 
-user		*load_user __P((int, struct passwd *, char *)),
+user		*load_user __P((int, struct passwd *, char *, char *, char *)),
 		*find_user __P((cron_db *, const char *));
 
 entry		*load_entry __P((FILE *, void (*)(),
diff --git a/database.c b/database.c
index fceb578..e08a3e1 100644
--- a/database.c
+++ b/database.c
@@ -98,7 +98,7 @@ load_database(old_db)
 	new_db.head = new_db.tail = NULL;
 
 	if (syscron_stat.st_mtime) {
-		process_crontab("root", "*system*",
+		process_crontab(SYSUSERNAME, "*system*",
 				SYSCRONTAB, &syscron_stat,
 				&new_db, old_db);
 	}
@@ -285,7 +285,8 @@ process_crontab(uname, fname, tabname, statbuf, new_db, old_db)
 		free_user(u);
 		log_it(fname, getpid(), "RELOAD", tabname);
 	}
-	u = load_user(crontab_fd, pw, fname);
+
+	u = load_user(crontab_fd, pw, uname, fname, tabname);
 	if (u != NULL) {
 		u->mtime = statbuf->st_mtime;
 		link_user(new_db, u);
diff --git a/do_command.c b/do_command.c
index dec189e..a04ed83 100644
--- a/do_command.c
+++ b/do_command.c
@@ -42,6 +42,11 @@ static const struct pam_conv conv = {
 	}
 #endif
 
+#ifdef WITH_SELINUX
+#include <selinux/selinux.h>
+/* #include <selinux/get_context_list.h> */
+#endif
+
 
 static void		child_process __P((entry *, user *)),
 			do_univ __P((user *));
@@ -313,6 +318,24 @@ child_process(e, u)
 				_exit(OK_EXIT);
 			}
 # endif /*DEBUGGING*/
+#ifdef WITH_SELINUX
+			if (is_selinux_enabled() > 0) {
+				if (u->scontext != 0L) {
+					if (setexeccon(u->scontext) < 0 &&
+							security_getenforce() > 0) {
+						fprintf(stderr, "Could not set exec context "
+								"to %s for user  %s\n",
+								u->scontext,u->name);
+						_exit(ERROR_EXIT);
+					}
+				} else if (security_getenforce() > 0) {
+					fprintf(stderr, "Error, must have a security context "
+							"for the cron job when in enforcing "
+							"mode.\nUser %s.\n", u->name);
+					_exit(ERROR_EXIT);
+				}
+			}
+#endif
 			execle(shell, shell, "-c", e->cmd, (char *)0, jobenv);
 			fprintf(stderr, "%s: execle: %s\n", shell, strerror(errno));
 			_exit(ERROR_EXIT);
diff --git a/user.c b/user.c
index 5226102..6a96bc5 100644
--- a/user.c
+++ b/user.c
@@ -25,6 +25,133 @@ static char rcsid[] = "$Id: user.c,v 2.8 1994/01/15 20:43:43 vixie Exp $";
 
 #include "cron.h"
 
+#ifdef WITH_SELINUX
+#include <selinux/context.h>
+#include <selinux/selinux.h>
+#include <selinux/get_context_list.h>
+
+static int get_security_context(char *name, int crontab_fd, security_context_t
+				*rcontext, char *tabname) {
+	security_context_t *context_list = NULL;
+	security_context_t current_con;
+	int list_count = 0;
+	security_context_t  file_context=NULL;
+	struct av_decision avd;
+	int retval=0;
+	char *seuser = NULL;
+	char *level = NULL;
+	int i;
+
+	if (getcon(&current_con)) {
+		log_it(name, getpid(), "Can't get current context", tabname);
+		return -1;
+	}
+
+	if (name != NULL) {
+		if (getseuserbyname(name, &seuser, &level)) {
+			log_it(name, getpid(), "getseuserbyname FAILED", tabname);
+			freecon(current_con);
+			return (security_getenforce() > 0);
+		}
+	} else {
+		context_t temp_con = context_new(current_con);
+		if (temp_con == NULL) {
+			log_it(name, getpid(), "context_new FAILED", tabname);
+			freecon(current_con);
+			return (security_getenforce() > 0);
+		}
+		seuser = strdup(context_user_get(temp_con));
+		context_free(temp_con);
+	}
+
+	*rcontext = NULL;
+	list_count = get_ordered_context_list_with_level(seuser, level, current_con, &context_list);
+	freecon(current_con);
+	free(seuser);
+	free(level);
+	if (list_count == -1) {
+		if (security_getenforce() > 0) {
+			log_it(name, getpid(), "No SELinux security context", tabname);
+			return -1;
+		} else {
+			log_it(name, getpid(),
+				"No security context but SELinux in permissive mode,"
+				" continuing", tabname);
+			return 0;
+		}
+	}
+
+	if (fgetfilecon(crontab_fd, &file_context) < OK) {
+		if (security_getenforce() > 0) {
+			log_it(name, getpid(), "getfilecon FAILED", tabname);
+			freeconary(context_list);
+			return -1;
+		} else {
+			log_it(name, getpid(), "getfilecon FAILED but SELinux in "
+				"permissive mode, continuing", tabname);
+			*rcontext = strdup(context_list[0]);
+			freeconary(context_list);
+			return 0;
+		}
+	}
+
+	/*
+	* Since crontab files are not directly executed,
+	* crond must ensure that the crontab file has
+	* a context that is appropriate for the context of
+	* the user cron job.  It performs an entrypoint
+	* permission check for this purpose.
+	*/
+
+	security_class_t tclass = string_to_security_class("file");
+	if (!tclass) {
+		log_it(name, getpid(), "Failed to translate security class file", tabname);
+		freeconary(context_list);
+		if (security_deny_unknown() == 0) {
+			return 0;
+		} else {
+			return -1;
+		}
+	}
+
+	access_vector_t bit = string_to_av_perm(tclass, "entrypoint");
+	if (!bit) {
+		log_it(name, getpid(), "Failed to translate av perm entrypoint", tabname);
+		freeconary(context_list);
+		if (security_deny_unknown() == 0) {
+			return 0;
+		} else {
+			return -1;
+		}
+	}
+
+	for (i = 0; i < list_count; i++) {
+		retval = security_compute_av(context_list[i],
+						 file_context,
+						 tclass,
+						 bit,
+						 &avd);
+		if(!retval && ((bit & avd.allowed) == bit)) {
+			*rcontext = strdup(context_list[i]);
+			freecon(file_context);
+			freeconary(context_list);
+			return 0;
+		}
+	}
+	freecon(file_context);
+	if (security_getenforce() > 0) {
+		log_it(name, getpid(), "ENTRYPOINT FAILED", tabname);
+		freeconary(context_list);
+		return -1;
+	} else {
+		log_it(name, getpid(), "ENTRYPOINT FAILED but SELinux in permissive mode, continuing", tabname);
+		*rcontext = strdup(context_list[0]);
+		freeconary(context_list);
+	}
+	return 0;
+}
+#endif
+
 
 void
 free_user(u)
@@ -37,22 +164,28 @@ free_user(u)
 		ne = e->next;
 		free_entry(e);
 	}
+#ifdef WITH_SELINUX
+	if (u->scontext)
+		freecon(u->scontext);
+#endif
 	free(u);
 }
 
 
 user *
-load_user(crontab_fd, pw, name)
+load_user(crontab_fd, pw, uname, fname, tabname)
 	int		crontab_fd;
 	struct passwd	*pw;		/* NULL implies syscrontab */
-	char		*name;
+	char		*uname;
+	char		*fname;
+	char		*tabname;
 {
 	char	envstr[MAX_ENVSTR];
 	FILE	*file;
 	user	*u;
 	entry	*e;
 	int	status;
-	char	**envp, **tenvp;
+	char	**envp = NULL, **tenvp;
 
 	if (!(file = fdopen(crontab_fd, "r"))) {
 		perror("fdopen on crontab_fd in load_user");
@@ -67,13 +200,30 @@ load_user(crontab_fd, pw, name)
 		errno = ENOMEM;
 		return NULL;
 	}
-	if ((u->name = strdup(name)) == NULL) {
+	if ((u->name = strdup(fname)) == NULL) {
 		free(u);
 		errno = ENOMEM;
 		return NULL;
 	}
 	u->crontab = NULL;
 
+#ifdef WITH_SELINUX
+	u->scontext = NULL;
+	if (is_selinux_enabled() > 0) {
+		char *sname=uname;
+		if (pw==NULL) {
+			sname=NULL;
+		}
+		if (get_security_context(sname, crontab_fd,
+					&u->scontext, tabname) != 0 ) {
+			u->scontext = NULL;
+			free_user(u);
+			u = NULL;
+			goto done;
+		}
+	}
+#endif
+
 	/* 
 	 * init environment.  this will be copied/augmented for each entry.
 	 */