Commit Graph

66 Commits

Author SHA1 Message Date
Lennart Poettering
5d1e57b820 serialize: add explicit calls for finishing serialization
These new calls will do three things:

1. in case of FILE* stuff: flush any pending bytes onto the fd, just in
   case
2. seal the backing memfd
3. seek back to the beginning.

Note that this adds sealing to serialization: once we serialized fully,
we'll seal the thing off for further modifications, before we pass the
fd over to the target process. This should add a bit of robustness, and
maybe finds a bug or two one day, if we accidentally write to a
serialization that is complete.
2024-12-17 18:26:15 +01:00
Lennart Poettering
00a415fc8f tree-wide: remove support for kernels lacking ambient caps
Let's bump the kernel baseline a bit to 4.3 and thus require ambient
caps.

This allows us to remove support for a variety of special casing, most
importantly the ExecStart=!! hack.
2024-12-17 17:34:46 +01:00
Yu Watanabe
46c26454bd exec-util: use strv_from_stdarg_alloca()
No functional change, just refactoring.
2024-12-12 08:35:16 +09:00
Yu Watanabe
f0ace1655d exec-util: use open_terminal() in fork_agent() for safety 2024-12-12 08:35:16 +09:00
Yu Watanabe
90579fd0b3 exec-util: drop handling of ENXIO in opening /dev/tty
This effectively reverts 0bcf167900.

The handling is not necessary anymore after 61242b1f0f.
2024-12-12 08:35:16 +09:00
Yu Watanabe
fc3691a70a exec-util: split out common checks before fork_agent() to can_fork_agent()
No functional change, just refactoring.
2024-12-12 08:32:42 +09:00
Yu Watanabe
0f81c8406f exec-util: allow to invoke polkit/ask-password agent even if STDIN is not a tty
Closes #35018.
2024-12-12 08:30:55 +09:00
Mike Yuan
f5dc74de2e shared/exec-util: modernize execute_strv() and friends a bit
do_spawn() is also called during execute_strv(), so rename
"direxec" to "exec-inner".
2024-10-15 01:16:57 +02:00
Mike Yuan
8e39ba3e5a shared/exec-util: minor rearrangement, drop unused EXEC_DIR_NONE 2024-10-15 01:16:57 +02:00
Lukas Nykryn
da32cac8a0 core: warn if a generator is world-writable
... because that is obviously a security risk.
2024-10-07 11:02:37 +09:00
Lennart Poettering
300b7e7620 tree-wide: use isatty_safe() more 2024-08-20 11:11:53 +02:00
Zbigniew Jędrzejewski-Szmek
df5b65e15d shared/exec-util: add macro to autoinsert sentinel for fork_agent() 2024-07-22 11:48:26 +02:00
Mauri de Souza Meneguzzo
a408d44531 shared: log error when execve fail
If there is an error with the execv call in fork_agent the
program exits without any meaningful log message. Log the
command and errno so the user gets more information about
the failure.

Fixes: #33418

Signed-off-by: Mauri de Souza Meneguzzo <mauri870@gmail.com>
2024-07-22 10:12:35 +01:00
Mike Yuan
05c754bc7f exec-util: modernize exec_command_flags_{to,from}_strv
- Rename ret params following our coding style
- Use assertion where appropriate
- Use BIT_FOREACH()
2024-07-20 09:37:07 +02:00
Lennart Poettering
a25acf70fe exec-util: make sure to close all fds for invoked generators
We should really have set O_CLOEXEC for all our fds, but better be safe
than sorry.
2024-05-29 14:43:40 +02:00
Lennart Poettering
6b90b04d3a exec-util: use the stdio array of safe_fork_full() where appropriate 2024-05-29 14:43:40 +02: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
Mike Yuan
0f9267501b exec-util: don't say sd-executor to avoid ambiguity
We have a systemd-executor binary now.
2023-11-23 11:39:07 +00: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
Zbigniew Jędrzejewski-Szmek
9ec4f7c7a4 exec-util: print executed commands in do_execute()
kernel-install uses do_execute(). We would log whenever a spawned child
finished, but we would not log anything when the child is launched. When the
children log output without a prefix (as the kernel-install plugins do), it
is hard to see where that output is coming from.
2023-09-28 12:46:22 +02:00
Yu Watanabe
8521338f95 exec-util: make execute_strv() optionally take root directory
Preparation for rewriting kernel-install in C.
2023-04-16 19:40:12 +09:00
Zbigniew Jędrzejewski-Szmek
73ed4874e9 shared/exec-util: reduce scope of iterator variables 2023-04-03 15:28:53 +02:00
Zbigniew Jędrzejewski-Szmek
7d0c47dad0 shared/exec-util: null_or_empty_path() does not return boolean
We shouldn't report that the file is empty if the stating fails. Let's do the
same as in other places, and just ignore the error and let the subsequent
operation fail.
2023-03-28 18:50:31 +02:00
Yu Watanabe
6ad9af0b13 exec-util: introduce EXEC_DIR_SKIP_REMAINING flag
Will be used in later commits.
2023-03-17 20:38:15 +09:00
Yu Watanabe
f691157b87 exec-util: extract the core logic of execute_directories() as execute_strv()
Then, we can use it with a custom enumerator of executables.

No functional change, preparation for later commits.
2023-03-17 20:38:15 +09:00
Yu Watanabe
753e38d984 exec-util: enumerate executables earlier
Then, return earlier if no executable found.
2023-03-17 20:38:15 +09:00
Yu Watanabe
fb0e5f12c6 exec-util: drop meaningless casts 2023-03-17 20:38:15 +09:00
Yu Watanabe
8e8df8292a exec-util: tighten variable scope a bit 2023-03-17 20:38:15 +09:00
Yu Watanabe
911f8f0183 process-util: rename FORK_NULL_STDIO -> FORK_REARRANGE_STDIO
And make safe_fork_full() takes fds to be assigned to stdio.
2023-02-21 07:37:51 +09:00
Yu Watanabe
ced30d69fa exec-util: use TAKE_FD() 2023-02-19 12:17:52 +09:00
Yu Watanabe
65cd9c7251 exec-util: propagate error in wait_for_terminate_and_check()
Then, the two error handlings becomes consistent with the one in
execute_directories().
2023-02-19 12:17:52 +09:00
Yu Watanabe
f3f2d02e97 tree-wide: set FORK_RLIMIT_NOFILE_SAFE flag
No functional changes, just refactoring.
2023-02-07 14:39:49 +09:00
Lennart Poettering
03469b770b shared: port various shared helpers basename() → path_extract_filename() 2022-12-23 17:35:21 +01:00
Zbigniew Jędrzejewski-Szmek
254d1313ae tree-wide: use -EBADF for fd initialization
-1 was used everywhere, but -EBADF or -EBADFD started being used in various
places. Let's make things consistent in the new style.

Note that there are two candidates:
EBADF 9 Bad file descriptor
EBADFD 77 File descriptor in bad state

Since we're initializating the fd, we're just assigning a value that means
"no fd yet", so it's just a bad file descriptor, and the first errno fits
better. If instead we had a valid file descriptor that became invalid because
of some operation or state change, the other errno would fit better.

In some places, initialization is dropped if unnecessary.
2022-12-19 15:00:57 +01:00
Zbigniew Jędrzejewski-Szmek
3ae6b3bf72 basic: rename util.h to logarithm.h
util.h is now about logarithms only, so we can rename it. Many files included
util.h for no apparent reason… Those includes are dropped.
2022-11-08 18:21:10 +01:00
David Tardon
995340074e tree-wide: use ASSERT_PTR more 2022-09-13 08:13:27 +02:00
Yu Watanabe
de010b0b2e strv: make iterator in STRV_FOREACH() declaread in the loop
This also avoids multiple evaluations in STRV_FOREACH_BACKWARDS()
2022-03-19 08:33:33 +09:00
Lennart Poettering
69339ae9f7 tree-wide: some additional checks to avoid CVE-2021-4034 style weaknesses 2022-01-31 23:07:19 +00:00
Lennart Poettering
aedec452b9 tree-wide: always use TAKE_FD() when calling rearrange_stdio()
rearrange_stdio() invalidates specified fds even on failure, which means
we should always invalidate the fds we pass in no matter what. Let's
make this explicit by using TAKE_FD() for that everywhere.

Note that in many places we such invalidation doesnt get us much
behaviour-wise, since we don't use the variables anymore later. But
TAKE_FD() in a way is also documentation, it encodes explicitly that the
fds are invalidated here, so I think it's a good thing to always make
this explicit here.
2021-11-03 23:05:26 +00:00
Lennart Poettering
c85cb3bc7f Revert "basic/fd-util: sort the 'except' array in place"
This reverts commit 9c46228b7d.
2021-10-27 17:56:36 +02:00
Lennart Poettering
8ddefb8eef basic: move freeze() from shared/exec-util.h to basic/process-util.h
That way we can use it in other code from basic/. It fits into both
headers equally well or badly, hence let's just move this one function.
2021-10-22 16:34:30 +02:00
Frantisek Sumsal
b98416e100 tree-wide: assorted Coccinelle fixes
It's that time of year again.
2021-10-08 15:03:27 +02:00
Lennart Poettering
0bcf167900 exec-util: handle gracefully if we want to fork an agent but have no controlling tty
Fixes: #20576
2021-08-30 13:37:06 +02:00
Zbigniew Jędrzejewski-Szmek
cbcf371abc Add variant of close_all_fds() that does not allocate and use it in freeze()
Even though it's just a fallback path, let's not be sloppy and allocate in
the crash handler.

> The deadlock happens because systemd crash in malloc() then in signal
> handler, it calls malloc() (close_all_fds()-> opendir()-> __alloc_dir())
> again. malloc() is not a signal-safe function, maybe we should re-think
> the logic here.

Fixes #20266.
2021-07-23 11:39:45 +02:00
Zbigniew Jędrzejewski-Szmek
45a68ed307 Move freeze() into shared/
Library code should not call freeze(), this is something that should
only be done by "application code", so moving it into shared/ is appropriate.

The fallback to call _exit() is dropped: let's trust that the infinite loop
is infinite.
2021-07-23 11:39:45 +02:00
Zbigniew Jędrzejewski-Szmek
3e24e8cd64 Move fork_agent() into shared/
Currently it's only used in two places in src/shared/, so the function was
already included just once in compiled code. But it seems appropriate to
move it there anyway, because library code should have no need to fork
agents, so it doesn't belong in basic/.
2021-07-23 11:39:45 +02:00
Zbigniew Jędrzejewski-Szmek
4c1ff29052 Merge pull request #18641 from benjarobin/fix-enum-invalid-val
Various follow-up: Fix build and EINVAL for _INVALID enum value
2021-02-17 08:46:09 +01:00
Benjamin Robin
9de42a6a5f shared: use -EINVAL for _EXEC_COMMAND_FLAGS_INVALID
Follow-up of #11484
2021-02-16 23:34:58 +01:00
Zbigniew Jędrzejewski-Szmek
b230baaeb7 shared/exec-util: fix minor memleak
p was not freed on error.
2021-02-16 16:10:14 +01:00
Yu Watanabe
43f565c677 core: also set $SYSTEMD_EXEC_PID= for generators 2021-02-01 01:13:44 +09:00