Repository navigation
Conversation
The "Encrypt" option is currently a no-op if the partition already exists. IOW it is only possible to apply encryption to newly created partitions. This is an unhelpful restriction when dealing with cloud images. It is undesirable to pre-encrypt the cloud image as at the time it is created there is no TPM to seal against, so it would have to be encrypted with a well known password. The image would then have to be re-encrypted on deployment to ensure every instance has a unique master key. It is simpler to distribute the cloud images with plain text root FS partition and then apply encryption to them at time of deployment. This halves the overhead, as there's no decrypt of existing data needed. There is of course still a risk that the cloud host has modified the original plain text image before the first-boot encryption runs, but even with this, guest owners will still benefit from having their image encrypted. To avoid changing the semantics of the existing "Encrypt" option, a new "EncryptDataShift" option is added, which is a boolean indicating whether existing partitions will be encrypted with a datashift operation applied to their contents For an existing partition to be encrypted, its size must grow by at least the LUKS2 header reservation size (32MB). Thus if the GrowFS=on option is set, we validate the growth is at least 32MB, treating it as a non-recoverable error if smaller. If GrowFS=off is set, however, we expect the image creator has adjusted the size of content in the partition such that the trailing 32MB are empty. This could be achieved by shrinking the filesystem after creation. Signed-off-by: Daniel P. Berrangé <[email protected]>
|
Hmm, let's take a step back:
|
|
note that right now repart is written in a style that makes it very robust towards abnormal termination: any new partitions it creates it only registers in the partition table as very last step, which means if the system is aborted in between, the next reboot will just start afresh. What you are proposing here is a lot more problematic, as it abnormal termination of the process will leave a half moved/converted partition around, which means the system is fucked. |
|
modern file systems such as btrfs leave space at the beginning (64k for btrfs), couldn't that be used for this? (would require some trickery to make sure the 64K of nothingness are prefixed again virtually on activation) |
Yes, that is a useful model and one I'm interested in supporting for Linux KVM hosts in future too. In particular if a guest owner requires an image to be encrypted from the very moment it leaves their local client, and wants to use TPM sealing, then pre-encrypting against an SRK that will be pre-enrolled with the future VM's TPM is probably the only usable and secure game in town. Without it, you're left having to use a plain passphrase on first boot and re-seal to the TPM afterwards which is tedious. This pre-sealing to the SRK though has downsides too though. The biggest is that this approach only works in Azure cloud today AFAIK. The second problem is that if you want to deploy the image many times, you don't want all the instances sharing the same LUKS master key and also don't want to upload many copies of the same template either. So something needs to do unique encryption per instance, and this needs to be completed either before booting the VM, or very early in boot before any unique data for the VM is stored, especially any secrets like SSH host keys. If a cloud supports pre-sealing against a future VM's TPM SRK that is certainly nice to have & use, but I'm looking for a more general solution that can be implemented in a guest disk image, that will work on /any/ cloud that provides a TPM and SecureBoot. This requires that it be self-contained within the guest, so there's no mandatory dependency on the cloud host to do re-encryption or TPM sealing. I could do this as an entirely standalone tool, but
Using
That is possible, but at the cost of having double the storage requirement for the running image. If the source of the files is before the encrypted root fs, deleting the partition just leaves a gap. If the source of thte files is after the encrypted root fs, then deleting the partition leaves space, but encrypting and growing would have to be done in two separate steps.
Actually with LUKS2 it should be possible to turn a plain text partition into an encrypted partition with minimal risk, because it keeps a journal tracking encryption progress for later resume from failure. This doesn't work with the code I've got in this MR, but IIUC it could be made to work. Essentially on later boots a check would be needed to see it re-encryption was marked as "in progress" and if so tell Since this logic is intended to run on first-boot of a guest, the consequences of abnormal termination are not severe anyway, as the guest won't have yet stored any important data. It can be thrown away and re-instantiated once more.
LUKS2 wants rather more than this - 32 MB is recommended for its re-encryption process, and what |
I should mention one further thing I'm interested in doing is proving integrity of the resulting encrypted FS, via some kind of signature that covers the original plain text content. If the plain text were a partition covered by dm-verity and a signature, then if either a block level copy + encrypt or in-replace re-encrypt is done, then the dm-verity should be able to validate the LUKS volume when first opened, before it is mounted / fscked. Obviously as soon as you start changing the block dev the dm-verity validation will no longer pass, but even a one-time only proof of the encrypted root FS partition is valuable. Thereafter, authenticated encryption of the LUKS volume is sufficient. |
|
So if we can make this robust towards abnormal termination I think this might be OK to do. Hmm, the shifting problem, isn't that irrelevant? I mean, we could simply require that either:
if you pick 2 then the partition table doesn't have to be changed at all, and instead this would become a job for cryptsetup or so. i.e. we'd add a new option "auto-encrypt" or so to crypttab. And it would take the offset as argument maybe. it would then check if the volume already has a lUKS header. If yes, it would just mount it as usual. If not, it would add it, and then initiate the reencryption. I am not sure how reencryption actually works though, never played around with it. Kinda leaning towards option 2, i.e. making this a systemd-cryptsetup problem rather than a repart problem. |
I think this could work, so I'm going to have a crack at a PoC for systemd-cryptsetup. |
|
Sorry it took me so long to comment here. It does sound like a good idea to me, but I have 2 thoughts:
We shouldn't need to do any shifting; cryptsetup's reencryption operation will handle that. If systemd-cryptsetup just resizes the filesystem down so it's $LUKS_HEADER_SIZE smaller than the partition size, the reencryption handles everything else. So really, the 'source' image just needs to make sure the filesystem has at least $LUKS_HEADER_SIZE bytes free. Then the same 'source' image can be used for both unencrypted VM instances as well as encrypted VM instances (from this perspective, at least). This is assuming, of course, that the block device/partition to be encrypted contains only a single filesystem; encrypting an entire block device with e.g. a partition table or lvm data or raid member would definitely require increasing the partition size.
I think the encryption needs to be done offline, and boot needs to be delayed until it's complete. Or, at minimum, there should be an option for the encryption to delay boot (that defaults to true). This is because if the end user wants an encrypted device/filesystem, they almost certainly expect it to be fully encrypted as soon as the instance first boots up. Otherwise, there is a window of time where part of their filesystem is not encrypted, and they shouldn't actually be using the system for sensitive operations. This will of course add a (probably significant) delay to first boot, which is why the pre-encryption (where possible) option is more attractive. |
But pre-encryption will not fix the delay issue, right? If you deploy a pre-encrypted image you need to change the master key, and for that you need a re-encryption, investing almost the same time. |
|
This seems to got stale so I wanted to revive the discussion. I believe that main goal of this PR and #29824 request is to be able to do self-encrypting CVMs. This may come handy when there's no support in the infrastructure for doing pre-encryption or to improve the scalability as a dedicated pre-encryption service may actually be a bottleneck as each VM needs to be passed through it. In addition to the encryption or re-encryption on the first boot, CVMs also require some sort of a verity protection. If we want to use the existing systemd-veritysetup, then the partition must me either / or /usr and the data must stay verity protected all the time before reencryption (or encryption) is over. I don't think it is easily achievable if we want to create a new LUKS container by re-using the data in the partition so I was picturing the following scheme:
Alternatively, we can go with 'copy': the image has a verity protected 'source' partition and verity for it (both dm-verity or fs-verity could do the job I guess) and enough space before it where systemd-repart can create a new root. This may be easier to implement, the only hurdle is to make sure verity information for the source is actually used. One additional CVM implication is that we probably want to preserve some knowledge about the source (e.g. verity hash) and the environment which did re-encryption (e.g. TPM quote) to the newly create (or reencrypted) target to protect against an attack when e.g. the host launches the image in a non-CVM environment and steals the new LUKS key from memory post-enctyption/re-encryption. |
Yeah this is an important concern. Today for e.g. Ignition, it's just not transactional in the general case of reprovisioning the rootfs. I think we could design something for this though at least for the simple case where all we're doing is adding a LUKS header. Another option is to just to always have the LUKS header but make it null cipher but sadly today that always adds a devmapper layer which adds nontrivial I/O overhead. |
This is also a security issue, as if anyone has a copy of that header, they can decrypt your volume even if you set a password later. |
(Replying to an old message here) We're talking about the rootfs here and since we're mounting that in the initramfs, we can initialize LUKS at the block device level before even mounting the filesystem - hence before any writes can be performed. Any writes that happen will be encrypted - we don't need to pause to wait for everything to be encrypted. For generic cloud images I think it's OK to have the "generic data" that's part of the OS remain unencrypted while the boot proceeds.
Hmm you seriously worried me here, this is a very important concern to look at. But as far as I can tell, That said, the more I think about this the more I feel it's misleading to have a LUKS header that isn't actually encrypting. I think what would be a lot cleaner here is a special "uninitialized LUKS" header. If that's detected, (probably in cryptsetup) then it's actually just mounted via dm-linear or so. But if the system provides credentials/config, we want to be able to initialize the LUKS setup for real (tpm2, passphrase, NBDE whatever). Involving repartitioning by default (especially this report discussion with |
Would a detached LUKS header help ? Start off with a plain volume, and when you want to encrypt, create a detached header with null-cipher and immediately trigger re-encrypt with a real cipher ? Downside is that you need to associate the detached header and corresponding volume in some manner, forever more. |
Uhm ... in disk-encryption-tool we are encrypting the rootfs device in the initrd, as commented here. Basically we move the file system a bit, to allocate the 32Kb required for the LUKS2 header, resize the filesystem to its minimal size, encrypt, and resize back to its full extend. I guess that for systemd-repart can be a problem the allocation of 32Kb unless uses libraries for specific file systems, but this is resolving all the issues mentioned here (pre allocation of LUKS2, speed of re-encryption, detached LUKS2 header, risk of re-use of volume key, etc) |
Clearly no matter what, some mechanism has to be defined to find and mount the rootfs. So far a somewhat implicit constraint/goal in some of these discussions is supporting the discoverable partition spec. It'd need to be changed for something like that AFAICS - like would we actually move the root type UUID from the original partition to the LUKS header or so? And we'd need to also define a mechanism to find the partition it covers since I don't think the header includes that (by design). One advantage of the "dynamic header" approach is we're not allocating any wasted space. AFAICS though most of the implementation work in that path would be on systemd (and uapi work on DDI spec changes), not cryptsetup. But Lennart's previous comment here seemed to lean towards making this something to solve in cryptsetup (closer to the "null header" proposal). Either way worth noting AFAICS such a disk image wouldn't be mountable with older tooling. I don't think that's a big deal though. I personally lean towards the cryptsetup side solution; it's relatively self-contained and the disk space overhead is negligible for most general purpose use cases. |
I don't want to move the rootfs by default just for LUKS, it's an unnecessary very large latency hit on first boot. |
This adds support for a new "EncryptDataShift=" option in systemd-repart config files, causing the pre-existing partition content to be shifted to make room for a LUKS header. See the commit message for further details.
My goal is to create a self-encrypting cloud disk image root FS for a traditional general purpose OS install. I'm aware that image based OS installs with immutable rootfs are conceptually nicer, but most cloud VM usage is still the traditional approach of fully writable root FS. I want to bring better protection to that legacy world and system-repart has 95% of the logic needed already.
This is intended to be run from the initrd before ANY partitions are mounted, and indeed before anything at all touches the root FS partition, even before fsck of root.
One problem is that if "EncryptDataShift=yes" is set, and systemd-repart is invoked while the partition in question is mounted (or otherwise in use, eg fsck), it happily runs the encryption process and the mounted (or otherwise used) FS gets very unhappy & crashy :-) I'm not sure what the best way to protect against this danger is ?
For testing I've modified the systemd-repart.service file in this nasty hacky way which is definitely not right:
I don't want to hardcode the device path, but I'm not sure how to otherwise identify the device path that contains the EFI boot device. systemd-repart can't detect the device directly because in initrd phase where re-encryption has to run, nothing is mounted, so it can't detect the blockdev from the /sysroot mount point.
Since this re-encryption adds a LUKS format, it caused dev-gpt\x2dauto\x2droot.device to be stopped and renamed to dev-gpt\x2dauto\x2droot\x2dluks.device which is then unlocked to create a new dev-gpt\x2dauto\x2droot.device. This caused a race with the following systemd-fsck-root.service running before LUKS had been unlocked, hence the 'sleep 3'. This is all because I'm really running this too later. I think the right thing is to get this run before the systemd-gpt-auto-generator runs but not sure how to express a dependency on a generator ?
More generally I'm not sure if its possible to have a single 'systemd-repart.service' unit file definition that will work correctly inside an initrd and also be able to run on the live root fs later, or whether a second 'initrd-systemd-repart.service' is a better bet.
As an illustration of this working in a real Fedora cloud image