Commit Graph

1036 Commits

Author SHA1 Message Date
Lennart Poettering
d9c5566c0f machined: make image locking runtime scope aware, too
We cannot create an image lock in /run if we are unpriv, hence create it
in $XDG_RUNTIME_DIR instead.
2025-09-25 22:43:59 +02:00
Lennart Poettering
1966baf2d6 machinectl: add support for user-scoped operation 2025-09-25 22:43:59 +02:00
Lennart Poettering
f5fa86f9f8 machined: allow running in --user mode 2025-09-25 22:43:59 +02:00
Lennart Poettering
e1b3319b6c discover-image: support runtime scope also for .nspawn settings files and the pool dir
discover-image.[ch] largely already supports per-scope operations, let's
extend this however to also cover finding .nspawn settings files and
managing the pool dir.
2025-09-25 22:43:59 +02:00
Lennart Poettering
b7f6bdbbd3 service-util: add generic parser for runtime scope 2025-09-25 22:43:59 +02:00
Luca Boccassi
c5e48e3a66 machined: do not allow unprivileged users to shell into the root namespace
We intend to make self-registering machines an unprivileged operation,
but currently that would allow an unprivileged user to register a
process they own in the root namespace, and then login as any
user they like, including root, which is not ideal.

Forbid non-root from shelling into a machine that is running in
the root user namespace.
2025-09-25 22:13:05 +02:00
Luca Boccassi
d80af3b97b machined: add PIDFD D-Bus variants for registering/creating machines
Current methods take a numeric PID, but we know that is unreliable for
the usual reasons. Add variants that take a PIDFD instead, or a
PID + PIDFDID combination for remote users.
2025-09-24 14:11:59 +01:00
Yu Watanabe
43cea09f95 machine: fix crash on update from older than v258
UID entry in the machine state file is introduced in v258,
hence when a host is upgraded to v258, the field does not exist in the
file, thus the variable 'uid' is NULL.

Follow-up for 276d200186.
Fixes #39061.
2025-09-22 04:53:57 +09:00
Daan De Meyer
9adb4685df tree-wide: Remove unused includes 2025-09-19 14:46:55 +02:00
Luca Boccassi
8324f9351c machine: add a comment to clarify that root_directory is informational only
To avoid any possible mistakes in the future, add a comment in the
object declaration
2025-09-16 15:58:28 +01:00
Luca Boccassi
44e3c4c8bc machine: validate root directory over varlink
Use strict validation to reject invalid directories as the D-Bus
API already does

Follow-up for 5b44c81ff8
2025-09-16 15:58:28 +01:00
Luca Boccassi
119d332d9c machine: do not allow unprivileged users to register other users' processes as machines
Registering a process as a machine means a caller can get machined
to send sigterm to it, and more. If an unpriv user is registering,
ensure the registered process is actually owned by the user.

Follow-up for adaff8eb35
2025-09-16 15:58:28 +01:00
Luca Boccassi
65badde82e machine: restrict register-machine action again
Follow-up for adaff8eb35
2025-09-05 19:29:09 +01:00
Yu Watanabe
cace9cf6f6 machinectl: set TERM=dumb when running on a dumb terminal or with a pipe
Fixes #38527.
2025-08-13 23:43:51 +09:00
Yu Watanabe
7cd26f3560 ptyfwd: replace pty_forward_set_ignore_vhangup() with pty_forward_honor_vhangup()
Currently, pty_forward_set_ignore_vhangup() is only used for disabling
the flag. To make the function also disable PTY_FORWARD_IGNORE_INITIAL_VHANGUP
flag, this renames it to pty_forward_honor_vhangup().

Also, for consistency, pty_forward_get_ignore_vhangup() and
ignore_vhangup() are replaced with pty_forward_vhangup_honored().
2025-07-30 01:14:57 +09:00
Lennart Poettering
d32ca63318 machined: use RET_GATHER() more 2025-07-15 13:09:08 +02:00
Lennart Poettering
4baede011d machined: align string table 2025-07-15 12:49:08 +02:00
Lennart Poettering
ca1daebdd6 machinectl: output supervisor info in status output 2025-07-11 18:15:12 +02:00
Lennart Poettering
596c596d09 machined: add a bit more debug logging 2025-07-11 18:15:12 +02:00
Lennart Poettering
74546a7e29 machined: explicitly watch machine cgroup for getting empty 2025-07-11 18:15:12 +02:00
Lennart Poettering
97754cd14d machined: also track 'supervisor' process of a machine
So far, machined strictly tracked the "leader" process of a machine,
i.e. the topmost process that is actually the payload of the machine.
Its runtime also defines the runtime of the machine, and we can directly
interact with it if we need to, for example for containers to join the
namespaces, or kill it.

Let's optionally also track the "supervisor" process of a machine, i.e.
the host process that manages the payload if there is one. This is
generally useful info, but in particular is useful because we might need
to communicate with it to shutdown a machine without cooperation of the
payload. Traditionally we did this by simply stopping the unit of the
machine, but this is not doable now that the host machined can be used
to track per-user machines.

In the long run we probably want a more bespoke protocol between
machined and supervisors (so that we can execute other commands too,
such as request cooperative reboots/shutdowns), but that's for later.

Some environments call the concept "monitor" rather than "supervisor" or
use some other term. I stuck to "supervisor" because nspawn uses this,
and ultimately one name is as good as another.

And of course, in other implementations of VM managers of containers
there might not be a single process tracking each VM/container. Because
of this, the concept of a supervisor is optional.
2025-07-11 18:15:12 +02:00
Lennart Poettering
adaff8eb35 machined: use different polkit actions for registering and creating a machine
The difference between these two operations are large: one is relatively
superficial: for "registration" all resources remain associated with the
invoking user, only the cgroup is reported to machined which then keeps
track of the machine, too. OTOH "creation" a scope is allocated in
system context, hence the invoked code will be owned by the system, and
its resource usage charged against the system.

Hence, use two distinct polkit actions for this, so that we can relax
access to registration, but keep access to creation tough.
2025-07-11 18:15:12 +02:00
Lennart Poettering
276d200186 machined: track UID owner of machines
Now that unpriv clients can register machines, let's register their UID
too. This allows us to do two things:

1. make sure the scope delegation is assigned to the right UID (so that
   the unpriv user can actually create cgroups below the delegated
   scope)

2. permit certain types of access (i.e. killing, or pty access) to the
   client without auth if it owns the machine.
2025-07-11 18:15:12 +02:00
Lennart Poettering
d5feeb373c machined: optionally track machines in cgroup subgroups 2025-07-11 18:15:12 +02:00
Lennart Poettering
a13fda9e67 machinectl: fix status output indentation
All other status output lines use tabs, use that for the ID shift line
too. otherwise output will appear unaligned if log viewers have fixed
tab stop positions.
2025-07-08 06:40:35 +02:00
Yu Watanabe
4f18ff2e29 tree-wide: include unistd.h where necessary
We use symbols provided by unistd.h without including it. E.g.
open(), close(), read(), write(), access(), symlink(), unlink(), rmdir(),
fsync(), syncfs(), lseek(), ftruncate(), fchown(), dup2(), pipe2(),
getuid(), getgid(), gettid(), getppid(), pipe2(), execv(), _exit(),
environ, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, F_OK, and their
friends and variants, so on.

Currently, unistd.h is indirectly included mainly in the following two paths:
- through missing_syscall.h, which is planned to covert to .c file.
- through signal.h -> bits/sigstksz.h, which is new since glibc-2.34.
  Note, signal.h is included by sd-eevent.h. So, many source files
  indirectly include unistd.h if newer glibc is used.

Currently, our baseline on glibc is 2.31. We need to support glibc older
than 2.34, but unfortunately, we do not have any CI environments with
such old glibc. CIFuzz uses glibc-2.31, but it builds only fuzzers, and
many files are even not compiled.
2025-06-30 09:19:15 +02:00
Lennart Poettering
51cc3825d1 tree-wide: use sd_bus_message_send() instead of sd_bus_send() wherever possible
Also, add a coccinelle script that patches this automatically.
2025-06-24 23:23:40 +09:00
Yu Watanabe
9d5f05ae22 bus-util: coding style cleanups
- replace `type *func()` -> `type* func()`,
- rename arguments for storing results.
2025-06-18 01:55:46 +09:00
Yu Watanabe
984ae058dc discover-image: several cleanups around image_discover() (#37813) 2025-06-13 07:57:28 +09:00
Yu Watanabe
624d369868 discover-image: make image_discover() allocate hashmap when necessary 2025-06-11 22:45:08 +09:00
Yu Watanabe
b52d575845 machinectl: use _RUNNER_MAX rather than ELEMENTSOF() 2025-06-11 22:41:54 +09:00
Yu Watanabe
093b025548 machinectl: align table 2025-06-11 22:41:54 +09:00
Lennart Poettering
790f516217 machined: open up machine registration for unpriv clients also via D-Bus
This is already opened up via Varlink. Let's also open it up via D-Bus
with the same polikit operation.
2025-06-07 00:27:11 +02:00
Lennart Poettering
02927af799 machined: properly open up all missing method calls via D-Bus to unpriv clients
These method calls all already have polkit hookup, hence actually allow
them to go through on all levels.

This is mostly playing catchup with a variety of calls added over the
years.
2025-06-06 22:15:25 +02:00
Yu Watanabe
ac004f4cc9 tree-wide: introduce bus_error_is_connection() and use it where applicable 2025-06-06 21:42:25 +09:00
Lennart Poettering
020d6c1dce machined: open up OpenMachinePTY() for unpriv clients
The method call already does a PK check, it was just forgotten to
allowlist this in the dbus policy. And in the dbus vtable for
OpenMachinePTY() call. (It was allowlisted in the per-machine
vtable…)

Anyway, clean this up.
2025-06-05 12:44:08 +02:00
Zbigniew Jędrzejewski-Szmek
42ba99748d various: do not include file names directly in error messages
git grep -l 'Failed to open /'|xargs sed -r -i 's|"Failed to open (/[^ ]+): %m"|"Failed to open %s: %m", "\1"|g'
git grep -l $'Failed to open \'/'|xargs sed -r -i $'s|"Failed to open \'(/[^ ]+)\': %m"|"Failed to open %s: %m", "\\1"|g'
git grep -l "Failed to open /"|xargs sed -r -i $'s|"Failed to open (/[^ ]+), ignoring: %m"|"Failed to open %s, ignoring: %m", "\\1"|g'
+ some manual fixups.
2025-06-02 11:10:38 +02:00
Lennart Poettering
48764e2e58 machine: fix log message, doesn't have to be scope unit, can by any 2025-05-27 10:27:37 +02:00
Lennart Poettering
ca02f658e0 machine: rework machine_gc()
Let's check the leader alive state, and let's log about dbus errors.
This mimics (but is not quite identical to) what we do these days in
logind for GC'ing user sessions.
2025-05-27 10:27:37 +02:00
Lennart Poettering
0c38bc6227 machine: shorten code 2025-05-27 10:23:36 +02:00
Lennart Poettering
ee5622f9f0 machine: as safety precaution also check parsed machine name 2025-05-27 10:23:36 +02:00
Lennart Poettering
6981e465a7 machine: also save/restore vsock CID properly 2025-05-27 10:23:36 +02:00
Lennart Poettering
320b370ab3 machine: modernizations of serializing/deserializing netif data 2025-05-27 10:23:36 +02:00
Lennart Poettering
20babd6038 machine: save/restore machine leader pidfdid 2025-05-27 10:23:36 +02:00
Lennart Poettering
4b9c918431 machine: use the correct escaping calls for machine metadata 2025-05-27 10:23:36 +02:00
Lennart Poettering
b8396f1102 machine: properly remove unit name symlink on removal 2025-05-27 10:23:36 +02:00
Lennart Poettering
a4dc3b16d9 machine: port machined state files to fopen_tmpfile_linkable()
Similar to the erlier commit for logind, switch to a more modern way to
write the state files.
2025-05-27 10:23:36 +02:00
Lennart Poettering
0c18c0deee machine: insist in a valid root directory 2025-05-27 10:23:36 +02:00
Lennart Poettering
98fc46f2a6 logind,machined: expose bus properties for leader PID fd ids, too 2025-05-27 10:23:36 +02:00
Mike Yuan
c3f900770d cgroup-util: drop handcrafted cg_is_empty(), always check cgroup.events populated field
This effectively renames cg_is_empty_recursive() to cg_is_empty().
Note that all existing code calls the former and not the latter,
hence with cgv1 support being dropped it's trivial to consult
cgroup.events directly for populated state everywhere.

Additionally, use more generic cg_get_keyed_attribute() helper
rather than cg_read_event().
2025-05-26 22:59:48 +02:00