HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/core/exec-invoke.c
Git blob: ff107cc753cf314807216d8e1747fc30661a6ef4
Lines 1–1000 of 6,799
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <grp.h>
#include <linux/securebits.h>
#include <poll.h>
#include <sched.h>
#include <sys/eventfd.h>
#include <sys/ioctl.h>
#include <sys/ioprio.h>
#include <sys/keyctl.h>
#include <sys/mount.h>
#include <sys/prctl.h> /* IWYU pragma: keep */
#include <sys/statvfs.h>
#include <unistd.h>
#include "sd-messages.h"
#include "sd-varlink.h"
#include "apparmor-util.h" /* IWYU pragma: keep */
#include "argv-util.h"
#include "ask-password-api.h"
#include "barrier.h"
#include "bitfield.h"
#include "bpf-util.h"
#include "bpf-restrict-fs.h"
#include "btrfs-util.h"
#include "capability-util.h"
#include "cgroup-setup.h"
#include "cgroup.h"
#include "chase.h"
#include "chattr-util.h"
#include "chown-recursive.h"
#include "constants.h"
#include "copy.h"
#include "coredump-util.h"
#include "crypto-util.h"
#include "cryptsetup-util.h"
#include "dissect-image.h"
#include "dynamic-user.h"
#include "env-util.h"
#include "escape.h"
#include "exec-credential.h"
#include "exec-invoke.h"
#include "execute.h"
#include "exit-status.h"
#include "fd-util.h"
#include "fs-util.h"
#include "hexdecoct.h"
#include "hostname-setup.h"
#include "image-policy.h"
#include "io-util.h"
#include "iovec-util.h"
#include "journal-send.h"
#include "libmount-util.h"
#include "manager.h"
#include "memfd-util.h"
#include "mkdir.h"
#include "mount-util.h"
#include "mountpoint-util.h"
#include "mstack.h"
#include "namespace-util.h"
#include "nsflags.h"
#include "nsresource.h"
#include "open-file.h"
#include "osc-context.h"
#include "pam-util.h"
#include "path-util.h"
#include "pidfd-util.h"
#include "pidref.h"
#include "proc-cmdline.h"
#include "process-util.h"
#include "psi-util.h"
#include "quota-util.h"
#include "random-util.h"
#include "rlimit-util.h"
#include "seccomp-util.h"
#include "selinux-util.h"
#include "set.h"
#include "signal-util.h"
#include "siphash24.h"
#include "smack-util.h"
#include "socket-util.h"
#include "stat-util.h"
#include "string-table.h"
#include "strv.h"
#include "strxcpyx.h"
#include "terminal-util.h"
#include "uid-range.h"
#include "user-util.h"
#include "utmp-wtmp.h"
#include "vpick.h"
#define IDLE_TIMEOUT_USEC (5*USEC_PER_SEC)
#define IDLE_TIMEOUT2_USEC (1*USEC_PER_SEC)
#define SNDBUF_SIZE (8*1024*1024)
/* Project id range for disk quotas */
#define PROJ_ID_MIN UINT32_C(2147483648)
#define PROJ_ID_MAX UINT32_C(4294967294)
#define PROJ_ID_CLAMP_INTO_QUOTA_RANGE(id) ((uint32_t) ((id) % (PROJ_ID_MAX - PROJ_ID_MIN + 1)) + PROJ_ID_MIN)
static int flag_fds(
const int fds[],
size_t n_socket_fds,
size_t n_fds,
bool nonblock) {
int r;
assert(fds || n_fds == 0);
/* Drops/Sets O_NONBLOCK and FD_CLOEXEC from the file flags.
* O_NONBLOCK only applies to socket activation though. */
for (size_t i = 0; i < n_fds; i++) {
if (i < n_socket_fds) {
r = fd_nonblock(fds[i], nonblock);
if (r < 0)
return r;
}
/* We unconditionally drop FD_CLOEXEC from the fds,
* since after all we want to pass these fds to our
* children */
r = fd_cloexec(fds[i], false);
if (r < 0)
return r;
}
return 0;
}
static int open_null_as(int flags, int nfd) {
int fd;
assert(nfd >= 0);
fd = open("/dev/null", flags|O_NOCTTY);
if (fd < 0)
return -errno;
return move_fd(fd, nfd, false);
}
static int connect_journal_socket(
int fd,
const char *log_namespace,
uid_t uid,
gid_t gid) {
uid_t olduid = UID_INVALID;
gid_t oldgid = GID_INVALID;
const char *j;
int r;
assert(fd >= 0);
j = journal_stream_path(log_namespace);
if (!j)
return -EINVAL;
if (gid_is_valid(gid)) {
oldgid = getgid();
if (setegid(gid) < 0)
return -errno;
}
if (uid_is_valid(uid)) {
olduid = getuid();
if (seteuid(uid) < 0) {
r = -errno;
goto restore_gid;
}
}
r = connect_unix_path(fd, AT_FDCWD, j);
/* If we fail to restore the uid or gid, things will likely fail later on. This should only happen if
an LSM interferes. */
if (uid_is_valid(uid))
(void) seteuid(olduid);
restore_gid:
if (gid_is_valid(gid))
(void) setegid(oldgid);
return r;
}
static bool exec_output_forward_to_console(ExecOutput o) {
return IN_SET(o,
EXEC_OUTPUT_JOURNAL_AND_CONSOLE,
EXEC_OUTPUT_KMSG_AND_CONSOLE);
}
static bool exec_output_forward_to_kmsg(ExecOutput o) {
return IN_SET(o,
EXEC_OUTPUT_KMSG,
EXEC_OUTPUT_KMSG_AND_CONSOLE);
}
static int connect_logger_as(
const ExecContext *context,
const ExecParameters *params,
ExecOutput output,
const char *ident,
int nfd,
uid_t uid,
gid_t gid) {
_cleanup_close_ int fd = -EBADF;
int r;
assert(context);
assert(params);
assert(output < _EXEC_OUTPUT_MAX);
assert(ident);
assert(nfd >= 0);
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0)
return -errno;
r = connect_journal_socket(fd, context->log_namespace, uid, gid);
if (r < 0)
return r;
if (shutdown(fd, SHUT_RD) < 0)
return -errno;
(void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
if (dprintf(fd,
"%s\n"
"%s\n"
"%i\n"
"%i\n"
"%i\n"
"%i\n"
"%i\n",
context->syslog_identifier ?: ident,
params->flags & EXEC_PASS_LOG_UNIT ? params->unit_id : "",
context->syslog_priority,
!!context->syslog_level_prefix,
false,
exec_output_forward_to_kmsg(output),
exec_output_forward_to_console(output)) < 0)
return -errno;
return move_fd(TAKE_FD(fd), nfd, false);
}
static int open_terminal_as(const char *path, int flags, int nfd) {
int fd;
assert(path);
assert(nfd >= 0);
fd = open_terminal(path, flags | O_NOCTTY);
if (fd < 0)
return fd;
return move_fd(fd, nfd, false);
}
static int acquire_path(const char *path, int flags, mode_t mode) {
_cleanup_close_ int fd = -EBADF;
int r;
assert(path);
if (IN_SET(flags & O_ACCMODE_STRICT, O_WRONLY, O_RDWR))
flags |= O_CREAT;
fd = open(path, flags|O_NOCTTY, mode);
if (fd >= 0)
return TAKE_FD(fd);
if (errno != ENXIO) /* ENXIO is returned when we try to open() an AF_UNIX file system socket on Linux */
return -errno;
/* So, it appears the specified path could be an AF_UNIX socket. Let's see if we can connect to it. */
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0)
return -errno;
r = connect_unix_path(fd, AT_FDCWD, path);
if (IN_SET(r, -ENOTSOCK, -EINVAL))
/* Propagate initial error if we get ENOTSOCK or EINVAL, i.e. we have indication that this
* wasn't an AF_UNIX socket after all */
return -ENXIO;
if (r < 0)
return r;
if ((flags & O_ACCMODE_STRICT) == O_RDONLY)
r = shutdown(fd, SHUT_WR);
else if ((flags & O_ACCMODE_STRICT) == O_WRONLY)
r = shutdown(fd, SHUT_RD);
else
r = 0;
if (r < 0)
return -errno;
return TAKE_FD(fd);
}
static int fixup_input(
const ExecContext *context,
bool apply_tty_stdin) {
ExecInput std_input;
assert(context);
std_input = context->std_input;
if (exec_input_is_terminal(std_input) && !apply_tty_stdin)
return EXEC_INPUT_NULL;
if (std_input == EXEC_INPUT_DATA && context->stdin_data_size == 0)
return EXEC_INPUT_NULL;
return std_input;
}
static int setup_input(
const ExecContext *context,
const ExecParameters *params,
int socket_fd,
const int named_iofds[static 3]) {
ExecInput i;
int r;
assert(context);
assert(params);
assert(named_iofds);
if (params->stdin_fd >= 0) {
if (dup2(params->stdin_fd, STDIN_FILENO) < 0)
return -errno;
/* Try to make this our controlling tty, if it is a tty */
if (isatty_safe(STDIN_FILENO) && ioctl(STDIN_FILENO, TIOCSCTTY, context->std_input == EXEC_INPUT_TTY_FORCE) < 0)
log_debug_errno(errno, "Failed to make standard input TTY our controlling terminal: %m");
return STDIN_FILENO;
}
i = fixup_input(context, params->flags & EXEC_APPLY_TTY_STDIN);
switch (i) {
case EXEC_INPUT_NULL:
return open_null_as(O_RDONLY, STDIN_FILENO);
case EXEC_INPUT_TTY:
case EXEC_INPUT_TTY_FORCE:
case EXEC_INPUT_TTY_FAIL: {
_cleanup_close_ int tty_fd = -EBADF;
_cleanup_free_ char *resolved = NULL;
const char *tty_path;
tty_path = ASSERT_PTR(exec_context_tty_path(context));
if (tty_is_console(tty_path)) {
r = resolve_dev_console(&resolved);
if (r < 0)
log_debug_errno(r, "Failed to resolve /dev/console, ignoring: %m");
else {
log_debug("Resolved /dev/console to %s", resolved);
tty_path = resolved;
}
}
tty_fd = acquire_terminal(tty_path,
i == EXEC_INPUT_TTY_FAIL ? ACQUIRE_TERMINAL_TRY :
i == EXEC_INPUT_TTY_FORCE ? ACQUIRE_TERMINAL_FORCE :
ACQUIRE_TERMINAL_WAIT,
USEC_INFINITY);
if (tty_fd < 0)
return tty_fd;
r = move_fd(tty_fd, STDIN_FILENO, /* cloexec= */ false);
if (r < 0)
return r;
TAKE_FD(tty_fd);
return r;
}
case EXEC_INPUT_SOCKET:
assert(socket_fd >= 0);
return RET_NERRNO(dup2(socket_fd, STDIN_FILENO));
case EXEC_INPUT_NAMED_FD:
assert(named_iofds[STDIN_FILENO] >= 0);
(void) fd_nonblock(named_iofds[STDIN_FILENO], false);
return RET_NERRNO(dup2(named_iofds[STDIN_FILENO], STDIN_FILENO));
case EXEC_INPUT_DATA: {
int fd;
fd = memfd_new_and_seal("exec-input", context->stdin_data, context->stdin_data_size);
if (fd < 0)
return fd;
return move_fd(fd, STDIN_FILENO, false);
}
case EXEC_INPUT_FILE: {
bool rw;
int fd;
assert(context->stdio_file[STDIN_FILENO]);
rw = (context->std_output == EXEC_OUTPUT_FILE && path_equal(context->stdio_file[STDIN_FILENO], context->stdio_file[STDOUT_FILENO])) ||
(context->std_error == EXEC_OUTPUT_FILE && path_equal(context->stdio_file[STDIN_FILENO], context->stdio_file[STDERR_FILENO]));
fd = acquire_path(context->stdio_file[STDIN_FILENO], rw ? O_RDWR : O_RDONLY, 0666 & ~context->umask);
if (fd < 0)
return fd;
return move_fd(fd, STDIN_FILENO, false);
}
default:
assert_not_reached();
}
}
static bool can_inherit_stderr_from_stdout(const ExecContext *context) {
ExecOutput o, e;
assert(context);
/* Returns true, if given the specified STDERR and STDOUT output we can directly dup() the stdout fd to the
* stderr fd */
o = context->std_output;
e = context->std_error;
if (e == EXEC_OUTPUT_INHERIT)
return true;
if (e != o)
return false;
/* Let's not shortcut named fds here, even though we in theory can by comparing fd names, since
* we have the named_iofds array readily available, and the inherit practice would simply be duplicative. */
if (e == EXEC_OUTPUT_NAMED_FD)
return false;
if (IN_SET(e, EXEC_OUTPUT_FILE, EXEC_OUTPUT_FILE_APPEND, EXEC_OUTPUT_FILE_TRUNCATE))
return path_equal(context->stdio_file[STDOUT_FILENO], context->stdio_file[STDERR_FILENO]);
return true;
}
static int maybe_inherit_stdout_from_stdin(const ExecContext *context, ExecInput i) {
int r;
assert(context);
if (context->std_output != EXEC_OUTPUT_INHERIT)
return 0;
/* If input got downgraded, inherit the original value */
if (i == EXEC_INPUT_NULL && exec_input_is_terminal(context->std_input))
return open_terminal_as(exec_context_tty_path(context), O_WRONLY, STDOUT_FILENO);
if (!exec_input_is_inheritable(i))
goto fallback;
r = fd_is_writable(STDIN_FILENO);
if (r <= 0) {
if (r < 0)
log_warning_errno(r, "Failed to check if inherited stdin is writable for stdout, using fallback: %m");
else
log_warning("Inherited stdin is not writable for stdout, using fallback: %m");
goto fallback;
}
return RET_NERRNO(dup2(STDIN_FILENO, STDOUT_FILENO));
fallback:
/* If we are not started from PID 1 we just inherit STDOUT from our parent process. */
if (getppid() != 1)
return STDOUT_FILENO;
/* We need to open /dev/null here anew, to get the right access mode. */
return open_null_as(O_WRONLY, STDOUT_FILENO);
}
static int setup_output(
const ExecContext *context,
const ExecParameters *params,
int fileno,
int socket_fd,
const int named_iofds[static 3],
const char *ident,
uid_t uid,
gid_t gid,
dev_t *journal_stream_dev,
ino_t *journal_stream_ino) {
ExecOutput o;
ExecInput i;
int r;
assert(context);
assert(params);
assert(named_iofds);
assert(ident);
assert(journal_stream_dev);
assert(journal_stream_ino);
if (fileno == STDOUT_FILENO && params->stdout_fd >= 0) {
if (dup2(params->stdout_fd, STDOUT_FILENO) < 0)
return -errno;
return STDOUT_FILENO;
}
if (fileno == STDERR_FILENO && params->stderr_fd >= 0) {
if (dup2(params->stderr_fd, STDERR_FILENO) < 0)
return -errno;
return STDERR_FILENO;
}
i = fixup_input(context, params->flags & EXEC_APPLY_TTY_STDIN);
if (fileno == STDERR_FILENO) {
/* This expects the input and output are already set up */
/* Don't change the stderr file descriptor if we inherit all
* the way and are not on a tty */
if (context->std_error == EXEC_OUTPUT_INHERIT &&
context->std_output == EXEC_OUTPUT_INHERIT &&
i == EXEC_INPUT_NULL && !exec_input_is_terminal(context->std_input) &&
getppid() != 1)
return fileno;
/* Duplicate from stdout if possible */
if (can_inherit_stderr_from_stdout(context))
return RET_NERRNO(dup2(STDOUT_FILENO, fileno));
o = context->std_error;
} else {
assert(fileno == STDOUT_FILENO);
r = maybe_inherit_stdout_from_stdin(context, i);
if (r != 0)
return r;
o = context->std_output;
}
switch (o) {
case EXEC_OUTPUT_NULL:
return open_null_as(O_WRONLY, fileno);
case EXEC_OUTPUT_TTY:
if (exec_input_is_terminal(i)) {
r = fd_is_writable(STDIN_FILENO);
if (r <= 0) {
if (r < 0)
log_warning_errno(r, "Failed to check if inherited stdin is writable for TTY's %s, falling back to opening terminal.",
fileno == STDOUT_FILENO ? "stdout" : "stderr");
else
log_warning("Inherited stdin is not writable for TTY's %s, falling back to opening terminal.",
fileno == STDOUT_FILENO ? "stdout" : "stderr");
return open_terminal_as(exec_context_tty_path(context), O_WRONLY, fileno);
}
return RET_NERRNO(dup2(STDIN_FILENO, fileno));
}
return open_terminal_as(exec_context_tty_path(context), O_WRONLY, fileno);
case EXEC_OUTPUT_KMSG:
case EXEC_OUTPUT_KMSG_AND_CONSOLE:
case EXEC_OUTPUT_JOURNAL:
case EXEC_OUTPUT_JOURNAL_AND_CONSOLE:
r = connect_logger_as(context, params, o, ident, fileno, uid, gid);
if (r < 0) {
log_warning_errno(r, "Failed to connect %s to the journal socket, ignoring: %m",
fileno == STDOUT_FILENO ? "stdout" : "stderr");
r = open_null_as(O_WRONLY, fileno);
} else {
struct stat st;
/* If we connected this fd to the journal via a stream, patch the device/inode into the passed
* parameters, but only then. This is useful so that we can set $JOURNAL_STREAM that permits
* services to detect whether they are connected to the journal or not.
*
* If both stdout and stderr are connected to a stream then let's make sure to store the data
* about STDERR as that's usually the best way to do logging. */
if (fstat(fileno, &st) >= 0 &&
(*journal_stream_ino == 0 || fileno == STDERR_FILENO)) {
*journal_stream_dev = st.st_dev;
*journal_stream_ino = st.st_ino;
}
}
return r;
case EXEC_OUTPUT_SOCKET:
assert(socket_fd >= 0);
return RET_NERRNO(dup2(socket_fd, fileno));
case EXEC_OUTPUT_NAMED_FD:
assert(named_iofds[fileno] >= 0);
(void) fd_nonblock(named_iofds[fileno], false);
return RET_NERRNO(dup2(named_iofds[fileno], fileno));
case EXEC_OUTPUT_FILE:
case EXEC_OUTPUT_FILE_APPEND:
case EXEC_OUTPUT_FILE_TRUNCATE: {
int fd, flags;
assert(context->stdio_file[fileno]);
/* stdin points to the same file hence setup_input() opened it as rw already?
* Then just duplicate it. */
if (context->std_input == EXEC_INPUT_FILE &&
path_equal(context->stdio_file[fileno], context->stdio_file[STDIN_FILENO]))
return RET_NERRNO(dup2(STDIN_FILENO, fileno));
flags = O_WRONLY;
if (o == EXEC_OUTPUT_FILE_APPEND)
flags |= O_APPEND;
else if (o == EXEC_OUTPUT_FILE_TRUNCATE)
flags |= O_TRUNC;
fd = acquire_path(context->stdio_file[fileno], flags, 0666 & ~context->umask);
if (fd < 0)
return fd;
return move_fd(fd, fileno, 0);
}
default:
assert_not_reached();
}
}
static int chown_terminal(int fd, uid_t uid) {
int r;
assert(fd >= 0);
/* Before we chown/chmod the TTY, let's ensure this is actually a tty */
if (!isatty_safe(fd))
return 0;
/* This might fail. What matters are the results. */
r = fchmod_and_chown(fd, TTY_MODE, uid, GID_INVALID);
if (r < 0)
return r;
return 1;
}
static int setup_confirm_stdio(
const ExecContext *context,
const char *vc,
int *ret_saved_stdin,
int *ret_saved_stdout) {
_cleanup_close_ int fd = -EBADF, saved_stdin = -EBADF, saved_stdout = -EBADF;
int r;
assert(context);
assert(vc);
assert(ret_saved_stdin);
assert(ret_saved_stdout);
saved_stdin = fcntl(STDIN_FILENO, F_DUPFD_CLOEXEC, 3);
if (saved_stdin < 0)
return -errno;
saved_stdout = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 3);
if (saved_stdout < 0)
return -errno;
fd = acquire_terminal(vc, ACQUIRE_TERMINAL_WAIT, DEFAULT_CONFIRM_USEC);
if (fd < 0)
return fd;
_cleanup_close_ int lock_fd = lock_dev_console();
if (lock_fd < 0)
log_debug_errno(lock_fd, "Failed to lock /dev/console, ignoring: %m");
r = chown_terminal(fd, getuid());
if (r < 0)
return r;
r = terminal_reset_defensive(fd, TERMINAL_RESET_SWITCH_TO_TEXT);
if (r < 0)
return r;
r = exec_context_apply_tty_size(context, fd, fd, vc);
if (r < 0)
return r;
r = rearrange_stdio(fd, fd, STDERR_FILENO); /* Invalidates 'fd' also on failure */
TAKE_FD(fd);
if (r < 0)
return r;
*ret_saved_stdin = TAKE_FD(saved_stdin);
*ret_saved_stdout = TAKE_FD(saved_stdout);
return 0;
}
static void write_confirm_error_fd(int err, int fd, const char *unit_id) {
assert(err != 0);
assert(fd >= 0);
assert(unit_id);
errno = abs(err);
if (errno == ETIMEDOUT)
dprintf(fd, "Confirmation question timed out for %s, assuming positive response.\n", unit_id);
else
dprintf(fd, "Couldn't ask confirmation for %s, assuming positive response: %m\n", unit_id);
}
static void write_confirm_error(int err, const char *vc, const char *unit_id) {
_cleanup_close_ int fd = -EBADF;
assert(vc);
assert(unit_id);
fd = open_terminal(vc, O_WRONLY|O_NOCTTY|O_CLOEXEC);
if (fd < 0)
return;
write_confirm_error_fd(err, fd, unit_id);
}
static int restore_confirm_stdio(int *saved_stdin, int *saved_stdout) {
int r = 0;
assert(saved_stdin);
assert(saved_stdout);
release_terminal();
if (*saved_stdin >= 0)
if (dup2(*saved_stdin, STDIN_FILENO) < 0)
r = -errno;
if (*saved_stdout >= 0)
if (dup2(*saved_stdout, STDOUT_FILENO) < 0)
r = -errno;
*saved_stdin = safe_close(*saved_stdin);
*saved_stdout = safe_close(*saved_stdout);
return r;
}
enum {
CONFIRM_PRETEND_FAILURE = -1,
CONFIRM_PRETEND_SUCCESS = 0,
CONFIRM_EXECUTE = 1,
};
static bool confirm_spawn_disabled(void) {
return access("/run/systemd/confirm_spawn_disabled", F_OK) >= 0;
}
static int ask_for_confirmation(const ExecContext *context, const ExecParameters *params, const char *cmdline) {
int saved_stdout = -EBADF, saved_stdin = -EBADF, r;
_cleanup_free_ char *e = NULL;
char c;
assert(context);
assert(params);
assert(cmdline);
/* For any internal errors, assume a positive response. */
r = setup_confirm_stdio(context, params->confirm_spawn, &saved_stdin, &saved_stdout);
if (r < 0) {
write_confirm_error(r, params->confirm_spawn, params->unit_id);
return CONFIRM_EXECUTE;
}
/* confirm_spawn might have been disabled while we were sleeping. */
if (!params->confirm_spawn || confirm_spawn_disabled()) {
r = 1;
goto restore_stdio;
}
e = ellipsize(cmdline, 60, 100);
if (!e) {
log_oom();
r = CONFIRM_EXECUTE;
goto restore_stdio;
}
for (;;) {
r = ask_char(&c, "yfshiDjcn", "Execute %s? [y, f, s – h for help] ", e);
if (r < 0) {
write_confirm_error_fd(r, STDOUT_FILENO, params->unit_id);
r = CONFIRM_EXECUTE;
goto restore_stdio;
}
switch (c) {
case 'c':
printf("Resuming normal execution.\n");
manager_disable_confirm_spawn();
r = 1;
break;
case 'D':
printf(" Unit: %s\n",
params->unit_id);
exec_context_dump(context, stdout, " ");
exec_params_dump(params, stdout, " ");
continue; /* ask again */
case 'f':
printf("Failing execution.\n");
r = CONFIRM_PRETEND_FAILURE;
break;
case 'h':
printf(" c - continue, proceed without asking anymore\n"
" D - dump, show the state of the unit\n"
" f - fail, don't execute the command and pretend it failed\n"
" h - help\n"
" i - info, show a short summary of the unit\n"
" j - jobs, show jobs that are in progress\n"
" s - skip, don't execute the command and pretend it succeeded\n"
" y - yes, execute the command\n");
continue; /* ask again */
case 'i':
printf(" Unit: %s\n"
" Command: %s\n",
params->unit_id, cmdline);
continue; /* ask again */
case 'j':
if (sigqueue(getppid(),
SIGRTMIN+18,
(const union sigval) { .sival_int = MANAGER_SIGNAL_COMMAND_DUMP_JOBS }) < 0)
return -errno;
continue; /* ask again */
case 'n':
/* 'n' was removed in favor of 'f'. */
printf("Didn't understand 'n', did you mean 'f'?\n");
continue; /* ask again */
case 's':
printf("Skipping execution.\n");
r = CONFIRM_PRETEND_SUCCESS;
break;
case 'y':
r = CONFIRM_EXECUTE;
break;
default:
assert_not_reached();
}
break;
}
restore_stdio:
restore_confirm_stdio(&saved_stdin, &saved_stdout);
return r;
}
static int get_supplementary_groups(
const ExecContext *c,
const char *user,
gid_t gid,
gid_t **ret_gids) {
int r;
assert(c);
assert(ret_gids);
/*
* If user is given, then look up GID and supplementary groups list.
* We avoid NSS lookups for gid=0. Also we have to initialize groups
* here and as early as possible so we keep the list of supplementary
* groups of the caller.
*/
bool keep_groups = false;
if (user && gid_is_valid(gid) && gid != 0) {
/* First step, initialize groups from /etc/groups */
r = initgroups_wrapper(user, gid);
if (r < 0) {
/* If our primary gid is already the one specified in Group= (i.e. we're running in
* user mode), gracefully handle the case where we have no privilege to re-initgroups().
*
* Note that group memberships of the current user might have been modified, but
* the change will only take effect after re-login. It's better to continue on with
* existing credentials rather than erroring out. */
if (!ERRNO_IS_PRIVILEGE(r) || gid != getgid())
return r;
}
keep_groups = true;
}
if (strv_isempty(c->supplementary_groups)) {
*ret_gids = NULL;
return 0;
}
/* If SupplementaryGroups= was passed then NGROUPS_MAX has to be positive, otherwise fail. */
_cleanup_free_ gid_t *l_gids = NULL;
int k = 0;
if (keep_groups) {
/* Look up the list of groups that the user belongs to.
* We avoid NSS lookups here too for gid=0. */
k = getgrouplist_malloc(user, gid, &l_gids);
if (k < 0)
return k;
}
int ngroups_max = sysconf_ngroups_max();
if (ngroups_max < 0)
return ngroups_max;
STRV_FOREACH(i, c->supplementary_groups) {
if (k >= ngroups_max)
return -E2BIG;
if (!GREEDY_REALLOC(l_gids, k + 1))
return -ENOMEM;
r = get_group_creds(*i, /* flags= */ 0, /* ret_name= */ NULL, l_gids + k);
if (r < 0)
return r;
k++;
}
if (k == 0) {
*ret_gids = NULL;
return 0;
}
/* We *could* trim the array size with realloc(3), but right now the only caller frees the array
* quickly anyway, so this is not worth the trouble. If other users pop up, this should be
* reconsidered. */
*ret_gids = TAKE_PTR(l_gids);
return k;
}
static int enforce_groups(gid_t gid, const gid_t *supplementary_gids, int ngids) {
int r;
assert(supplementary_gids || ngids == 0);
/* Handle SupplementaryGroups= if it is not empty */
if (ngids > 0) {
r = maybe_setgroups(ngids, supplementary_gids);
if (r < 0)
return r;
}
if (gid_is_valid(gid)) {
/* Then set our gids */
if (setresgid(gid, gid, gid) < 0)
return -errno;
}
return 0;
}
static int set_securebits(unsigned bits, unsigned mask) {
unsigned applied;
int current, r;
current = prctl_safe(PR_GET_SECUREBITS, 0, 0, 0, 0);
if (current < 0)
return current;
/* Clear all securebits defined in mask and set bits */
applied = ((unsigned) current & ~mask) | bits;
if ((unsigned) current == applied)