Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Linux as a bootloader? Encrypted LVM2 rootfs + kernel?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
Guenther Brunthaler
Apprentice
Apprentice


Joined: 22 Jul 2006
Posts: 217
Location: Vienna

PostPosted: Thu Sep 07, 2006 1:22 pm    Post subject: Linux as a bootloader? Encrypted LVM2 rootfs + kernel? Reply with quote

Hi all,

when studying the complexity and power of certain bootloaders such as GRUB which understand filesystems, allow editing and viewing of files etc, I wonder why not also next logical step on the "boostrap adventure game" was taken yet:

Why not using a fully-blown Linux kernel itself as a second-stage bootloader?

I think it must be possible somehow to achieve this: loadlin does a very similar job, only from within a running DOS. So why should the same not also be possible from within a running Linux? And I know there is a kexec system call, allowing to replace a running kernel by another one much in the same way an exec can replace an executable by a different one within a running process.

I know, it's just a weird idea and I have not yet any idea how to actually make all that work as intended, but read on if you are the adventurous type of person!

The possibility to "boot Linux from Linux" should then allow a boot sequence like the following:


  • Precondition 1: There are only 2 partitions on your hard disk. The first partition is as small as possible (depends on logical HDD geometry and partition scheme used, but will typically be 8 or 16 MB). This partition contains nothing else than a Linux kernel image with an included ramfs or an initrd image.
  • Precondition 2: The rest of the harddisk is one big partition, completely encrypted using truecrypt or LUKS. Inside that encrypted container (when decypting it), a hard disk image can be found, which consists of one big LVM2 partition. This will prevent any bad guy who stole your notebook to figure out your actual partition layout.
  • Precondition 3: Within that encrypted LVM2 partition, there are all the partitions (as LVM2 locical volumes) one actually wants to work with, incuding the root filesystem partition, and also a /boot partition.
  • Precondition 4: The /boot partition (inside its encrypted container) contains the actual Linux kernel to be used for the installation. Which means it is also encrypted and makes it impossible for an attacker to easily modify it (such as adding a keylogger to the kernel to spy out your passwords).
  • When actually starting the boot sequence, the Linux image on the first (unencrypted) disk partition is booted first. This can be a rather stripped-down version of the kernel, because it needs no drivers for most of the hardware devices except keyboard, console and block-level hard disk access. If you are using an initramfs image instead of an initrd image, you will not even need a filesystem driver. (See the "early userspace" document in the Linux kernel Documentation subdirectory for more details.)
  • As the last stage of the first Linux kernel's boot process, it will decompress the contents of its embedded initramfs cpio-archive into a RAM-based root filesystem, and transfer control to the /init script it finds there, or to /linux in the case of the initrd method. I'll assume the initramfs method for the rest of this posting.
  • This script loads any necessary keymap, opens a VT console and prompts the user for a password.
  • Then it passes that password to truecrypt/LUKs, which will in turn provide a block device in /dev/mapper/, which will decrypt the contents of the second hard disk partition on the fly when accessing it, basically turning it into a virtual decrypted hard disk.
  • Now the script must initialize LVM2, allowing access to the logical LVM2 volumes on that virtual disk.
  • It now has access to the /boot partition on the LVM2 volume also.
  • The script must then somehow manage to load the kernel image from that /boot partition into a RAM-buffer which has been allocated in a way which allows co-operation with the kexec system call. If this is only a single image file, it may not even be necessary to use a filesystem on /boot. Just open the contents of /boot as a .gz or .zip or .bz2 "file" directly from the block device, and extract what's inside. But of course a filesystem can also be used if you prefer.
  • Now the password required for decrypting the encrypted disk must be added to the kernel image in its RAM buffer in a way which allows the second kernel to extract that information later.
  • Next, the encrypted disk, LVM2 and any filesystems that might have been mounted must be unmounted/shut down/remounted readonly.
  • As the last action of its life, our first-stage /init script now must trigger a kexec kernel call, transferring control to the second kernel in its RAM buffer.
  • Now the second stage Linux kernel image, originating from the encrypted LVM2 /boot partition, begins its own boot sequence. This is the actual kernel we want to be using. It will typically also be a more up-to-date version than the stage 1 kernel. The stage 1 kernel will not be updated very often (if ever). And as the stage 1 kernel will never be used to do networking or other potentially dangerous tasks, there is also no need to update it regularly. Its only job is to finally launch the second stage kernel. It doesn't matter whether it may be buggy in some other respect. It will only have a a very short lifetime (= while it is running).
  • The second stage Linux will also contain an initramfs image and an /init script, passing control to it.
  • This second stage /init script must now somehow extract the password which has been attached to the second stage kernel in-RAM image by the first stage /init script.
  • Then it should pass that password to its own copy of truecrypt/LUKS in order to mount the encrypted second hard disk partition again as a virtual decrypted hard disk.
  • After doing that, the /init script must be sure to wipe the password from RAM, so nobody will be able to find it there by examining /proc/kcore or by similar methods.
  • /init must then initialize LVM2 and mount the intended root file system of the installation as a read-only filesystem from its logical LVM2 volume.
  • As its last step, /init must erase any files in the current RAM based root filesystem, including itself, and do a chroot to the mounted root file system for the installation, exec'ing /sbin/init there.
  • /sbin/init will then do the remaining system initializations as usual. However, the RC scripts must be somehow tweaked to not reinitialize the disk encryption or LVM2 driver in a way which renders the mounted LVM2 root filesystem unusable.


So, that's what I want!

I know that it's crazy, mad, absolutely nuts, pure science fiction and that no one ever has made anything like that before. (At least from what I've heard so far.)

But - wouldn't it be nice to have such as system?

As stated at the beginning of my posting, my knowledge about Gentoo and Linux in general is not deep enough yet to implement this on my own.

But perhaps one of you, reading this, can contribute useful ideas how to actually implement a system like (or at least similar to) the one described.

So, let's just discuss it!

And finally, here are some more thoughts about the issue:

  • Having an LVM2 root filesystem is extremely useful. As LVM2 volumes can be resized on the fly, you need not know how to partition your system in advance.
  • For instance, it may be clear from Gentoo experience that it might be a good idea to use a separate partition for /var/tmp. And another one for the swap file. But what about smaller "/"-subdirectory trees like /var, /opt, /usr or /usr/share? Should they also go into separate partitions, or remain part of "/"? And how large should they be made?
  • If the rootfs is an LVM2 volume, it will be all easy to change this. You can shrink or enlarge "/", add or remove partitions; just as you like.
  • If "/" is on a conventional partition with a fixed size, you are screwed in such cases, and it may take a complete backup, repartitioning, and full restore to adapt the partition layout to changed storage requirements.
  • Having an encrypted kernel also has several advantages: As it will be loaded from an encrypted volume, you can trust it to a much higher degree to not have been tampered with since you installed it there.
  • Data integrity. Disk encryption will usually render the contents of a complete disk cluster completely unusable if only a single bit of the original (encrypted) disk cluster was flipped. Which means you will notice it when you try to use the data. There is no chance that only a single digit in your spreadsheet or C source code file will subtly be changed into another character, going by unnoticed with possible catastrophic consequences.
  • If an attacker still wants to get your password, his only chance is to modify the stage 1 kernel, because everything else is encrypted. However, this stage 1 kernel has only a very limited lifetime: It will soon be replaced by the stage 2 kernel, which cannot directly be modified by the attacker.
  • The stage 1 kernel is also a minimal kernel only, not providing an IP stack or any other means for the attacker to communicate any captured password via a network connection.
  • Of course, the attacker could replace the stage 1 kernel with a modifed version which includes an IP stack. But the very restricted size requirements of the stage 1 kernel in its small partition will make this at least difficult.
  • But in order to be sure, the first partition can be removed completely from the hard disk, moving it onto some external storage medium, such as an USB memory stick or a bootable CD. As long as you always remove that boot medium after successful booting (and take it with you when you go away from your notebook), no-one who steals your notebook will be happy with the contents of your hard drive.
  • Or the first (stage 1 linux) partition may remain on the hard disk, but its contents will be encrypted as well. Your USB/CDROM boot medium will then only need to contain a primitive real-mode BIOS-based boot loader, which hooks INT13 and its LBA extensions, providing on-the-fly decryption of the encrypted partition which contains the stage1 kernel. Then control can directly be transferred to some boot loader on the encrypted partition, being itself encrypted. As decryption is transparent due to the hooked INT13 driver, the Linux loader will be able to load the encrypted stage 1 kernel without any additional modifications. The INT13 hook code from your USB stick may even use a constant, hard-coded decryption key, because the attacker won't have access to the USB stick anyway. Also, the stage1 kernel will still prompt for the actual password to use, so the stage 1 decryption password is not too security-critical. This approach may be interesting to you if your USB stick has only a very limited capacity (is too small to contain a compressed linux kernel image on its own).
  • Or a combined method can be used: The USB stick contains the kernel image and an initramfs, but this initramfs image is only very minimal: It just contains a single, statically linked /init executable which prompts for a password. (This will eliminate the need for a shell or shared libraries.) It does not contain any device mapper or LVM2 or truecrypt/LUKS disk drivers. These have been compiled as modules, but the driver files are not part of the initramfs image. Then the statically-linked executable uses the password to decrypt the contents of the first hard disk partition on the fly, using its own compiled-in decryption routines, and populates the initramfs with the decrypted files extracted from the first hard disk partition. Those extracted files will include the kernel modules required for supporting device mapper, disk encryption etc. It also contains any required shell (such as ash or busybox), helper binaries and all the required shared libraries, as well as the actual initialization script for doing the remaining setup. This way the actual kernel image including its initramfs can be very small, as the rest of the required binaries will be read from the encrypted first partition on the hard disk.
  • As a modification of the above scheme, it is also possible to skip the password query and use a hard-coded password from within the scripts instead. Considering your USB stick to be a "key" then, it's not actually necessary to prompt the user for a password. But I am far from recommending this method generally, as even keys can get stolen.
Back to top
View user's profile Send private message
MrUlterior
Guru
Guru


Joined: 22 Mar 2005
Posts: 511
Location: Switzerland

PostPosted: Fri Sep 08, 2006 9:49 am    Post subject: Reply with quote

Frankly I don't see the advantage of your two kernel approach, it sounds like an overly complex method to achieve something that is already quite common: full disk encryption.

I presently boot my workstation off a business card CD that goes with me everywhere. This CD contains;

  • grub
  • my full kernel
  • root.gpg
  • gnupg statically compiled
  • gentoo 2006.1 minimal livecd (for use in rescue/repair)


Basically when I boot, loop-aes prompts me for a password to decrypt "root.gpg" (gnupg convential encryption) which is the key for the RAID1 which makes my / - this is ciphered with aes256, once the root FS is mounted & pivot_root executed, it then compares the SHA1 sums stored on / against the contents of the boot CD. Checksum failure results in immediate poweroff _BEFORE_ net.eth0 and net.eth1 are started or /home is mounted. If the checksum's pass, it then starts crypto-loop which will mount all the other partitions using keys from /etc/keys and the boot continues as normal.

In the event that the CD is compromised, all I have to do is re-encrypt / (only 1gb) the larger partitions keys are not contained on the boot cd therefore they can remain. Additionally even if a potential attackers has the CD, they still need a the passphrase to decrypt the keys it contains. Physical access to the PC is now secure as it's unbootable without the CD and the harddrives contain no cleartext at all ...

Am I missing some vital function that the 2 kernel approach would provide?
_________________

Misanthropy 2.0 - enough hate to go around
Back to top
View user's profile Send private message
batistuta
Veteran
Veteran


Joined: 29 Jul 2005
Posts: 1384
Location: Aachen

PostPosted: Fri Sep 08, 2006 9:58 am    Post subject: Reply with quote

Whichever approach you guys decide that is the easiest, I would like to learn more about it.
MrUlterior, you say that you have already implemented this. However, your description is not enough for a newbee in encryption to get the thing working. I know it's a lot of work, but if you manage to write a tutorial or how-to on this I'm sure a lot of people will benefit from this. I know people looking for a solution like you are describing, but I haven't found any decent beginners tutorial. However, full disc encryption is something that even beginers need to do sometimes (by beginners I mean Gentoo beginers, to Win beginers :wink: )

If on the other hand you've learned this from some already published site, I would really apprecite some link. I like your approach, I would only change Gentoo LiveCD with System rescue since Gentoo Live doesn't support reiser4
Thanks!
Back to top
View user's profile Send private message
MrUlterior
Guru
Guru


Joined: 22 Mar 2005
Posts: 511
Location: Switzerland

PostPosted: Fri Sep 08, 2006 1:10 pm    Post subject: Reply with quote

batistuta wrote:
...
MrUlterior, you say that you have already implemented this. However, your description is not enough for a newbee in encryption to get the thing working. I know it's a lot of work, but if you manage to write a tutorial or how-to on this I'm sure a lot of people will benefit from this ...


I began by reading this:

http://loop-aes.sourceforge.net/loop-AES.README

And based off that, I wrote the following whilst building my first encrypted workstation:

http://xs.mw/wiki/index.php/Gentoo_Loop-AES_Root_Usb_Boot

However that doesn't describe my current system, it differs in that:


  • It loads the keys from a usb key
  • It boots off a hard disk ( /boot/ is actually cleartext on the hard drive )
  • You need a working installation, and it requires RAID you build your encrypted installation on a degraded array until you're confident and then overwrite your (old) install and add the disk into your array once the process complete
  • It says you require "sys-apps/util-linux-2.12r-r2" which is no longer in portage, in reality any version subsequent to 2.12r-r2 will work aslong as you have the "crypt" and "keyscrub" USE flags.


It's also quite badly written and overly complex. If want & you're patient, I'll sit down & write one beginning with a fresh install & including the scripts I've implemented more reccently. It's actually quite easy to achieve.

Quote:
If on the other hand you've learned this from some already published site, I would really apprecite some link. I like your approach, I would only change Gentoo LiveCD with System rescue since Gentoo Live doesn't support reiser4
Thanks!


I actually use reiserv4 too, I just patched & rebuilt the kernel on the livecd before building my boot cd.

You can get an idea of how I did that from this:

http://xs.mw/wiki/index.php/Linux:_Tips_%26_Tricks#Create_a_Gentoo_rescue_CD
_________________

Misanthropy 2.0 - enough hate to go around
Back to top
View user's profile Send private message
Guenther Brunthaler
Apprentice
Apprentice


Joined: 22 Jul 2006
Posts: 217
Location: Vienna

PostPosted: Fri Sep 08, 2006 6:42 pm    Post subject: Reply with quote

MrUlterior wrote:
Frankly I don't see the advantage of your two kernel approach, it sounds like an overly complex method to achieve something that is already quite common: full disk encryption.


Full disk encryption will of course be the ultimate solution.

MrUlterior wrote:
Am I missing some vital function that the 2 kernel approach would provide?


If you only have a single box, your solution will certainly be sufficient.

But I have the problem of having got different boxes with completely different kernel settings.

Using modular kernels can eliminate some of the differences between boxes, but not all.

For instance, you have to decide for which specific CPU family to tune your kernel.

That's certainly not a matter if all your boxes have the same CPU family.

But otherwise, you may want a different kernel for each box.

So, using the single-kernel CD approach, this would either mean of using a generic kernel on that CD, or creating multiple CDs; one for each box. Or you put multiple kernels on a single CD, but that requires you to select the right kernel at startup manually.

Also there is the update problem: Each time a kernel update arrives, you have to re-burn your CD. Which is certainly doable, but I would rather prefer to avoid this.

If the stage-2 kernel of each machine is in its (encrypted) /boot directory on each local machine, kernel updates are trivial: Just run the kernel makefile, install the kernel to the local machine's encrypted /boot directory and that's all for the update. No need ever to re-burn that CD again, because the kernel there can be as old as it can get.

The only reason to update the CD kernel would be a change in the filesystem on the local machines which disabled the CD kernel from reading the stage-2 kernel from the /boot partitions.

But even that can be cirumwented by not using a filesystem on /boot at all; just write the stage-2 kernel to the raw block device at partition /boot, wrapped in a tar.gz file which takes care of the file lengths within the tar file four you.

In this case you only needed to update your CD if the TAR file format ever will be changed.

Or choke on TAR and do it yourself: 4 bytes length, followed by the uncompressed (no problems in case of a gzip file format change either) kernel image, both written on the block device. Then you won't ever need to update your CD again.

That's why I wanted to have 2 kernels: One stage-1 kernel for booting, which is a very generic kernel and slow. But this won't matter, because it will only be used to boot from that USB stick, ask for the password, and then use the local, optimized kernel from the local machine to take over its job and continue booting.

That stuff in my post about having a non-encrypted kernel in the first partition was rather intended for those who don't have a bootable CD or USB stick.

The stuff about the minimal kernel was for those with very old USB sticks, where kernel size was a problem.

The version which I would prefer personally was that of the whole stage-1 kernel being moved to an USB stick, which is similar to your CD approach.

The difference is only that I want to continue with a machine-specific kernel, which requires that kexec call.

The problem with the kexec call is that it seems only to be used for a crash dump currently.

So, in the worst case I will need to write a kernel module which allows loading the stage-2 kernel from userspace, but as I'm still a Linux newbie this is a bit beyond my reach yet. (There is still so much to learn, and kernel programming is among the last things on my list.)

So I'm hoping there are some tools out for that purpose already, even though I was not yet able to find one.
Back to top
View user's profile Send private message
batistuta
Veteran
Veteran


Joined: 29 Jul 2005
Posts: 1384
Location: Aachen

PostPosted: Sun Sep 10, 2006 8:56 pm    Post subject: Reply with quote

Thanks very muc MrUlterior for your tips and links, I really appreciate and I will certainly look into that.

I'm sorry Guenther for having diverged a bit from your topic. Your approach also sounds like it could have its place in some more complex systems. I will start with MrUlterior's approach in order to learn a bit on the subject and follow your progress as well.

My only suggestion, is that whatever you guys do then gets documented, I've found very time consuming and frustrating experience to use HD encryption, due to the fragmentation of docs in the net, or outdated material. It's just impossible to get the thing working without a deep understanding of many things and reading tons of docs. And even then (I've read a lot, believe me), it's hard to get it done. I know some people who even sticked to XP because of encryption easyiness...

So please document what you guys do, tons of people will appreciate it. :D
Back to top
View user's profile Send private message
MrUlterior
Guru
Guru


Joined: 22 Mar 2005
Posts: 511
Location: Switzerland

PostPosted: Mon Sep 11, 2006 1:12 pm    Post subject: Reply with quote

Guenther Brunthaler wrote:
But I have the problem of having got different boxes with completely different kernel settings. Using modular kernels can eliminate some of the differences between boxes, but not all.
...
So, using the single-kernel CD approach, this would either mean of using a generic kernel on that CD, or creating multiple CDs; one for each box. Or you put multiple kernels on a single CD, but that requires you to select the right kernel at startup manually.


Actually so do I, I use full encryption on 1 workstation, 1 fileserver (1.08tb), 1 firewall and 1 laptops.

Your average kernel + modules (in initrd or initramfs) is less than 20mb which means a single bootable CD could contain grub + approximately 32 kernels listed in the boot menu. If that's not sufficient a 1GB CF card is better still, a bootable DVD is even better because it's guaranteed read-only after being finalized.

How many kernels do you need?

I think that if you've kernels that are significantly different, you usually have binaries which are compiled to different CFLAGS (we are talking about Gentoo right? :-) -- in which case your two kernel approach is actually going to be much more problematic if you intend to share the boot medium; how're you going to go from 32bit busybox in your 1st stage to (for example) 64bit in the 2nd?

I think building a single kernel per architecture (32bit or 64bit) and per processor family (Intel Xeon, PIII, AMD Athlon, AMD Opteron, VIA C3 etc ) and simply building everything as a module except for whatever each piece of hardware needs to boot will be your best bet.

Guenther Brunthaler wrote:

Also there is the update problem: Each time a kernel update arrives, you have to re-burn your CD. Which is certainly doable, but I would rather prefer to avoid this.


That is true. Personally I don't upgrade kernels unless there's a good reason to do so. For example: remotely exploitable vulnerability or drivers I use have been improved. I used 2.6.9, then 2.6.15, skipped 2.6.16 and went to 2.6.17.7; I don't expect to change kernels again for several months, as 2.6.17 seems to be the most stable kernel I've had since 2.6.9 and performs slightly faster.

So I don't mind reburning the CD when I do. It's hardly inconvenient and I typically boot the .ISO in vmware-server first before burning them to CD; this filters out the most common probs.

Guenther Brunthaler wrote:

The difference is only that I want to continue with a machine-specific kernel, which requires that kexec call.

The problem with the kexec call is that it seems only to be used for a crash dump currently.

So, in the worst case I will need to write a kernel module which allows loading the stage-2 kernel from userspace, but as I'm still a Linux newbie this is a bit beyond my reach yet. (There is still so much to learn, and kernel programming is among the last things on my list.)

So I'm hoping there are some tools out for that purpose already, even though I was not yet able to find one.


I've not heard of anything like that either, if you do find it or develop something; keep us informed. It does sound interesting now that I understand why you want this a little better!
_________________

Misanthropy 2.0 - enough hate to go around
Back to top
View user's profile Send private message
batistuta
Veteran
Veteran


Joined: 29 Jul 2005
Posts: 1384
Location: Aachen

PostPosted: Mon Sep 11, 2006 1:38 pm    Post subject: Reply with quote

Guenther Brunthaler wrote:
Also there is the update problem: Each time a kernel update arrives, you have to re-burn your CD. Which is certainly doable, but I would rather prefer to avoid this.

If you use a USB stick this is not an issue, just mount boot which will happen to be in your pen drive and update your kernel. If writing is a worry, get a USB with write lock switch.

MrUlterior, could you post some of your scripts in your web site? I've taken a look at it and you have tons of nice tips, thanks for sharing that :D
Back to top
View user's profile Send private message
MrUlterior
Guru
Guru


Joined: 22 Mar 2005
Posts: 511
Location: Switzerland

PostPosted: Mon Sep 11, 2006 1:47 pm    Post subject: Reply with quote

batistuta wrote:
MrUlterior, could you post some of your scripts in your web site? I've taken a look at it and you have tons of nice tips, thanks for sharing that :D


I will do, glad the site's of use :-)
_________________

Misanthropy 2.0 - enough hate to go around
Back to top
View user's profile Send private message
Voltago
Advocate
Advocate


Joined: 02 Sep 2003
Posts: 2593
Location: userland

PostPosted: Mon Sep 11, 2006 3:17 pm    Post subject: Reply with quote

Just when you think you had a really clever idea, you can rest assured it will turn out someone had it before you ;)
http://kboot.sourceforge.net/
Back to top
View user's profile Send private message
MrUlterior
Guru
Guru


Joined: 22 Mar 2005
Posts: 511
Location: Switzerland

PostPosted: Mon Sep 11, 2006 3:20 pm    Post subject: Reply with quote

Voltago wrote:
Just when you think you had a really clever idea, you can rest assured it will turn out someone had it before you ;)
http://kboot.sourceforge.net/


Nice find! I'm going to be playing with that shortly.
_________________

Misanthropy 2.0 - enough hate to go around
Back to top
View user's profile Send private message
Guenther Brunthaler
Apprentice
Apprentice


Joined: 22 Jul 2006
Posts: 217
Location: Vienna

PostPosted: Mon Sep 11, 2006 3:29 pm    Post subject: Reply with quote

MrUlterior wrote:
Nice find! I'm going to be playing with that shortly.


Cool! Thanks for the link, Voltago!
Back to top
View user's profile Send private message
Guenther Brunthaler
Apprentice
Apprentice


Joined: 22 Jul 2006
Posts: 217
Location: Vienna

PostPosted: Mon Sep 11, 2006 3:34 pm    Post subject: Reply with quote

Voltago wrote:
Just when you think you had a really clever idea, you can rest assured it will turn out someone had it before you ;)
http://kboot.sourceforge.net/


I especially like finding out something like that before actually implementing that clever idea... That's why forums like this one are so useful! :-)

Thanks for the link.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum