Commit Graph

48 Commits

Author SHA1 Message Date
Vasiliy Stelmachenok
9bb89a55a2 core/exec-invoke: Fix missing arguments for PR_SET_MEMORY_MERGE call
Without it prctl will always fail, even if using a kernel version that
supports the PR_SET_MEMORY_MERGE call.
2024-03-09 00:19:16 +00:00
Lennart Poettering
db7136ec73 signal-util: imply sentinel -1 in sigprocmask_many() + sigset_add_many() args list 2024-02-23 09:35:12 -08:00
Mike Yuan
210ca71cb5 core/execute: clean up log_exec_full_errno and friends
Also drop unused log_exec_struct_iovec().
2024-02-19 23:12:59 +08:00
Mike Yuan
b159680db0 core/exec-invoke: raise the log level of missing executable to notice
Suggested in
https://github.com/systemd/systemd/pull/31351#discussion_r1494450831:

> one above just informational, but also not a warning yet.
2024-02-19 23:12:59 +08:00
Mike Yuan
80b18d217a core/exec-invoke: record correct exit status when failed to locate executable
Follow-up for 4d8b0f0f7a

After the mentioned commit, when the ExecCommand executable is missing,
and failure will be ignored by manager, we exit with EXIT_SUCCESS at executor
side too. The behavior however contradicts systemd.service(5), which states:

> If the executable path is prefixed with "-", an exit code of the command
> normally considered a failure (i.e. non-zero exit status or abnormal exit
> due to signal is _recorded_, but has no further effect and is considered
> equivalent to success.

and thus makes debugging unexpected failures harder. Therefore, let's still
exit with EXIT_EXEC, but just skip LOG_ERR level log.
2024-02-19 23:12:59 +08:00
Adrian Vovk
85f660d46b fd-util: Expose helper to pack fds into 3,4,5,...
This is useful for situations where an array of FDs is to be passed into
a child process (i.e. by passing it through safe_fork). This function
can be called in the child (before calling exec) to pack the FDs to all
be next to each-other starting from SD_LISTEN_FDS_START (i.e. 3)
2024-02-19 11:18:11 +00:00
Yu Watanabe
d42b81f93f core/exec: do not crash with UtmpMode=user without User= setting
Fixes https://bugzilla.redhat.com/show_bug.cgi?id=2264404.

Replaces #31356.
2024-02-19 10:51:00 +01:00
Mike Yuan
1221ba0f6f core: introduce exec_params_need_credentials
Also rename EXEC_WRITE_CREDENTIALS to EXEC_SETUP_CREDENTIALS.
2024-02-07 00:43:33 +08:00
Lennart Poettering
a13fb98ba7 exec-invoke: extend comment on placement of apply_working_directory() call
Inspired by CVE-2024-21626, let's add a longer comment explaining why
the code really shouldn#t be moved any earlier.

Just in the hope that anyone who feels tempted to move this around maybe
actually reads the comment and reconsiders.
2024-02-06 11:13:28 +01:00
Lennart Poettering
b63e3bd308 exec-invoke: rework apply_working_directory() around chase()
let's be more careful and get rid of one more prefix_roota() use, in
favour of the safe chase().
2024-02-06 10:28:30 +01:00
Mike Yuan
d3131ea28c core/exec-invoke: don't duplicate needs_sandboxing condition 2024-02-04 16:35:16 +08:00
Yu Watanabe
e1effd2974 exec-invoke: update log message a bit 2024-02-01 17:45:47 +09:00
Yu Watanabe
41ad015205 core/exec-invoke: call pam_setcred(PAM_DELETE_CRED) after pam_close_session()
The man page pam_setcred(3) states:
> The credentials should be deleted after the session has been closed
> (with pam_close_session(3)).

Follow-up for 3bb39ea936.
2024-02-01 17:45:47 +09:00
Lennart Poettering
352ec23c7f bpf-restrict-fs: also rename functions to bpf_restrict_fs_xyz()
Rename the functions too, to make clear this is really just about the
restrict-fs, and not generic LSM_BPF code.
2024-01-25 16:11:33 +01:00
Lennart Poettering
169b56758b core: rename "bpf-lsm.[ch]" → "bpf-restrict-fs.[ch]"
This file is a bit misnamed. What it actually implements is one specific
BPF LSM module, that restricts file systems. As such it really should be
named after that, and not primarily by the mechanism it uses for that.

With this our glue code is now named the same way as the actual bpf code
files in src/core/bpf/, thus things become a bit more symmetric.

This is particular relevant as we'll soon have another BPF LSM in our
tree, see #26826, and we should be able to distinguish them by name.

This commit just renames the files and does some dumb search/replace of
the string. A follow-up commit will name some functions more expressively
inside the files.
2024-01-25 14:08:26 +01:00
Yu Watanabe
330c080eeb core/exec-invoke: drop unused pam_pid 2024-01-24 14:43:18 +09:00
Lennart Poettering
69f3c61990 tree-wide: port over various pieces of code to strv_extend_many() 2024-01-17 11:32:11 +01:00
Lennart Poettering
5863f1da42 execute: make sure Type=exec and PAMName= work together
If PAMName= is used we'll spawn a PAM session for the service, and leave
a process around that closes the PAM session eventually. That process
must close the "exec_fd" that we use to implement Type=exec. After all
the logic relies on the fact that execve() will implicitly close the
exec_fd, and the EOF seen on it is hence indication for the service
manager that execve() has worked. But if we keep an fd open in the PAM
service process, then this is not going to work.

Hence close the fd explicitly so that it definitely doesn't stay pinned
in the child.
2024-01-04 21:03:51 +08:00
Lennart Poettering
0cb110231e execute: teach RootDirectory= and RootImage= the new vpick logic 2024-01-03 18:38:46 +01:00
Yu Watanabe
36013380a9 core/executor: use log level specified in LogLevelMax=
Follow-up for cc9f4cad8c.

Otherwise, still unexpected lines may be logged by executor.
2023-12-24 06:44:03 +09:00
Mike Yuan
dd9c8da865 various: clean up isatty() handling
As per https://github.com/systemd/systemd/pull/30547#discussion_r1434371627
2023-12-22 23:06:49 +08:00
Lennart Poettering
d1a5be82ef core: imply SetLoginEnvironment= if PAMName= is set
This geneally makes sense as setting up a PAM session pretty much
defines what a login session is.

In context of #30547 this has the benefit that we can take benefit of
the SetLoginEnvironment= effect without having to set it explicitly,
thus retaining some compat of the uid0 client towards older systemd
service managers.
2023-12-21 10:14:21 +01:00
Luca Boccassi
1eeaa93de3 executor: don't duplicate FD array to avoid double closing
Just use ExecParam directly, as these are all internal to sd-exec now
anyway. Avoids double close when execution fails after FDs are set up
for inheritance and were already re-arranged.

Fixes https://github.com/systemd/systemd/issues/30412
2023-12-11 15:55:50 +00:00
Mike Yuan
c8f7c9a11d core/exec-invoke: sigwait() returns positive errno and never EINTR
Follow-up for 5b6319dcee (gosh this is
ancient), and effectively reverts 3dead8d925.

sigwait() is documented to "suspend execution of the calling thread
until one of the signals specified in the signal set becomes pending".
And the only error it returns is EINVAL, when "set contains an invalid
signal number". Therefore, there's no need to run it in a loop or
to check for runtime error.
2023-12-10 09:44:44 +01:00
Mike Yuan
c0e82e3a23 core/exec-invoke: voidify one rename_process call 2023-12-08 19:46:53 +08:00
Lennart Poettering
4482ea0c24 Merge pull request #30271 from YHNdnzj/executor-cloexec
fdset,core/executor: ocloexecification ™️
2023-12-06 22:26:40 +01:00
Lennart Poettering
6498a0c2cc user-util: add new helper fully_set_uid_gid()
Usually when we do setresuid() we also do setesgid() and setgroups().
Let's add a common helper that does all three, and use it everywhere.
2023-12-06 22:11:38 +01:00
Luca Boccassi
cc9f4cad8c executor: apply LogLevelMax earlier
SELinux logs before we have a chance to apply it, move it up as it
breaks TEST-04-JOURNAL:

[  408.578624] testsuite-04.sh[11463]: ++ journalctl -b -q -u silent-success.service
[  408.578743] testsuite-04.sh[11098]: + [[ -z Dec 03 13:38:41 H systemd-executor[11459]: SELinux enabled state cached to: disabled ]]

Follow-up for: bb5232b6a3
2023-12-04 11:45:22 +09:00
Luca Boccassi
ebaf2821e6 Merge pull request #30291 from keszybz/seccomp-unknown-syscall
Backwardscompatibly handle syscalls unknown to us or libseccomp
2023-12-02 02:04:24 +00:00
Zbigniew Jędrzejewski-Szmek
86a1ee93f3 core: fix comment 2023-12-01 19:40:26 +01:00
Luca Boccassi
f4a35f2ad9 core: do not drop CAP_SETUID if it is in AmbientCapabilities=
Follow-up for 24832d10b6
2023-12-01 10:48:14 +00:00
Mike Yuan
5a5fdfe3ac core/exec-invoke: prevent potential double-close of exec_fd
If exec_fd is closed in add_shifted_fd() by close_and_replace(),
but something goes wrong later, we may close exec_fd twice
in exec_params_shallow_clear().
2023-12-01 00:14:37 +08:00
Mike Yuan
d8da25b5d9 core/exec-invoke: rename flags_fds to flag_fds 2023-12-01 00:07:04 +08:00
Lennart Poettering
29ed1f0203 execute: make clear what reset_terminal_fd()'s 2nd param is about 2023-11-10 21:38:26 +01:00
Lennart Poettering
bb2dbe7c6a exec-invoke: only reset tty if we are told to
We have a setting for this, honour it also if we are invoked with
stdin passed in as fd.

Fixes: #29714
2023-11-10 21:38:26 +01:00
Lennart Poettering
d2b9e75524 execute: add new helper exec_context_apply_tty_size()
This combines exec_context_determine_tty_size() and
terminal_set_size_fd() since we always use one after the other.

Also make exec_context_determine_tty_size() return void, since it cannot
fail.
2023-11-10 21:38:26 +01:00
Luca Boccassi
de3612dbd7 core: fix build with -Dutmp=false 2023-11-08 18:41:47 +00:00
Iago López Galeiras
24832d10b6 core: allow using seccomp without no_new_privs when unprivileged
Until now, using any form of seccomp while being unprivileged (User=)
resulted in systemd enabling no_new_privs.

There's no need for doing this because:

* We trust the filters we apply
* If User= is set and a process wants to apply a new seccomp filter, it
will need to set no_new_privs itself

An example of application that might want seccomp + !no_new_privs is a
program that wants to run as an unprivileged user but uses file
capabilities to start a web server on a privileged port while
benefitting from a restrictive seccomp profile.

We now keep the privileges needed to do seccomp before calling
enforce_user() and drop them after the seccomp filters are applied.

If the syscall filter doesn't allow the needed syscalls to drop the
privileges, we keep the previous behavior by enabling no_new_privs.
2023-11-07 11:31:53 +01:00
Lennart Poettering
e9ccae3135 process-util: add new FORK_DEATHSIG_SIGKILL flag, rename FORK_DEATHSIG → FORK_DEATHSIG_SIGTERM
Sometimes it makes sense to hard kill a client if we die. Let's hence
add a third FORK_DEATHSIG flag for this purpose: FORK_DEATHSIG_SIGKILL.

To make things less confusing this also renames FORK_DEATHSIG to
FORK_DEATHSIG_SIGTERM to make clear it sends SIGTERM. We already had
FORK_DEATHSIG_SIGINT, hence this makes things nicely symmetric.

A bunch of users are switched over for FORK_DEATHSIG_SIGKILL where we
know it's safe to abort things abruptly. This should make some kernel
cases more robust, since we cannot get confused by signal masks or such.

While we are at it, also fix a bunch of bugs where we didn't take
FORK_DEATHSIG_SIGINT into account in safe_fork()
2023-11-02 14:09:23 +01:00
Frantisek Sumsal
6634e66ded core: fix a couple of typos 2023-10-31 18:52:00 +01:00
Luca Boccassi
7b6d3dcdd2 exec-invoke: don't double-close FDs on error
When a late error occurs in sd-executor, the cleanup-on-close of the
context structs happen, but at that time all FDs might have already
been closed via close_all_fds(), so a double-close happens. This
can be seen when DynamicUser is enabled, with a non-existing
WorkingDirectory.

Invalidate the FDs in the context structs if close_all_fds succeeds.
2023-10-28 16:56:25 +02:00
Lennart Poettering
7113640493 fd-uitl: rename PIPE_EBADF → EBADF_PAIR, and add EBADF_TRIPLET
We use it for more than just pipe() arrays. For example also for
socketpair(). Hence let's give it a generic name.

Also add EBADF_TRIPLET to mirror this for things like
stdin/stdout/stderr arrays, which we use a bunch of times.
2023-10-26 22:30:42 +02:00
Lennart Poettering
bd1ae17833 io-util: split out "struct iovec" related calls into their own .c/.h files
This is preparation for #28891, which adds a bunch more helpers around
"struct iovec", at which point this really deserves its own .c/.h file.

The idea is that we sooner or later can consider "struct iovec" as an
entirely generic mechanism to reference some binary blob, and is the
go-to type for this purpose whenever we need one.
2023-10-20 10:34:33 +02:00
Luca Boccassi
856bed0abe executor: return instead of assert on invalid command line arguments
Before the split, it made sense to assert, as checks were on setup.
But now these come from deserialization, and the fuzzer hits the
asserts, so simply return an error instead.
2023-10-20 01:00:36 +01:00
Mike Yuan
dbc0342e61 core/exec-invoke: use correct exit status
These calls can fail not only due to OOM.
2023-10-19 21:37:33 +08:00
Frantisek Sumsal
abcf59970d core: don't downgrade multi-state settings to boolean
Protect{Home,System,Proc,Subset}= are not booleans, so make sure we use
the intended value instead of just true/false.

See: https://github.com/systemd/systemd/pull/29552
Follow-up to: 79d956d
2023-10-15 14:04:28 +08:00
Mike Yuan
8d85efae91 core/exec-invoke: rename parameters of get_fixed_{user,group}
Follow-up for 1c9433559a

The user/group passed in could be either the name or the uid/gid.
2023-10-14 09:09:32 +01:00
Luca Boccassi
75689fb2d4 core: move code from execute.c to exec-invoke.c
No functional changes, only moving code that is only needed in
exec_invoke, and adding new dependencies for seccomp/selinux/apparmor/pam
in meson for the sd-executor binary.
2023-10-12 15:01:51 +01:00