Commit Graph

1615 Commits

Author SHA1 Message Date
Yu Watanabe
91c4d1affd nspawn-oci: update overflow check
Fixes CID#1548072.
2025-03-19 11:41:25 +09:00
Yu Watanabe
b8d5303733 nspawn: make failure in sd_listen_fds() critical
Also, drop doubled call of sd_listen_fds().
2025-03-19 02:09:40 +09:00
Yu Watanabe
c9c2679a62 some user record fixes (#36776)
Fixes: https://github.com/systemd/systemd/issues/36775
2025-03-18 09:24:05 +09:00
Lennart Poettering
91d11d53a1 tree-wide: refuse user/group records lacking UID or GID
userdb allows user/group records without UID/GID (it only really
requires a name), in order to permit "unfixated" records. But that means
we cannot just rely on the field to be valid. And we mostly got that
right, but not everywhere. Fix that.
2025-03-17 22:37:14 +01:00
Lennart Poettering
d43a440767 nspawn: go via user_record_home_directory() accessor for .home_directory UserRecord field
Fixes: #36775
2025-03-17 22:23:18 +01:00
Lennart Poettering
ca4560f6a8 tree-wide: go via user_record_gid() accessor for UserRecord's .gid field
Sometimes we went for the field directly, where we really should go via
the accessor. Fix it.
2025-03-17 22:21:58 +01:00
Lennart Poettering
716bf93c4b nsresource: add client-side wrapper for creating tap links 2025-03-17 16:03:18 +01:00
Lennart Poettering
afdca6c6c2 nspawn,run,machinectl,socket-activate: propagate $COLORTERM + $NO_COLOR
When we pass information about our calling terminal on to some service
or command we invoke, propagate $COLORTERM + $NO_COLOR in addition to
$TERM, in order to always consider the triplet of the three env vars the
real deal.
2025-03-17 10:17:54 +01:00
Yu Watanabe
c06a630f0c nspawn: introduce --cleanup option to clear propagation and unix-export directories
This is useful when the previous invocation is unexpectedly killed.

Otherwise, if systemd-nspawn is killed forcibly, then unix-export
directory is not cleared and unmounted, and the subsequent invocation
will fail. E.g.
===
[   18.895515] TEST-13-NSPAWN.sh[645]: + machinectl start long-running
[   18.945703] systemd-nspawn[1387]: Mount point '/run/systemd/nspawn/unix-export/long-running' exists already, refusing.
[   18.949236] systemd[1]: systemd-nspawn@long-running.service: Failed with result 'exit-code'.
[   18.949743] systemd[1]: Failed to start systemd-nspawn@long-running.service.
===
2025-03-16 11:02:09 +09:00
Zbigniew Jędrzejewski-Szmek
1ae9b0cfa8 basic/glyph-util: rename "special glyph" to just "glyph"
Admittedly, some of our glyphs _are_ special, e.g. "O=" for SPECIAL_GLYPH_TOUCH ;)
But we don't need this in the name. The very long names make some invocations
very wordy, e.g. special_glyph(SPECIAL_GLYPH_SLIGHTLY_UNHAPPY_SMILEY).
Also, I want to add GLYPH_SPACE, which is not special at all.
2025-03-15 14:40:39 +01:00
Lennart Poettering
f0f5d54202 nspawn: add ability to poweroff container cleanly with ^]^]p
It's sometimes very useful to be able to terminate a container quickly
but cleanly while talking to it. Introduce a hotkey for that: ^]^]p for
powering it off. In similar style add ^]^]r for rebooting it.
2025-03-04 23:02:31 +01:00
Yu Watanabe
ec32732043 basic: introduce our own sys/mount.h implementation
To resolve conflict with sys/mount.h and linux/mount.h or linux/fs.h.

The conflict between sys/mount.h and linux/mount.h is resolved in
glibc-2.37 (774058d72942249f71d74e7f2b639f77184160a6), but our baseline
is still glibc-2.31. Also, even with the version or newer, still
sys/mount.h conflicts with linux/fs.h, which is included by
linux/btrfs.h.

This introduces our own implementation of sys/mount.h, that can be
simultaneously included with linux/mount.h and linux/fs.h. This also
imports linux/fs.h, linux/mount.h, and several other dependent headers.
The introduced sys/mount.h header itself may not be enough simple, but
by using the header, we can drop most of workarounds in other source files.
2025-03-04 02:24:49 +09:00
Yu Watanabe
a7cb43d8d1 missing_resource.h: RLIMIT_RTTIME is defined since glibc-2.14
Now our baseline is glibc-2.31.
2025-03-04 02:24:49 +09:00
Lennart Poettering
19ade24464 notify-recv: add notify_recv() flavour that returns a split up strv instead of he message text as string
This is useful at various places, since we split up the message as first
thing there anyway.
2025-02-28 14:17:52 +01:00
Lennart Poettering
ddb503b7f6 nspawn: set TERM to "dumb" if we are invoked outside of TTY context 2025-02-27 15:13:15 +01:00
Lennart Poettering
466b285bdb nspawn: output context OSC 2025-02-27 15:09:25 +01:00
Mike Yuan
74cd56d31b notify-recv: several followups
Follow-up for 7f6af95dab

- Allocate internal buf on the stack, memdup() only at the end.
  This ensures we're able to handle OOM gracefully, i.e.
  return -EAGAIN on OOM while still emptying socket buffer.
- Do not treat empty notify message as error.
- Raise log level since all callers log loudly anyway.
2025-02-26 13:02:23 +01:00
Lennart Poettering
060c4b99bd nspawn: port to notify_recv() 2025-02-20 18:11:31 +01:00
Yu Watanabe
114d191a17 nspawn: move the accessibility check for device nodes into copy_devnode_one() 2025-02-18 23:35:13 +09:00
Yu Watanabe
c51e472bd4 nspawn: drop unused argument for copy_devnode_one() 2025-02-18 23:24:20 +09:00
Yu Watanabe
9fff6bf59e nspawn: create /dev/net/tun only when it is accessible
Follow-up for 985ea98e7f.

When DevicePolicy= is enabled, but DeviceAllow= for /dev/net/tun is not
specified, bind-mounting the device node from the host system is
meaningless, as it cannot be used in the container anyway.

Let's check the device node is accessible before creating or
bind-mounting.
2025-02-18 23:24:20 +09:00
Yu Watanabe
3cc23a2c23 nspawn: enable FUSE unconditionally
FUSE is userns-safe since kernel v4.18 (da315f6e03988a7127680bbc26e1028991b899b8),
and now our kernel base line is 5.4. Let's drop the logic of checking
the version of FUSE, and unconditionally enable FUSE.
2025-02-18 23:24:20 +09:00
Yu Watanabe
1236f06c42 nspawn: use FOREACH_ARRAY() where applicable 2025-02-18 23:24:20 +09:00
Anthony Messina
6cbd126b0b nspawn: add @keyring to default syscall allow_list
Keyring namespacing introduced in kernel version v5.3-rc1 c84ca912b0

Fixes #17606
2025-02-12 08:05:11 +09:00
Lennart Poettering
7419291670 userdb: move UserDBMatch handling from userdbctl into generic userdb code to allow it to be done server side
This moves around the UserDBMatch handling, moves it out of userdbctl
and into generic userdb code, so that it can be passed to the server
side, to allow server side filtering.

This is preparation for one day allowing complex software to do such
filtering server side, and thus reducing the necessary traffic.

Right now no server side actually knows this, hence care is taken to
downgrade to the userdb varlink API as it was in v257 in case the new
options are not understood. This retains compatibility with any
implementation hence.
2025-01-27 23:51:57 +01:00
Lennart Poettering
46b7e96783 nspawn: add support for 'managed' userns mode even when we run privileged
So far, we supported two modes:

1. when running unpriv we'd get the mounts from mountfsd, and the userns
   from nsresourced
2. when running priv we'd do the mounts/userns ourselves

This untangles this a bit, so that we can also use mountfsd/nsresourced
when running privilged.

I think this is generally a bit nicer, and probably something we should
switch to entirely one day, as it reduces the variety of codepaths.

With this patch the default behaviour remains unchanged, but by
selecting the new "managed" option for --private-users= the codepaths
via mountfsd/nsresourced can be explicitly requested even when running
with privs.

This is mostly just reworks that we check for arg_userns_mode !=
USER_NAMESPACE_MANAGED rather than arg_privileged for a number of
codepaths, but requires more fixes, too. The devil is in the details.
2025-01-23 21:48:02 +01:00
Lennart Poettering
ca23deae09 nspawn: support foreign mappings also when nspawn doing the mapping itself
This adds a new "foreign" value to --private-users-ownership= which is a
lot like "map", but maps from the host's foreign UID range rather than from the
host's 0.

(This has nothing much to do with making unprivileged directory-based
containers work, it's just very handy that we can run privileged
contains with such a mapping too, with an easy switch)
2025-01-23 21:48:02 +01:00
Lennart Poettering
88252ca889 nspawn: allow to run unpriv from dir
This simply calls into mountfsd to acquire the root mount and uses it as
root for the container.

Note that this also makes one more change: previously we ran containers
directory off their backing directory. Except when we didn't, and there
were a variety of exceptions: if we had no privs, if we ran off a disk
image, if the directory was the host's root dir, and some others.

This simplifies the logic a bit: we now simply always create a temporary
directory in /tmp/ and bind mount everything there, in all code paths.
This simplifies our code a bit. After all, in order to control
propagation we need to turn the root into a mount point anyway, hence we
might just do it at one place for all cases.
2025-01-23 21:48:02 +01:00
Luca Boccassi
8fa7863a93 nspawn: downgrade log message about usrquota to debug
This is shown every time nspawn is started, which is annoying
and there's nothing a user can do about it, since it depends on
an extremely new kernel. Downgrade to debug.

Follow-up for 611ae59888
2025-01-20 21:16:12 +01:00
Lennart Poettering
611ae59888 nspawn: enable usrquota support on /tmp/ and /dev/shm/ 2025-01-18 23:13:06 +01:00
Lennart Poettering
d58577d45e nspawn: assorted coding style fixes 2025-01-17 11:12:24 +01:00
Lennart Poettering
bf37a69c3f tree-wide: switch various uses of mkdtemp() over to mkdtemp_malloc() 2025-01-17 10:56:35 +01:00
Mike Yuan
70923ed358 meson: enable -Wzero-as-null-pointer-constant
Support for C added in gcc 15:
236c0829ee
2025-01-16 02:26:56 +01:00
Mike Yuan
347eb8fbe3 tree-wide: remove unnecessary gcc >= 7 version check
Our baseline is gcc 8.4.
2025-01-16 02:26:56 +01:00
Lennart Poettering
76d2b8e281 nspawn: trivial scope reduction 2025-01-15 15:35:57 +00:00
Lennart Poettering
8c0da3af28 tree-wide: port more code to namespace_open_by_type() 2025-01-10 14:09:48 +01:00
Lennart Poettering
44eb6b81db userdb: synthesize stub user records for the foreign UID 2025-01-08 21:41:03 +01:00
Lennart Poettering
336acebc77 basic: port various pidfd/pidref helpers to PIDFD_GET_INFO and PIDFD_GET_*_NAMESPACE (#35242)
Supersedes #35308 (cherry-picked one commit and replaced the rest)

(I left a few comments that's folded by GitHub. Please make sure to
check them too.)
2025-01-06 11:23:08 +01:00
Mike Yuan
07610cafcf namespace-util: modernize fd_is_namespace() and is_our_namespace()
- Make fd_is_namespace() take NamespaceType
- Drop support for kernel without NS_GET_NSTYPE (< 4.11)
- Port is_our_namespace() to namespace_open_by_type()
  (preparation for later commits, where the latter
  would go by pidfd if available, avoiding procfs)
2025-01-04 17:07:59 +01:00
Mike Yuan
bed4386e29 signal-util: generalize sigaction_nop_nocldstop 2025-01-04 16:40:53 +01:00
Lennart Poettering
b1fb2d971c nspawn: move uid shift/chown() code into shared/ 2025-01-04 12:23:31 +09:00
Daan De Meyer
b6507d81ce nspawn: trivial tweaklets (#35831) 2025-01-03 15:54:43 +01:00
Lennart Poettering
4b9203ac65 nspawn: rework userns_mkdir() around chase() 2025-01-03 12:56:52 +01:00
Lennart Poettering
2623052e14 nspawn: improve log messages a bit 2025-01-03 10:21:34 +01:00
Lennart Poettering
ef99711d0f nspawn: drop some redundant {} 2025-01-03 10:21:27 +01:00
Lennart Poettering
1c0ade2e1f discover-image: introduce per-user image directories
We nowadays support unprivileged invocation of systemd-nspawn +
systemd-vmspawn, but there was no support for discovering suitable disk
images (i.e. no per-user counterpart of /var/lib/machines). Add this
now, and hook it up everywhere.

Instead of hardcoding machined's, importd's, portabled's, sysupdated's
image discovery to RUNTIME_SCOPE_SYSTEM I introduced a field that make
the scope variable, even if this field is always initialized to
RUNTIME_SCOPE_SYSTEM for now. I think these four services should
eventually be updated to support a per-user concept too, this is
preparation for that, even though it doesn't outright add support for
this.

This is for the largest part not user visible, except for in nspawn,
vmspawn and the dissect tool. For the latter I added a pair of
--user/--system switches to select the discovery scope.
2024-12-20 18:04:01 +01:00
Lennart Poettering
5ceb38cb1e nspawn: switch to read_virtual_file() for reading audit loginuid 2024-12-19 15:37:00 +01:00
Lennart Poettering
009a02b263 nspawn: trivial improvements 2024-12-19 15:33:34 +01:00
Lennart Poettering
b83358b87f nspawn: rename pin_fully_visible_fs() → pin_fully_visible_api_fs()
This function pins the *API* FS, i.e. /proc/ + /sys/, not just any fs.
Hence clarify this in the name.

(At least we call these two fs "API (V)FS" in our codebase, hence
continue to do so here)
2024-12-19 15:33:24 +01:00
Lennart Poettering
bf1ef54d30 nspawn: add some additional useful debug logging 2024-12-19 15:33:10 +01:00