Encrypting your root partition on Fedora Core 5 and 6
For a variety of reasons I use Fedora Core 6 as my primary operating system, both at home and on my laptop. And for security reasons, I need to have my filesystem encrypted, so that in the event that my laptop is lost or stolen, my confidential data does not fall into the wrong hands.
Unfortunately, while Fedora Core 5 and 6 support an encrypted swap partition, it doesn’t yet support encrypted root filesystems. (Due to release timing, official support is currently targeted for Fedora Core 7.) However, that doesn’t mean it’s impossible. In fact, I’ve done it. This is the second of a two-part series on encrypting your Fedora Core system.
(Read Part 1: Encrypting your swap partition on Fedora Core)
Before we get started, I must make one thing perfectly clear: This code is still experimental, and it could eat your files for breakfast, your children for lunch, drink all your beer, or do worse things. It works perfectly for me, but I can’t be responsible for your system, especially if you screw something up yourself. So follow along carefully. Read through the whole thing once before trying it, so you know what to expect.
Currently neither the Fedora Core installer nor the Fedora Core installation media support encrypted filesystems, so in order to create an encrypted system, you will need an already installed system, as well as a second hard drive (or partition) large enough to hold all the files on your root partition. For this reason, it’s best to do this just after you install the system, as you won’t yet have any sensitive files on the computer. Even if you do, though, we’ll securely erase the drive to help prevent recovery of your data.
In addition, you’ll need a Linux rescue CD which does support encrypted filesystems. For this example, I use the Gentoo minimal Live CD, as it has all the necessary kernel and userspace support to create an encrypted filesystem.
First, download the Gentoo minimal Live CD from OSUOSL or another mirror. (Currently the latest Live CD is /releases/x86/2006.1/installcd/install-x86-minimal-2006.1.iso.) Then burn it to a CD. It’s small enough to fit on a business card size CD. You can use another Linux live CD as long as it has a recent kernel and recent LUKS userspace utilities.
I am going to assume that you accepted all the partitioning defaults when you installed Fedora Core, and that your root partition is located at /dev/VolGroup00/LogVol00. If you installed it elsewhere, you’ll have to substitute the correct device. Also, I’ll assume that your second hard drive (or partition) which we’ll use temporarily will be at /dev/sdb1. Substitute your actual partition when running the commands below. Keep in mind that anything on that drive or partition will be destroyed!
The first thing you need to do is boot your (freshly installed?) Fedora Core system. You’ll need to obtain a patched copy of mkinitrd that understands encrypted root filesystems and can prompt you for the password. You can either get the patch yourself from Red Hat’s bugzilla and build RPMs yourself, or you can use the ones I’ve built for my own use. If you’re extremely paranoid, I suggest you build your own, but the instructions on how to integrate patches into and build RPMs are beyond the scope of this article.
Download the mkinitrd RPM and its dependency, libbdevid-python (and nash on FC6). The mkinitrd-devel package is optional. I’ve also provided the source RPM, with patch included, in case you really want to rebuild the packages yourself.
Fedora Core 5:
- libbdevid-python-5.1.15-1.i386.rpm (required)
- mkinitrd-5.1.15-1.i386.rpm (required)
- mkinitrd-devel-5.1.15-1.i386.rpm
- mkinitrd-5.1.15-1.src.rpm (source)
Fedora Core 6: (This is currently BROKEN on Fedora Core 6. Do not proceed until an update has been posted and this notice removed.)
- libbdevid-python-5.1.19-1.i386.rpm (required)
- mkinitrd-5.1.19-1.i386.rpm (required)
- nash-5.1.19-1.i386.rpm (required)
- mkinitrd-devel-5.1.19-1.i386.rpm
- mkinitrd-5.1.19-1.src.rpm (source)
Then install the packages. On Fedora Core 5, you may need to use the --nodeps option with these files as they were built against rawhide (but they work fine).
Fedora Core 5:
[root@fedora ~]#
rpm --nodeps --replacefiles --replacepkgs -Uvh mkinitrd-5.1.15-1.i386.rpm libbdevid-python-5.1.15-1.i386.rpm
Fedora Core 6:
[root@fedora ~]#
rpm --replacefiles --replacepkgs -Uvh mkinitrd-5.1.19-1.i386.rpm libbdevid-python-5.1.19-1.i386.rpm nash-5.1.19-1.i386.rpm
The next thing you’ll need to do is update everything. Among other things this will pull in the latest kernel, which you’ll need for encrypted filesystem support. The patched mkinitrd will, when the kernel update installs, create an initrd which understands encrypted root filesystems. The patched mkinitrd requires at least kernel version 2.6.17-2174. (2.6.18-2200 is current in FC5 at the time of this writing, and 2.6.18-2798 is current in FC6.)
root@livecd ~ #
yum upgrade
Connect your second hard drive to the system, which we’ll use temporarily to encrypt all your files, and then boot from the Gentoo live CD or other live CD. Eventually you’ll get a root shell prompt. The first thing we’ll do is create mount points to mount our partitions:
root@livecd ~ #
mkdir -p /mnt/disk1 /mnt/disk2
Next we need to load up the logical volumes:
root@livecd ~ #
vgchange -ay
2 logical volume(s) in volume group “VolGroup00″ now active
Next we’ll create the temporary partition on your second drive. It will be created at /dev/mapper/temp. We’ll also encrypt it using a throw-away key so that the data can’t easily be recovered later. (The /tmp directory is in RAM on the Gentoo live CD.)
root@livecd ~ #
dd bs=32 count=1 if=/dev/random of=/tmp/temp-key
root@livecd ~ #cat /tmp/temp-key | cryptsetup luksFormat /dev/sdb1
root@livecd ~ #cryptsetup -d /tmp/temp-key luksOpen /dev/sdb1 temp
Now we’ll mount both the original partition and the temporary partition.
root@livecd ~ #
mount /dev/VolGroup00/LogVol00 /mnt/disk1
root@livecd ~ #mke2fs -j /dev/mapper/temp
root@livecd ~ #mount /dev/mapper/temp /mnt/disk2
Then we’ll copy all the files over to the temporary partition, then unmount the original partition.
root@livecd ~ #
cp -ax /mnt/disk1/* /mnt/disk2
root@livecd ~ #umount /mnt/disk1
The next step is to securely erase the original parititon which contained the unencrypted files. By default this command will write 25 different known and random patterns to the entire partition, which should leave the data formerly on it unrecoverable. Depending on the size of your drive, this may take several hours. You can add the -v option to see a progress report.
root@livecd ~ #
shred /dev/VolGroup00/LogVol00
If you didn’t have any sensitive data on the disk, or you don’t care very much, then you can just write one pass of random data to the drive, which you’ll need to do anyway to help frustrate any future cryptanalysis. If you fail to do this, an attacker could be able to determine what data on your drive is encrypted, and what is garbage, and have an advantage in trying to recover your data.
root@livecd ~ #
shred -n 1 /dev/VolGroup00/LogVol00
Finally, it’s time to set up the real encrypted filesystem and move your files back to your hard drive. First, you need to decide whether you want to use a passphrase to encrypt (an intermediate key which will then encrypt) your data, and boot directly from the hard drive, or whether you want to boot from a USB stick which contains your encryption key. I won’t be covering the USB stick method here, as right now it’s much more complex, the code isn’t well tested, and it’s much more inconvenient to use and maintain. As the code develops, I may post an update in the future.
Whether using a passphrase or a USB stick containing the key is the best method depends on your particular situation and the threats you are likely to face. For most people, the passphrase should be quite sufficient (provided it’s long enough). People who choose the USB stick method are those rare people who face torture or death if their data is compromised, and where simply losing the USB stick is sufficient to protect the data; since you don’t know the key, it can’t be rubber-hosed out of you. At the same time, though, you risk being captured with the USB stick still in your possession, which means all your efforts are for nothing. So there’s no one best solution for everyone.
The good news is you can start out with a passphrase now, and switch to a key on a USB stick later (when the code is better tested), without having to copy all your files around again.
So let’s encrypt your filesystem. Choose a long passphrase that’s easy for you to remember, but very difficult for anyone else to guess.
root@livecd ~ #
cryptsetup -y -d aes-cbc-essiv:sha256 luksFormat /dev/VolGroup00/LogVol00
Enter LUKS passphrase:
Verify passphrase:
Now we’ll map the encrypted device at /dev/mapper/root, create the filesystem and then mount it.
root@livecd ~ #
cryptsetup luksOpen /dev/VolGroup00/LogVol00 root
Enter LUKS passphrase:
Verify passphrase:
root@livecd ~ #mke2fs -j /dev/mapper/root
root@livecd ~ #mount /dev/mapper/root /mnt/disk1
Then we’ll copy all the files back from the temporary filesystem.
root@livecd ~ #
cp -ax /mnt/disk2/* /mnt/disk1
One last thing. The Gentoo live CD doesn’t have support for SELinux, so if you use SELinux, (it’s enabled in the default Fedora Core installation) then everything on the new filesystem needs to be relabeled. So we’ll ask for that to happen on the next reboot.
root@livecd ~ #
touch /mnt/disk1/.autorelabel
That just about covers it. Reboot the system, take out the live CD, and boot into your freshly encrypted new Fedora Core system! You’ll almost immediately be prompted to enter your passphrase. You have to get it right the first time; if you don’t, then you must hard reset the machine before trying again.
You can now erase the temporary hard drive you used for transferring your files around, if you like. The encryption key for it was random data which resided only in memory, and by this point will be long gone. Just to be safe, you can overwrite the disk partition you used again with some random data:
[root@fedora ~]#
shred -n 1 /dev/sdb1
That should cover it. This thing has gotten just about as long as I’m comfortable making a post, so for the USB stick instructions, you’ll have to wait for part 3 (you can read the bugzilla for hints, but the instructions there are for PowerPC platforms). I also plan a quick tutorial on encrypting non-root partitions, in case you’re one who made your disks into multiple partitions and put different parts of your system on each partition. So I’ll wrap both of those into the update in probably a week or two.
If you find anything confusing, or catch any technical errors, please let me know and I’ll get them cleared up or corrected as soon as possible. Thanks!
[...] (Read Part 2: Encrypting your root partition on Fedora Core 5) [...]
Pingback by Encrypting your swap partition on Fedora Core - Life with Linux — September 21, 2006 @ 2:33 pm
Great article, very useful indeed and I keenly look forward to the howto on encrypting non-root partitions. What about encrypting individual directories using the loop-AES or similar?
I love the style, very easy to follow, and current, most of the other stuff I’ve found is quite out of date.
Comment by Callum Macdonald — October 6, 2006 @ 3:36 am
Any idea of the CPU overhead of running an encrypted root partition? I’m seriously tempted, but I’m keen to know the performance implications up front.
Comment by Callum Macdonald — October 10, 2006 @ 4:33 am
Disk reads and writes will be slower, of course! How much slower depends on your CPU, but in practice (on a 1.4GHz Celeron M) it’s been almost unnoticeable.
Comment by Michael Hampton — October 10, 2006 @ 10:36 am
This is actually pretty important now that OMB has mandated that all civilian agencies in the U.S. government MUST encrypt all data on laptops.
As of right now windows users can buy PGP whole disk, Mac users are stuck (Filevault does not meet the requirements). LINUX users can at least try this option out (if they are competant).
Comment by Doug — October 19, 2006 @ 8:50 am
Well, this feature won’t be in Fedora Core 6 which will be released next week, but the unofficial directions provided here will still work, and I’ll keep the packages updated if necessary.
According to the bugzilla, it’s currently targeted for Fedora Core 7. Hopefully they’ll also get support into the installer, so you can have an encrypted system from the point of installation.
Comment by Michael Hampton — October 19, 2006 @ 3:19 pm
I also encrypt the root partition, but take a very different approach:
http://wiki.blagblagblag.org/Encrypting_Root_Filesystem
It’s also busted on FC6, which is why I came hunting here…
Comment by jeff — October 31, 2006 @ 11:38 pm
The problem on FC6 seems to be related to the 2.6.18 kernel. I haven’t yet had a chance to trace it farther than that, but it works fine with the 2.6.17 kernel.
Comment by Michael Hampton — November 1, 2006 @ 2:34 am
Aww, I saw the title and got my hopes up. I then read the article to find that this does not actually work in FC6
I hope someone can figure out how to make it work on the newest fedora.
Comment by ken — November 5, 2006 @ 4:02 pm
Have you tried the errata kernel-2.6.18-1.2239 kernel yet?
Comment by jeff — November 16, 2006 @ 2:46 pm
You know, https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124789 has been updated with newer support for FC6. You should update your blog to reflect the changes.
Comment by Kurt — December 4, 2006 @ 1:06 am
I have found a way to get my approach to this to work with 2.6.18 kernels. I had to append this to the kernel boot line:
ramdisk_size=32768 ramdisk_blocksize=1024
Perhaps it will help you too. I couldn’t even get 2.6.18 to work on an FC5 based system before. All happy now.
Comment by jeff — December 8, 2006 @ 5:55 pm
the mkinitrd script won’t install crypto stuff into the initrd.img until it finds that root is encrypted.
So, after I encrypt root, the machine won’t boot. Chicken/egg problem?
What am I missing?
Comment by Peter Jakab — December 27, 2006 @ 4:42 pm
Hm, yes, now I remember I did have that problem.
I worked around it by booting the fresh system from a Gentoo live CD, mounting it, chrooting into it and re-running mkinitrd. I had to use the Gentoo CD because the Fedora CD doesn’t have any of the crypto stuff.
I thought I’d forgotten something in the directions. I’ll get them updated sometime in the next week.
Comment by Michael Hampton — December 27, 2006 @ 5:36 pm
Excellent instructions Michael.
OK, have followed the instructions and both root and swap are encrypted and I can mount them and access them only through a bootable gentoo cd. My config is a 50Mb /boot partition and the root and swap partition 200Mb each.
The problem is that on reboot the kernel panics as it cannot access root (fc6). Can you please include instructions for how to use mkinitrd to patch the fc6 initrd.img with cryptsetup support? Currently no password prompt is displayed.
Many thanks
Manesh
Comment by Manesh Parmar — January 21, 2007 @ 7:44 pm
Hey,
for a secure system even try archlinux. Encrypted rootfs is a breeze with it. Go to archlinux.org visit the wiki and search for encrypted
have fun
Comment by Uhm — January 24, 2007 @ 12:51 pm
Wanted to give everyone a heads up that this does work on Fedora Core 6, using the latest patches from bugzilla.
My first attempt I was unsuccessful, and made a few changes for the second attempt. I believe the problem was my grub.conf (more later) but other changes were standard partitions (1st attempt) vs logical volumes (2nd) and the patch from comment #74 (1st) and #72 (2nd). Again, I believe either patch will work with either partitioning scheme, but I will explain what I have done.
First, install Fedora Core 6. You will need to build the RPMs on the same OS version you will install them (FC6 for FC6, FC5 for FC5).
Grab the mkinitrd source RPM from
http://mirrors.kernel.org/fedora/core/updates/6/SRPMS/
I have used
mkinitrd-5.1.19.0.3-1.src.rpm
Install the RPM as a standard user (not root). My ~/.rpmmacros contains
%_topdir /harddrive3/Temp/rpmbuild
which has a directory structure same as /usr/src/redhat/.
Download the patch from
https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124789
I have used the patch from comment #72 per above. Add to SPECS/mkinitrd.spec the second line
Source0: mkinitrd-%{version}.tar.bz2
Patch0: mkinitrd-5.1.19.0.2-enc-sup.patch
and later to apply the patch add to the prep section the third line
%prep
%setup -q -n mkinitrd-%{version}
%patch -p1
Do `rpmbuild -ba SPECS/mkinitrd.spec` and watch it build. Install the RPMs (as root) using the force method `rpm –replacefiles –replacepkgs -Uvh …`.
Reboot into Gentoo. Mount the drives, copy to a temporary location, encrypt the original drive, and copy back. To chroot (assuming root is mounted at /mnt/disk1/) do
mount /dev/hda1 /mnt/disk1/boot (or change hda1 to wherever your boot partition is)
mount –bind /dev /mnt/disk1/dev
chroot /mnt/disk1
mount -t proc proc /proc
mount -t sysfs sysfs /sys
Edit /etc/fstab on the mounted partition to point to the encrypted partition.
#/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
/dev/mapper/root / ext3 defaults 1 1
Swap should already have changed per instructions from page 1.
#/dev/VolGroup00/LogVol01 swap swap defaults 0 0
/dev/mapper/swap swap swap defaults 0 0
And /etc/crypttab
swap /dev/VolGroup00/LogVol01 /dev/random swap,cipher=aes-cbc-essiv:sha256
I believe you must edit fstab before running mkinitrd. Now cd to /boot and run
mkinitrd initrd-2.6.18-1.2798.fc6.img 2.6.18-1.2798.fc6
You will need to change kernel versions for a newer kernel.
DO NOT CHANGE grub.conf. DO NOT CHANGE this to /dev/mapper/root. See, my file looks the same.
title Fedora Core (2.6.18-1.2798.fc6)
root (hd0,1)
kernel /vmlinuz-2.6.18-1.2798.fc6 ro root=/dev/VolGroup00/LogVol00 rhgb quiet
initrd /initrd-2.6.18-1.2798.fc6.img
(You can see that my /boot partition is at hda2)
To view files in an initrd, copy the file to a temporary directory, gunzip, then run `cpio -i
Comment by Greg — April 9, 2007 @ 5:13 pm
You’re right, it doesn’t (currently) work in FC6, mainly because of a kernel update that came out post-release. Hopefully everything will be in place for this to work in FC7.
In the meantime, as soon as I get some time, I plan to update the directions and the code to get it working again on FC6.
Comment by Michael Hampton — April 10, 2007 @ 12:25 am
I’m a little confused. It does work, it’s working on my computer now. The two kernels I am using are the install kernel (2.6.18-1.2798.fc6) and the most recent (2.6.20-1.2933.fc6). Naturally, I have not installed any in between. Everything is working great with the instructions I posted as an addendum to yours.
Comment by Greg — April 11, 2007 @ 4:46 am
Of course. I just can’t officially endorse it until I get a chance to try it myself. It might be a few days before that happens.
Comment by Michael Hampton — April 11, 2007 @ 5:42 am
Confirmed working on Fedora7. Used patch from comment #81.
Comment by Greg — June 6, 2007 @ 4:12 pm