1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-11-04 12:24:26 +03:00
Commit Graph

98 Commits

Author SHA1 Message Date
Lennart Poettering
1a81ddef00 dissect-image: optionally, reference dissected partition device nodes by diskseq
This is useful to make the dissection logic at boot a bit safer, as we
can reference device nodes by diskseq.

This locks down dissection a bit, since it makes it harder to swap out
the backing device between the time we dissected and validated it, until
we actually mounted it.

This is not complete though, as /bin/mount would have to verify the
diskseq after opening the diskseq symlink again.

See: https://github.com/util-linux/util-linux/issues/1786
2022-12-23 12:44:57 +01:00
Lennart Poettering
80ce8580f5 dissect-image: let's lock down fstypes a bit
When we dissect images automatically, let's be a bit more conservative
with the file system types we are willing to mount: only mount common
file systems automatically.

Explicit mounts requested by admins should always be OK, but when we do
automatic mounts, let's not permit barely maintained, possibly legacy
file systems.

The list for now covers the four common writable and two common
read-only file systems. Sooner or later we might want to add more to the
list.

Also, it might make sense to eventually make this configurable via the
image dissection policy logic.
2022-12-22 10:30:35 +09: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
Lennart Poettering
c2534821dc dissect: add new helper verity_settings_data_covers()
This function checks if the external verity data referenced in
VeritySettings covers the specified partition (indicated via
designator).

Right now, we'll use that at one place, but in a later commit in more.
2022-12-03 00:22:23 +01:00
Lennart Poettering
d90b03f80d dissect: pick up gpt partition flags
Let's store the GPT partition flags in the dissected partition info.

Right now we won't actually use them for anything yet, but later we'll
add that, when enforcing policy on dissection.
2022-12-02 22:52:48 +01:00
Lennart Poettering
c80c9079c8 dissect-image: probe file system via main block device fd/image file fd
let's make sure we can probe file systems also when unprivileged:
instead of probing the partition block devices for file system
signatures, let's go via the original "whole" fd.

libblkid makes this easy actually, as it allows us to specify the
offset/size of the area to probe. And we have the partition
offsets/sizes anyway, so it's trivial for us to make use of.

This thus enables fs probing also when lacking privs and operating on
naked regular files without loopback devices or anything like this.
2022-12-01 22:09:30 +01:00
Lennart Poettering
73d88b806b dissect: rework DISSECT_IMAGE_ADD_PARTITION_DEVICES + DISSECT_IMAGE_OPEN_PARTITION_DEVICES
Curently, these two flags were implied by dissect_loop_device(), but
that's not right, because this means systemd-gpt-auto-generator will
dissect the root block device with these flags set and that's not
desirable: the generator should not cause the partition devices to be
created (we don't intend to use them right-away after all, but expect
udev to find/probe them first, and then mount them though .mount units).
And there's no point in opening the partition devices, since we do not
intend to mount them via fds either.

Hence, rework this: instead of implying the flags, specify them
explicitly.

While we are at it, let's also rename the flags to make them more
descriptive:

DISSECT_IMAGE_MANAGE_PARTITION_DEVICES becomes
DISSECT_IMAGE_ADD_PARTITION_DEVICES, since that's really all this does:
add the partition devices via BLKPG.

DISSECT_IMAGE_OPEN_PARTITION_DEVICES becomes
DISSECT_IMAGE_PIN_PARTITION_DEVICES, since we not only open the devices,
but keep the devices open continously (i.e. we "pin" them).

Also, drop the DISSECT_IMAGE_BLOCK_DEVICE combination flag, since it is
misleading, i.e. it suggests it was appropriate to specify on all
dissected blocking devices, but that's precisely not the case, see the
systemd-gpt-auto-generator case. My guess is that the confusion around
this was actually the cause for this bug we are addressing here.

Fixes: #25528
2022-12-01 11:32:30 +01:00
Yu Watanabe
088377e092 dissect-image: do not try to close invalid fd
Fixes a bug introduced by f7725647bb.

Hopefully fixes #25348.
2022-11-13 21:02:53 +01:00
Daan De Meyer
46fddd8eb6 Merge pull request #24803 from DaanDeMeyer/repart-copy-deny-list
repart: Don't descend into directories assigned to other partitions
2022-11-11 13:19:58 +01:00
Daan De Meyer
dd8940235f gpt: Replace bitfields with designator field in GptPartitionType
To achieve this we move the PartitionDesignator enum from
dissect-image.h to gpt.h
2022-11-10 16:40:33 +01:00
Lennart Poettering
b387778c5b dissect: also parse out the top-level GPT table uuid and expose this as image UUID
systemd-repart generates this in a suitably stable fashion, hence let's
actually use it as an identifier for the image. As a first step parse
it, and show it.
2022-11-10 16:00:59 +01:00
Lennart Poettering
fab2294646 dissect: officially support initrd DDIs (to implement 2nd stage initrds)
Let's complete support for DDI discovery, and also support 2nd stage
initrds.
2022-11-09 14:43:55 +01:00
Yu Watanabe
d2c6e79d89 dissect-image: introduce probe_filesystem_full() which can take file descriptor of device node
In dissect_loop_device(), we have opened the device node. Let's reuse
the file descriptor.
2022-10-13 05:09:27 +09:00
Yu Watanabe
f7725647bb dissect-image: open dissected or decrypted partitions and mount through the file descriptor
If multiple services with the same encrypted image are simultaneously
starting, one may deactivate the dm device while others using it.
Or, similary, after (regular) partitions are dissected, another process
may try to remove them before we mount them.

To prevent such situations, let's keep the dissected and decrypted
partitions opened. Then, use the file descriptors when we mount the
partitions.

Fixes #24617.
2022-10-13 05:09:26 +09:00
Luca Boccassi
06768b90a3 portable: allow caller to override extension-release name check
When the --force flag is used, do not insist that the extension-release
file has to match the extension image name
2022-10-12 09:57:24 +01:00
Yu Watanabe
08f14be417 dissect-image: introduce dissect_image_file() which works for regular file 2022-09-28 03:06:27 +09:00
Yu Watanabe
e330f97a89 tree-wide: drop unused reference to DecryptedImage 2022-09-18 23:47:20 +09:00
Yu Watanabe
f906075a15 dissect-image: introduce dissected_image_relinquish() 2022-09-18 23:47:20 +09:00
Daan De Meyer
c72aed4c20 Merge pull request #24720 from yuwata/dissect-image-take-reference
dissect-image: make DissectedImage object take reference to DecryptedImage and LoopDevice
2022-09-18 16:08:13 +02:00
Yu Watanabe
babd5b08b5 dissect-image: introduce DISSECTED_PARTITION_NULL
Currently, it is not necessary to set partno or architecture in
dissect_image_new(), but just for safety.
Preparation for later commits.
2022-09-18 17:26:22 +09:00
Yu Watanabe
1e63dc4f1c dissect-image: take a reference of LoopDevice into DissectedImage
To make LoopDevice object freed after DissectedImage is freed.
At least currently, this should not change anything. Preparation for
later commits.
2022-09-18 07:56:32 +09:00
Yu Watanabe
ac1e1b5fd7 dissect-image: take reference of DecryptedImage into DissectedImage
No functional changes. Preparation for later commits.
2022-09-18 07:56:32 +09:00
Yu Watanabe
9321ad5118 dissect-image: introduce reference counter for DecryptedImage 2022-09-18 07:56:32 +09:00
Yu Watanabe
0b214aa0d0 dissect-image: reuse LoopDevice.node in dissect_image()
Currently, dissect_image() is only called through dissect_loop_device(),
and the LoopDevice object has device name. Hence, it is not necessary to
get device name in dissect_image().
2022-09-07 20:45:24 +09:00
Yu Watanabe
bec0c37c9e dissect-image: drop currently unused arguments 2022-09-07 20:45:24 +09:00
Yu Watanabe
64dd3a245f dissect-image: use loop backing file or device node as name of the image
Note, currently, for each call of dissect_loop_device_and_warn(), the
specified name is equivalent to the path passed to loop_device_make_by_path().
Hence, this should not change the current behavios.
2022-09-07 20:45:24 +09:00
Yu Watanabe
369de26fdf dissect-image: use backing_file stored in LoopDevice object to generate image name
Follow-up for e374439f4b (#24322).

This also simplify the logic of generating image name from image path.
2022-09-07 20:45:24 +09:00
Kai Lueke
e374439f4b Use original filename for extension name check
The loading of an extension image from a symlink "NAME.raw" to
"NAME-VERSION.raw" failed because the release file name check worked
with the backing file of the loop device which already resolves the
symlink and thus the found name "NAME-VERSION" mismatched "NAME".
Pass the original filename and use it instead of the backing file
when available. This fixes the loading of "NAME.raw" extensions which
are a symlink to "NAME-VERSION.raw" as, e.g., may be the case when
systemd-sysupdate manages multiple versions.

Fixes https://github.com/systemd/systemd/issues/24293
2022-09-06 03:53:03 +09:00
Yu Watanabe
bad31660ed dissect-image: introduce dissect_loop_device() which takes LoopDevice object 2022-09-03 01:32:01 +09:00
Lennart Poettering
234c2e16e5 dissect: drop partition removal code
This reverts a major chunk of 75d7e04eb4

Now that the loopback device code already destroys the partitions we
don't have to do this here anymore.

I am sure the right place to delete the partitions is in the loopback
code, since we really only should do that for loopback devices, see
bug #24431, and not on "real" block devices.

I am also not convinced dropping partitions the dissection logic doesn't
care about is a good idea, after all. The dissection stuff should
probably not consider itself the "owner" of the block devices it
analyzes, but take a more passive role: figure out what is what, but not
modify it.

Fixes: #24431
2022-09-01 22:05:10 +02:00
Daan De Meyer
75d7e04eb4 dissect-image: Explicitly remove partitions when done with image
When closing a loop device, the kernel will asynchronously remove
the probed partitions. This can lead to race conditions where we
try to reuse a partition device that still needs to be removed by
the kernel. To avoid such issues, let's explicitly try to remove
any partitions using BLKPG_DEL_PARTITION when we're done with an
image.

To make sure we don't try to remove partitions when we want them
to remain (e.g. systemd-dissect --mount), we add
dissected_image_relinquish() in a similar vein to loop_device_relinquish()
and decrypted_image_relinquish().
2022-05-23 18:15:16 +01:00
Lennart Poettering
1b010ae7c9 dissect: rework how we wait for partition block devices
This revisits the mess around waiting for partition block devices in
the image dissection code. It implements a nice little trick:

Instead of waiting for the kernel to probe the partition table for us
and generate the block devices from it, we'll just do that ourselves.
How can we do it? Via the BLKPG_ADD_PARTITION ioctl, that the kernel has
supported for a while. This ioctl allows creating partition block
devices off "whole" block devices from userspace, without the partitions
necessarily being present in the partition table at all.

So, whenever we want a partition to be there, we'll just issue
BLKPG_ADD_PARTITION. This can either work, in which case we know the
partition is there, and can use it. Yay. Or it can fail with EBUSY,
which the kernel returns if a partition by the selected partition index
already exists (or if an existing partition overlaps with the new one).
But if that's the case, then that's also OK, because the partition will
already exist.

So, regardless if we win or the kernel wins, for us the outcome is the
same: the partition block device will exist after invoking the ioctl.
Yay.

Net effect: we are not dependent on asynchronous uevent messages to wait
for the devices. Instead we synchronously get what we need. This makes
us independent of the (apparently less than reliable) netlink transport,
and should almost always be quicker.

Hopefully addresses #17469 even on older kernels.

Fixes: #17469
2022-04-10 22:52:29 +09:00
Luca Boccassi
cedf5b1aef core: fix dm-verity auto-discovery in MountImageUnit()
The implementation of MountImageUnit()/systemctl mount-image was
changed to use a /proc/self/fd path as the source, but that causes
the dm-verity files autodiscovery to fail, as it looks for files
in the same directory as the image.

Use the original file path when setting up dm-verity.
2022-04-07 17:31:04 +01:00
Lennart Poettering
6b41a7b281 tree-wide: use 'Architecture' type consistently across the tree
Some parts of our tree used 'Architecture' for storing architectures,
others used ints. Let's unify on the former.

Inspired by #22952's rework of the 'Virtualization' enum.
2022-04-05 02:19:37 +09:00
Lennart Poettering
7ff048a718 Merge pull request #21487 from DaanDeMeyer/dissect-image-other-arch
Allow dissect_image() to dissect images from architectures other than the native one
2021-11-25 17:36:57 +01:00
Zbigniew Jędrzejewski-Szmek
2284750820 dissect-image: do not enable "verification" when trying to acquire metadata
The whole point of acquiring metadata is quite often to figure out why the
image does not pass verification. Refusing to provide metadata is just being
hostile to the user.

When called from other places (e.g. image_read_metadata()), verification is
still performed.
2021-11-24 17:42:19 +01:00
Daan De Meyer
49ae9d91f9 shared: Add support for non-native architectures to dissect_image()
To allow dissecting images of architectures other than the native
(or secondary) one, we add a third designator 'OTHER' to represent
architectures other than the native or secondary one.

If no partitions of the native or secondary arch are available, we
check if a root partition of any other arch is available and use that
instead if we found one.
2021-11-24 15:06:38 +01:00
Lennart Poettering
60c5f7002b extension-release.d/: add a new field SYSEXT_SCOPE= for clarifying what a system extension is for
This should make things a bit more robust since it ensures system
extension can only applied to the right environments. Right now three
different "scopes" are defined:

1. "system" (for regular OS systems, after the initrd transition)
2. "initrd" (for sysext images that apply to the initrd environment)
3. "portable" (for sysext images that apply to portable images)

If not specified we imply a default of "system portable", i.e. any image
where the field is not specified is implicitly OK for application to OS
images and for portable services – but not for initrds.
2021-11-23 22:55:11 +01:00
Lennart Poettering
a4e0d61713 dissect-image: when extracting metadata from image also check if it contains init system
It's good to know whether we can boot the image.
2021-11-23 22:55:11 +01:00
Lennart Poettering
88b3300fdc dissect-image: load embedded verity signature info from image
This adds support for actually using embedded signature data from
partitions.
2021-09-28 17:02:54 +02:00
Lennart Poettering
8ee9615e10 dissect-image: discover verity signature partitions
This doesn't make use of the discovered partitions yet, but it finds
them at least.
2021-09-28 17:02:27 +02:00
Lennart Poettering
495367666b dissect-image: rename dissected_image_has_verity()/_can_do_verity()
Let's also pick more precise names for these helpers that are used for
the tabular output: one checks whether a partition is candidate for
verity at all, and the other checks if it is ready to be used for it.
Let's make this clearer in the name.
2021-09-10 14:15:00 +02:00
Lennart Poettering
c3c88d67c0 dissect-image: rename verity flag booleans
Let's make the booleans indicating verity state a bit more descriptive.

Let's rename:

    can_verity → has_verity: because that's really what this about
    whether verity data is included in the image. Whether we actually
    can use it is a different story.

    verity → verity_ready: this one should tell us if we have everything
    need to actually set it up, hence explicitly say "ready to use" in
    the name.

No change in behaviour. Just a bit of renaming.
2021-09-10 14:14:53 +02:00
Luca Boccassi
9ccb531a5f dissect-image: add extension-specific validation flag
Allows callers to specify which image type they are looking for
2021-09-06 12:13:53 +01:00
Luca Boccassi
a36429971a dissect: use DISKSEQ when waiting for block devices
DISKSEQ is a reliable way to find out if we missed a uevent or not, as
it's monotonically increasing. If we parse an event with a smaller or
no sequence number, we know we need to wait longer. If we parse an
event with a greater sequence number, we know we missed it and the
device was reused.
2021-07-28 20:07:01 +01:00
Yu Watanabe
19cc6d5e54 tree-wide: fix typo 2021-05-09 14:36:19 +09:00
Lennart Poettering
21b61b1dd2 dissect-image: add support for optionally mounting images with idmapping on 2021-05-07 22:44:05 +02:00
Lennart Poettering
81939d9d5e dissect-image: optionally, grow file systems on mount
The new GPT partition flag the previous commits added is now honoured on
mount.
2021-04-23 17:56:23 +02:00
Lennart Poettering
de98f63140 dissect: look for new GPT partition flag marking partitions for growing
systemd-repart can grow partitions dynamically at boot, but it won't
grow the file systems inside them. In /etc/fstab you can request that
via x-systemd.growfs. So far we didn't have a nice scheme for images
with GPT auto-discovery however, and that meant in particular in tools
such as systemd-nspawn the file systems couldn't be grown automatically.

Let's address this: let's define a new GPT partition flag that can be
set for our partition types. If set it indicates that the file system
should be grown to the partition size on mount.

This commit adds the flag and adds code to discover it when dissecting
images. There's no code yet to actually do something about it.
2021-04-23 17:55:45 +02:00
Lennart Poettering
4a62257d68 dissect: ignore udev database entries from before the loopback attachment
This tries to shorten the race of device reuse a bit more: let's ignore
udev database entries that are older than the time where we started to
use a loopback device.

This doesn't fix the whole loopback device raciness mess, but it makes
the race window a bit shorter.
2021-04-20 17:20:38 +02:00