HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/shared/selinux-util.c
Git blob: c58e1713a4ff51db066523c8ae819130a4df5f16
Lines 1001–1182 of 1,182
return RET_NERRNO(bind(fd, addr, addrlen));
}
#if HAVE_SELINUX
static int get_selinux_policy_root(int rfd, const char *root, char **ret) {
_cleanup_free_ char *policytype = NULL;
int r;
assert(rfd >= 0);
assert(root);
assert(ret);
/* Read the SELinux config from the alternate root, trying /etc and /usr/lib */
FOREACH_STRING(config_dir, "/etc/selinux/config", "/usr/lib/selinux/config") {
_cleanup_close_ int fd = -EBADF;
r = chaseat(rfd, rfd, config_dir, CHASE_MUST_BE_REGULAR, /* ret_path= */ NULL, &fd);
if (IN_SET(r, -ENOENT, -ENOTDIR, -EISDIR))
continue;
if (r < 0)
return log_debug_errno(r, "Failed to resolve SELinux config path in root '%s': %m", root);
r = parse_env_file_fd(fd, config_dir, "SELINUXTYPE", &policytype);
if (r == -ENOENT)
continue;
if (r < 0)
return log_debug_errno(r, "Failed to parse SELinux config '%s' in root '%s': %m", config_dir, root);
if (!isempty(policytype))
break;
}
if (isempty(policytype)) {
log_debug("No SELINUXTYPE configured in alternate root '%s', skipping label context setup.", root);
*ret = NULL;
return 0;
}
if (!filename_is_valid(policytype))
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
"SELINUXTYPE '%s' in alternate root '%s' is not a valid filename.", policytype, root);
/* Look for the policy database under the alternate root */
FOREACH_STRING(policy_dir, "/etc/selinux", "/usr/share/selinux") {
_cleanup_free_ char *p = NULL, *policy_subdir = NULL;
policy_subdir = path_join(policy_dir, policytype);
if (!policy_subdir)
return log_oom();
r = chaseat(rfd, rfd, policy_subdir, CHASE_MUST_BE_DIRECTORY, &p, /* ret_fd= */ NULL);
if (IN_SET(r, -ENOENT, -ENOTDIR))
continue;
if (r < 0) {
log_debug_errno(r, "Failed to resolve policy directory '%s' in root '%s': %m", policy_subdir, root);
continue;
}
/* Validate that the file_contexts file resolves within the root, since libselinux
* will open it with plain fopen() which follows symlinks without confinement */
_cleanup_free_ char *fc_subpath = NULL;
fc_subpath = path_join(policy_subdir, "contexts/files/file_contexts");
if (!fc_subpath)
return log_oom();
r = chaseat(rfd, rfd, fc_subpath, CHASE_MUST_BE_REGULAR, /* ret_path= */ NULL, /* ret_fd= */ NULL);
if (IN_SET(r, -ENOENT, -ENOTDIR)) {
log_debug_errno(r, "file_contexts not found at '%s' in root '%s', skipping.", fc_subpath, root);
continue;
}
if (r < 0) {
log_debug_errno(r, "Failed to resolve file_contexts '%s' in root '%s': %m", fc_subpath, root);
continue;
}
r = chaseat_prefix_root(p, root, ret);
if (r < 0)
return r;
return 1;
}
log_debug("SELinux policy directory for type '%s' not found in alternate root '%s', skipping.", policytype, root);
*ret = NULL;
return 0;
}
#endif
int mac_selinux_label_context_new(const char *root, LabelContext **ret) {
assert(root);
assert(!empty_or_root(root));
assert(ret);
#if HAVE_SELINUX
_cleanup_free_ char *policyroot = NULL, *canonical_root = NULL;
_cleanup_close_ int rfd = -EBADF;
int r;
if (mac_smack_use()) {
*ret = NULL;
return 0;
}
r = dlopen_libselinux(LOG_ERR);
if (r < 0)
return r;
mac_selinux_disable_logging();
r = chaseat(AT_FDCWD, AT_FDCWD, root, CHASE_MUST_BE_DIRECTORY, &canonical_root, &rfd);
if (r < 0)
return log_debug_errno(r, "Failed to open root directory '%s': %m", root);
r = get_selinux_policy_root(rfd, root, &policyroot);
if (r < 0)
return r;
if (r == 0) {
*ret = NULL;
return 0;
}
/* Temporarily switch the policy root so selabel_open() reads the image's file_contexts.
* The handle bakes in the database at open time, so we can reset the root immediately after.
* We must strdup the saved root because selinux_set_policy_root() frees the internal buffer. */
_cleanup_(selinux_policy_root_reset_and_freep) char *saved_policy_root = NULL;
const char *p = sym_selinux_policy_root();
if (p) {
saved_policy_root = strdup(p);
if (!saved_policy_root)
return log_oom();
}
r = RET_NERRNO(sym_selinux_set_policy_root(policyroot));
if (r < 0)
return log_debug_errno(r, "Failed to set SELinux policy root to '%s': %m", policyroot);
_cleanup_(selabel_closep) struct selabel_handle *hnd = sym_selabel_open(SELABEL_CTX_FILE, /* opts= */ NULL, /* nopts= */ 0);
int saved_errno = errno;
/* Reset immediately - the handle baked in the database at open time */
selinux_policy_root_reset_and_freep(&saved_policy_root);
if (!hnd)
return log_debug_errno(saved_errno, "Failed to open SELinux label database for alternate root '%s': %m", root);
r = label_ops_set(&selinux_label_ops);
if (r < 0 && r != -EBUSY)
return r;
_cleanup_free_ LabelContext *c = new(LabelContext, 1);
if (!c)
return log_oom();
*c = (LabelContext) {
.label_hnd = TAKE_PTR(hnd),
.rfd = TAKE_FD(rfd),
.root = TAKE_PTR(canonical_root),
};
*ret = TAKE_PTR(c);
return 0;
#else
*ret = NULL;
return 0;
#endif
}
LabelContext* mac_selinux_label_context_free(LabelContext *c) {
if (!c)
return NULL;
#if HAVE_SELINUX
if (c->label_hnd)
sym_selabel_close(c->label_hnd);
safe_close(c->rfd);
free(c->root);
#endif
return mfree(c);
}