| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <sys/socket.h> |
| 5 |
#include <sys/stat.h> |
| 6 |
#include <syslog.h> |
| 7 |
|
| 8 |
#include "log.h" |
| 9 |
|
| 10 |
#if HAVE_SELINUX |
| 11 |
#include <malloc.h> |
| 12 |
#include <string.h> |
| 13 |
#include <sys/un.h> |
| 14 |
|
| 15 |
#include <selinux/avc.h> |
| 16 |
#include <selinux/context.h> |
| 17 |
#include <selinux/label.h> |
| 18 |
#include <selinux/selinux.h> |
| 19 |
|
| 20 |
#include "sd-dlopen.h" |
| 21 |
|
| 22 |
#include "alloc-util.h" |
| 23 |
#include "fd-util.h" |
| 24 |
#include "path-util.h" |
| 25 |
#include "string-util.h" |
| 26 |
#include "time-util.h" |
| 27 |
#endif |
| 28 |
|
| 29 |
#include "errno-util.h" |
| 30 |
#include "label-util.h" |
| 31 |
#include "selinux-util.h" |
| 32 |
|
| 33 |
#if HAVE_SELINUX |
| 34 |
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_RENAME(context_t, sym_context_free, context_freep, NULL); |
| 35 |
|
| 36 |
typedef enum Initialized { |
| 37 |
UNINITIALIZED, |
| 38 |
INITIALIZED, |
| 39 |
LAZY_INITIALIZED, |
| 40 |
} Initialized; |
| 41 |
|
| 42 |
static int cached_use = -1; |
| 43 |
static Initialized initialized = UNINITIALIZED; |
| 44 |
static int last_policyload = 0; |
| 45 |
static struct selabel_handle *label_hnd = NULL; |
| 46 |
static bool have_status_page = false; |
| 47 |
|
| 48 |
static int mac_selinux_label_pre(int dir_fd, const char *path, mode_t mode) { |
| 49 |
return mac_selinux_create_file_prepare_at(dir_fd, path, mode); |
| 50 |
} |
| 51 |
|
| 52 |
static int mac_selinux_label_post(int dir_fd, const char *path, bool created) { |
| 53 |
mac_selinux_create_file_clear(); |
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
DLSYM_PROTOTYPE(avc_open) = NULL; |
| 58 |
DLSYM_PROTOTYPE(context_free) = NULL; |
| 59 |
DLSYM_PROTOTYPE(context_new) = NULL; |
| 60 |
DLSYM_PROTOTYPE(context_range_get) = NULL; |
| 61 |
DLSYM_PROTOTYPE(context_range_set) = NULL; |
| 62 |
DLSYM_PROTOTYPE(context_str) = NULL; |
| 63 |
DLSYM_PROTOTYPE(fgetfilecon_raw) = NULL; |
| 64 |
DLSYM_PROTOTYPE(fini_selinuxmnt) = NULL; |
| 65 |
DLSYM_PROTOTYPE(freecon) = NULL; |
| 66 |
DLSYM_PROTOTYPE(getcon_raw) = NULL; |
| 67 |
DLSYM_PROTOTYPE(getfilecon_raw) = NULL; |
| 68 |
DLSYM_PROTOTYPE(getpeercon_raw) = NULL; |
| 69 |
DLSYM_PROTOTYPE(getpidcon_raw) = NULL; |
| 70 |
DLSYM_PROTOTYPE(is_selinux_enabled) = NULL; |
| 71 |
DLSYM_PROTOTYPE(security_compute_create_raw) = NULL; |
| 72 |
DLSYM_PROTOTYPE(security_getenforce) = NULL; |
| 73 |
DLSYM_PROTOTYPE(selabel_close) = NULL; |
| 74 |
DLSYM_PROTOTYPE(selabel_lookup_raw) = NULL; |
| 75 |
DLSYM_PROTOTYPE(selabel_open) = NULL; |
| 76 |
DLSYM_PROTOTYPE(selinux_check_access) = NULL; |
| 77 |
DLSYM_PROTOTYPE(selinux_getenforcemode) = NULL; |
| 78 |
DLSYM_PROTOTYPE(selinux_init_load_policy) = NULL; |
| 79 |
DLSYM_PROTOTYPE(selinux_path) = NULL; |
| 80 |
DLSYM_PROTOTYPE(selinux_set_callback) = NULL; |
| 81 |
DLSYM_PROTOTYPE(selinux_status_close) = NULL; |
| 82 |
DLSYM_PROTOTYPE(selinux_status_getenforce) = NULL; |
| 83 |
DLSYM_PROTOTYPE(selinux_status_open) = NULL; |
| 84 |
DLSYM_PROTOTYPE(selinux_status_policyload) = NULL; |
| 85 |
DLSYM_PROTOTYPE(setcon_raw) = NULL; |
| 86 |
DLSYM_PROTOTYPE(setexeccon_raw) = NULL; |
| 87 |
DLSYM_PROTOTYPE(setfilecon_raw) = NULL; |
| 88 |
DLSYM_PROTOTYPE(setfscreatecon_raw) = NULL; |
| 89 |
DLSYM_PROTOTYPE(setsockcreatecon_raw) = NULL; |
| 90 |
DLSYM_PROTOTYPE(string_to_security_class) = NULL; |
| 91 |
#endif |
| 92 |
|
| 93 |
int dlopen_libselinux(int log_level) { |
| 94 |
#if HAVE_SELINUX |
| 95 |
static void *libselinux_dl = NULL; |
| 96 |
|
| 97 |
LIBSELINUX_NOTE(SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED); |
| 98 |
|
| 99 |
return dlopen_many_sym_or_warn( |
| 100 |
&libselinux_dl, |
| 101 |
"libselinux.so.1", |
| 102 |
log_level, |
| 103 |
DLSYM_ARG(avc_open), |
| 104 |
DLSYM_ARG(context_free), |
| 105 |
DLSYM_ARG(context_new), |
| 106 |
DLSYM_ARG(context_range_get), |
| 107 |
DLSYM_ARG(context_range_set), |
| 108 |
DLSYM_ARG(context_str), |
| 109 |
DLSYM_ARG(fgetfilecon_raw), |
| 110 |
DLSYM_ARG(fini_selinuxmnt), |
| 111 |
DLSYM_ARG(freecon), |
| 112 |
DLSYM_ARG(getcon_raw), |
| 113 |
DLSYM_ARG(getfilecon_raw), |
| 114 |
DLSYM_ARG(getpeercon_raw), |
| 115 |
DLSYM_ARG(getpidcon_raw), |
| 116 |
DLSYM_ARG(is_selinux_enabled), |
| 117 |
DLSYM_ARG(security_compute_create_raw), |
| 118 |
DLSYM_ARG(security_getenforce), |
| 119 |
DLSYM_ARG(selabel_close), |
| 120 |
DLSYM_ARG(selabel_lookup_raw), |
| 121 |
DLSYM_ARG(selabel_open), |
| 122 |
DLSYM_ARG(selinux_check_access), |
| 123 |
DLSYM_ARG(selinux_getenforcemode), |
| 124 |
DLSYM_ARG(selinux_init_load_policy), |
| 125 |
DLSYM_ARG(selinux_path), |
| 126 |
DLSYM_ARG(selinux_set_callback), |
| 127 |
DLSYM_ARG(selinux_status_close), |
| 128 |
DLSYM_ARG(selinux_status_getenforce), |
| 129 |
DLSYM_ARG(selinux_status_open), |
| 130 |
DLSYM_ARG(selinux_status_policyload), |
| 131 |
DLSYM_ARG(setcon_raw), |
| 132 |
DLSYM_ARG(setexeccon_raw), |
| 133 |
DLSYM_ARG(setfilecon_raw), |
| 134 |
DLSYM_ARG(setfscreatecon_raw), |
| 135 |
DLSYM_ARG(setsockcreatecon_raw), |
| 136 |
DLSYM_ARG(string_to_security_class)); |
| 137 |
#else |
| 138 |
return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 139 |
"libselinux support is not compiled in."); |
| 140 |
#endif |
| 141 |
} |
| 142 |
|
| 143 |
bool mac_selinux_use(void) { |
| 144 |
#if HAVE_SELINUX |
| 145 |
if (_unlikely_(cached_use < 0)) { |
| 146 |
if (dlopen_libselinux(LOG_DEBUG) < 0) |
| 147 |
return (cached_use = false); |
| 148 |
|
| 149 |
cached_use = sym_is_selinux_enabled() > 0; |
| 150 |
log_trace("SELinux enabled state cached to: %s", enabled_disabled(cached_use)); |
| 151 |
} |
| 152 |
|
| 153 |
return cached_use; |
| 154 |
#else |
| 155 |
return false; |
| 156 |
#endif |
| 157 |
} |
| 158 |
|
| 159 |
bool mac_selinux_enforcing(void) { |
| 160 |
int r = 0; |
| 161 |
#if HAVE_SELINUX |
| 162 |
|
| 163 |
/* If the SELinux status page has been successfully opened, retrieve the enforcing |
| 164 |
* status over it to avoid system calls in security_getenforce(). */ |
| 165 |
|
| 166 |
if (dlopen_libselinux(LOG_DEBUG) < 0) |
| 167 |
return false; |
| 168 |
|
| 169 |
if (have_status_page) |
| 170 |
r = sym_selinux_status_getenforce(); |
| 171 |
else |
| 172 |
r = sym_security_getenforce(); |
| 173 |
|
| 174 |
#endif |
| 175 |
return r != 0; |
| 176 |
} |
| 177 |
|
| 178 |
void mac_selinux_retest(void) { |
| 179 |
#if HAVE_SELINUX |
| 180 |
cached_use = -1; |
| 181 |
#endif |
| 182 |
} |
| 183 |
|
| 184 |
#if HAVE_SELINUX |
| 185 |
static int open_label_db(void) { |
| 186 |
struct selabel_handle *hnd; |
| 187 |
/* Avoid maybe-uninitialized false positives */ |
| 188 |
usec_t before_timestamp = USEC_INFINITY, after_timestamp = USEC_INFINITY; |
| 189 |
struct mallinfo2 before_mallinfo = {}; |
| 190 |
int r; |
| 191 |
|
| 192 |
r = dlopen_libselinux(LOG_DEBUG); |
| 193 |
if (r < 0) |
| 194 |
return r; |
| 195 |
|
| 196 |
if (DEBUG_LOGGING) { |
| 197 |
before_mallinfo = mallinfo2(); |
| 198 |
before_timestamp = now(CLOCK_MONOTONIC); |
| 199 |
} |
| 200 |
|
| 201 |
hnd = sym_selabel_open(SELABEL_CTX_FILE, NULL, 0); |
| 202 |
if (!hnd) |
| 203 |
return log_selinux_enforcing_errno(errno, "Failed to initialize SELinux labeling handle: %m"); |
| 204 |
|
| 205 |
if (DEBUG_LOGGING) { |
| 206 |
after_timestamp = now(CLOCK_MONOTONIC); |
| 207 |
struct mallinfo2 after_mallinfo = mallinfo2(); |
| 208 |
size_t l = LESS_BY(after_mallinfo.uordblks, before_mallinfo.uordblks); |
| 209 |
log_debug("Successfully loaded SELinux database in %s, size on heap is %zuK.", |
| 210 |
FORMAT_TIMESPAN(after_timestamp - before_timestamp, 0), |
| 211 |
DIV_ROUND_UP(l, 1024)); |
| 212 |
} |
| 213 |
|
| 214 |
/* release memory after measurement */ |
| 215 |
if (label_hnd) |
| 216 |
sym_selabel_close(label_hnd); |
| 217 |
label_hnd = TAKE_PTR(hnd); |
| 218 |
|
| 219 |
return 0; |
| 220 |
} |
| 221 |
#endif |
| 222 |
|
| 223 |
static int selinux_init(bool force) { |
| 224 |
#if HAVE_SELINUX |
| 225 |
static const LabelOps label_ops = { |
| 226 |
.pre = mac_selinux_label_pre, |
| 227 |
.post = mac_selinux_label_post, |
| 228 |
}; |
| 229 |
int r; |
| 230 |
|
| 231 |
if (!mac_selinux_use()) |
| 232 |
return 0; |
| 233 |
|
| 234 |
if (initialized == INITIALIZED) |
| 235 |
return 1; |
| 236 |
|
| 237 |
/* Internal call from this module? Unless we were explicitly configured to allow lazy initialization |
| 238 |
* bail out immediately. Pretend all is good, we do not want callers to abort here, for example at |
| 239 |
* early boot when the policy is being initialised. */ |
| 240 |
if (!force && initialized != LAZY_INITIALIZED) |
| 241 |
return 1; |
| 242 |
|
| 243 |
mac_selinux_disable_logging(); |
| 244 |
|
| 245 |
r = sym_selinux_status_open(/* netlink fallback= */ 1); |
| 246 |
if (r < 0) { |
| 247 |
if (!ERRNO_IS_PRIVILEGE(errno)) |
| 248 |
return log_selinux_enforcing_errno(errno, "Failed to open SELinux status page: %m"); |
| 249 |
log_warning_errno(errno, "selinux_status_open() with netlink fallback failed, not checking for policy reloads: %m"); |
| 250 |
} else if (r == 1) |
| 251 |
log_warning("selinux_status_open() failed to open the status page, using the netlink fallback."); |
| 252 |
else |
| 253 |
have_status_page = true; |
| 254 |
|
| 255 |
r = open_label_db(); |
| 256 |
if (r < 0) { |
| 257 |
sym_selinux_status_close(); |
| 258 |
return r; |
| 259 |
} |
| 260 |
|
| 261 |
r = label_ops_set(&label_ops); |
| 262 |
if (r < 0) |
| 263 |
return r; |
| 264 |
|
| 265 |
/* Save the current policyload sequence number, so mac_selinux_maybe_reload() does not trigger on |
| 266 |
* first call without any actual change. */ |
| 267 |
last_policyload = sym_selinux_status_policyload(); |
| 268 |
|
| 269 |
initialized = INITIALIZED; |
| 270 |
return 1; |
| 271 |
#else |
| 272 |
return 0; |
| 273 |
#endif |
| 274 |
} |
| 275 |
|
| 276 |
int mac_selinux_init(void) { |
| 277 |
return selinux_init(/* force= */ true); |
| 278 |
} |
| 279 |
|
| 280 |
int mac_selinux_init_lazy(void) { |
| 281 |
#if HAVE_SELINUX |
| 282 |
if (initialized == UNINITIALIZED) |
| 283 |
initialized = LAZY_INITIALIZED; /* We'll be back later */ |
| 284 |
#endif |
| 285 |
|
| 286 |
return 0; |
| 287 |
} |
| 288 |
|
| 289 |
#if HAVE_SELINUX |
| 290 |
static int mac_selinux_reload(int seqno) { |
| 291 |
log_debug("SELinux reload %d", seqno); |
| 292 |
|
| 293 |
(void) open_label_db(); |
| 294 |
|
| 295 |
return 0; |
| 296 |
} |
| 297 |
#endif |
| 298 |
|
| 299 |
void mac_selinux_maybe_reload(void) { |
| 300 |
#if HAVE_SELINUX |
| 301 |
int policyload; |
| 302 |
|
| 303 |
if (!initialized) |
| 304 |
return; |
| 305 |
|
| 306 |
if (dlopen_libselinux(LOG_DEBUG) < 0) |
| 307 |
return; |
| 308 |
|
| 309 |
/* Do not use selinux_status_updated(3), cause since libselinux 3.2 selinux_check_access(3), |
| 310 |
* called in core and user instances, does also use it under the hood. |
| 311 |
* That can cause changes to be consumed by selinux_check_access(3) and not being visible here. |
| 312 |
* Also do not use selinux callbacks, selinux_set_callback(3), cause they are only automatically |
| 313 |
* invoked since libselinux 3.2 by selinux_status_updated(3). |
| 314 |
* Relevant libselinux commit: https://github.com/SELinuxProject/selinux/commit/05bdc03130d741e53e1fb45a958d0a2c184be503 |
| 315 |
* Debian Bullseye is going to ship libselinux 3.1, so stay compatible for backports. */ |
| 316 |
policyload = sym_selinux_status_policyload(); |
| 317 |
if (policyload < 0) { |
| 318 |
log_debug_errno(errno, "Failed to get SELinux policyload from status page: %m"); |
| 319 |
return; |
| 320 |
} |
| 321 |
|
| 322 |
if (policyload != last_policyload) { |
| 323 |
mac_selinux_reload(policyload); |
| 324 |
last_policyload = policyload; |
| 325 |
} |
| 326 |
#endif |
| 327 |
} |
| 328 |
|
| 329 |
void mac_selinux_finish(void) { |
| 330 |
|
| 331 |
#if HAVE_SELINUX |
| 332 |
if (label_hnd) { |
| 333 |
sym_selabel_close(label_hnd); |
| 334 |
label_hnd = NULL; |
| 335 |
} |
| 336 |
|
| 337 |
if (sym_selinux_status_close) |
| 338 |
sym_selinux_status_close(); |
| 339 |
|
| 340 |
have_status_page = false; |
| 341 |
|
| 342 |
initialized = false; |
| 343 |
#endif |
| 344 |
} |
| 345 |
|
| 346 |
#if HAVE_SELINUX |
| 347 |
_printf_(2,3) |
| 348 |
static int selinux_log_glue(int type, const char *fmt, ...) { |
| 349 |
return 0; |
| 350 |
} |
| 351 |
#endif |
| 352 |
|
| 353 |
void mac_selinux_disable_logging(void) { |
| 354 |
#if HAVE_SELINUX |
| 355 |
/* Turn off all of SELinux' own logging, we want to do that ourselves */ |
| 356 |
if (dlopen_libselinux(LOG_DEBUG) < 0) |
| 357 |
return; |
| 358 |
|
| 359 |
sym_selinux_set_callback(SELINUX_CB_LOG, (const union selinux_callback) { .func_log = selinux_log_glue }); |
| 360 |
#endif |
| 361 |
} |
| 362 |
|
| 363 |
#if HAVE_SELINUX |
| 364 |
static int setfilecon_idempotent(int fd, const char *context) { |
| 365 |
_cleanup_freecon_ char *oldcon = NULL; |
| 366 |
|
| 367 |
assert(fd >= 0); |
| 368 |
assert(context); |
| 369 |
|
| 370 |
/* Read current context via /proc/self/fd/ so this works for O_PATH fds too */ |
| 371 |
if (sym_getfilecon_raw(FORMAT_PROC_FD_PATH(fd), &oldcon) >= 0 && streq_ptr(context, oldcon)) |
| 372 |
return 0; /* Already correct */ |
| 373 |
|
| 374 |
return RET_NERRNO(sym_setfilecon_raw(FORMAT_PROC_FD_PATH(fd), context)); |
| 375 |
} |
| 376 |
|
| 377 |
static int selinux_fix_fd( |
| 378 |
int fd, |
| 379 |
const char *label_path, |
| 380 |
LabelFixFlags flags) { |
| 381 |
|
| 382 |
_cleanup_freecon_ char* fcon = NULL; |
| 383 |
struct stat st; |
| 384 |
int r; |
| 385 |
|
| 386 |
assert(fd >= 0); |
| 387 |
assert(label_path); |
| 388 |
assert(path_is_absolute(label_path)); |
| 389 |
|
| 390 |
if (fstat(fd, &st) < 0) |
| 391 |
return -errno; |
| 392 |
|
| 393 |
/* Check for policy reload so 'label_hnd' is kept up-to-date by callbacks */ |
| 394 |
mac_selinux_maybe_reload(); |
| 395 |
if (!label_hnd) |
| 396 |
return 0; |
| 397 |
|
| 398 |
if (sym_selabel_lookup_raw(label_hnd, &fcon, label_path, st.st_mode) < 0) { |
| 399 |
/* If there's no label to set, then exit without warning */ |
| 400 |
if (errno == ENOENT) |
| 401 |
return 0; |
| 402 |
|
| 403 |
return log_selinux_enforcing_errno(errno, "Unable to lookup intended SELinux security context of %s: %m", label_path); |
| 404 |
} |
| 405 |
|
| 406 |
r = setfilecon_idempotent(fd, fcon); |
| 407 |
if (r >= 0) |
| 408 |
return 0; |
| 409 |
|
| 410 |
/* If the FS doesn't support labels, then exit without warning */ |
| 411 |
if (ERRNO_IS_NOT_SUPPORTED(r)) |
| 412 |
return 0; |
| 413 |
|
| 414 |
/* If the FS is read-only and we were told to ignore failures caused by that, suppress error */ |
| 415 |
if (r == -EROFS && (flags & LABEL_IGNORE_EROFS)) |
| 416 |
return 0; |
| 417 |
|
| 418 |
return log_selinux_enforcing_errno(r, "Unable to fix SELinux security context of %s: %m", label_path); |
| 419 |
} |
| 420 |
#endif |
| 421 |
|
| 422 |
int mac_selinux_fix_full( |
| 423 |
int atfd, |
| 424 |
const char *inode_path, |
| 425 |
const char *label_path, |
| 426 |
LabelFixFlags flags) { |
| 427 |
|
| 428 |
assert(atfd >= 0 || atfd == AT_FDCWD); |
| 429 |
assert(atfd >= 0 || inode_path); |
| 430 |
|
| 431 |
#if HAVE_SELINUX |
| 432 |
_cleanup_close_ int opened_fd = -EBADF; |
| 433 |
_cleanup_free_ char *p = NULL; |
| 434 |
int inode_fd, r; |
| 435 |
|
| 436 |
r = selinux_init(/* force= */ false); |
| 437 |
if (r <= 0) |
| 438 |
return r; |
| 439 |
|
| 440 |
if (!label_hnd) |
| 441 |
return 0; |
| 442 |
|
| 443 |
if (inode_path) { |
| 444 |
opened_fd = openat(atfd, inode_path, O_NOFOLLOW|O_CLOEXEC|O_PATH); |
| 445 |
if (opened_fd < 0) { |
| 446 |
if ((flags & LABEL_IGNORE_ENOENT) && errno == ENOENT) |
| 447 |
return 0; |
| 448 |
|
| 449 |
return -errno; |
| 450 |
} |
| 451 |
|
| 452 |
inode_fd = opened_fd; |
| 453 |
} else |
| 454 |
inode_fd = atfd; |
| 455 |
|
| 456 |
if (!label_path) { |
| 457 |
if (path_is_absolute(inode_path)) |
| 458 |
label_path = inode_path; |
| 459 |
else { |
| 460 |
r = fd_get_path(inode_fd, &p); |
| 461 |
if (r < 0) |
| 462 |
return r; |
| 463 |
|
| 464 |
label_path = p; |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
return selinux_fix_fd(inode_fd, label_path, flags); |
| 469 |
#else |
| 470 |
return 0; |
| 471 |
#endif |
| 472 |
} |
| 473 |
|
| 474 |
int mac_selinux_apply(const char *path, const char *label) { |
| 475 |
|
| 476 |
assert(path); |
| 477 |
|
| 478 |
#if HAVE_SELINUX |
| 479 |
int r; |
| 480 |
|
| 481 |
r = selinux_init(/* force= */ false); |
| 482 |
if (r <= 0) |
| 483 |
return r; |
| 484 |
|
| 485 |
assert(label); |
| 486 |
|
| 487 |
if (sym_setfilecon_raw(path, label) < 0) |
| 488 |
return log_selinux_enforcing_errno(errno, "Failed to set SELinux security context %s on path %s: %m", label, path); |
| 489 |
#endif |
| 490 |
return 0; |
| 491 |
} |
| 492 |
|
| 493 |
int mac_selinux_apply_fd(int fd, const char *path, const char *label) { |
| 494 |
|
| 495 |
assert(fd >= 0); |
| 496 |
|
| 497 |
#if HAVE_SELINUX |
| 498 |
int r; |
| 499 |
|
| 500 |
r = selinux_init(/* force= */ false); |
| 501 |
if (r <= 0) |
| 502 |
return r; |
| 503 |
|
| 504 |
assert(label); |
| 505 |
|
| 506 |
r = setfilecon_idempotent(fd, label); |
| 507 |
if (r < 0) |
| 508 |
return log_selinux_enforcing_errno(r, "Failed to set SELinux security context %s on path %s: %m", label, strna(path)); |
| 509 |
#endif |
| 510 |
return 0; |
| 511 |
} |
| 512 |
|
| 513 |
int mac_selinux_get_create_label_from_exe(const char *exe, char **ret_label) { |
| 514 |
#if HAVE_SELINUX |
| 515 |
_cleanup_freecon_ char *mycon = NULL, *fcon = NULL; |
| 516 |
security_class_t sclass; |
| 517 |
int r; |
| 518 |
|
| 519 |
assert(exe); |
| 520 |
assert(ret_label); |
| 521 |
|
| 522 |
r = selinux_init(/* force= */ false); |
| 523 |
if (r < 0) |
| 524 |
return r; |
| 525 |
if (r == 0) |
| 526 |
return -EOPNOTSUPP; |
| 527 |
|
| 528 |
if (sym_getcon_raw(&mycon) < 0) |
| 529 |
return -errno; |
| 530 |
if (!mycon) |
| 531 |
return -EOPNOTSUPP; |
| 532 |
|
| 533 |
if (sym_getfilecon_raw(exe, &fcon) < 0) |
| 534 |
return -errno; |
| 535 |
if (!fcon) |
| 536 |
return -EOPNOTSUPP; |
| 537 |
|
| 538 |
sclass = sym_string_to_security_class("process"); |
| 539 |
if (sclass == 0) |
| 540 |
return -ENOSYS; |
| 541 |
|
| 542 |
return RET_NERRNO(sym_security_compute_create_raw(mycon, fcon, sclass, ret_label)); |
| 543 |
#else |
| 544 |
return -EOPNOTSUPP; |
| 545 |
#endif |
| 546 |
} |
| 547 |
|
| 548 |
int mac_selinux_get_our_label(char **ret_label) { |
| 549 |
assert(ret_label); |
| 550 |
|
| 551 |
#if HAVE_SELINUX |
| 552 |
int r; |
| 553 |
|
| 554 |
r = selinux_init(/* force= */ false); |
| 555 |
if (r < 0) |
| 556 |
return r; |
| 557 |
if (r == 0) |
| 558 |
return -EOPNOTSUPP; |
| 559 |
|
| 560 |
_cleanup_freecon_ char *con = NULL; |
| 561 |
if (sym_getcon_raw(&con) < 0) |
| 562 |
return -errno; |
| 563 |
if (!con) |
| 564 |
return -EOPNOTSUPP; |
| 565 |
|
| 566 |
*ret_label = TAKE_PTR(con); |
| 567 |
return 0; |
| 568 |
#else |
| 569 |
return -EOPNOTSUPP; |
| 570 |
#endif |
| 571 |
} |
| 572 |
|
| 573 |
int mac_selinux_get_peer_label(int socket_fd, char **ret_label) { |
| 574 |
assert(socket_fd >= 0); |
| 575 |
assert(ret_label); |
| 576 |
|
| 577 |
#if HAVE_SELINUX |
| 578 |
int r; |
| 579 |
|
| 580 |
r = selinux_init(/* force= */ false); |
| 581 |
if (r < 0) |
| 582 |
return r; |
| 583 |
if (r == 0) |
| 584 |
return -EOPNOTSUPP; |
| 585 |
|
| 586 |
_cleanup_freecon_ char *con = NULL; |
| 587 |
if (sym_getpeercon_raw(socket_fd, &con) < 0) |
| 588 |
return -errno; |
| 589 |
if (!con) |
| 590 |
return -EOPNOTSUPP; |
| 591 |
|
| 592 |
*ret_label = TAKE_PTR(con); |
| 593 |
return 0; |
| 594 |
#else |
| 595 |
return -EOPNOTSUPP; |
| 596 |
#endif |
| 597 |
} |
| 598 |
|
| 599 |
int mac_selinux_get_child_mls_label(int socket_fd, const char *exe, const char *exec_label, char **ret_label) { |
| 600 |
#if HAVE_SELINUX |
| 601 |
_cleanup_freecon_ char *mycon = NULL, *peercon = NULL, *fcon = NULL; |
| 602 |
_cleanup_(context_freep) context_t pcon = NULL, bcon = NULL; |
| 603 |
const char *range = NULL, *bcon_str = NULL; |
| 604 |
security_class_t sclass; |
| 605 |
int r; |
| 606 |
|
| 607 |
assert(socket_fd >= 0); |
| 608 |
assert(exe); |
| 609 |
assert(ret_label); |
| 610 |
|
| 611 |
r = selinux_init(/* force= */ false); |
| 612 |
if (r < 0) |
| 613 |
return r; |
| 614 |
if (r == 0) |
| 615 |
return -EOPNOTSUPP; |
| 616 |
|
| 617 |
if (sym_getcon_raw(&mycon) < 0) |
| 618 |
return -errno; |
| 619 |
if (!mycon) |
| 620 |
return -EOPNOTSUPP; |
| 621 |
|
| 622 |
if (sym_getpeercon_raw(socket_fd, &peercon) < 0) |
| 623 |
return -errno; |
| 624 |
if (!peercon) |
| 625 |
return -EOPNOTSUPP; |
| 626 |
|
| 627 |
if (!exec_label) { /* If there is no context set for next exec let's use context of target executable */ |
| 628 |
if (sym_getfilecon_raw(exe, &fcon) < 0) |
| 629 |
return -errno; |
| 630 |
if (!fcon) |
| 631 |
return -EOPNOTSUPP; |
| 632 |
} |
| 633 |
|
| 634 |
bcon = sym_context_new(mycon); |
| 635 |
if (!bcon) |
| 636 |
return -ENOMEM; |
| 637 |
|
| 638 |
pcon = sym_context_new(peercon); |
| 639 |
if (!pcon) |
| 640 |
return -ENOMEM; |
| 641 |
|
| 642 |
range = sym_context_range_get(pcon); |
| 643 |
if (!range) |
| 644 |
return -errno; |
| 645 |
|
| 646 |
if (sym_context_range_set(bcon, range) != 0) |
| 647 |
return -errno; |
| 648 |
|
| 649 |
bcon_str = sym_context_str(bcon); |
| 650 |
if (!bcon_str) |
| 651 |
return -ENOMEM; |
| 652 |
|
| 653 |
sclass = sym_string_to_security_class("process"); |
| 654 |
if (sclass == 0) |
| 655 |
return -ENOSYS; |
| 656 |
|
| 657 |
return RET_NERRNO(sym_security_compute_create_raw(bcon_str, fcon, sclass, ret_label)); |
| 658 |
#else |
| 659 |
return -EOPNOTSUPP; |
| 660 |
#endif |
| 661 |
} |
| 662 |
|
| 663 |
#if HAVE_SELINUX |
| 664 |
static int selinux_create_file_prepare_abspath(const char *abspath, mode_t mode) { |
| 665 |
_cleanup_freecon_ char *filecon = NULL; |
| 666 |
int r; |
| 667 |
|
| 668 |
assert(abspath); |
| 669 |
assert(path_is_absolute(abspath)); |
| 670 |
|
| 671 |
r = selinux_init(/* force= */ false); |
| 672 |
if (r <= 0) |
| 673 |
return r; |
| 674 |
|
| 675 |
/* Check for policy reload so 'label_hnd' is kept up-to-date by callbacks */ |
| 676 |
mac_selinux_maybe_reload(); |
| 677 |
if (!label_hnd) |
| 678 |
return 0; |
| 679 |
|
| 680 |
r = sym_selabel_lookup_raw(label_hnd, &filecon, abspath, mode); |
| 681 |
if (r < 0) { |
| 682 |
/* No context specified by the policy? Proceed without setting it. */ |
| 683 |
if (errno == ENOENT) |
| 684 |
return 0; |
| 685 |
|
| 686 |
return log_selinux_enforcing_errno(errno, "Failed to determine SELinux security context for %s: %m", abspath); |
| 687 |
} |
| 688 |
|
| 689 |
if (sym_setfscreatecon_raw(filecon) < 0) |
| 690 |
return log_selinux_enforcing_errno(errno, "Failed to set SELinux security context %s for %s: %m", filecon, abspath); |
| 691 |
|
| 692 |
return 0; |
| 693 |
} |
| 694 |
#endif |
| 695 |
|
| 696 |
int mac_selinux_create_file_prepare_at( |
| 697 |
int dir_fd, |
| 698 |
const char *path, |
| 699 |
mode_t mode) { |
| 700 |
|
| 701 |
#if HAVE_SELINUX |
| 702 |
_cleanup_free_ char *abspath = NULL; |
| 703 |
int r; |
| 704 |
|
| 705 |
assert(dir_fd >= 0 || dir_fd == AT_FDCWD); |
| 706 |
|
| 707 |
r = selinux_init(/* force= */ false); |
| 708 |
if (r <= 0) |
| 709 |
return r; |
| 710 |
|
| 711 |
if (!label_hnd) |
| 712 |
return 0; |
| 713 |
|
| 714 |
if (isempty(path) || !path_is_absolute(path)) { |
| 715 |
r = fd_get_path(dir_fd, &abspath); |
| 716 |
if (r < 0) |
| 717 |
return r; |
| 718 |
|
| 719 |
if (!isempty(path) && !path_extend(&abspath, path)) |
| 720 |
return -ENOMEM; |
| 721 |
|
| 722 |
path = abspath; |
| 723 |
} |
| 724 |
|
| 725 |
return selinux_create_file_prepare_abspath(path, mode); |
| 726 |
#else |
| 727 |
return 0; |
| 728 |
#endif |
| 729 |
} |
| 730 |
|
| 731 |
int mac_selinux_create_file_prepare_label(const char *path, const char *label) { |
| 732 |
#if HAVE_SELINUX |
| 733 |
int r; |
| 734 |
|
| 735 |
if (!label) |
| 736 |
return 0; |
| 737 |
|
| 738 |
r = selinux_init(/* force= */ false); |
| 739 |
if (r <= 0) |
| 740 |
return r; |
| 741 |
|
| 742 |
if (sym_setfscreatecon_raw(label) < 0) |
| 743 |
return log_selinux_enforcing_errno(errno, "Failed to set specified SELinux security context '%s' for '%s': %m", label, strna(path)); |
| 744 |
#endif |
| 745 |
return 0; |
| 746 |
} |
| 747 |
|
| 748 |
void mac_selinux_create_file_clear(void) { |
| 749 |
|
| 750 |
#if HAVE_SELINUX |
| 751 |
PROTECT_ERRNO; |
| 752 |
|
| 753 |
if (selinux_init(/* force= */ false) <= 0) |
| 754 |
return; |
| 755 |
|
| 756 |
(void) sym_setfscreatecon_raw(NULL); |
| 757 |
#endif |
| 758 |
} |
| 759 |
|
| 760 |
int mac_selinux_create_socket_prepare(const char *label) { |
| 761 |
|
| 762 |
#if HAVE_SELINUX |
| 763 |
int r; |
| 764 |
|
| 765 |
assert(label); |
| 766 |
|
| 767 |
r = selinux_init(/* force= */ false); |
| 768 |
if (r <= 0) |
| 769 |
return r; |
| 770 |
|
| 771 |
if (sym_setsockcreatecon_raw(label) < 0) |
| 772 |
return log_selinux_enforcing_errno(errno, "Failed to set SELinux security context %s for sockets: %m", label); |
| 773 |
#endif |
| 774 |
|
| 775 |
return 0; |
| 776 |
} |
| 777 |
|
| 778 |
void mac_selinux_create_socket_clear(void) { |
| 779 |
|
| 780 |
#if HAVE_SELINUX |
| 781 |
PROTECT_ERRNO; |
| 782 |
|
| 783 |
if (selinux_init(/* force= */ false) <= 0) |
| 784 |
return; |
| 785 |
|
| 786 |
(void) sym_setsockcreatecon_raw(NULL); |
| 787 |
#endif |
| 788 |
} |
| 789 |
|
| 790 |
int mac_selinux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) { |
| 791 |
|
| 792 |
/* Binds a socket and label its file system object according to the SELinux policy */ |
| 793 |
|
| 794 |
#if HAVE_SELINUX |
| 795 |
_cleanup_freecon_ char *fcon = NULL; |
| 796 |
const struct sockaddr_un *un; |
| 797 |
bool context_changed = false; |
| 798 |
size_t sz; |
| 799 |
char *path; |
| 800 |
int r; |
| 801 |
|
| 802 |
assert(fd >= 0); |
| 803 |
assert(addr); |
| 804 |
assert(addrlen >= sizeof(sa_family_t)); |
| 805 |
|
| 806 |
if (selinux_init(/* force= */ false) <= 0) |
| 807 |
goto skipped; |
| 808 |
|
| 809 |
if (!label_hnd) |
| 810 |
goto skipped; |
| 811 |
|
| 812 |
/* Filter out non-local sockets */ |
| 813 |
if (addr->sa_family != AF_UNIX) |
| 814 |
goto skipped; |
| 815 |
|
| 816 |
/* Filter out anonymous sockets */ |
| 817 |
if (addrlen < offsetof(struct sockaddr_un, sun_path) + 1) |
| 818 |
goto skipped; |
| 819 |
|
| 820 |
/* Filter out abstract namespace sockets */ |
| 821 |
un = (const struct sockaddr_un*) addr; |
| 822 |
if (un->sun_path[0] == 0) |
| 823 |
goto skipped; |
| 824 |
|
| 825 |
sz = addrlen - offsetof(struct sockaddr_un, sun_path); |
| 826 |
if (sz > PATH_MAX) |
| 827 |
goto skipped; |
| 828 |
path = strndupa_safe(un->sun_path, sz); |
| 829 |
|
| 830 |
/* Check for policy reload so 'label_hnd' is kept up-to-date by callbacks */ |
| 831 |
mac_selinux_maybe_reload(); |
| 832 |
if (!label_hnd) |
| 833 |
goto skipped; |
| 834 |
|
| 835 |
if (path_is_absolute(path)) |
| 836 |
r = sym_selabel_lookup_raw(label_hnd, &fcon, path, S_IFSOCK); |
| 837 |
else { |
| 838 |
_cleanup_free_ char *newpath = NULL; |
| 839 |
|
| 840 |
r = path_make_absolute_cwd(path, &newpath); |
| 841 |
if (r < 0) |
| 842 |
return r; |
| 843 |
|
| 844 |
r = sym_selabel_lookup_raw(label_hnd, &fcon, newpath, S_IFSOCK); |
| 845 |
} |
| 846 |
|
| 847 |
if (r < 0) { |
| 848 |
/* No context specified by the policy? Proceed without setting it */ |
| 849 |
if (errno == ENOENT) |
| 850 |
goto skipped; |
| 851 |
|
| 852 |
r = log_selinux_enforcing_errno(errno, "Failed to determine SELinux security context for %s: %m", path); |
| 853 |
if (r < 0) |
| 854 |
return r; |
| 855 |
} else { |
| 856 |
if (sym_setfscreatecon_raw(fcon) < 0) { |
| 857 |
r = log_selinux_enforcing_errno(errno, "Failed to set SELinux security context %s for %s: %m", fcon, path); |
| 858 |
if (r < 0) |
| 859 |
return r; |
| 860 |
} else |
| 861 |
context_changed = true; |
| 862 |
} |
| 863 |
|
| 864 |
r = RET_NERRNO(bind(fd, addr, addrlen)); |
| 865 |
|
| 866 |
if (context_changed) |
| 867 |
(void) sym_setfscreatecon_raw(NULL); |
| 868 |
|
| 869 |
return r; |
| 870 |
|
| 871 |
skipped: |
| 872 |
#endif |
| 873 |
return RET_NERRNO(bind(fd, addr, addrlen)); |
| 874 |
} |