| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
/*** |
| 3 |
Copyright © 2013 Intel Corporation |
| 4 |
Authors: |
| 5 |
Nathaniel Chen <[email protected]> |
| 6 |
***/ |
| 7 |
|
| 8 |
#include <fcntl.h> |
| 9 |
#include <stdio.h> |
| 10 |
#include <unistd.h> |
| 11 |
|
| 12 |
#include "sd-messages.h" |
| 13 |
|
| 14 |
#include "alloc-util.h" |
| 15 |
#include "dirent-util.h" |
| 16 |
#include "errno-util.h" |
| 17 |
#include "fd-util.h" |
| 18 |
#include "fileio.h" |
| 19 |
#include "log.h" |
| 20 |
#include "smack-setup.h" |
| 21 |
#include "string-util.h" |
| 22 |
|
| 23 |
#if ENABLE_SMACK |
| 24 |
|
| 25 |
static int fdopen_unlocked_at(int dfd, const char *dir, const char *name, int *status, FILE **ret_file) { |
| 26 |
int fd, r; |
| 27 |
FILE *f; |
| 28 |
|
| 29 |
assert(status); |
| 30 |
assert(ret_file); |
| 31 |
|
| 32 |
fd = openat(dfd, name, O_RDONLY|O_CLOEXEC); |
| 33 |
if (fd < 0) { |
| 34 |
if (*status == 0) |
| 35 |
*status = -errno; |
| 36 |
|
| 37 |
return log_warning_errno(errno, "Failed to open \"%s/%s\": %m", dir, name); |
| 38 |
} |
| 39 |
|
| 40 |
r = fdopen_unlocked(fd, "r", &f); |
| 41 |
if (r < 0) { |
| 42 |
if (*status == 0) |
| 43 |
*status = r; |
| 44 |
|
| 45 |
safe_close(fd); |
| 46 |
return log_error_errno(r, "Failed to open \"%s/%s\": %m", dir, name); |
| 47 |
} |
| 48 |
|
| 49 |
*ret_file = f; |
| 50 |
return 0; |
| 51 |
} |
| 52 |
|
| 53 |
static int write_access2_rules(const char *srcdir) { |
| 54 |
_cleanup_close_ int load2_fd = -EBADF, change_fd = -EBADF; |
| 55 |
_cleanup_closedir_ DIR *dir = NULL; |
| 56 |
int dfd, r; |
| 57 |
|
| 58 |
load2_fd = r = RET_NERRNO(open("/sys/fs/smackfs/load2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY)); |
| 59 |
if (r < 0) { |
| 60 |
if (r != -ENOENT) |
| 61 |
log_warning_errno(r, "Failed to open %s: %m", "/sys/fs/smackfs/load2"); |
| 62 |
return r; |
| 63 |
} |
| 64 |
|
| 65 |
change_fd = r = RET_NERRNO(open("/sys/fs/smackfs/change-rule", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY)); |
| 66 |
if (r < 0) { |
| 67 |
if (r != -ENOENT) |
| 68 |
log_warning_errno(r, "Failed to open %s: %m", "/sys/fs/smackfs/change-rule"); |
| 69 |
return r; |
| 70 |
} |
| 71 |
|
| 72 |
/* write rules to load2 or change-rule from every file in the directory */ |
| 73 |
dir = opendir(srcdir); |
| 74 |
if (!dir) { |
| 75 |
if (errno != ENOENT) |
| 76 |
log_warning_errno(errno, "Failed to open %s/: %m", srcdir); |
| 77 |
return errno; /* positive on purpose */ |
| 78 |
} |
| 79 |
|
| 80 |
dfd = dirfd(dir); |
| 81 |
assert(dfd >= 0); |
| 82 |
|
| 83 |
r = 0; |
| 84 |
FOREACH_DIRENT(entry, dir, return 0) { |
| 85 |
_cleanup_fclose_ FILE *policy = NULL; |
| 86 |
|
| 87 |
if (!dirent_is_file(entry)) |
| 88 |
continue; |
| 89 |
|
| 90 |
if (fdopen_unlocked_at(dfd, srcdir, entry->d_name, &r, &policy) < 0) |
| 91 |
continue; |
| 92 |
|
| 93 |
/* load2 write rules in the kernel require a line buffered stream */ |
| 94 |
for (;;) { |
| 95 |
_cleanup_free_ char *buf = NULL, *sbj = NULL, *obj = NULL, *acc1 = NULL, *acc2 = NULL; |
| 96 |
int q; |
| 97 |
|
| 98 |
q = read_line(policy, NAME_MAX, &buf); |
| 99 |
if (q < 0) |
| 100 |
return log_error_errno(q, "%s/%s: failed to read line: %m", srcdir, entry->d_name); |
| 101 |
if (q == 0) |
| 102 |
break; |
| 103 |
|
| 104 |
if (isempty(buf) || strchr(COMMENTS, buf[0])) |
| 105 |
continue; |
| 106 |
|
| 107 |
/* if 3 args -> load rule : subject object access1 */ |
| 108 |
/* if 4 args -> change rule : subject object access1 access2 */ |
| 109 |
if (sscanf(buf, "%ms %ms %ms %ms", &sbj, &obj, &acc1, &acc2) < 3) { |
| 110 |
log_error_errno(errno, "%s/%s: failed to parse rule '%s', ignoring.", |
| 111 |
srcdir, entry->d_name, buf); |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
q = RET_NERRNO(write(isempty(acc2) ? load2_fd : change_fd, buf, strlen(buf))); |
| 116 |
if (q < 0) { |
| 117 |
log_error_errno(q, "%s/%s: failed to write '%s' to '%s': %m", |
| 118 |
srcdir, entry->d_name, |
| 119 |
buf, isempty(acc2) ? "/sys/fs/smackfs/load2" : "/sys/fs/smackfs/change-rule"); |
| 120 |
RET_GATHER(r, q); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
return r; |
| 126 |
} |
| 127 |
|
| 128 |
static int write_cipso2_rules(const char *srcdir) { |
| 129 |
_cleanup_close_ int cipso2_fd = -EBADF; |
| 130 |
_cleanup_closedir_ DIR *dir = NULL; |
| 131 |
int dfd, r; |
| 132 |
|
| 133 |
cipso2_fd = r = RET_NERRNO(open("/sys/fs/smackfs/cipso2", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY)); |
| 134 |
if (r < 0) { |
| 135 |
if (r != -ENOENT) |
| 136 |
log_warning_errno(r, "Failed to open %s: %m", "/sys/fs/smackfs/cipso2"); |
| 137 |
return r; |
| 138 |
} |
| 139 |
|
| 140 |
/* write rules to cipso2 from every file in the directory */ |
| 141 |
dir = opendir(srcdir); |
| 142 |
if (!dir) { |
| 143 |
if (errno != ENOENT) |
| 144 |
log_warning_errno(errno, "Failed to open %s/: %m", srcdir); |
| 145 |
return errno; /* positive on purpose */ |
| 146 |
} |
| 147 |
|
| 148 |
dfd = dirfd(dir); |
| 149 |
assert(dfd >= 0); |
| 150 |
|
| 151 |
r = 0; |
| 152 |
FOREACH_DIRENT(entry, dir, return 0) { |
| 153 |
_cleanup_fclose_ FILE *policy = NULL; |
| 154 |
|
| 155 |
if (!dirent_is_file(entry)) |
| 156 |
continue; |
| 157 |
|
| 158 |
if (fdopen_unlocked_at(dfd, srcdir, entry->d_name, &r, &policy) < 0) |
| 159 |
continue; |
| 160 |
|
| 161 |
/* cipso2 write rules in the kernel require a line buffered stream */ |
| 162 |
for (;;) { |
| 163 |
_cleanup_free_ char *buf = NULL; |
| 164 |
int q; |
| 165 |
|
| 166 |
q = read_line(policy, NAME_MAX, &buf); |
| 167 |
if (q < 0) |
| 168 |
return log_error_errno(q, "%s/%s: failed to read line: %m", |
| 169 |
srcdir, entry->d_name); |
| 170 |
if (q == 0) |
| 171 |
break; |
| 172 |
|
| 173 |
if (isempty(buf) || strchr(COMMENTS, buf[0])) |
| 174 |
continue; |
| 175 |
|
| 176 |
q = RET_NERRNO(write(cipso2_fd, buf, strlen(buf))); |
| 177 |
if (q < 0) { |
| 178 |
log_error_errno(q, "%s/%s: failed to write '%s' to %s: %m", |
| 179 |
srcdir, entry->d_name, |
| 180 |
buf, "/sys/fs/smackfs/cipso2"); |
| 181 |
RET_GATHER(r, q); |
| 182 |
break; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return r; |
| 188 |
} |
| 189 |
|
| 190 |
static int write_netlabel_rules(const char *srcdir) { |
| 191 |
_cleanup_fclose_ FILE *dst = NULL; |
| 192 |
_cleanup_closedir_ DIR *dir = NULL; |
| 193 |
int dfd, r; |
| 194 |
|
| 195 |
dst = fopen("/sys/fs/smackfs/netlabel", "we"); |
| 196 |
if (!dst) { |
| 197 |
if (errno != ENOENT) |
| 198 |
log_warning_errno(errno, "Failed to open %s/: %m", "/sys/fs/smackfs/netlabel"); |
| 199 |
return -errno; /* negative error */ |
| 200 |
} |
| 201 |
|
| 202 |
/* write rules to dst from every file in the directory */ |
| 203 |
dir = opendir(srcdir); |
| 204 |
if (!dir) { |
| 205 |
if (errno != ENOENT) |
| 206 |
log_warning_errno(errno, "Failed to open %s/: %m", srcdir); |
| 207 |
return errno; /* positive on purpose */ |
| 208 |
} |
| 209 |
|
| 210 |
dfd = dirfd(dir); |
| 211 |
assert(dfd >= 0); |
| 212 |
|
| 213 |
r = 0; |
| 214 |
FOREACH_DIRENT(entry, dir, return 0) { |
| 215 |
_cleanup_fclose_ FILE *policy = NULL; |
| 216 |
|
| 217 |
if (fdopen_unlocked_at(dfd, srcdir, entry->d_name, &r, &policy) < 0) |
| 218 |
continue; |
| 219 |
|
| 220 |
/* load2 write rules in the kernel require a line buffered stream */ |
| 221 |
for (;;) { |
| 222 |
_cleanup_free_ char *buf = NULL; |
| 223 |
int q; |
| 224 |
|
| 225 |
q = read_line(policy, NAME_MAX, &buf); |
| 226 |
if (q < 0) |
| 227 |
return log_error_errno(q, "%s/%s: failed to read line: %m", |
| 228 |
srcdir, entry->d_name); |
| 229 |
if (q == 0) |
| 230 |
break; |
| 231 |
|
| 232 |
if (!fputs(buf, dst)) { |
| 233 |
log_error_errno(errno, "Failed to write line to %s: %m", "/sys/fs/smackfs/netlabel"); |
| 234 |
RET_GATHER(r, -errno); |
| 235 |
break; |
| 236 |
} |
| 237 |
q = fflush_and_check(dst); |
| 238 |
if (q < 0) { |
| 239 |
log_error_errno(q, "Failed to flush %s: %m", "/sys/fs/smackfs/netlabel"); |
| 240 |
RET_GATHER(r, q); |
| 241 |
break; |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
return r; |
| 247 |
} |
| 248 |
|
| 249 |
static int write_onlycap_list(void) { |
| 250 |
_cleanup_close_ int onlycap_fd = -EBADF; |
| 251 |
_cleanup_free_ char *list = NULL; |
| 252 |
_cleanup_fclose_ FILE *f = NULL; |
| 253 |
size_t len = 0; |
| 254 |
int r; |
| 255 |
|
| 256 |
f = fopen("/etc/smack/onlycap", "re"); |
| 257 |
if (!f) { |
| 258 |
if (errno != ENOENT) |
| 259 |
log_warning_errno(errno, "Failed to open %s: %m", "/etc/smack/onlycap"); |
| 260 |
|
| 261 |
return errno == ENOENT ? ENOENT : -errno; |
| 262 |
} |
| 263 |
|
| 264 |
for (;;) { |
| 265 |
_cleanup_free_ char *buf = NULL; |
| 266 |
size_t l; |
| 267 |
|
| 268 |
r = read_line(f, LONG_LINE_MAX, &buf); |
| 269 |
if (r < 0) |
| 270 |
return log_error_errno(r, "%s: failed to read line: %m", "/etc/smack/onlycap"); |
| 271 |
if (r == 0) |
| 272 |
break; |
| 273 |
|
| 274 |
if (isempty(buf) || strchr(COMMENTS, *buf)) |
| 275 |
continue; |
| 276 |
|
| 277 |
l = strlen(buf); |
| 278 |
if (!GREEDY_REALLOC(list, len + l + 1)) |
| 279 |
return log_oom(); |
| 280 |
|
| 281 |
stpcpy(list + len, buf)[0] = ' '; |
| 282 |
len += l + 1; |
| 283 |
} |
| 284 |
|
| 285 |
if (len == 0) |
| 286 |
return 0; |
| 287 |
|
| 288 |
list[len - 1] = 0; |
| 289 |
|
| 290 |
onlycap_fd = r = RET_NERRNO(open("/sys/fs/smackfs/onlycap", O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY)); |
| 291 |
if (r < 0) { |
| 292 |
if (r != -ENOENT) |
| 293 |
log_warning_errno(r, "Failed to open %s: %m", "/sys/fs/smackfs/onlycap"); |
| 294 |
return r; |
| 295 |
} |
| 296 |
|
| 297 |
r = RET_NERRNO(write(onlycap_fd, list, len)); |
| 298 |
if (r < 0) |
| 299 |
return log_error_errno(r, "%s: failed to write onlycap list(%s): %m", |
| 300 |
"/sys/fs/smackfs/onlycap", list); |
| 301 |
|
| 302 |
return 0; |
| 303 |
} |
| 304 |
|
| 305 |
#endif |
| 306 |
|
| 307 |
int mac_smack_setup(bool *loaded_policy) { |
| 308 |
|
| 309 |
#if ENABLE_SMACK |
| 310 |
int r; |
| 311 |
|
| 312 |
assert(loaded_policy); |
| 313 |
|
| 314 |
r = write_access2_rules("/etc/smack/accesses.d"); |
| 315 |
switch (r) { |
| 316 |
case -ENOENT: |
| 317 |
log_debug("Smack is not enabled in the kernel."); |
| 318 |
return 0; |
| 319 |
case ENOENT: |
| 320 |
log_debug("Smack access rules directory '/etc/smack/accesses.d/' not found"); |
| 321 |
return 0; |
| 322 |
case 0: |
| 323 |
log_info("Successfully loaded Smack policies."); |
| 324 |
break; |
| 325 |
default: |
| 326 |
log_warning_errno(r, "Failed to load Smack access rules, ignoring: %m"); |
| 327 |
return 0; |
| 328 |
} |
| 329 |
|
| 330 |
#if HAVE_SMACK_RUN_LABEL |
| 331 |
r = write_string_file("/proc/self/attr/current", SMACK_RUN_LABEL, WRITE_STRING_FILE_DISABLE_BUFFER); |
| 332 |
if (r < 0) |
| 333 |
log_warning_errno(r, "Failed to set SMACK label \"" SMACK_RUN_LABEL "\" on self: %m"); |
| 334 |
r = write_string_file("/sys/fs/smackfs/ambient", SMACK_RUN_LABEL, WRITE_STRING_FILE_DISABLE_BUFFER); |
| 335 |
if (r < 0) |
| 336 |
log_warning_errno(r, "Failed to set SMACK ambient label \"" SMACK_RUN_LABEL "\": %m"); |
| 337 |
r = write_string_file("/sys/fs/smackfs/netlabel", |
| 338 |
"0.0.0.0/0 " SMACK_RUN_LABEL, WRITE_STRING_FILE_DISABLE_BUFFER); |
| 339 |
if (r < 0) |
| 340 |
log_warning_errno(r, "Failed to set SMACK netlabel rule \"0.0.0.0/0 " SMACK_RUN_LABEL "\": %m"); |
| 341 |
r = write_string_file("/sys/fs/smackfs/netlabel", "127.0.0.1 -CIPSO", WRITE_STRING_FILE_DISABLE_BUFFER); |
| 342 |
if (r < 0) |
| 343 |
log_warning_errno(r, "Failed to set SMACK netlabel rule \"127.0.0.1 -CIPSO\": %m"); |
| 344 |
#endif |
| 345 |
|
| 346 |
r = write_cipso2_rules("/etc/smack/cipso.d"); |
| 347 |
switch (r) { |
| 348 |
case -ENOENT: |
| 349 |
log_debug("Smack/CIPSO is not enabled in the kernel."); |
| 350 |
return 0; |
| 351 |
case ENOENT: |
| 352 |
log_debug("Smack/CIPSO access rules directory '/etc/smack/cipso.d/' not found"); |
| 353 |
break; |
| 354 |
case 0: |
| 355 |
log_info("Successfully loaded Smack/CIPSO policies."); |
| 356 |
break; |
| 357 |
default: |
| 358 |
log_warning_errno(r, "Failed to load Smack/CIPSO access rules, ignoring: %m"); |
| 359 |
} |
| 360 |
|
| 361 |
r = write_netlabel_rules("/etc/smack/netlabel.d"); |
| 362 |
switch (r) { |
| 363 |
case -ENOENT: |
| 364 |
log_debug("Smack/CIPSO is not enabled in the kernel."); |
| 365 |
return 0; |
| 366 |
case ENOENT: |
| 367 |
log_debug("Smack network host rules directory '/etc/smack/netlabel.d/' not found"); |
| 368 |
break; |
| 369 |
case 0: |
| 370 |
log_info("Successfully loaded Smack network host rules."); |
| 371 |
break; |
| 372 |
default: |
| 373 |
log_warning_errno(r, "Failed to load Smack network host rules: %m, ignoring."); |
| 374 |
} |
| 375 |
|
| 376 |
r = write_onlycap_list(); |
| 377 |
switch (r) { |
| 378 |
case -ENOENT: |
| 379 |
log_debug("Smack is not enabled in the kernel."); |
| 380 |
break; |
| 381 |
case ENOENT: |
| 382 |
log_debug("Smack onlycap list file '/etc/smack/onlycap' not found"); |
| 383 |
break; |
| 384 |
case 0: |
| 385 |
log_info("Successfully wrote Smack onlycap list."); |
| 386 |
break; |
| 387 |
default: |
| 388 |
return log_struct_errno(LOG_EMERG, r, |
| 389 |
LOG_MESSAGE("Failed to write Smack onlycap list: %m"), |
| 390 |
LOG_MESSAGE_ID(SD_MESSAGE_SMACK_FAILED_WRITE_STR)); |
| 391 |
} |
| 392 |
|
| 393 |
*loaded_policy = true; |
| 394 |
|
| 395 |
#endif |
| 396 |
|
| 397 |
return 0; |
| 398 |
} |