Commit Graph
136 Commits
Author SHA1 Message Date
Yu Watanabe d2881ef96e tree-wide: add short comments for namespace_open() and namespace_enter()
Also use -EBADF when unspecified.
2024-01-19 15:06:08 +09:00
AtariDreams 5ba46b99f7 simplify bitwise checking (#30722)
Some of these checks before bitwise operations are redundant and compilers
do not always recognize them, so let's simplify the code to make the intentions
clearer.
2024-01-09 10:18:10 +09:00
Lennart Poettering bb59b92256 mount-util: make sure mount_switch_root() works as clean NOP when '/' is specified as target 2023-12-14 13:13:45 +09:00
Luca Boccassi 918ef2eeeb Merge pull request #29748 from poettering/tgtmode
Storage target mode, inspired by "target disk mode" á la MacOS, but with NVMe-TCP
2023-11-02 17:26:05 +00:00
Luca Boccassi 6d5202ccaa mount-util: call CLEANUP_ARRAY after allocating array
Coverity gets confused if it is called before.

CID#1523757
2023-11-02 15:34:10 +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
Lennart Poettering dba4fa8910 nspawn: make sure idmapped logic works if DDI contains only /usr/ tree
If we have a DDI that contains only a /usr/ tree (and which is thus
combined with a tmpfs for root on boot) we previously would try to apply
idmapping to the tmpfs, but not the /usr/ mount. That's broken of
course.

Fix this by applying it to both trees.
2023-11-01 00:50:43 +00: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 83d5bbaf1b mount-util: add one more const 2023-10-25 15:53:10 +09:00
Mike Yuan 0f095d0b8f shared/mount-util: log correct errno
Follow-up for 5f48198af8
2023-10-21 06:25:36 +08:00
Luca Boccassi 5f48198af8 mount tunnel: use PidRef 2023-10-19 16:31:05 +01:00
Luca Boccassi 7c83d42ef8 mount-util: use mount beneath to replace previous namespace mount
Instead of mounting over, do an atomic swap using mount beneath, if
available. This way assets can be mounted again and again (e.g.:
updates) without leaking mounts.
2023-10-16 14:33:47 +01:00
Luca Boccassi 5897469a08 core: fix checking for extension-releases for ExtensionImages/Directories
The parsing is done after the image has been opened, not before, as it
cannot be done on an block device. Also fix returning on any error for
ExtensionDirectories, not just ENOENT.

Follow-up for 55ea4ef096
2023-10-12 12:17:58 +01:00
Luca Boccassi ddba5a1ad6 mount-util: use new mount API for image mount tunnel 2023-10-02 14:30:23 +01:00
Luca Boccassi 3e1072726b dissect-image: support verity_dissect_and_mount() in two steps too
With the new mount API first the image is opened, and later mounted
2023-10-02 14:02:32 +01:00
Lennart Poettering 44cc82bfbf mount-util: add make_fsmount() 2023-10-02 14:02:32 +01:00
Luca Boccassi a7e0199ece mount-util: use new mount API for bind mount tunnel
If we have the new mount API we can simplify bind mounting into a namespace
a lot
2023-10-02 14:02:32 +01:00
Yu Watanabe 2b60ce545c mount-util: drop unused remount_and_move_sub_mounts() 2023-08-22 23:21:43 +09:00
Lennart Poettering 1155f44f48 execute: split out mounting of credentials fs
Let's add two new helpers: mount_credentials_fs() and
credentials_fs_mount_flags(). The former mounts a file system suitable
for storing of unencrypted credentials at runtime (i.e. a ramfs or
tmpfs). The latter determines the right mount flags to use for such a
mount.

Both functions mostly just take code from execute.c, but make two
changes:

1. If the kernel supports it we'll use a tmpfs with the new "noswap"
   mount option instead of ramfs. Was added in kernel 6.4, hence is very
   recent, but tmpfs is so much less crappy than ramfs, hence worth it.

2. We'll set MS_NOSYMFOLLOW on the mounts if supported. These file
   systems should only contain regulra files, hence no need to allow
   symlinks.
2023-07-04 23:03:24 +02:00
Daan De Meyer 506c1bb594 stat-util: Make sure we trigger automounts when looking for ESP/XBOOTLDR
Fixes #25417
2023-06-30 12:25:07 +01:00
Lennart Poettering f56f9c6b3e mount-util: tweak flags decoding in mount_verbose_full()
Fine-tune the decoding of mount options in mount_verbose_full() to
provide more helpful log output:

1. decode changing of propagation changes
2. discern changing of superblock flags/mount option string from mount
   flags
3. don't check secondary fields when deciding which mount op is
   executed, only the flags decide that.
2023-06-29 22:15:16 +01:00
Lennart Poettering 11e2be3a19 mount-util: use inode_type_to_string() in mode_to_inaccessible_inode()
Let's reuse the inode type name table here and shorten/simplify the code
a bit.
2023-06-26 17:24:35 +02:00
Lennart Poettering 1b618bf141 mount-util: add helper that can bind mount submounts of one dir to another 2023-06-23 18:30:59 +02:00
Lennart Poettering 4a1dca795c mount-util: don't claim maybe-uninitialized was false that is valid 2023-06-23 18:30:16 +02:00
Zbigniew Jędrzejewski-Szmek 12cc960163 shared: improve messages about switch root operations
"change" is not clear without context.
2023-06-19 13:36:30 +02:00
Lennart Poettering f9ad896ee2 mount-util: add fd_make_mount_point() helper 2023-06-02 18:43:10 +02:00
Daan De Meyer 0690160e2c label: Rename to label-util.h 2023-05-30 14:50:56 +02:00
Daan De Meyer 30868c1c8d tree-wide: Downgrade a few more noisy log messages to trace 2023-05-27 14:47:56 +02:00
Daan De Meyer 61f695f40a mount-util: Downgrade another noisy debug log to trace level 2023-05-26 17:01:58 +02:00
Daan De Meyer 62c505e1c7 mount-util: Downgrade log message to trace
This debug log message is extremely noisy so let's downgrade it to
trace.
2023-05-25 12:48:14 +02:00
Lennart Poettering ef742415e6 mount-util: make "prefix" parameter optional for umount_recursive()
When switching root via MS_MOVE there's no need to filter the mount
table by prefix
2023-05-17 10:29:05 +02:00
Lennart Poettering 84bcb394c8 mount-util: extend umount_recursive() to optionally take list of dirs to exclude from the unmounting 2023-05-17 10:28:09 +02:00
Lennart Poettering 890c14e343 mount-util: keep fd to /proc/self/mountinfo continously open in umount_recursive()
That way, if we end up unmounting /proc/ in our loop we can still
operate correctly, since we don't have to go through /proc/ again to
open the mount table again.
2023-05-17 10:26:51 +02:00
Lennart Poettering ea0f3289a2 mount-util: simplify mount_switch_root() a bit
There's no need to fchdir() out of the rootfs and back into it around
the umount2(), hence don't.

This brings the logic closer to what the pivot_root() man page suggests.

While we are at it, always operate based on fds, once we opened the
original dir, and pass the path string along only for generating
messages (i.e. as "decoration").

Add tests for both code paths: the pivot_root() one and the MS_MOUNT.
2023-05-03 20:52:19 +01:00
Lennart Poettering 17b798d915 mount-util: split remount_idmap() in two
This will make things a bit longer for now, but more powerful as we can
reuse the userns fd between calls to remount_idmap() if we need to
adjust multiple mounts.

No change in behaviour, just some minor refactoring.
2023-04-25 17:39:16 +02:00
Lennart Poettering 84be0c710d tree-wide: hook up image dissection policy logic everywhere 2023-04-05 20:45:30 +02:00
Daan De Meyer f461a28da7 chase-symlinks: Rename chase_symlinks() to chase()
Chasing symlinks is a core function that's used in a lot of places
so it deservers a less verbose names so let's rename it to chase()
and chaseat().

We also slightly change the pattern used for the chaseat() helpers
so we get chase_and_openat() and similar.
2023-03-24 13:43:51 +01:00
Yu Watanabe f63a2c4821 mount-util: introduce remount_sysfs()
This is useful when creating a new network namespace. Unlike procfs,
we need to remount sysfs, otherwise properties of the network interfaces
in the main network namespace are still accessible through the old sysfs,
e.g. /sys/class/net/eth0. All sub-mounts previously mounted on the sysfs
are moved onto the new sysfs mount.

The function will be used in later commits.
2023-02-23 15:09:13 +09:00
Lennart Poettering 03469b770b shared: port various shared helpers basename() → path_extract_filename() 2022-12-23 17:35:21 +01:00
Yu Watanabe 19ee48a6c2 tree-wide: introduce PIPE_EBADF macro 2022-12-20 11:12:58 +09:00
Zbigniew Jędrzejewski-Szmek 3401477982 tree-wide: use -EBADF also in pipe initializers
In some places, initialization is dropped when unnecesary.
2022-12-19 15:00:59 +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
Yu Watanabe 9d50f8508b mount-util: make mount_switch_root() take a mount propagation flag 2022-12-15 14:17:22 +09:00
Yu Watanabe 6c6eb219d5 mount-util: mount flag is unsigned long 2022-12-15 14:15:09 +09:00
Yu Watanabe b8b4f80a8a mount-util: drop unnecessary inline attributes 2022-12-15 14:15:09 +09:00
Zbigniew Jędrzejewski-Szmek 9f563f2792 tree-wide: use mode=0nnn for mount option
This is an octal number. We used the 0 prefix in some places inconsistently.
The kernel always interprets in base-8, so this has no effect, but I think
it's nicer to use the 0 to remind the reader that this is not a decimal number.
2022-12-14 22:12:44 +01:00
Zbigniew Jędrzejewski-Szmek 61ef30515b shared: add new safe_fork flag FORK_PRIVATE_TMP
If the flag is set, we mount /tmp/ in a way that is suitable for generators and
other quick jobs.

Unfortunately I had to move some code from shared/mount-util.c to
basic/mountpoint-util.c. The functions that are moved are very thin wrappers
around mount(2), so this doesn't actually change much in the code split between
libbasic and libshared.

Implications for the host would be weird if a private mount namespace is not
used, so assert on FORK_NEW_MOUNTNS when the flag is used.
2022-12-14 22:12:44 +01:00
Christian Brauner 57c10a5650 nspawn: support pivot_root()
In order to support pivot_root() we need to move mount propagation
changes after the pivot_root(). While MS_MOVE requires the source mount
to not be a shared mount pivot_root() also requires the target mount to
not be a shared mount. This guarantees that pivot_root() doesn't leak
any mounts.

Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
2022-12-05 18:34:25 +01:00
Christian Brauner 2e776ed6c8 shared: use move_pivot_root() for services
Currently, services use mount_move_root() in order to setup the root
directory of services using a mount namespace. This relies on MS_MOVE
and chroot(). However, this has serious drawbacks even for relatively
simple mount propagation scenarios.

What systemd currently does is roughly equivalent to the following shell
code:

  unshare --mount --propagation=shared
  cd /
  mount --make-rslave /
  mkdir /new-root
  mount --rbind / /new-root
  cd /new-root
  mount --move /new-root /
  chroot .

This looks simple enough but has the consequence that two separate mount
trees exist for the lifetime of the service. The first one was created
when the mount namespace was created, and the second one when a new
mount for the rootfs was created. The first mount tree sticks around as
a shadow mount tree. Both mount trees are dependent mounts with the host
rootfs as their dominating mount.

Now, when mount propagation is triggered by the host by e.g.,

   mount --bind /opt /mnt

it means that two propagation events are generated. I'm skipping over
the exact kernel details as they aren't that important. The gist is that
for every propagation event that is generated a second one is generated
for the shadow mount tree. In other words, the kernel creates two copies
for each mount that is propagated instead of one.

This isn't necessary. We can simply change the sequence above to:

  unshare --mount --propagation=shared
  cd /
  mount --make-rslave /
  mkdir /new-root
  # stash fd to old rootfs
  # stash fd to new rootfs
  mount --rbind / /new-root
  mkdir /new-root
  cd /new-root
  pivot_root . .
  # new root is tucked under old root
  # chdir into old rootfs via stashed fd
  umount -l /old-root

The pivot_root allows us to get rid of the old mount tree that was
created when the mount namespace was created. So after this sequence
only one mount tree is alive. Plus, it's safer and nicer. Moving mounts
isn't pleasnt.

This patch doesn't convert nspawn yet as the requirements are more
tricky given that it wants to preserve the rootfs as a shared mount
which goes against pivot_root() requirements.

Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
2022-11-24 10:58:26 +01:00
Zbigniew Jędrzejewski-Szmek 3ca4ec2012 shared/mount-util: fix comment
Just typos and grammar. In the end didn't add a use of a function, but
I read the comment carefully, and this commit is the result of that.
2022-11-09 09:20:34 +01:00