HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/core/exec-invoke.c
Git blob: ff107cc753cf314807216d8e1747fc30661a6ef4
Lines 6001–6799 of 6,799
r = setrlimit_closest_all((const struct rlimit* const *) context->rlimit, &which_failed);
if (r < 0) {
*exit_status = EXIT_LIMITS;
return log_error_errno(r, "Failed to adjust resource limit RLIMIT_%s: %m", rlimit_to_string(which_failed));
}
}
if (needs_setuid && context->pam_name && username) {
/* Let's call into PAM after we set up our own idea of resource limits so that pam_limits
* wins here. (See above.) */
/* All fds passed in the fds array will be closed in the pam child process. */
r = setup_pam(context, cgroup_context, params, username, uid, gid, &accum_env,
needs_sandboxing, params->exec_fd);
if (r < 0) {
*exit_status = EXIT_PAM;
return log_error_errno(r, "Failed to set up PAM session: %m");
}
/* PAM modules might have set some ambient caps. Query them here and merge them into
* the caps we want to set in the end, so that we don't end up unsetting them. */
uint64_t ambient_after_pam;
r = capability_get_ambient(&ambient_after_pam);
if (r < 0) {
*exit_status = EXIT_CAPABILITIES;
return log_error_errno(r, "Failed to query ambient caps: %m");
}
capability_ambient_set |= ambient_after_pam;
ngids_after_pam = getgroups_alloc(&gids_after_pam);
if (ngids_after_pam < 0) {
*exit_status = EXIT_GROUP;
return log_error_errno(ngids_after_pam, "Failed to obtain groups after setting up PAM: %m");
}
}
if (context->private_bpf != PRIVATE_BPF_NO) {
/* To create a BPF token, the bpffs has to be mounted with the fsopen()/fsmount() API.
* More specifically, fsopen() must be called within the user namespace, then all the
* fsconfig() as privileged user, and finally and fsmount() and move_mount() in
* the user namespace.
* To do this, we split the code into a bpffs_prepare() and mount_bpffs() functions,
* the first runs as privileged user the second as unprivileged one, and they coordinate
* by sending messages and file descriptors via a socket pair.
* The user and mount namespaces need to be unshared in this exact order and before
* the fsopen() call for the fsopen() API to work as unprivileged.
* This is the kernel sample doing this:
* https://github.com/torvalds/linux/blob/master/tools/testing/selftests/bpf/prog_tests/token.c
*/
r = bpffs_prepare(context, &bpffs_pidref, &bpffs_socket_fd, &bpffs_errno_pipe);
if (r < 0) {
*exit_status = EXIT_BPF;
return log_error_errno(r, "Failed to mount bpffs in bpffs_prepare(): %m");
}
}
_cleanup_(pinned_resource_done) PinnedResource rootfs = PINNED_RESOURCE_NULL;
_cleanup_free_ char *error_path = NULL;
r = pin_rootfs(context, params, &rootfs, &error_path);
if (r < 0) {
*exit_status = EXIT_NAMESPACE;
return log_error_errno(r, "Failed to open service's root fs%s%s: %m", error_path ? ": " : "", strempty(error_path));
}
r = setup_ephemeral(context, runtime, &rootfs, &error_path);
if (r < 0) {
*exit_status = EXIT_NAMESPACE;
return log_error_errno(r, "Failed to make ephemeral copy of service's root fs%s%s: %m", error_path ? ": " : "", strempty(error_path));
}
/* Load a bunch of libraries we'll possibly need later, before we turn off dlopen() */
(void) dlopen_bpf(LOG_DEBUG);
(void) dlopen_cryptsetup(LOG_DEBUG);
(void) dlopen_libmount(LOG_DEBUG);
(void) dlopen_libseccomp(LOG_DEBUG);
/* Needed for userspace verity verification fallback */
(void) dlopen_libcrypto(LOG_DEBUG);
/* Let's now disable further dlopen()ing of libraries, since we are about to do namespace
* shenanigans, and do not want to mix resources from host and namespace */
block_dlopen();
if (needs_sandboxing && !have_cap_sys_admin && exec_needs_cap_sys_admin(context, params)) {
/* If we're unprivileged, set up the user namespace first to enable use of the other namespaces.
* Users with CAP_SYS_ADMIN can set up user namespaces last because they will be able to
* set up all of the other namespaces (i.e. network, mount, UTS) without a user namespace. */
if (context->user_namespace_path && runtime->shared->userns_storage_socket[0] >= 0) {
*exit_status = EXIT_USER;
return log_error_errno(SYNTHETIC_ERRNO(EPERM), "UserNamespacePath= is configured, but user namespace setup not permitted");
}
PrivateUsers pu = exec_context_get_effective_private_users(context, params);
if (pu == PRIVATE_USERS_NO)
pu = PRIVATE_USERS_SELF;
/* The kernel requires /proc/pid/setgroups be set to "deny" prior to writing /proc/pid/gid_map in
* unprivileged user namespaces. */
r = setup_private_users(
nsresource_link,
pu,
saved_uid,
saved_gid,
&uid,
&gid,
&outside_uid,
&outside_gid,
/* allow_setgroups= */ false);
/* If it was requested explicitly and we can't set it up, fail early. Otherwise, continue and let
* the actual requested operations fail (or silently continue). */
if (r < 0) {
if (context->private_users != PRIVATE_USERS_NO) {
*exit_status = EXIT_USER;
return log_error_errno(r, "Failed to set up user namespacing for unprivileged user: %m");
}
log_notice_errno(r, "Failed to set up user namespacing for unprivileged user, ignoring: %m");
} else {
assert(r > 0);
userns_set_up = true;
log_debug("Set up unprivileged user namespace");
}
}
/* Call setup_delegated_namespaces() the first time to unshare all non-delegated namespaces. */
r = setup_delegated_namespaces(
context,
params,
runtime,
&rootfs,
/* delegate= */ false,
pressure_path,
uid,
gid,
command,
needs_sandboxing,
have_cap_sys_admin,
&bpffs_pidref,
bpffs_socket_fd,
bpffs_errno_pipe,
mountfsd_link,
&keep_parent_fd,
exit_status);
if (r < 0)
return r;
/* Drop groups as early as possible.
* This needs to be done after PrivateDevices=yes setup as device nodes should be owned by the host's root.
* For non-root in a userns, devices will be owned by the user/group before the group change, and nobody. */
if (needs_setuid) {
_cleanup_free_ gid_t *gids_to_enforce = NULL;
int ngids_to_enforce;
ngids_to_enforce = merge_gid_lists(gids,
ngids,
gids_after_pam,
ngids_after_pam,
&gids_to_enforce);
if (ngids_to_enforce < 0) {
*exit_status = EXIT_GROUP;
return log_error_errno(ngids_to_enforce, "Failed to merge group lists. Group membership might be incorrect: %m");
}
r = enforce_groups(gid, gids_to_enforce, ngids_to_enforce);
if (r < 0) {
*exit_status = EXIT_GROUP;
return log_error_errno(r, "Changing group credentials failed: %m");
}
}
/* If the user namespace was not set up above, try to do it now.
* It's preferred to set up the user namespace later (after all other namespaces) so as not to be
* restricted by rules pertaining to combining user namespaces with other namespaces (e.g. in the
* case of mount namespaces being less privileged when the mount point list is copied from a
* different user namespace). */
if (needs_sandboxing && context->user_namespace_path && runtime->shared && runtime->shared->userns_storage_socket[0] >= 0) {
if (!namespace_type_supported(NAMESPACE_USER)) {
*exit_status = EXIT_USER;
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "UserNamespacePath= is not supported, refusing.");
}
r = setup_shareable_ns(runtime->shared->userns_storage_socket, CLONE_NEWUSER);
if (ERRNO_IS_NEG_PRIVILEGE(r)) {
*exit_status = EXIT_USER;
return log_error_errno(r, "UserNamespacePath= is configured, but user namespace setup not permitted, refusing.");
}
if (r < 0) {
*exit_status = EXIT_USER;
return log_error_errno(r, "Failed to set up user namespacing: %m");
}
log_debug("Set up existing user namespace");
} else if (needs_sandboxing && !userns_set_up) {
PrivateUsers pu = exec_context_get_effective_private_users(context, params);
r = setup_private_users(
nsresource_link,
pu,
saved_uid,
saved_gid,
&uid,
&gid,
&outside_uid,
&outside_gid,
/* allow_setgroups= */ pu == PRIVATE_USERS_FULL);
if (r < 0) {
*exit_status = EXIT_USER;
return log_error_errno(r, "Failed to set up user namespacing: %m");
}
if (r > 0)
log_debug("Set up privileged user namespace");
}
if (params->user_lookup_fd >= 0) {
/* If we haven't sent the UIDs/GIDs we settled on upstream yet, let's do so now, as we
* finally know our final UID/GID range */
r = send_user_lookup(params->unit_id, params->user_lookup_fd, outside_uid, outside_gid);
if (r < 0) {
*exit_status = EXIT_USER;
return log_error_errno(r, "Failed to send user credentials to PID1: %m");
}
params->user_lookup_fd = safe_close(params->user_lookup_fd);
}
/* Call setup_delegated_namespaces() the second time to unshare all delegated namespaces. */
r = setup_delegated_namespaces(
context,
params,
runtime,
&rootfs,
/* delegate= */ true,
pressure_path,
uid,
gid,
command,
needs_sandboxing,
have_cap_sys_admin,
&bpffs_pidref,
bpffs_socket_fd,
bpffs_errno_pipe,
mountfsd_link,
&keep_parent_fd,
exit_status);
if (r < 0)
return r;
/* We are done now with the nsresourced/mountfsd shenanigans, let's close the connections */
nsresource_link = sd_varlink_unref(nsresource_link);
mountfsd_link = sd_varlink_unref(mountfsd_link);
/* With PrivatePIDs=, this releases the parent process that set up the connections */
keep_parent_fd = safe_close(keep_parent_fd);
/* We don't need the pinned rootfs anymore at this point. Close the fds now, so that they are
* definitely gone before we do our fd rearrangements below. */
pinned_resource_done(&rootfs);
/* Kill unnecessary process, for the case that e.g. when the bpffs mount point is hidden. */
pidref_done_sigkill_wait(&bpffs_pidref);
bpffs_socket_fd = safe_close(bpffs_socket_fd);
bpffs_errno_pipe = safe_close(bpffs_errno_pipe);
if (needs_sandboxing && exec_needs_cgroup_namespace(context) && params->cgroup_path) {
/* Move ourselves into the subcgroup now *after* we've unshared the cgroup namespace, which
* ensures the root of the cgroup namespace is the top level service cgroup and not the
* subcgroup. Adjust the prefix accordingly since we're in a cgroup namespace now. */
r = attach_to_subcgroup(context, cgroup_context, params, /* prefix= */ NULL);
if (r < 0) {
*exit_status = EXIT_CGROUP;
return r;
}
}
/* Now that the mount namespace has been set up and privileges adjusted, let's look for the thing we
* shall execute. */
const char *path = command->path;
if (FLAGS_SET(command->flags, EXEC_COMMAND_VIA_SHELL)) {
if (shell_is_placeholder(shell)) {
log_debug("Shell prefixing requested for user without default shell, using /bin/sh: %s",
strna(username));
assert(streq(path, _PATH_BSHELL));
} else
path = shell;
}
_cleanup_free_ char *executable = NULL;
_cleanup_close_ int executable_fd = -EBADF;
r = find_executable_full(path, /* root= */ NULL, context->exec_search_path, false, &executable, &executable_fd);
if (r < 0) {
*exit_status = EXIT_EXEC;
log_struct_errno(LOG_NOTICE, r,
LOG_MESSAGE_ID(SD_MESSAGE_SPAWN_FAILED_STR),
LOG_EXEC_MESSAGE(params, "Unable to locate executable '%s': %m", path),
LOG_ITEM("EXECUTABLE=%s", path));
/* If the error will be ignored by manager, tune down the log level here. Missing executable
* is very much expected in this case. */
return r != -ENOMEM && FLAGS_SET(command->flags, EXEC_COMMAND_IGNORE_FAILURE) ? 1 : r;
}
r = add_shifted_fd(&keep_fds, &n_keep_fds, &executable_fd);
if (r < 0) {
*exit_status = EXIT_FDS;
return log_error_errno(r, "Failed to collect shifted fd: %m");
}
#if HAVE_SELINUX
if (needs_sandboxing && use_selinux && params->selinux_context_net) {
int fd = -EBADF;
if (socket_fd >= 0)
fd = socket_fd;
else if (params->n_socket_fds == 1)
/* If stdin is not connected to a socket but we are triggered by exactly one socket unit then we
* use context from that fd to compute the label. */
fd = params->fds[0];
if (fd >= 0) {
r = mac_selinux_get_child_mls_label(fd, executable, context->selinux_context, &mac_selinux_context_net);
if (r < 0) {
if (!context->selinux_context_ignore) {
*exit_status = EXIT_SELINUX_CONTEXT;
return log_error_errno(r, "Failed to determine SELinux context: %m");
}
log_debug_errno(r, "Failed to determine SELinux context, ignoring: %m");
}
}
}
#endif
/* We repeat the fd closing here, to make sure that nothing is leaked from the PAM modules. Note that
* we are more aggressive this time, since we don't need socket_fd and the netns and ipcns fds any
* more. We do keep exec_fd and handoff_timestamp_fd however, if we have it, since we need to keep
* them open until the final execve(). But first, close the remaining sockets in the context
* objects. */
socket_fd = safe_close(socket_fd);
exec_runtime_close(runtime);
exec_params_close(params);
r = close_all_fds(keep_fds, n_keep_fds);
if (r >= 0)
r = pack_fds(params->fds, params->n_socket_fds + params->n_stashed_fds);
if (r >= 0)
r = flag_fds(params->fds, params->n_socket_fds, params->n_socket_fds + params->n_stashed_fds,
context->non_blocking);
if (r < 0) {
*exit_status = EXIT_FDS;
return log_error_errno(r, "Failed to adjust passed file descriptors: %m");
}
/* At this point, the fds we want to pass to the program are all ready and set up, with O_CLOEXEC turned off
* and at the right fd numbers. The are no other fds open, with one exception: the exec_fd if it is defined,
* and it has O_CLOEXEC set, after all we want it to be closed by the execve(), so that our parent knows we
* came this far. */
secure_bits = context->secure_bits;
if (needs_sandboxing) {
uint64_t bset;
/* Set the RTPRIO resource limit to 0, but only if nothing else was explicitly requested.
* (Note this is placed after the general resource limit initialization, see above, in order
* to take precedence.) */
if (context->restrict_realtime && !context->rlimit[RLIMIT_RTPRIO]) {
if (setrlimit(RLIMIT_RTPRIO, &RLIMIT_MAKE_CONST(0)) < 0) {
*exit_status = EXIT_LIMITS;
return log_error_errno(errno, "Failed to adjust RLIMIT_RTPRIO resource limit: %m");
}
}
#if ENABLE_SMACK
/* LSM Smack needs the capability CAP_MAC_ADMIN to change the current execution security context of the
* process. This is the latest place before dropping capabilities. Other MAC context are set later. */
if (use_smack) {
r = setup_smack(context, params, executable_fd);
if (r < 0 && !context->smack_process_label_ignore) {
*exit_status = EXIT_SMACK_PROCESS_LABEL;
return log_error_errno(r, "Failed to set SMACK process label: %m");
}
}
#endif
bset = context->capability_bounding_set;
#if HAVE_SECCOMP
/* If the service has any form of a seccomp filter and it allows dropping privileges, we'll
* keep the needed privileges to apply it even if we're not root. */
if (needs_setuid &&
uid_is_valid(uid) &&
context_has_seccomp(context) &&
seccomp_allows_drop_privileges(context)) {
keep_seccomp_privileges = true;
r = prctl_safe(PR_SET_KEEPCAPS, 1, 0, 0, 0);
if (r < 0) {
*exit_status = EXIT_USER;
return log_error_errno(r, "Failed to enable keep capabilities flag: %m");
}
/* Save the current bounding set so we can restore it after applying the seccomp
* filter */
saved_bset = bset;
bset |= (UINT64_C(1) << CAP_SYS_ADMIN) |
(UINT64_C(1) << CAP_SETPCAP);
}
#endif
if (!cap_test_all(bset)) {
r = capability_bounding_set_drop(bset, /* right_now= */ false);
if (r < 0) {
*exit_status = EXIT_CAPABILITIES;
return log_error_errno(r, "Failed to drop capabilities: %m");
}
}
/* Ambient capabilities are cleared during setresuid() (in enforce_user()) even with
* keep-caps set.
*
* To be able to raise the ambient capabilities after setresuid() they have to be added to
* the inherited set and keep caps has to be set (done in enforce_user()). After setresuid()
* the ambient capabilities can be raised as they are present in the permitted and
* inhertiable set. However it is possible that someone wants to set ambient capabilities
* without changing the user, so we also set the ambient capabilities here.
*
* The requested ambient capabilities are raised in the inheritable set if the second
* argument is true. */
if (capability_ambient_set != 0) {
r = capability_ambient_set_apply(capability_ambient_set, /* also_inherit= */ true);
if (r < 0) {
*exit_status = EXIT_CAPABILITIES;
return log_error_errno(r, "Failed to apply ambient capabilities (before UID change): %m");
}
}
}
/* chroot to root directory first, before we lose the ability to chroot */
r = apply_root_directory(context, params, runtime, needs_mount_namespace, exit_status);
if (r < 0)
return log_error_errno(r, "Chrooting to the requested root directory failed: %m");
if (needs_setuid) {
if (uid_is_valid(uid)) {
r = enforce_user(context, uid, capability_ambient_set);
if (r < 0) {
*exit_status = EXIT_USER;
return log_error_errno(r, "Failed to change UID to " UID_FMT ": %m", uid);
}
if (keep_seccomp_privileges) {
if (!BIT_SET(capability_ambient_set, CAP_SETUID)) {
r = drop_capability(CAP_SETUID);
if (r < 0) {
*exit_status = EXIT_USER;
return log_error_errno(r, "Failed to drop CAP_SETUID: %m");
}
}
r = keep_capability(CAP_SYS_ADMIN);
if (r < 0) {
*exit_status = EXIT_USER;
return log_error_errno(r, "Failed to keep CAP_SYS_ADMIN: %m");
}
r = keep_capability(CAP_SETPCAP);
if (r < 0) {
*exit_status = EXIT_USER;
return log_error_errno(r, "Failed to keep CAP_SETPCAP: %m");
}
}
if (capability_ambient_set != 0) {
/* Raise the ambient capabilities after user change. */
r = capability_ambient_set_apply(capability_ambient_set, /* also_inherit= */ false);
if (r < 0) {
*exit_status = EXIT_CAPABILITIES;
return log_error_errno(r, "Failed to apply ambient capabilities (after UID change): %m");
}
}
}
}
/* Apply working directory here, because the working directory might be on NFS and only the user
* running this service might have the correct privilege to change to the working directory. Also, it
* is absolutely 💣 crucial 💣 we applied all mount namespacing rearrangements before this, so that
* the cwd cannot be used to pin directories outside of the sandbox. */
r = apply_working_directory(context, params, runtime, pwent_home, accum_env);
if (r < 0) {
*exit_status = EXIT_CHDIR;
return log_error_errno(r, "Changing to the requested working directory failed: %m");
}
if (needs_sandboxing) {
/* Apply other MAC contexts late, but before seccomp syscall filtering, as those should really be last to
* influence our own codepaths as little as possible. Moreover, applying MAC contexts usually requires
* syscalls that are subject to seccomp filtering, hence should probably be applied before the syscalls
* are restricted. */
#if HAVE_SELINUX
if (use_selinux) {
char *exec_context = mac_selinux_context_net ?: context->selinux_context;
if (exec_context) {
r = sym_setexeccon_raw(exec_context);
if (r < 0) {
if (!context->selinux_context_ignore) {
*exit_status = EXIT_SELINUX_CONTEXT;
return log_error_errno(r, "Failed to change SELinux context to %s: %m", exec_context);
}
log_debug_errno(r, "Failed to change SELinux context to %s, ignoring: %m", exec_context);
}
}
}
#endif
#if HAVE_APPARMOR
if (use_apparmor && context->apparmor_profile) {
r = ASSERT_PTR(sym_aa_change_onexec)(context->apparmor_profile);
if (r < 0 && !context->apparmor_profile_ignore) {
*exit_status = EXIT_APPARMOR_PROFILE;
return log_error_errno(errno, "Failed to prepare AppArmor profile change to %s: %m",
context->apparmor_profile);
}
}
#endif
/* PR_GET_SECUREBITS is not privileged, while PR_SET_SECUREBITS is. So to suppress potential
* EPERMs we'll try not to call PR_SET_SECUREBITS unless necessary. Setting securebits
* requires CAP_SETPCAP. */
r = prctl_safe(PR_GET_SECUREBITS, 0, 0, 0, 0);
if (r != secure_bits) {
/* CAP_SETPCAP is required to set securebits. This capability is raised into the
* effective set here.
*
* The effective set is overwritten during execve() with the following values:
*
* - ambient set (for non-root processes)
*
* - (inheritable | bounding) set for root processes)
*
* Hence there is no security impact to raise it in the effective set before execve
*/
r = capability_gain_cap_setpcap();
if (r < 0) {
*exit_status = EXIT_CAPABILITIES;
return log_error_errno(r, "Failed to gain CAP_SETPCAP for setting secure bits");
}
r = prctl_safe(PR_SET_SECUREBITS, secure_bits, 0, 0, 0);
if (r < 0) {
*exit_status = EXIT_SECUREBITS;
return log_error_errno(r, "Failed to set process secure bits: %m");
}
}
if (context_has_no_new_privileges(context)) {
r = proc_set_nnp();
if (r < 0) {
*exit_status = EXIT_NO_NEW_PRIVILEGES;
return log_error_errno(r, "Failed to disable new privileges: %m");
}
}
#if HAVE_SECCOMP
r = apply_address_families(context, params);
if (r < 0) {
*exit_status = EXIT_ADDRESS_FAMILIES;
return log_error_errno(r, "Failed to restrict address families: %m");
}
r = apply_memory_deny_write_execute(context, params);
if (r < 0) {
*exit_status = EXIT_SECCOMP;
return log_error_errno(r, "Failed to disable writing to executable memory: %m");
}
r = apply_restrict_realtime(context, params);
if (r < 0) {
*exit_status = EXIT_SECCOMP;
return log_error_errno(r, "Failed to apply realtime restrictions: %m");
}
r = apply_restrict_suid_sgid(context, params);
if (r < 0) {
*exit_status = EXIT_SECCOMP;
return log_error_errno(r, "Failed to apply SUID/SGID restrictions: %m");
}
r = apply_restrict_namespaces(context, params);
if (r < 0) {
*exit_status = EXIT_SECCOMP;
return log_error_errno(r, "Failed to apply namespace restrictions: %m");
}
r = apply_protect_sysctl(context, params);
if (r < 0) {
*exit_status = EXIT_SECCOMP;
return log_error_errno(r, "Failed to apply sysctl restrictions: %m");
}
r = apply_protect_kernel_modules(context, params);
if (r < 0) {
*exit_status = EXIT_SECCOMP;
return log_error_errno(r, "Failed to apply module loading restrictions: %m");
}
r = apply_protect_kernel_logs(context, params);
if (r < 0) {
*exit_status = EXIT_SECCOMP;
return log_error_errno(r, "Failed to apply kernel log restrictions: %m");
}
r = apply_protect_clock(context, params);
if (r < 0) {
*exit_status = EXIT_SECCOMP;
return log_error_errno(r, "Failed to apply clock restrictions: %m");
}
r = apply_private_devices(context, params);
if (r < 0) {
*exit_status = EXIT_SECCOMP;
return log_error_errno(r, "Failed to set up private devices: %m");
}
r = apply_syscall_archs(context, params);
if (r < 0) {
*exit_status = EXIT_SECCOMP;
return log_error_errno(r, "Failed to apply syscall architecture restrictions: %m");
}
r = apply_lock_personality(context, params);
if (r < 0) {
*exit_status = EXIT_SECCOMP;
return log_error_errno(r, "Failed to lock personalities: %m");
}
r = apply_syscall_log(context, params);
if (r < 0) {
*exit_status = EXIT_SECCOMP;
return log_error_errno(r, "Failed to apply system call log filters: %m");
}
#endif
#if HAVE_LIBBPF
r = apply_restrict_filesystems(context, params);
if (r < 0) {
*exit_status = EXIT_BPF;
return log_error_errno(r, "Failed to restrict filesystems: %m");
}
#endif
#if HAVE_SECCOMP
/* This really should remain as close to the execve() as possible, to make sure our own code is affected
* by the filter as little as possible. */
r = apply_syscall_filter(context, params);
if (r < 0) {
*exit_status = EXIT_SECCOMP;
return log_error_errno(r, "Failed to apply system call filters: %m");
}
if (keep_seccomp_privileges) {
/* Restore the capability bounding set with what's expected from the service + the
* ambient capabilities hack */
if (!cap_test_all(saved_bset)) {
r = capability_bounding_set_drop(saved_bset, /* right_now= */ false);
if (r < 0) {
*exit_status = EXIT_CAPABILITIES;
return log_error_errno(r, "Failed to drop bset capabilities: %m");
}
}
/* Only drop CAP_SYS_ADMIN if it's not in the bounding set, otherwise we'll break
* applications that use it. */
if (!BIT_SET(saved_bset, CAP_SYS_ADMIN)) {
r = drop_capability(CAP_SYS_ADMIN);
if (r < 0) {
*exit_status = EXIT_USER;
return log_error_errno(r, "Failed to drop CAP_SYS_ADMIN: %m");
}
}
/* Only drop CAP_SETPCAP if it's not in the bounding set, otherwise we'll break
* applications that use it. */
if (!BIT_SET(saved_bset, CAP_SETPCAP)) {
r = drop_capability(CAP_SETPCAP);
if (r < 0) {
*exit_status = EXIT_USER;
return log_error_errno(r, "Failed to drop CAP_SETPCAP: %m");
}
}
r = prctl_safe(PR_SET_KEEPCAPS, 0, 0, 0, 0);
if (r < 0) {
*exit_status = EXIT_USER;
return log_error_errno(r, "Failed to drop keep capabilities flag: %m");
}
}
#endif
}
_cleanup_strv_free_ char **replaced_argv = NULL, **argv_via_shell = NULL;
char **final_argv = FLAGS_SET(command->flags, EXEC_COMMAND_VIA_SHELL) ? strv_skip(command->argv, 1) : command->argv;
if (final_argv && !FLAGS_SET(command->flags, EXEC_COMMAND_NO_ENV_EXPAND)) {
_cleanup_strv_free_ char **unset_variables = NULL, **bad_variables = NULL;
r = replace_env_argv(final_argv, accum_env, &replaced_argv, &unset_variables, &bad_variables);
if (r < 0) {
*exit_status = EXIT_MEMORY;
return log_error_errno(r, "Failed to replace environment variables: %m");
}
final_argv = replaced_argv;
if (!strv_isempty(unset_variables)) {
_cleanup_free_ char *ju = strv_join(unset_variables, ", ");
log_warning("Referenced but unset environment variable evaluates to an empty string: %s", strna(ju));
}
if (!strv_isempty(bad_variables)) {
_cleanup_free_ char *jb = strv_join(bad_variables, ", ");
log_warning("Invalid environment variable name evaluates to an empty string: %s", strna(jb));
}
}
/* Apply UnsetEnvironment= after the command line has been expanded, so that the variables it
* removes are still available for expansion. Only the environment block passed to the executed
* process is affected, not the expansion. */
if (!strv_isempty(context->unset_environment)) {
char **ee = NULL;
ee = strv_env_delete(accum_env, 1, context->unset_environment);
if (!ee) {
*exit_status = EXIT_MEMORY;
return log_oom();
}
strv_free_and_replace(accum_env, ee);
}
if (FLAGS_SET(command->flags, EXEC_COMMAND_VIA_SHELL)) {
r = strv_extendf(&argv_via_shell, "%s%s", command->argv[0][0] == '-' ? "-" : "", path);
if (r < 0) {
*exit_status = EXIT_MEMORY;
return log_oom();
}
if (!strv_isempty(final_argv)) {
_cleanup_free_ char *cmdline_joined = NULL;
cmdline_joined = strv_join(final_argv, " ");
if (!cmdline_joined) {
*exit_status = EXIT_MEMORY;
return log_oom();
}
r = strv_extend_many(&argv_via_shell, "-c", cmdline_joined);
if (r < 0) {
*exit_status = EXIT_MEMORY;
return log_oom();
}
}
final_argv = argv_via_shell;
}
log_command_line(context, params, "Executing", executable, final_argv);
/* We have finished with all our initializations. Let's now let the manager know that. From this
* point on, if the manager sees POLLHUP on the exec_fd, then execve() was successful. */
r = exec_fd_mark_hot(context, params, /* hot= */ true, exit_status);
if (r < 0)
return r;
/* As last thing before the execve(), let's send the handoff timestamp */
r = send_handoff_timestamp(context, params, exit_status);
if (r < 0) {
/* If this handoff timestamp failed, let's undo the marking as hot */
(void) exec_fd_mark_hot(context, params, /* hot= */ false, /* reterr_exit_status= */ NULL);
return r;
}
/* NB: we leave executable_fd, exec_fd, handoff_timestamp_fd open here. This is safe, because they
* have O_CLOEXEC set, and the execve() below will thus automatically close them. In fact, for
* exec_fd this is pretty much the whole raison d'etre. */
r = fexecve_or_execve(executable_fd, executable, final_argv, accum_env);
/* The execve() failed, let's undo the marking as hot */
(void) exec_fd_mark_hot(context, params, /* hot= */ false, /* reterr_exit_status= */ NULL);
*exit_status = EXIT_EXEC;
return log_error_errno(r, "Failed to execute %s: %m", executable);
}