smack-setup.c revision 4c1fc3e404d648c70bd2f50ac50aeac6ece8872e
ffbd2c4d45787ba5ba85a32db6551efba66a1ee6Nathaniel Chen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
ffbd2c4d45787ba5ba85a32db6551efba66a1ee6Nathaniel Chen This file is part of systemd.
ffbd2c4d45787ba5ba85a32db6551efba66a1ee6Nathaniel Chen Copyright (C) 2013 Intel Corporation
ffbd2c4d45787ba5ba85a32db6551efba66a1ee6Nathaniel Chen Nathaniel Chen <nathaniel.chen@intel.com>
ffbd2c4d45787ba5ba85a32db6551efba66a1ee6Nathaniel Chen systemd is free software; you can redistribute it and/or modify it
ffbd2c4d45787ba5ba85a32db6551efba66a1ee6Nathaniel Chen under the terms of the GNU Lesser General Public License as published
ffbd2c4d45787ba5ba85a32db6551efba66a1ee6Nathaniel Chen by the Free Software Foundation; either version 2.1 of the License,
ffbd2c4d45787ba5ba85a32db6551efba66a1ee6Nathaniel Chen or (at your option) any later version.
ffbd2c4d45787ba5ba85a32db6551efba66a1ee6Nathaniel Chen systemd is distributed in the hope that it will be useful, but
ffbd2c4d45787ba5ba85a32db6551efba66a1ee6Nathaniel Chen WITHOUT ANY WARRANTY; without even the implied warranty of
ffbd2c4d45787ba5ba85a32db6551efba66a1ee6Nathaniel Chen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ffbd2c4d45787ba5ba85a32db6551efba66a1ee6Nathaniel Chen Lesser General Public License for more details.
ffbd2c4d45787ba5ba85a32db6551efba66a1ee6Nathaniel Chen You should have received a copy of the GNU Lesser General Public License
ffbd2c4d45787ba5ba85a32db6551efba66a1ee6Nathaniel Chen along with systemd; If not, see <http://www.gnu.org/licenses/>.
6656aefb42385b468dd96867118d049f945cbf81WaLyong Chostatic int write_access2_rules(const char* srcdir) {
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho _cleanup_close_ int load2_fd = -1, change_fd = -1;
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho load2_fd = open("/sys/fs/smackfs/load2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho log_warning_errno(errno, "Failed to open '/sys/fs/smackfs/load2': %m");
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho change_fd = open("/sys/fs/smackfs/change-rule", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho log_warning_errno(errno, "Failed to open '/sys/fs/smackfs/change-rule': %m");
a4783bd17ad96f55b0fe83a50959da13555292bfZbigniew Jędrzejewski-Szmek return -errno; /* negative error */
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho /* write rules to load2 or change-rule from every file in the directory */
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho log_warning_errno(errno, "Failed to opendir '%s': %m", srcdir);
a4783bd17ad96f55b0fe83a50959da13555292bfZbigniew Jędrzejewski-Szmek return errno; /* positive on purpose */
a4783bd17ad96f55b0fe83a50959da13555292bfZbigniew Jędrzejewski-Szmek fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho log_warning_errno(errno, "Failed to open '%s': %m", entry->d_name);
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho log_error_errno(errno, "Failed to open '%s': %m", entry->d_name);
ffbd2c4d45787ba5ba85a32db6551efba66a1ee6Nathaniel Chen /* load2 write rules in the kernel require a line buffered stream */
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho log_error_errno(errno, "Failed to read line from '%s': %m",
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho _cleanup_free_ char *sbj = NULL, *obj = NULL, *acc1 = NULL, *acc2 = NULL;
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho /* if 3 args -> load rule : subject object access1 */
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho /* if 4 args -> change rule : subject object access1 access2 */
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho if (sscanf(buf, "%ms %ms %ms %ms", &sbj, &obj, &acc1, &acc2) < 3) {
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho log_error_errno(errno, "Failed to parse rule '%s' in '%s', ignoring.", buf, entry->d_name);
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho if (write(isempty(acc2) ? load2_fd : change_fd, buf, strlen(buf)) < 0) {
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho log_error_errno(errno, "Failed to write '%s' to '%s' in '%s'",
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho buf, isempty(acc2) ? "/sys/fs/smackfs/load2" : "/sys/fs/smackfs/change-rule", entry->d_name);
6656aefb42385b468dd96867118d049f945cbf81WaLyong Chostatic int write_cipso2_rules(const char* srcdir) {
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho cipso2_fd = open("/sys/fs/smackfs/cipso2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho log_warning_errno(errno, "Failed to open '/sys/fs/smackfs/cipso2': %m");
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho /* write rules to cipso2 from every file in the directory */
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho log_warning_errno(errno, "Failed to opendir '%s': %m", srcdir);
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC);
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho log_error_errno(errno, "Failed to open '%s': %m", entry->d_name);
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho log_error_errno(errno, "Failed to open '%s': %m", entry->d_name);
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho /* cipso2 write rules in the kernel require a line buffered stream */
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho log_error_errno(errno, "Failed to read line from '%s': %m",
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho log_error_errno(errno, "Failed to write '%s' to '/sys/fs/smackfs/cipso2' in '%s'",
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho r = write_access2_rules("/etc/smack/accesses.d/");
a4783bd17ad96f55b0fe83a50959da13555292bfZbigniew Jędrzejewski-Szmek log_debug("Smack is not enabled in the kernel.");
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho log_debug("Smack access rules directory '/etc/smack/accesses.d/' not found");
a4783bd17ad96f55b0fe83a50959da13555292bfZbigniew Jędrzejewski-Szmek log_info("Successfully loaded Smack policies.");
abbacb1defaaecb8d2477685f7bb3fabcf58585bNathaniel Chen log_warning("Failed to load Smack access rules: %s, ignoring.",
4c1fc3e404d648c70bd2f50ac50aeac6ece8872eDaniel Mack r = write_string_file("/proc/self/attr/current", SMACK_RUN_LABEL, 0);
8b197c3a8a57c3f7c231b39e5660856fd9580c80Auke Kok log_warning("Failed to set SMACK label \"%s\" on self: %s",
abbacb1defaaecb8d2477685f7bb3fabcf58585bNathaniel Chen log_debug("Smack/CIPSO is not enabled in the kernel.");
6656aefb42385b468dd96867118d049f945cbf81WaLyong Cho log_debug("Smack/CIPSO access rules directory '/etc/smack/cipso.d/' not found");
abbacb1defaaecb8d2477685f7bb3fabcf58585bNathaniel Chen log_info("Successfully loaded Smack/CIPSO policies.");
abbacb1defaaecb8d2477685f7bb3fabcf58585bNathaniel Chen log_warning("Failed to load Smack/CIPSO access rules: %s, ignoring.",