You are not logged in.

#1 2014-11-08 17:49:47

Moviuro
Member
Registered: 2012-06-03
Posts: 74

Protect kernel & initramfs against offline tampering (encrypted /)

Hi all!

If like you're paranoid (yeah! paranoia FTW!), you most surely have an encrypted /.
If like me, you happen to not have secureboot or happen to have only legacy boot, there is no way to protect your kernel against tampering.
Also, there is AFAIK no way of protecting the initramfs as it is not checked by secureboot either.

So, assuming that the fact that your kernel has been tampered with is only critical if your computer reaches a network target, I wrote those units that will watch for changes in the kernel and initramfs files and update according hashsums, and then check them at each boot (sha512 runs in ~0.05sec on a i686 Pentium 4 with slow SATA drive). If checksums mismatch, you will be sent to rescue and network won't start.

Enjoy,

/etc/systemd/system/update-checksum@.path
[Unit]
Description=Watch /boot/%i to update its checksums

[Path]
PathChanged=/boot/%i

[Install]
WantedBy=multi-user.target
/etc/systemd/system/update-checksum@.service
[Unit]
Description=Update /boot/%i's checksums

[Service]
Type=oneshot
Environment=destination=/etc/sha512
ExecStartPre=-/usr/bin/mkdir ${destination}
ExecStart=/usr/bin/sh -c "/usr/bin/sha512sum /boot/%i > ${destination}/%i.sha512"
/etc/systemd/system/verifyhash@.service
[Unit]
Description=Verify /boot/%i's hash against the one stored on the encrypted drive
OnFailure=gotorescue.service

[Service]
Type=oneshot
Environment=location=/etc/sha512
ExecStart=/usr/bin/sha512sum -c ${location}/%i
/etc/systemd/system/gotorescue.service
[Unit]
Description=Go to rescue.target

[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl isolate rescue.target
/etc/systemd/system/<your network manager service>.service.d/afterhashcheck.conf
[Unit]
After=verifyhash@vmlinuz-linux.service verifyhash@initramfs-linux.img.service
Requires=verifyhash@vmlinuz-linux.service verifyhash@initramfs-linux.img.service

And then:

# systemctl enable update-checksum@vmlinuz-linux.path update-checksum@initramfs-linux.img.path
# ^enable^start
# pacman -S linux # to make sure the .path things work
# systemctl start verifyhash@vmlinuz-linux.service verifyhash@initramfs-linux.img.service # shouldn't send you to rescue
# # modify one of the hashsums in /etc/sha512 or wherever you put them, and close everything you're doing
# # yes, I'm going to throw you in rescue
# systemctl start verifyhash@vmlinuz-linux.service verifyhash@initramfs-linux.img.service # should send you to rescue

Last edited by Moviuro (2014-11-08 17:50:56)


bspwm, BTRFS over LUKS
Archlinux a lot, FreeBSD more and more...
Murphy's rule: The day you need a backup, you tell yourself you should have created some.

Offline

#2 2014-11-08 19:00:34

andy123
Member
Registered: 2011-11-04
Posts: 169
Website

Re: Protect kernel & initramfs against offline tampering (encrypted /)

Yeah, and why would someone that tampers with your kernel not simply modify it in a way that makes those checks fail/return true.

That's the problem with paranoia, where do you stop? How do you know, for example noone remotely exploited your Ethernet controllers firmware?


i'm sorry for my poor english wirting skills…

Offline

#3 2014-11-08 19:21:27

clfarron4
Member
From: London, UK
Registered: 2013-06-28
Posts: 2,163
Website

Re: Protect kernel & initramfs against offline tampering (encrypted /)

andy123 wrote:

Yeah, and why would someone that tampers with your kernel not simply modify it in a way that makes those checks fail/return true.

This is exactly why I haven't bothered with something like in opening post.

If I were that paranoid in practice, I'd have write a script which I'd run from a LiveCD to check the damn boot partition itself, along with encrypted backups of the boot partition to be done before I'd boot into the encrypted partition. I would also not have the /boot partition on the disk itself, but on a separate USB drive.

And this leads me to my second point. Would what I'd do be practical? Probably not the first bit, but the second bit is.


Claire is fine.
Problems? I have dysgraphia, so clear and concise please.
My public GPG key for package signing
My x86_64 package repository

Offline

#4 2014-11-08 19:32:33

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Protect kernel & initramfs against offline tampering (encrypted /)

Not a Sysadmin issue, moving to Community Contributions...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2014-11-08 21:57:25

Stebalien
Member
Registered: 2010-04-27
Posts: 1,239
Website

Re: Protect kernel & initramfs against offline tampering (encrypted /)

clfarron4 wrote:
andy123 wrote:

Yeah, and why would someone that tampers with your kernel not simply modify it in a way that makes those checks fail/return true.

This is exactly why I haven't bothered with something like in opening post.

If I were that paranoid in practice, I'd have write a script which I'd run from a LiveCD to check the damn boot partition itself, along with encrypted backups of the boot partition to be done before I'd boot into the encrypted partition. I would also not have the /boot partition on the disk itself, but on a separate USB drive.

And this leads me to my second point. Would what I'd do be practical? Probably not the first bit, but the second bit is.

Anyone who goes through the trouble to physically tamper with you computer could probably just flash the BIOS (or add/replace a random PCI device with DMA access...). The real purpose of secure boot isn't to prevent attacks like this, it's to provide a user-friendly (recoverable) data protection mechanism.

TL; DR;  If your paranoid, don't use your computer if it ever leaves your sight...

Last edited by Stebalien (2014-11-08 22:00:17)


Steven [ web : git ]
GPG:  327B 20CE 21EA 68CF A7748675 7C92 3221 5899 410C

Offline

Board footer

Powered by FluxBB