2013-03-07 20:06:58 +01:00
|
|
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
|
|
|
|
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright (C) 2013 Intel Corporation
|
|
|
|
Authors:
|
|
|
|
Nathaniel Chen <nathaniel.chen@intel.com>
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU Lesser General Public License as published
|
|
|
|
by the Free Software Foundation; either version 2.1 of the License,
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/vfs.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "macro.h"
|
|
|
|
#include "smack-setup.h"
|
|
|
|
#include "util.h"
|
2013-09-26 00:49:42 +02:00
|
|
|
#include "fileio.h"
|
2013-03-07 20:06:58 +01:00
|
|
|
#include "log.h"
|
|
|
|
#include "label.h"
|
|
|
|
|
2013-03-14 04:23:06 +01:00
|
|
|
#define SMACK_CONFIG "/etc/smack/accesses.d/"
|
2013-10-01 02:43:38 +02:00
|
|
|
#define CIPSO_CONFIG "/etc/smack/cipso.d/"
|
2013-03-07 20:06:58 +01:00
|
|
|
|
2013-05-16 10:40:03 +02:00
|
|
|
#ifdef HAVE_SMACK
|
|
|
|
|
2013-03-14 04:23:06 +01:00
|
|
|
static int write_rules(const char* dstpath, const char* srcdir) {
|
|
|
|
_cleanup_fclose_ FILE *dst = NULL;
|
2013-03-07 20:06:58 +01:00
|
|
|
_cleanup_closedir_ DIR *dir = NULL;
|
|
|
|
struct dirent *entry;
|
|
|
|
char buf[NAME_MAX];
|
|
|
|
int dfd = -1;
|
2013-03-14 04:23:06 +01:00
|
|
|
int r = 0;
|
2013-03-07 20:06:58 +01:00
|
|
|
|
2013-03-14 04:23:06 +01:00
|
|
|
dst = fopen(dstpath, "we");
|
|
|
|
if (!dst) {
|
|
|
|
if (errno != ENOENT)
|
|
|
|
log_warning("Failed to open %s: %m", dstpath);
|
|
|
|
return -errno; /* negative error */
|
2013-03-07 20:06:58 +01:00
|
|
|
}
|
|
|
|
|
2013-03-14 04:23:06 +01:00
|
|
|
/* write rules to dst from every file in the directory */
|
|
|
|
dir = opendir(srcdir);
|
2013-03-07 20:06:58 +01:00
|
|
|
if (!dir) {
|
2013-03-14 04:23:06 +01:00
|
|
|
if (errno != ENOENT)
|
|
|
|
log_warning("Failed to opendir %s: %m", srcdir);
|
|
|
|
return errno; /* positive on purpose */
|
2013-03-07 20:06:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dfd = dirfd(dir);
|
2013-03-11 23:03:13 +01:00
|
|
|
assert(dfd >= 0);
|
2013-03-07 20:06:58 +01:00
|
|
|
|
|
|
|
FOREACH_DIRENT(entry, dir, return 0) {
|
2013-03-14 04:23:06 +01:00
|
|
|
int fd;
|
2013-03-07 20:06:58 +01:00
|
|
|
_cleanup_fclose_ FILE *policy = NULL;
|
|
|
|
|
2013-03-14 04:23:06 +01:00
|
|
|
fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
|
|
|
|
if (fd < 0) {
|
|
|
|
if (r == 0)
|
|
|
|
r = -errno;
|
|
|
|
log_warning("Failed to open %s: %m", entry->d_name);
|
2013-03-07 20:06:58 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-03-14 04:23:06 +01:00
|
|
|
policy = fdopen(fd, "re");
|
2013-03-07 20:06:58 +01:00
|
|
|
if (!policy) {
|
2013-03-14 04:23:06 +01:00
|
|
|
if (r == 0)
|
|
|
|
r = -errno;
|
|
|
|
close_nointr_nofail(fd);
|
|
|
|
log_error("Failed to open %s: %m", entry->d_name);
|
2013-03-07 20:06:58 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load2 write rules in the kernel require a line buffered stream */
|
2013-03-11 23:03:13 +01:00
|
|
|
FOREACH_LINE(buf, policy,
|
2013-03-14 04:23:06 +01:00
|
|
|
log_error("Failed to read line from %s: %m",
|
2013-03-11 23:03:13 +01:00
|
|
|
entry->d_name)) {
|
2013-03-14 04:23:06 +01:00
|
|
|
if (!fputs(buf, dst)) {
|
|
|
|
if (r == 0)
|
|
|
|
r = -EINVAL;
|
|
|
|
log_error("Failed to write line to %s", dstpath);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fflush(dst)) {
|
|
|
|
if (r == 0)
|
|
|
|
r = -errno;
|
|
|
|
log_error("Failed to flush writes to %s: %m", dstpath);
|
|
|
|
break;
|
|
|
|
}
|
2013-03-07 20:06:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-14 04:23:06 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-05-16 10:40:03 +02:00
|
|
|
#endif
|
2013-03-07 20:06:58 +01:00
|
|
|
|
2013-03-14 04:23:06 +01:00
|
|
|
int smack_setup(void) {
|
2013-05-16 10:40:03 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_SMACK
|
|
|
|
|
2013-03-14 04:23:06 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
r = write_rules("/sys/fs/smackfs/load2", SMACK_CONFIG);
|
|
|
|
switch(r) {
|
|
|
|
case -ENOENT:
|
|
|
|
log_debug("Smack is not enabled in the kernel.");
|
|
|
|
return 0;
|
|
|
|
case ENOENT:
|
|
|
|
log_debug("Smack access rules directory " SMACK_CONFIG " not found");
|
|
|
|
return 0;
|
|
|
|
case 0:
|
|
|
|
log_info("Successfully loaded Smack policies.");
|
2013-03-13 00:16:44 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log_warning("Failed to load Smack access rules: %s, ignoring.",
|
|
|
|
strerror(abs(r)));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-26 00:49:42 +02:00
|
|
|
#ifdef SMACK_RUN_LABEL
|
|
|
|
r = write_string_file("/proc/self/attr/current", SMACK_RUN_LABEL);
|
|
|
|
if (r)
|
|
|
|
log_warning("Failed to set SMACK label \"%s\" on self: %s",
|
|
|
|
SMACK_RUN_LABEL, strerror(-r));
|
|
|
|
#endif
|
|
|
|
|
2013-03-13 00:16:44 +01:00
|
|
|
r = write_rules("/sys/fs/smackfs/cipso2", CIPSO_CONFIG);
|
|
|
|
switch(r) {
|
|
|
|
case -ENOENT:
|
|
|
|
log_debug("Smack/CIPSO is not enabled in the kernel.");
|
|
|
|
return 0;
|
|
|
|
case ENOENT:
|
|
|
|
log_debug("Smack/CIPSO access rules directory " CIPSO_CONFIG " not found");
|
|
|
|
return 0;
|
|
|
|
case 0:
|
|
|
|
log_info("Successfully loaded Smack/CIPSO policies.");
|
2013-03-14 04:23:06 +01:00
|
|
|
return 0;
|
|
|
|
default:
|
2013-03-13 00:16:44 +01:00
|
|
|
log_warning("Failed to load Smack/CIPSO access rules: %s, ignoring.",
|
2013-03-14 04:23:06 +01:00
|
|
|
strerror(abs(r)));
|
|
|
|
return 0;
|
|
|
|
}
|
2013-05-16 10:40:03 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
2013-03-07 20:06:58 +01:00
|
|
|
}
|