smack-setup.c revision b26fa1a2fbcfee7d03b0c8fd15ec3aa64ae70b9f
/***
This file is part of systemd.
Copyright (C) 2013 Intel Corporation
Authors:
Nathaniel Chen <nathaniel.chen@intel.com>
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 <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "alloc-util.h"
#include "dirent-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "log.h"
#include "macro.h"
#include "smack-setup.h"
#include "string-util.h"
#include "util.h"
#ifdef HAVE_SMACK
static int write_access2_rules(const char* srcdir) {
int dfd = -1;
int r = 0;
if (load2_fd < 0) {
return -errno; /* negative error */
}
if (change_fd < 0) {
return -errno; /* negative error */
}
/* write rules to load2 or change-rule from every file in the directory */
if (!dir) {
return errno; /* positive on purpose */
}
int fd;
if (!dirent_is_file(entry))
continue;
if (fd < 0) {
if (r == 0)
r = -errno;
continue;
}
if (!policy) {
if (r == 0)
r = -errno;
safe_close(fd);
continue;
}
/* load2 write rules in the kernel require a line buffered stream */
continue;
/* if 3 args -> load rule : subject object access1 */
/* if 4 args -> change rule : subject object access1 access2 */
continue;
}
if (r == 0)
r = -errno;
}
}
}
return r;
}
static int write_cipso2_rules(const char* srcdir) {
int dfd = -1;
int r = 0;
if (cipso2_fd < 0) {
return -errno; /* negative error */
}
/* write rules to cipso2 from every file in the directory */
if (!dir) {
return errno; /* positive on purpose */
}
int fd;
if (!dirent_is_file(entry))
continue;
if (fd < 0) {
if (r == 0)
r = -errno;
continue;
}
if (!policy) {
if (r == 0)
r = -errno;
safe_close(fd);
continue;
}
/* cipso2 write rules in the kernel require a line buffered stream */
continue;
if (r == 0)
r = -errno;
break;
}
}
}
return r;
}
static int write_netlabel_rules(const char* srcdir) {
int dfd = -1;
int r = 0;
if (!dst) {
return -errno; /* negative error */
}
/* write rules to dst from every file in the directory */
if (!dir) {
return errno; /* positive on purpose */
}
int fd;
if (fd < 0) {
if (r == 0)
r = -errno;
continue;
}
if (!policy) {
if (r == 0)
r = -errno;
safe_close(fd);
continue;
}
/* load2 write rules in the kernel require a line buffered stream */
if (r == 0)
r = -EINVAL;
break;
}
if (r == 0)
r = -errno;
break;
}
}
}
return r;
}
#endif
int mac_smack_setup(bool *loaded_policy) {
#ifdef HAVE_SMACK
int r;
r = write_access2_rules("/etc/smack/accesses.d/");
switch(r) {
case -ENOENT:
log_debug("Smack is not enabled in the kernel.");
return 0;
case ENOENT:
log_debug("Smack access rules directory '/etc/smack/accesses.d/' not found");
return 0;
case 0:
log_info("Successfully loaded Smack policies.");
break;
default:
log_warning_errno(r, "Failed to load Smack access rules, ignoring: %m");
return 0;
}
#ifdef SMACK_RUN_LABEL
if (r < 0)
if (r < 0)
r = write_string_file("/sys/fs/smackfs/netlabel",
"0.0.0.0/0 " SMACK_RUN_LABEL, 0);
if (r < 0)
if (r < 0)
log_warning_errno(r, "Failed to set SMACK netlabel rule \"127.0.0.1 -CIPSO\": %m");
#endif
r = write_cipso2_rules("/etc/smack/cipso.d/");
switch(r) {
case -ENOENT:
return 0;
case ENOENT:
break;
case 0:
break;
default:
log_warning_errno(r, "Failed to load Smack/CIPSO access rules, ignoring: %m");
break;
}
r = write_netlabel_rules("/etc/smack/netlabel.d/");
switch(r) {
case -ENOENT:
return 0;
case ENOENT:
log_debug("Smack network host rules directory '/etc/smack/netlabel.d/' not found");
break;
case 0:
log_info("Successfully loaded Smack network host rules.");
break;
default:
log_warning_errno(r, "Failed to load Smack network host rules: %m, ignoring.");
break;
}
*loaded_policy = true;
#endif
return 0;
}