]>
Commit | Line | Data |
---|---|---|
ffbd2c4d NC |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright (C) 2013 Intel Corporation | |
7 | Authors: | |
8 | Nathaniel Chen <nathaniel.chen@intel.com> | |
9 | ||
10 | systemd is free software; you can redistribute it and/or modify it | |
11 | under the terms of the GNU Lesser General Public License as published | |
12 | by the Free Software Foundation; either version 2.1 of the License, | |
13 | or (at your option) any later version. | |
14 | ||
15 | systemd is distributed in the hope that it will be useful, but | |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | Lesser General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU Lesser General Public License | |
21 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
22 | ***/ | |
23 | ||
24 | #include <stdio.h> | |
25 | #include <errno.h> | |
26 | #include <string.h> | |
ffbd2c4d | 27 | #include <stdlib.h> |
ffbd2c4d | 28 | #include <fcntl.h> |
ffbd2c4d | 29 | #include <dirent.h> |
ffbd2c4d NC |
30 | |
31 | #include "macro.h" | |
32 | #include "smack-setup.h" | |
33 | #include "util.h" | |
8b197c3a | 34 | #include "fileio.h" |
ffbd2c4d | 35 | #include "log.h" |
ffbd2c4d | 36 | |
a4783bd1 | 37 | #define SMACK_CONFIG "/etc/smack/accesses.d/" |
a1c9563c | 38 | #define CIPSO_CONFIG "/etc/smack/cipso.d/" |
ffbd2c4d | 39 | |
2b3e18de KL |
40 | #ifdef HAVE_SMACK |
41 | ||
a4783bd1 ZJS |
42 | static int write_rules(const char* dstpath, const char* srcdir) { |
43 | _cleanup_fclose_ FILE *dst = NULL; | |
ffbd2c4d NC |
44 | _cleanup_closedir_ DIR *dir = NULL; |
45 | struct dirent *entry; | |
46 | char buf[NAME_MAX]; | |
47 | int dfd = -1; | |
a4783bd1 | 48 | int r = 0; |
ffbd2c4d | 49 | |
a4783bd1 ZJS |
50 | dst = fopen(dstpath, "we"); |
51 | if (!dst) { | |
52 | if (errno != ENOENT) | |
56f64d95 | 53 | log_warning_errno(errno, "Failed to open %s: %m", dstpath); |
a4783bd1 | 54 | return -errno; /* negative error */ |
ffbd2c4d NC |
55 | } |
56 | ||
a4783bd1 ZJS |
57 | /* write rules to dst from every file in the directory */ |
58 | dir = opendir(srcdir); | |
ffbd2c4d | 59 | if (!dir) { |
a4783bd1 | 60 | if (errno != ENOENT) |
56f64d95 | 61 | log_warning_errno(errno, "Failed to opendir %s: %m", srcdir); |
a4783bd1 | 62 | return errno; /* positive on purpose */ |
ffbd2c4d NC |
63 | } |
64 | ||
65 | dfd = dirfd(dir); | |
fea7838e | 66 | assert(dfd >= 0); |
ffbd2c4d NC |
67 | |
68 | FOREACH_DIRENT(entry, dir, return 0) { | |
a4783bd1 | 69 | int fd; |
ffbd2c4d | 70 | _cleanup_fclose_ FILE *policy = NULL; |
ffbd2c4d | 71 | |
a4783bd1 ZJS |
72 | fd = openat(dfd, entry->d_name, O_RDONLY|O_CLOEXEC); |
73 | if (fd < 0) { | |
74 | if (r == 0) | |
75 | r = -errno; | |
56f64d95 | 76 | log_warning_errno(errno, "Failed to open %s: %m", entry->d_name); |
ffbd2c4d NC |
77 | continue; |
78 | } | |
79 | ||
a4783bd1 | 80 | policy = fdopen(fd, "re"); |
ffbd2c4d | 81 | if (!policy) { |
a4783bd1 ZJS |
82 | if (r == 0) |
83 | r = -errno; | |
03e334a1 | 84 | safe_close(fd); |
56f64d95 | 85 | log_error_errno(errno, "Failed to open %s: %m", entry->d_name); |
ffbd2c4d NC |
86 | continue; |
87 | } | |
88 | ||
ffbd2c4d | 89 | /* load2 write rules in the kernel require a line buffered stream */ |
fea7838e | 90 | FOREACH_LINE(buf, policy, |
56f64d95 | 91 | log_error_errno(errno, "Failed to read line from %s: %m", |
fea7838e | 92 | entry->d_name)) { |
a4783bd1 ZJS |
93 | if (!fputs(buf, dst)) { |
94 | if (r == 0) | |
95 | r = -EINVAL; | |
96 | log_error("Failed to write line to %s", dstpath); | |
97 | break; | |
98 | } | |
99 | if (fflush(dst)) { | |
100 | if (r == 0) | |
101 | r = -errno; | |
56f64d95 | 102 | log_error_errno(errno, "Failed to flush writes to %s: %m", dstpath); |
a4783bd1 ZJS |
103 | break; |
104 | } | |
ffbd2c4d NC |
105 | } |
106 | } | |
107 | ||
a4783bd1 ZJS |
108 | return r; |
109 | } | |
110 | ||
2b3e18de | 111 | #endif |
ffbd2c4d | 112 | |
8a188de9 | 113 | int mac_smack_setup(bool *loaded_policy) { |
2b3e18de KL |
114 | |
115 | #ifdef HAVE_SMACK | |
116 | ||
a4783bd1 ZJS |
117 | int r; |
118 | ||
e49d3c01 ŁS |
119 | assert(loaded_policy); |
120 | ||
a4783bd1 ZJS |
121 | r = write_rules("/sys/fs/smackfs/load2", SMACK_CONFIG); |
122 | switch(r) { | |
123 | case -ENOENT: | |
124 | log_debug("Smack is not enabled in the kernel."); | |
125 | return 0; | |
126 | case ENOENT: | |
127 | log_debug("Smack access rules directory " SMACK_CONFIG " not found"); | |
128 | return 0; | |
129 | case 0: | |
130 | log_info("Successfully loaded Smack policies."); | |
abbacb1d NC |
131 | break; |
132 | default: | |
133 | log_warning("Failed to load Smack access rules: %s, ignoring.", | |
134 | strerror(abs(r))); | |
135 | return 0; | |
136 | } | |
137 | ||
8b197c3a AK |
138 | #ifdef SMACK_RUN_LABEL |
139 | r = write_string_file("/proc/self/attr/current", SMACK_RUN_LABEL); | |
140 | if (r) | |
141 | log_warning("Failed to set SMACK label \"%s\" on self: %s", | |
142 | SMACK_RUN_LABEL, strerror(-r)); | |
143 | #endif | |
144 | ||
abbacb1d NC |
145 | r = write_rules("/sys/fs/smackfs/cipso2", CIPSO_CONFIG); |
146 | switch(r) { | |
147 | case -ENOENT: | |
148 | log_debug("Smack/CIPSO is not enabled in the kernel."); | |
149 | return 0; | |
150 | case ENOENT: | |
151 | log_debug("Smack/CIPSO access rules directory " CIPSO_CONFIG " not found"); | |
152 | return 0; | |
153 | case 0: | |
154 | log_info("Successfully loaded Smack/CIPSO policies."); | |
b9289d4c | 155 | break; |
a4783bd1 | 156 | default: |
abbacb1d | 157 | log_warning("Failed to load Smack/CIPSO access rules: %s, ignoring.", |
a4783bd1 ZJS |
158 | strerror(abs(r))); |
159 | return 0; | |
160 | } | |
2b3e18de | 161 | |
e49d3c01 ŁS |
162 | *loaded_policy = true; |
163 | ||
2b3e18de KL |
164 | #endif |
165 | ||
166 | return 0; | |
ffbd2c4d | 167 | } |