Commit Graph
33 Commits
Author SHA1 Message Date
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