Debian Codegraph @

debian/README.initramfs

raw
1 Debian Cryptsetup Initramfs integration
2 =======================================
3
4 1. Introduction
5 ---------------
6
7 Kernels more recent than 2.6.12 have dropped support for devfs, which
8 means that initrd-tools can no longer be used to boot into an encrypted
9 root partition. Instead, a similar functionality has been developed for
10 use with an initramfs-image.
11
12
13 2. A fresh installation
14 -----------------------
15
16 If you plan to perform a completely new installation of Debian onto a
17 machine and to do so using an encrypted root partition, you might want
18 to consider using a version of Debian Installer with partman-crypto
19 (see https://wiki.debian.org/DebianInstaller/PartmanCrypto).
20
21 The installation will then take care of all the details and perform the
22 necessary configuration for you, meaning that you should not have to
23 read the rest of this document to get a machine with an encrypted
24 root filesystem up and running.
25
26 However, if you are not planning to perform a new installation from scratch,
27 the following information might be useful to you.
28
29
30 3. Requirements
31 ---------------
32
33 In order to boot from an encrypted root filesystem, you need an
34 initramfs-image which includes the necessary kernel modules and scripts to
35 setup the root device after the kernel has been initialized, but before the
36 rest of the operating system is booted.
37
38 To do so, you need two partitions:
39 * an unencrypted `/boot` partition
40 * an encrypted `/` partition
41
42 In addition, you need to have both initramfs-tools and busybox installed.
43
44 NOTE: You should make sure that your swap partition is either encrypted, or
45 that you are using a swap file on an encrypted partition, as crypto keys and
46 other sensitive information might otherwise be written out to the swap
47 partition in unencrypted form.
48
49
50 4. Setup (regular dm-crypt)
51 ---------------------------
52
53 First of all, you must edit `/etc/crypttab` and add a line describing your
54 root device, for example:
55
56 cryptroot /dev/sda2 none cipher=aes-xts-plain64,size=256,hash=sha1
57
58 This will allow cryptsetup to create `/dev/mapper/cryptroot` from the
59 encrypted partition `/dev/sda2` during boot.
60
61 In addition, you must also make sure that the root device is listed in
62 `/etc/fstab`, for example:
63
64 /dev/mapper/cryptroot / ext4 defaults 0 1
65
66 This will allow the initramfs support scripts to know which of the devices
67 in the crypttab that is the root device.
68
69 After doing these changes, you should regenerate the initramfs by running
70 `update-initramfs -u`, then make sure that your boot loader is configured
71 to feed the initramfs to the kernel when booting. The kernel root argument
72 should also be changed to `/dev/mapper/cryptroot`.
73
74 Now, reboot the machine, and if everything is correctly configured, you
75 should be given a prompt to type in the passphrase for the encrypted
76 root partition before the boot can continue.
77
78 NOTE: In order to ensure that the crypto setup works in a consistent
79 manner, you should make sure that the hash function is specified in the
80 /etc/crypttab file if you are using regular dm-crypt (with LUKS the hash
81 function to use is stored in the LUKS header).
82
83
84 5. Setup (using LUKS)
85 ---------------------
86
87 If you are using the LUKS feature of cryptsetup, the above setup recipe should
88 still apply, but since most options can be derived from the information stored
89 in the LUKS header on-disk, the line to add to `/etc/crypttab` should look
90 something like this:
91
92 cryptroot /dev/sda2 none luks,discard
93
94
95 6. Exotic key types
96 -------------------
97
98 The above examples assume that you use a regular passphrase as the key to the
99 encrypted filesystem. However, if you wish to make use of more complex setups
100 (such as root-key-on-usb-memory), you can create a script which does all the
101 steps necessary to retrieve the key and then prints it to stdout.
102
103 Then add a `keyscript=/path/to/your/script.sh` to the options (fourth column)
104 in the above mentioned `/etc/crypttab` line, so that it looks something like
105 this:
106
107 cryptroot /dev/sda2 none luks,discard,keyscript=/usr/local/sbin/cryptkey
108
109 Next, regenerate your initramfs image. This will copy the script into the
110 initramfs image under the `/lib/cryptsetup/keyscripts/` directory.
111
112 NOTE: there is a limited set of tools available when the script is executing
113 as part of the initramfs bootup, you have to make sure that you do not use
114 any tools which are not available or your script, and therefore boot, will
115 fail.
116
117
118 7. "cryptopts" boot argument
119 ----------------------------
120
121 In general, you should use the above approach with a line describing your
122 root partition in `/etc/crypttab` and `/etc/fstab`. However, if for some
123 reason you wish to override the settings that are derived from these files
124 and stored in the initramfs image, you can use the "cryptopts" boot argument
125 (this *only* works for the root partition).
126
127 The format of cryptopts is:
128
129 cryptopts=<opt1>[=<value1>],<opt2>[=<value2>]...
130
131 Beside options from the 4th field of /etc/crypttab, the options
132 `target`, `source` and `key` are also supported: they respectively
133 correspond to the first, second and third field of /etc/crypttab.
134 Consult the crypttab manual page for further details.
135
136 Several `cryptopts` boot arguments can also be specified in case more than
137 one mapping needs to be setup in the initramfs stage of the boot.
138
139 Example boot arguments:
140
141 root=/dev/mapper/crypt0 cryptopts=target=crypt0,source=/dev/sda1,cipher=aes-xts-plain64,size=256,hash=sha1
142
143 In particular, if all `cryptopts` boot arguments have an empty value
144 then no mapping is setup. This can be used to disable the cryptsetup
145 initramfs scripts for a particular boot.
146
147 8. Resume device support
148 ------------------------
149
150 The initramfs scripts will also try to automatically determine the devices,
151 if any, that are used for software suspend (swsusp, suspend2 or uswsusp) and
152 to set them up during the initramfs stage in order to allow suspend and resume
153 in combination with encryption to keep the resume image safe from potential
154 attackers.
155
156 If your resume device and your root partition use two different cryptsetup
157 mappings, you might want to use the `decrypt_derived` keyscript as described
158 below.
159
160 9. The `decrypt_derived` keyscript
161 ----------------------------------
162
163 Assume that you have two entries in `/etc/crypttab`:
164
165 cryptroot /dev/sda1 none luks,discard
166 cryptswap /dev/sda2 none luks
167
168 If cryptswap is used as your suspend/resume device, you'd normally need to
169 enter two different passphrases during the boot, but the `decrypt_derived`
170 script can generate the key for the second mapping using a hash of the key
171 for the first mapping.
172
173 In short, you'll need to do something like the following to take advantage
174 of the decrypt_derived script:
175
176 1. `swapoff -a`
177 2. `cryptsetup luksClose cryptswap`
178 3. edit `/etc/crypttab` and change the cryptswap line to e.g.:
179 `cryptswap /dev/sda2 cryptroot cipher=aes-xts-plain65,size=256,hash=sha1,keyscript=decrypt_derived,swap`
180 4. `cryptdisks_start cryptswap`
181 5. Make sure that `/dev/mapper/cryptswap` has been created
182 6. `swapon -a`
183 7. (optional) `update-initramfs -u`
184
185 After you've followed the above steps, your swap device should be setup
186 automatically after the root device has been setup during the boot stage.
187
188 WARNING: If you use the decrypt_derived keyscript for devices with persistent
189 data (i.e. not swap or temp devices), then you will lose access to that data
190 permanently if something damages the LUKS header of the LUKS device you derive
191 from. The same applies if you luksFormat the device, even if you use the same
192 passphrase(s). A LUKS header backup, or better a backup of the data on the
193 derived device may be a good idea. See the Cryptsetup FAQ on how to do this
194 right.
195
196 Note: The decrypt_derived keyscript won't work when the volume key of the
197 device being derived from is offloaded to the kernel keyring service (thus not
198 readable by userspace). That behavior is the default for LUKS2 devices (unless
199 opened with the `--disable-keyring` option) since Linux 4.10. For such devices,
200 an alternative is to use the same passphrase and unlock the source device using
201 the `decrypt_keyctl` keyscript.
202
203 Note: If you don't use suspend device support, it's better to use completely
204 random keys for your encrypted swap device. See the section '2. Encrypted
205 swap partition(s)' in `/usr/share/doc/cryptsetup/README.Debian.gz` for
206 information on how to setup this.
207
208 10. The `passdev` keyscript
209 ----------------------------
210
211 If you have a keyfile on a removable device (e.g. a USB-key), you can use the
212 passdev keyscript. It will wait for the device to appear, mount it read-only,
213 read the key and then unmount the device.
214
215 The `key` part of `/etc/crypttab` will be interpreted as `<device>:<path>[:<timeout>]`,
216 it is strongly recommended that you use one of the persistent device names from
217 `/dev/disk/*`, e.g. `/dev/disk/by-label/myusbkey`.
218
219 This is an example of a suitable line in cryptsetup:
220
221 cryptroot /dev/sda2 /dev/disk/by-label/myusbkey:/keys/root.key discard,cipher=aes-xts-plain64,size=256,hash=sha1,keyscript=passdev
222
223 The above line would cause the boot to pause until `/dev/disk/by-label/myusbkey`
224 appears in the fs, then mount that device and use the file `/keys/root.key`
225 on the device as the key (without any hashing) as the key for the fs.
226
227 The timeout option has to be in seconds.
228
229 If any modules are required in order to mount the filesystem on the removable
230 device, then initramfs-tools needs to be configured to add these modules to
231 the initramfs. This can be done by listing the required modules in
232 `/etc/initramfs-tools/modules`.
233
234 11. Limitation: renaming of target name for encrypted root device
235 -----------------------------------------------------------------
236
237 As spotted by Adam Lee in bug report [#671037], it's not possible to simply
238 rename the target name for encrypted root devices. It breaks the initramfs
239 creation process. The bug report submitter found a solution to work around
240 this limitation:
241
242 0. enter another system (like livecd)
243 1. open luks device with the new name, change the target name to the new one
244 2. chroot into it (now, the current target name is the same as it in conf)
245 3. `update-initramfs -u`
246 4. reboot
247
248 [#671037]: https://bugs.debian.org/671037
249
250 12. Storing keyfiles directly in the initramfs
251 ----------------------------------------------
252
253 Normally devices using a keyfile are ignored (with a loud warning), and
254 the key file itself is not included in the initramfs, because the initramfs
255 image typically lives on an unencrypted `/boot` partition. However in
256 some cases it is desirable to include the key file in the initramfs; for
257 instance recent versions of GRUB support booting from encrypted block
258 devices, allowing an encrypted `/boot` partition.
259
260 Among the key files listed in the crypttab(5), those matching the value
261 of the environment variable KEYFILE_PATTERN (interpreted as a shell
262 pattern) will be included in the initramfs image. For instance if
263 `/etc/crypttab` lists two key files `/etc/keys/{root,swap}.key`, you can
264 add the following to `/etc/cryptsetup-initramfs/conf-hook` to add them to
265 the initramfs.
266
267 KEYFILE_PATTERN="/etc/keys/*.key"
268
269 Furthermore if the initramfs image is to include private key material,
270 you'll want to create it with a restrictive umask in order to keep
271 non-privileged users at bay. This can be achieved by adding the
272 following to `/etc/initramfs-tools/initramfs.conf`.
273
274 UMASK=0077
275
276 13. The stages in the initramfs at which dm-crypt devices are mapped
277 --------------------------------------------------------------------
278
279 The devices necessary for the root filesystem, /usr, any resume swap device and
280 any device with the `initramfs`-option in `crypttab` are first tried to be
281 mapped (that is: "opened" or "decrypted") in `initramfs-tools`'s `local-top`-
282 phase.
283 Any which couldn't be mapped there are retried in the `local-block`-phase.
284
285 This may be subject to change.
286
287 -- David Härdeman <[email protected]>
288
289 -- Jonas Meurer <[email protected]> Thu, 01 Nov 2012 13:44:31 +0100
290
291 -- Guilhem Moulin <[email protected]> Wed, 09 Dec 2015 04:53:41 +0100