DM-verity
This material is work-in-progress ... Do not follow instructions here until this notice is removed. |
Why DM-Verity?
Possibly you want to continue the RoT (Root of Trust) from the hardware (Hardware TPM as Root of Trust, which ensures only the hardware with the correct secret in the TPM and decrypt the disk/file encryption) -> secure boot -> to NOW dm-verity rootfs (which ensures your root-image/root-parition has not been tampered with).
What's the difference between me just setting the root partition as read-only (or using Fedora's Immutable Root Partition) vs this?
DM-Verity disallows tampering with the read-only partition, and with this consideration, you may use ERO-FS or SquashFS to generate Read-Only Root-Paritition Images. If you set your EXT4 file system to writable, and DM-Verity were to use it, it would be seen as "corurpted" and not boot anymore, because even just ONE tiny data change to the root image/partition would render it "corrupted". This is the same technology used in Android and Chrome OS Devices.
What is ERO-FS?
[Google thought about implementing ERO-FS with DM-Verity] [1]. But even if Google didn't use it for some reason, ERO-FS is still an excellent and fast compressed read-only filesystem.
Here is an excerpt about mkfs.erofs
on [Arch Linux Wiki] [2]:
mkfs.erofs(1) offers an attractive alternative to ext4 or squashfs on the root partition. EROFS, like squashfs, does not allow writes by design and has better performance in many cases than comparable filesystems on flash and solid-state media. It uses lz4 compression by default and was designed for Android phones by Huawei, which make extensive use of dm-verity.
Installing
Working with ERO-FS
First, some considerations, you may use a strictly READ-ONLY file system, such as SquashFS or ERO-FS, this wiki will be using EROFS (if you use a custom kernel, make sure it supports a file system such as ERO-FS, the default linux-lts does). [ERO-FS Github] [3] contains some information about how to compress and make your own read-only root image.
$ apk install dracut erofs-utils
$ mkdir image && cd image
$ wget https://dl-cdn.alpinelinux.org/alpine/v3.21/releases/x86_64/alpine-minirootfs-3.21.0-x86_64.tar.gz
$ tar -xvf alpine-minirootfs-3.21.0-x86_64.tar.gz && rm alpine-minirootfs-3.21.0-x86_64.tar.gz
Command below will generate an ERO-FS Image compressed with lz4 as ./erofs.img (erofs.img would be the root-partition file to use for dracut)
$ cd .. && mkfs.erofs -zlz4hc erofs.img ./image
Working with Dracut
If you want to use DM-Verity on Alpine Linux, you need to use Dracut (Thus also generating a secure boot EFIStub with it as well). Reason is because dracut is one of the only initramfs generators to let you import your own modules and files into its initramfs.
Also remember, mount your $ROOT_PARTITION
as read only (assuming you use EXT4, if using ERO-FS or SquashFS, ignore this), since any changes will cause dm-verity to detect it as "corrupt"
Install basic stuff:
apk add dracut dracut-core cryptsetup gummiboot gummiboot-efistub
Create a new partition for your DM-verity:
Edit "/usr/lib/dracut/modules.d/90crypt/cryptroot-ask.sh
" and add these lines right BEFORE "exit 0
" (add your $ROOT_PARTITION
directory and $VERITY_PARTITION
directory):
Contents of /usr/lib/dracut/modules.d/90crypt/cryptroot-ask.sh
To setup dm-verity (borrowed from Arch Linux Wiki):
veritysetup format /dev/$ROOT_PARTITION /dev/$VERITY_PARTITION
/etc/dracut.conf (put whatever your boot parameters are in "kernel_cmdline
"):
Contents of /etc/dracut.conf
Finally, generate UKI using dracut (replace 6.X.X-X-lts
with your kernel $VERSION
) (also replace /boot/efi/EFI/Linux/alpine-linux.efi
with your mount efi partition):
export DRACUT_KMODDIR_OVERRIDE=1
dracut --include /usr/lib/dracut/roothash.txt /usr/lib/dracut/roothash.txt --host-only --kernel-image /boot/vmlinuz-lts --kmoddir /lib/modules/6.X.X-X-lts --kver 6.X.X-X-lts --uefi --uefi-stub /usr/lib/gummiboot/linuxx64.efi.stub --force --compress lz4 /boot/efi/EFI/Linux/alpine-linux.efi /boot/efi/EFI/Linux/alpine-linux.efi efibootmgr
efibootmgr --create --disk /dev/[efi_partition] --part 1 --label alpine-linux --loader EFI/Linux/alpine-linux.efi
Unfortunately, using the default alpine linux kernel (linux-lts
) didn't work so download another distributions root system (like debians or devuan), and replace 6.X.X-X-lts, and vmlinuz-lts with anothers distributions kernel modules and kernel image (Or consider compiling your own linux-hardened kernel).
If you want to test if DM-Verity fully works, change the $ROOT_PARTITION
's files and it should say it is "corrupt", and not continue the boot process.