{"id":684,"date":"2014-08-17T15:18:38","date_gmt":"2014-08-17T19:18:38","guid":{"rendered":"http:\/\/blog.fritzhardy.com\/?p=684"},"modified":"2014-11-04T21:11:36","modified_gmt":"2014-11-05T02:11:36","slug":"fedora-linux-migration-part-4-encrypted-swap","status":"publish","type":"post","link":"https:\/\/blog.fritzhardy.com\/?p=684","title":{"rendered":"Fedora Linux Migration Part 4: Encrypted Swap and More"},"content":{"rendered":"<p>Encrypted swap is at least as important as encrypted data.  This is because the contents of nearly anything in memory can be swapped to disk, at any time, and for any length of time.  That includes passwords and like, en claire.  Encrypting the system swap volume cannot be overlooked.<\/p>\n<p>During our migration process we have cloned drives in <a href=\"https:\/\/blog.fritzhardy.com\/?p=549\">part one<\/a>, switched to an encrypted root in <a href=\"https:\/\/blog.fritzhardy.com\/?p=584\">part two<\/a>, and moved partitions around to make room for home data encryption in <a href=\"https:\/\/blog.fritzhardy.com\/?p=650\">part three<\/a>.  Now we will encrypt the swap partition.<\/p>\n<p><strong>Goals<\/strong><\/p>\n<p>We are free of many of the concerns with which we dealt when dealing with the encrypted root in <a href=\"https:\/\/blog.fritzhardy.com\/?p=584\">part two<\/a>, but we have two key goals:<\/p>\n<ol>\n<li>Support for hibernate<\/li>\n<li>Avoid multiple passphrase entry at boot without compromising security<\/li>\n<\/ol>\n<p>The first is a property unique to swap, that being the place were the contents of RAM are copied when a machine is put into hibernation.  Our encryption setup should support that.  The second is an artifact of our disk layout, this being a second encrypted volume to be encountered during boot.  There are more than a few ways to meet both of these goals, and some shortcuts that can be taken should one or the other not be desired.<\/p>\n<p><strong>Suspend vs Hibernate<\/strong><\/p>\n<p>Imprecision characterizes the names and labels for &#8220;hardware sleep&#8221; as it does many areas of technology, even after the  ideas and processes have been codified.  It is worth stating a couple of definitions:<\/p>\n<ul>\n<li>Suspend: Processes paused, most parts of the computer turned off except for RAM in order to save and resume machine state (aka &#8220;Sleep&#8221; or &#8220;Standby&#8221;)<\/li>\n<li>Hibernate: Processes paused, memory flushed to swap area of the disk before complete shutdown in order to save and resume machine state from full poweroff\/poweron (aka &#8220;Suspend-to-disk&#8221;)<\/li>\n<\/ul>\n<p>We pursue the latter, as that is all that is relevant for a swap change.<\/p>\n<p><strong>Possibilities<\/strong><\/p>\n<p>There are a number approaches for volume encryption, most of which we did not explore in parts <a href=\"https:\/\/blog.fritzhardy.com\/?p=584\">two<\/a> and <a href=\"https:\/\/blog.fritzhardy.com\/?p=650\">three<\/a>.  All boot-time decryption is handled by systemd-cryptsetup-generator, which understands \/etc\/crypttab entries as well as the rd.luks kernel command-line arguments we used previously.<\/p>\n<ol>\n<li>encrypted physical volume (not available with our chosen volume layout)<\/li>\n<li>crypttab swap entry with random key<\/li>\n<li>crypttab volume entry assuming passphrase<\/li>\n<li>crypttab volume entry specifying key stored on some device<\/li>\n<li>kernel rd.luks argument assuming passphrase entry<\/li>\n<li>kernel rd.luks argument specifying key stored on some device<\/li>\n<\/ol>\n<p>Option #1 is not available without redoing this particular storage layout from scratch, but would offer the advantage that all logical volumes atop a crypto-pv are implicitly encrypted: root, swap, whatever.  This route was not chosen simply because it did not seem to align with install-time Fedora defaults.<\/p>\n<p>Options #2, #3, #4 make use of \/etc\/crypttab, something we did not explore when setting up encrypted root and home, and entries here are generally set up by systemd later in the boot process after initrd.  In the entries below, the first is the simplest means of encrypting swap, and a facility unique to crypttab: recreating it with random key at each boot, leaving a plain-crypt (not LUKS) device mapper entry named crypt-swap.  The latter two point to a LUKS volume that would need to be cryptsetup manually ahead of time, with masochistically named device-mapper entries in the form luks-UUID:<\/p>\n<pre>\r\ncrypt-swap\t\/dev\/mapper\/Volume00-swap\t\/dev\/urandom\tswap\r\nluks-XXX-YYY-ZZZ-AAA-BBB-CCC\t\/dev\/mapper\/Volume00-swap\r\nluks-XXX-YYY-ZZZ-AAA-BBB-CCC\t\/dev\/mapper\/Volume00-swap\t\/etc\/luks\/swap.key\r\n<\/pre>\n<p>Out of this bunch, the first does not support hibernate by nature of its use of random key.  But <u>#2 and #3 are viable<\/u>, with concerns about key storage for the latter assuaged by its location within the encrypted root.<\/p>\n<p>Options #5 and #6 are the way we handled root in <a href=\"https:\/\/blog.fritzhardy.com\/?p=584\">part two<\/a>, for decryption by the initial ram disk.  This would add another rd.luks.uuid kernel argument in grub.conf pointing to the device in question:<\/p>\n<pre>\r\nrd.luks.uuid=XXX-YYY-ZZZ-AAA-BBB-CCC\r\nrd.luks.uuid=XXX-YYY-ZZZ-AAA-BBB-CCC rd.luks.key=\/path\/to\/swap.key\r\n<\/pre>\n<p>Out of these two, the second seems to succumb to problems with systemd within the initial ramdisk being able to make use of the argument, exacerbated by myriad documentation conflicts between the use of this option beneath the init\/upstart and systemd regimes.  This <a href=\"http:\/\/forums.fedoraforum.org\/showpost.php?p=1681988&#038;postcount=44\">post<\/a> contains a good explanation and possible workaround, but even should it work, there is the problem of where to store a key, since the encrypted root would not yet be available.  That leaves <u>#5 viable<\/u>.<\/p>\n<p>With some of our options relying on passphrase entry, entering multiple phrases one after another could be a problem.  It turns out that this is not a concern as long as we adhere to one condition: use of the same passphrase.  Though it is not documented well (or at all), passphrases and keys are cached during systemd cryptsetup (by plymouth) for attempt against every encrypted volume.  With the same one in use on both, the end result is one prompt at boot time.<\/p>\n<p>Three options meet our two goals: #2, #3, #5.  All work, but we select #5.  Because we already have the initial ramdisk decrypting root by passphrase, it makes some bit of sense to do the same for swap at the same stage.  Also, this approach means there are no concerns about key storage.  And lastly, specifying UUIDs on the kernel command line for luks means that it could still work in the event a default ramdisk is used.<\/p>\n<p><strong>Setup Encrypted Swap<\/strong><\/p>\n<p>Our disk layout is as follows:<\/p>\n<pre>\r\n\/dev\/sda (150GB)\r\n     sda1: ext3 \/boot (300MB)\r\n     sda2: lvm Volume00 (150GB)\r\n          Volume00-swap: swap (2GB)\r\n          Volume00-root: LUKS (127GB)\r\n               luks-cc7e78ee-87a9-4ad0-9c82-31de01389b34 ext3 (127GB)\r\n          unallocated: (19GB)\r\n<\/pre>\n<p>We will simply re-use the existing swap logical volume.  Though encrypting an existing volume destroys data, that of the swap partition is normally of no lasting consequence.<\/p>\n<p>First we need to disable swap:<\/p>\n<pre>\r\nswapoff -a\r\n<\/pre>\n<p>Write random data to the volume for best results:<\/p>\n<p>dd if=\/dev\/urandom of=\/dev\/mapper\/Volume00-swap<br \/>\ndd: writing to \u00e2\u20ac\u02dc\/dev\/mapper\/Volume00-swap\u00e2\u20ac\u2122: No space left on device<br \/>\n4194305+0 records in<br \/>\n4194304+0 records out<br \/>\n2147483648 bytes (2.1 GB) copied, 567.601 s, 3.8 MB\/s<\/p>\n<p>Encrypt the volume:<\/p>\n<pre>\r\ncryptsetup --verbose --verify-passphrase luksFormat \/dev\/mapper\/Volume00-swap\r\n\r\nWARNING!\r\n========\r\nThis will overwrite data on \/dev\/mapper\/Volume00-swap irrevocably.\r\n\r\nAre you sure? (Type uppercase yes): YES\r\nEnter passphrase:\r\nVerify passphrase:\r\nCommand successful.\r\n<\/pre>\n<p>Grab the volume UUID:<\/p>\n<pre>\r\ncryptsetup luksDump \/dev\/mapper\/Volume00-swap | grep UUID\r\nUUID:           8f747652-3e73-4297-9721-9e8cdd8d89b1\r\n<\/pre>\n<p>Masochistically open the volume at name reflective of the UUID:<\/p>\n<pre>\r\ncryptsetup luksOpen \/dev\/mapper\/Volume00-swap luks-8f747652-3e73-4297-9721-9e8cdd8d89b1\r\nEnter passphrase for \/dev\/mapper\/Volume00-swap:\r\n<\/pre>\n<p>Make swap:<\/p>\n<pre>\r\nmkswap \/dev\/mapper\/luks-8f747652-3e73-4297-9721-9e8cdd8d89b1\r\nSetting up swapspace version 1, size = 2095100 KiB\r\nno label, UUID=35d0810f-accb-4fc8-ad9c-3c88a28429ad\r\n<\/pre>\n<p>Edit \/etc\/fstab to reflect the new swap location:<\/p>\n<pre>\r\n\/dev\/mapper\/luks-8f747652-3e73-4297-9721-9e8cdd8d89b1   none    swap    defaults        0 0\r\n<\/pre>\n<p>Turn on swap to ensure all is well:<\/p>\n<pre>\r\nswapon -a\r\nswapon -s\r\nFilename\t\t\t\tType\t\tSize\tUsed\tPriority\r\n\/dev\/dm-2                              \tpartition\t2095100\t0\t-1\r\n<\/pre>\n<p>With working swap, we need to ensure it comes up on boot.<\/p>\n<p>Edit \/etc\/default\/grub to add this new rd.luks.uuid and make config:<\/p>\n<pre>\r\nGRUB_CMDLINE_LINUX=\"quiet rd.luks.uuid=cc7e78ee-87a9-4ad0-9c82-31de01389b34 rd.luks.uuid=luks-8f747652-3e73-4297-9721-9e8cdd8d89b1\"\r\n\r\ngrub2-mkconfig -o \/boot\/grub2\/grub.cfg\r\n<\/pre>\n<p>Create new ramdisk so it can find the new volume:<\/p>\n<pre>\r\ncd \/boot\r\ncp -a initramfs-3.12.8-300.fc20.i686.img initramfs-3.12.8-300.fc20.i686.img.20140817\r\n\r\ndracut -f \/boot\/initramfs-3.12.8-300.fc20.i686.img 3.12.8-300.fc20.i686\r\n<\/pre>\n<p><strong>Boot with New Swap<\/strong><\/p>\n<p>At this point, we can boot with the new crypto swap in place, and our system disk is as follows:<\/p>\n<pre>\r\n\/dev\/sda (150GB)\r\n     sda1: ext3 \/boot (300MB)\r\n     sda2: lvm Volume00 (150GB)\r\n          Volume00-swap: swap (2GB)\r\n               luks-8f747652-3e73-4297-9721-9e8cdd8d89b1 swap (2GB)\r\n          Volume00-root: LUKS (127GB)\r\n               luks-cc7e78ee-87a9-4ad0-9c82-31de01389b34 ext3 (127GB)\r\n          unallocated: (19GB)\r\n<\/pre>\n<p>Now everything is truly encrypted.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Encrypted swap is at least as important as encrypted data. This is because the contents of nearly anything in memory can be swapped to disk, at any time, and for any length of time. That includes passwords and like, en claire. Encrypting the system swap volume cannot be overlooked. During our migration process we have [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-684","post","type-post","status-publish","format-standard","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/blog.fritzhardy.com\/index.php?rest_route=\/wp\/v2\/posts\/684","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.fritzhardy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fritzhardy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fritzhardy.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fritzhardy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=684"}],"version-history":[{"count":22,"href":"https:\/\/blog.fritzhardy.com\/index.php?rest_route=\/wp\/v2\/posts\/684\/revisions"}],"predecessor-version":[{"id":710,"href":"https:\/\/blog.fritzhardy.com\/index.php?rest_route=\/wp\/v2\/posts\/684\/revisions\/710"}],"wp:attachment":[{"href":"https:\/\/blog.fritzhardy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fritzhardy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fritzhardy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}