DM-verity

== Why DM-Verity? ==
Possibly you want to continue the RoT (Root of Trust [1]) - from the hardware (Hardware TPM as Root of Trust, which ensures only the hardware with the correct secret cryptographic key in the TPM to decrypt the disk/file encryption) -> secure boot (Secure Boot to only run approved signed EFIStub/signed Bootloader) -> To NOW DM-Verity rootfs (which ensures your root-image/root-parition has not been tampered with) -> Mandatory Access Control (AppArmor or SELinux - SELinux is disabled on default linux-lts btw) -> Sandboxed/Virtual-Machined Applications.
=== What's the difference between me just setting the root partition as read-only (basically just an Immutable Root Partition) vs this? ===
DM-Verity COMPLETELY disallows modifying any files in the root-partition/root-image, or even disallow a part of a root-image/root-partition (the root-image/root-partition needs to be INTACT and match the signed cryptography verified by DM-Verity on a different partition), and with this consideration, you may use ERO-FS or SquashFS to generate Read-Only Root-Paritition/File Images. If you set your EXT4 file system to writable, and DM-Verity were to use it, it would be seen as "corrupted" 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. Also related to use in A/B root, also used by Fedora.
=== What is ERO-FS? ===
Google thought about implementing ERO-FS with DM-Verity [7]. Google was reported to be using it in Android 13, and ERO-FS is an excellent and fast compressed read-only filesystem.
Here is an excerpt about mkfs.erofs on [Arch Linux Wiki] [8]:
 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 ERO-FS (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] [9] contains some information about how to compress and make your own read-only root image.
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)
----
==== Choose between a file or a partition ====
You can verify the integrity of your erofs/squashfs image, by comparing the hashtable. The hashtable can be a file or a partition. 
* The partition should be at least 10 percent the size of the final root-image squashfs/erofs. 
* When making a hashtable, running veritysetup should also provide a root hash. Keep this roothash for later use when unlocking the device via a kernel parameter/in the initramfs.
==== Working with mkinitfs/kernel-hooks/secureboot-hooks ====
Using mkinitfs, it is possible to force veritysetup in initramfs generation. It is recommended to make a chroot or docker so you don't mess with your system files, and by adding these files:
Edit /sbin/mkinitfs (put "# Copy custom init" before "# copy modloop signature"):
PUT "# Call custom script" before "if [ "$SINGLEMODE" = "yes" ]; then"
Then just do:
----
==== Working with Dracut ====
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: =====
===== Using Clevis for TPM =====
Input LUKS passphrase, remember that yes now it has tpm, but if possible remove old luks passphrase and use keyfile or different passphrase to unlock because tpm is better, and so if your computer breaks, you won't be locked out of your data!
===== Create a new partition for your DM-Verity: =====
Replace /dev/sdX with your actual device name.
Once in the fdisk interface:
# Press n to create a new partition
# Choose p for primary partition
# Select the partition number (e.g., 4)
# For the first sector, press Enter to use the default
# For the last sector, calculate 10% of your root partition size and use +SIZE format. For example, if your root partition is 100GB, enter +10G
# Press t to change the partition type
# Enter the partition number you just created
# Type 8e for Linux LVM type (closest to verity)
# Press w to write changes and exit
===== Adding custom lines for full disk decryption =====
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):
===== Generating an EFIStub with Dracut + DM-Verity =====
To setup DM-Verity (borrowed from Arch Linux Wiki):
/etc/dracut.conf (put whatever your boot parameters are in "kernel_cmdline"):
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):
Unfortunately, using the default Alpine Linux kernel (linux-lts) didn't work so download another distributions root system (like debians or devuan), and replace /lib/modules/6.X.X-X-lts, and /boot/vmlinuz-lts with anothers distributions kernel modules and kernel image (Or consider compiling your own linux-hardened kernel [11]).
== External Links ==
==== Root of Trust/Chain of Trust ====
* https://en.wikipedia.org/wiki/Chain_of_trust [1]
==== Full disk encryption with LVM and LUKS (AlpineWiki): ====
* LVM_on_LUKS [2]
==== Clevis Github ====
* https://github.com/latchset/clevis?tab=readme-ov-file#pin-tpm2 [3]
==== AppArmor Wiki (AlpineWiki) ====
* AppArmor [4]
==== GVisor ====
* https://gvisor.dev/docs/user_guide/install/ [5]
==== Gvisor Github stating docker is not a sandbox ====
* https://github.com/google/gvisor?tab=readme-ov-file#why-does-gvisor-exist [6]
==== Android-13 Possibility of using ERO-FS: ====
* https://pocketnow.com/erofs-android-13-explained/ [7]
==== DM-Verity (Arch Wiki): ====
* https://wiki.archlinux.org/title/Dm-verity [8]
==== ERO-FS Github ====
* https://github.com/erofs/erofs-utils [9]
==== Dracut (Arch Wiki): ====
* https://wiki.archlinux.org/title/Dracut [10]
==== Linux-Hardened Kernel (AlpineWiki): ====
* Hardened_linux [11]
==== My old post on how it worked ====
* https://www.reddit.com/r/AlpineLinux/comments/15oibcc/comment/k1l7jv0/ [12]
Category:Security Category:Booting