summaryrefslogtreecommitdiff
path: root/src/mcstransd.c
blob: 4a3feaf30b5f3b9fd5ded14351c1396ab73367d6 (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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/* Copyright (c) 2006 Trusted Computer Solutions, Inc. */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <syslog.h>
#include <selinux/selinux.h>
#include <sys/types.h>
#include <sys/capability.h>
#include <sys/resource.h>
#include "mcstrans.h"

#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#else
# define UNUSED(x) x
#endif

#define SETRANS_UNIX_SOCKET "/var/run/setrans/.setrans-unix"

#define SETRANS_INIT			1
#define RAW_TO_TRANS_CONTEXT		2
#define TRANS_TO_RAW_CONTEXT		3
#define RAW_CONTEXT_TO_COLOR		4
#define MAX_DATA_BUF			4096
#define MAX_DESCRIPTORS			8192

#ifdef DEBUG
//#define log_debug(fmt, ...) syslog(LOG_DEBUG, fmt, __VA_ARGS__)
#define log_debug(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
#else
#define log_debug(fmt, ...) ;
#endif

extern int init_translations(void);
extern void finish_context_translations(void);
extern int trans_context(const security_context_t, security_context_t *);
extern int untrans_context(const security_context_t, security_context_t *);

extern int init_colors(void);
extern void finish_context_colors(void);
extern int raw_color(const security_context_t, char **);

#define SETRANSD_PATHNAME "/sbin/mcstransd"

/* name of program (for error messages) */
#define SETRANSD_PROGNAME "mcstransd"

static int sockfd = -1;	/* socket we are listening on */

static volatile int restart_daemon = 0;
static void cleanup_exit(int ret) __attribute__ ((noreturn));
static void
cleanup_exit(int ret) 
{
	finish_context_colors();
	finish_context_translations();
	if (sockfd >=0)
		(void)unlink(SETRANS_UNIX_SOCKET);

	log_debug("%s\n", "cleanup_exit");

	exit(ret);
}

static void clean_exit(void);
static  __attribute__((noreturn)) void clean_exit(void)
{
	log_debug("%s\n", "clean_exit");
	cleanup_exit(0);
}

static int
send_response(int fd, uint32_t function, char *data, int32_t ret_val)
{
	struct iovec resp_hdr[3];
	uint32_t data_size;
	struct iovec resp_data;
	ssize_t count;

	if (!data)
		data = "";

	data_size = strlen(data) + 1;

	resp_hdr[0].iov_base = &function;
	resp_hdr[0].iov_len = sizeof(function);
	resp_hdr[1].iov_base = &data_size;
	resp_hdr[1].iov_len = sizeof(data_size);
	resp_hdr[2].iov_base = &ret_val;
	resp_hdr[2].iov_len = sizeof(ret_val);

	while (((count = writev(fd, resp_hdr, 3)) < 0) && (errno == EINTR));
	if (count != (sizeof(function) + sizeof(data_size) + sizeof(ret_val))) {
		syslog(LOG_ERR, "Failed to write response header");
		return -1;
	}

	resp_data.iov_base = data;
	resp_data.iov_len = data_size;

	while (((count = writev(fd, &resp_data, 1)) < 0) && (errno == EINTR));
	if (count < 0 || (size_t)count != data_size) {
		syslog(LOG_ERR, "Failed to write response data");
		return -1;
	}

	return ret_val;
}

static int
get_peer_pid(int fd, pid_t *pid)
{
	int ret;
	socklen_t size = sizeof(struct ucred);
	struct ucred peercred;

	/* get the context of the requesting process */
	ret = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &peercred, &size);
	if (ret < 0) {
		syslog(LOG_ERR, "Failed to get PID of client process");
		return -1;
	}
	*pid = peercred.pid;
	return ret;
}


static int
get_peer_con(int fd, char **peercon)
{
	int ret;
	pid_t pid;
	ret = get_peer_pid(fd, &pid);
	if (ret)
		return -1;
	ret = getpidcon_raw(pid, peercon);
	if (ret) {
		syslog(LOG_ERR, 
			"Failed to get context of client process (pid=%u)",
			pid);
		return -1;
	}
	return 0;
}

static int
process_request(int fd, uint32_t function, char *data1, char *UNUSED(data2))
{
	int32_t result;
	char *out = NULL;
	char *peercon = NULL;
	int ret;

	ret = get_peer_con(fd, &peercon);
	if (ret)
		return ret;

	/* TODO: Check if MLS clearance (in peercon) dominates the MLS label
	 * (in the request input).
	 */
  
	switch (function) {
	case SETRANS_INIT:
		result = 0;
		ret = send_response(fd, function, NULL, result);
		break;
	case RAW_TO_TRANS_CONTEXT:
		result = trans_context(data1, &out);
		ret = send_response(fd, function, out, result);
		break;
	case TRANS_TO_RAW_CONTEXT:
		result = untrans_context(data1, &out);
		ret = send_response(fd, function, out, result);
		break;
	case RAW_CONTEXT_TO_COLOR:
		result = raw_color(data1, &out);
		ret = send_response(fd, function, out, result);
		break;
	default:
		result = -1;
		ret = -1;
		break;
	}

	if (result) {
		pid_t pid = 0;
		get_peer_pid(fd, &pid);
		syslog(LOG_ERR, "Invalid request func=%d from=%u",
		       function, pid);
	}

	free(out);
	freecon(peercon);

	return ret;
}

static int
service_request(int fd)
{
	struct iovec req_hdr[3];
	uint32_t function;
	uint32_t data1_size;
	uint32_t data2_size;
	struct iovec req_data[2];
	char *data1;
	char *data2;
	int ret;
	ssize_t count;

	req_hdr[0].iov_base = &function;
	req_hdr[0].iov_len = sizeof(function);
	req_hdr[1].iov_base = &data1_size;
	req_hdr[1].iov_len = sizeof(data1_size);
	req_hdr[2].iov_base = &data2_size;
	req_hdr[2].iov_len = sizeof(data2_size);

	while (((count = readv(fd, req_hdr, 3)) < 0) && (errno == EINTR));
	if (count <= 0) {
		return 1;
	}
	if (count != (sizeof(function) + sizeof(data1_size) +
	              sizeof(data2_size) )) {
		log_debug("Failed to read request header %d != %u\n",(int)count,
			(unsigned)(sizeof(function) + sizeof(data1_size) +
                      sizeof(data2_size) ));
		return -1;
	}

	if (!data1_size || !data2_size || data1_size > MAX_DATA_BUF ||
						data2_size > MAX_DATA_BUF ) {
		log_debug("Header invalid data1_size=%u data2_size=%u\n",
		        data1_size, data2_size);
		return -1;
	}

	data1 = malloc(data1_size);
	if (!data1) {
		log_debug("Could not allocate %d bytes\n", data1_size);
		return -1; 
	}
	data2 = malloc(data2_size);
	if (!data2) {
		free(data1);
		log_debug("Could not allocate %d bytes\n", data2_size);
		return -1;
	}

	req_data[0].iov_base = data1;
	req_data[0].iov_len = data1_size;
	req_data[1].iov_base = data2;
	req_data[1].iov_len = data2_size;

	while (((count = readv(fd, req_data, 2)) < 0) && (errno == EINTR));
	if (count <= 0 || (size_t)count != (data1_size + data2_size) ||
	    data1[data1_size - 1] != '\0' || data2[data2_size - 1] != '\0') {
		free(data1);
		free(data2);
		log_debug("Failed to read request data (%d)\n", (int)count);
		return -1;
	}

	ret = process_request(fd, function, data1, data2);

	free(data1);
	free(data2);

	return ret;
}

static int
add_pollfd(struct pollfd **ufds, int *nfds, int connfd)
{
	int ii = 0;

	/* First see if we can find an already invalidated ufd */
	for (ii = 0; ii < *nfds; ii++) {
		if ((*ufds)[ii].fd == -1)
			break;
	}

	if (ii == *nfds) {
		struct pollfd *tmp = (struct pollfd *)realloc(*ufds,
					(*nfds+1)*sizeof(struct pollfd));
		if (!tmp) {
			syslog(LOG_ERR, "realloc failed for %d fds", *nfds+1);
			return -1;
		}

		*ufds = tmp;
		(*nfds)++;
	}

	(*ufds)[ii].fd = connfd;
	(*ufds)[ii].events = POLLIN|POLLPRI;
	(*ufds)[ii].revents = 0;

	return 0;
}

static void
adj_pollfds(struct pollfd **ufds, int *nfds)
{
	int ii, jj;

	jj = 0;
	for (ii = 0; ii < *nfds; ii++) {
		if ((*ufds)[ii].fd != -1) {
			if (jj < ii)
				(*ufds)[jj] = (*ufds)[ii];
			jj++;
		}
	}
	*nfds = jj;
}

static int
process_events(struct pollfd **ufds, int *nfds)
{
	int ii = 0;
	int ret = 0;

	for (ii = 0; ii < *nfds; ii++) {
		short revents = (*ufds)[ii].revents;
		int connfd = (*ufds)[ii].fd;

		if (revents & (POLLIN | POLLPRI)) {
			if (connfd == sockfd) {

				/* Probably received a connection */
				if ((connfd = accept(sockfd, NULL, NULL)) < 0) {
					syslog(LOG_ERR, "accept() failed: %m");
					return -1;
				}

				if (add_pollfd(ufds, nfds, connfd)) {
					syslog(LOG_ERR,
					  "Failed to add fd (%d) to poll list\n",
						connfd);
					return -1;
				}
			} else {
				ret = service_request(connfd);
				if (ret) {
					if (ret < 0) {
						syslog(LOG_ERR,
							"Servicing of request "
							"failed for fd (%d)\n",
							connfd);
					}
					/* Setup pollfd for deletion later. */
					(*ufds)[ii].fd = -1;
					close(connfd);
					/* So we don't get bothered later */
					revents = revents & ~(POLLHUP);
				}
			}
			revents = revents & ~(POLLIN | POLLPRI);
		}
		if (revents & POLLHUP) {
			log_debug("The connection with fd (%d) hung up\n",
				connfd);

			/* Set the pollfd up for deletion later. */
			(*ufds)[ii].fd = -1;
			close(connfd);

			revents = revents & ~(POLLHUP);
		}
		if (revents) {
			syslog(LOG_ERR, "Unknown/error events (%x) encountered"
					" for fd (%d)\n", revents, connfd);

			/* Set the pollfd up for deletion later. */
			(*ufds)[ii].fd = -1;
			close(connfd);
		}

		(*ufds)[ii].revents = 0;
	}

	/* Delete any invalidated ufds */
	adj_pollfds(ufds, nfds);

	return 0;
}

static void
process_connections(void) __attribute__ ((noreturn));

static void
process_connections(void)
{
	int ret = 0;
	int nfds = 1;

	struct pollfd *ufds = (struct pollfd *)malloc(sizeof(struct pollfd));
	if (!ufds) {
		syslog(LOG_ERR, "Failed to allocate a pollfd");
		cleanup_exit(1);
	}
	ufds[0].fd = sockfd;
	ufds[0].events = POLLIN|POLLPRI;
	ufds[0].revents = 0;

	while (1) {
		if (restart_daemon) {
			syslog(LOG_NOTICE, "Reload Translations");
			finish_context_colors();
			finish_context_translations();
			if (init_translations()) {
				syslog(LOG_ERR, "Failed to initialize label translations");
				cleanup_exit(1);
			}
			if (init_colors()) {
				syslog(LOG_ERR, "Failed to initialize color translations");
				cleanup_exit(1);
			}
			restart_daemon = 0;
		}

		ret = poll(ufds, nfds, -1);
		if (ret < 0) {
			if (errno == EINTR) {
				continue;
			}
			syslog(LOG_ERR, "poll() failed: %m");
			cleanup_exit(1);
		}

		ret = process_events(&ufds, &nfds);
		if (ret) {
			syslog(LOG_ERR, "Error processing events");
			cleanup_exit(1);
		}
	}
}

static void
sigterm_handler(int sig) __attribute__ ((noreturn));

static void
sigterm_handler(int UNUSED(sig))
{
	cleanup_exit(0);
}

static void
sighup_handler(int UNUSED(sig))
{
	restart_daemon = 1;
}

static void
initialize(void)
{
	struct sigaction act;
	struct sockaddr_un addr;
	struct rlimit rl ;

	if (init_translations()) {
		syslog(LOG_ERR, "Failed to initialize label translations");
		cleanup_exit(1);
	}
	if (init_colors()) {
		syslog(LOG_ERR, "Failed to initialize color translations");
		cleanup_exit(1);
	}

	/* the socket will be unlinked when the daemon terminates */
	act.sa_handler = sigterm_handler;
	sigemptyset(&act.sa_mask);
	sigaddset(&act.sa_mask, SIGINT);
	sigaddset(&act.sa_mask, SIGQUIT);
	sigaddset(&act.sa_mask, SIGTERM);
	sigaddset(&act.sa_mask, SIGHUP);
	act.sa_flags = 0;
	sigaction(SIGINT, &act, NULL);
	sigaction(SIGQUIT, &act, NULL);
	sigaction(SIGTERM, &act, NULL);

	/* restart the daemon on SIGHUP */
	act.sa_handler = sighup_handler;
	sigemptyset(&act.sa_mask);
	sigaddset(&act.sa_mask, SIGINT);
	sigaddset(&act.sa_mask, SIGQUIT);
	sigaddset(&act.sa_mask, SIGTERM);
	act.sa_flags = 0;
	sigaction(SIGHUP, &act, NULL);

	/* ignore SIGPIPE (in case a client terminates after sending request) */
	act.sa_handler = SIG_IGN;
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;
	sigaction(SIGPIPE, &act, NULL);

	atexit(clean_exit);

	sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
	if (sockfd < 0)	{
		syslog(LOG_ERR, "socket() failed: %m");
		cleanup_exit(1);
	}

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	strncpy(addr.sun_path, SETRANS_UNIX_SOCKET, sizeof(addr.sun_path) - 1);

	(void)unlink(SETRANS_UNIX_SOCKET);

	if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
		syslog(LOG_ERR, "bind() failed: %m");
		cleanup_exit(1);
	}

	if (listen(sockfd, SOMAXCONN) < 0) {
		syslog(LOG_ERR, "listen() failed: %m");
		cleanup_exit(1);
	}

	if (chmod(SETRANS_UNIX_SOCKET, S_IRWXU | S_IRWXG | S_IRWXO)) {
		syslog(LOG_ERR, "chmod() failed: %m");
		cleanup_exit(1);
	}

	/* Raise the rlimit for file descriptors... */
	rl.rlim_max = MAX_DESCRIPTORS;
	rl.rlim_cur = MAX_DESCRIPTORS;
	setrlimit(RLIMIT_NOFILE, &rl);

}

void dropprivs(void)
{
	cap_t new_caps;

	new_caps = cap_init();
	if (cap_set_proc(new_caps)) {
		syslog(LOG_ERR, "Error dropping capabilities, aborting: %s\n",
			 strerror(errno));
		exit(-1);
	}
	cap_free(new_caps);
}

int
main(int UNUSED(argc), char *argv[])
{
#ifndef DEBUG
	/* Make sure we are root */
	if (getuid() != 0) {
		syslog(LOG_ERR, "You must be root to run this program.\n");
		return 4;
	}
#endif

	openlog(SETRANSD_PROGNAME, 0, LOG_DAEMON);
	syslog(LOG_NOTICE, "%s starting", argv[0]);

	initialize();

#ifndef DEBUG
	dropprivs();

	/* run in the background as a daemon */
	if (daemon(0, 0)) {
		syslog(LOG_ERR, "daemon() failed: %m");
		exit(1);
	}
#endif

	syslog(LOG_NOTICE, "%s initialized", argv[0]);
	process_connections();

	/* we should never get here */
	return 1;
}