| 1 | #!/bin/sh |
| 2 | |
| 3 | PREREQ="" |
| 4 | |
| 5 | prereqs() |
| 6 | { |
| 7 | echo "$PREREQ" |
| 8 | } |
| 9 | |
| 10 | case "$1" in |
| 11 | prereqs) |
| 12 | prereqs |
| 13 | exit 0 |
| 14 | ;; |
| 15 | esac |
| 16 | |
| 17 | . /usr/share/initramfs-tools/hook-functions |
| 18 | . /lib/cryptsetup/functions |
| 19 | TABFILE="/etc/crypttab" |
| 20 | |
| 21 | |
| 22 | # crypttab_find_and_print_entry($target) |
| 23 | # Find the crypttab(5) entry for the given (unmangled) $target and |
| 24 | # print it - preserving the mangling - to FD nr. 3; but only if the |
| 25 | # target has not already been processed during an earlier function |
| 26 | # call. (Processed target names are stored in |
| 27 | # $DESTDIR/cryptroot/targets.) |
| 28 | # Return 0 on success, 1 on error. |
| 29 | crypttab_find_and_print_entry() { |
| 30 | local target="$1" |
| 31 | local _CRYPTTAB_NAME _CRYPTTAB_SOURCE _CRYPTTAB_KEY _CRYPTTAB_OPTIONS |
| 32 | if ! grep -Fxqz -e "$target" -- "$DESTDIR/cryptroot/targets"; then |
| 33 | printf '%s\0' "$target" >>"$DESTDIR/cryptroot/targets" |
| 34 | crypttab_find_entry "$target" || return 1 |
| 35 | crypttab_parse_options --missing-path=warn || return 1 |
| 36 | crypttab_print_entry |
| 37 | fi |
| 38 | } |
| 39 | |
| 40 | # crypttab_print_entry() |
| 41 | # Print an unmangled crypttab(5) entry to FD nr. 3, using CRYPTTAB_* |
| 42 | # and _CRYPTTAB_* values. |
| 43 | # _CRYPTTAB_SOURCE is replaced with UUID=<uuid> if possible (eg, for |
| 44 | # LUKS), unless the value starts with /dev/disk/by- or /dev/mapper/, |
| 45 | # or is already a device specification (such as LABEL= or PARTUUID=). |
| 46 | # If the entry uses the 'decrypt_derived' keyscript, the other |
| 47 | # crypttab(5) entries it depends on are (recursively) printed before |
| 48 | # hand. |
| 49 | # Various checks are performed on the key and crypttab options, but no |
| 50 | # parsing is done so it's the responsibility of the caller to call |
| 51 | # crypttab_parse_options(). |
| 52 | # Return 0 on success, 1 on error. |
| 53 | crypttab_print_entry() { |
| 54 | local DEV MAJ MIN name_uses uuid keyfile |
| 55 | if _resolve_device "$CRYPTTAB_SOURCE"; then |
| 56 | name_uses="$(dmsetup info -c --noheadings -o devnos_used -- "$CRYPTTAB_NAME" 2>/dev/null)" || name_uses="N/A" |
| 57 | if [ "$name_uses" != "$MAJ:$MIN" ]; then |
| 58 | cryptsetup_message "ERROR: Source mismatch: $CRYPTTAB_NAME uses $name_uses, but $CRYPTTAB_SOURCE is $MAJ:$MIN" |
| 59 | elif [ "${_CRYPTTAB_SOURCE#[A-Za-z]*=}" = "$_CRYPTTAB_SOURCE" ] && \ |
| 60 | [ "${CRYPTTAB_SOURCE#/dev/disk/by-}" = "$CRYPTTAB_SOURCE" ] && \ |
| 61 | [ "${CRYPTTAB_SOURCE#/dev/mapper/}" = "$CRYPTTAB_SOURCE" ] && \ |
| 62 | uuid="$(_device_uuid "$DEV")"; then |
| 63 | _CRYPTTAB_SOURCE="UUID=$uuid" |
| 64 | fi |
| 65 | # on failure _resolve_device() prints a warning and we try our |
| 66 | # luck with the unchanged _CRYPTTAB_SOURCE value |
| 67 | fi |
| 68 | |
| 69 | # if keyscript is set, the "key" is just an argument to the script |
| 70 | if [ -z "${CRYPTTAB_OPTION_keyscript+x}" ] && [ "$CRYPTTAB_KEY" != "none" ]; then |
| 71 | crypttab_key_check || return 1 |
| 72 | case "$CRYPTTAB_KEY" in |
| 73 | $KEYFILE_PATTERN) |
| 74 | mkdir -pm0700 -- "$DESTDIR/cryptroot/keyfiles" |
| 75 | # $CRYPTTAB_NAME can't contain '/' (even after unmangling) |
| 76 | keyfile="/cryptroot/keyfiles/$CRYPTTAB_NAME.key" |
| 77 | if [ ! -f "$DESTDIR$keyfile" ] && ! copy_file keyfile "$CRYPTTAB_KEY" "$keyfile"; then |
| 78 | cryptsetup_message "WARNING: couldn't copy keyfile $CRYPTTAB_KEY" |
| 79 | fi |
| 80 | _CRYPTTAB_KEY="/cryptroot/keyfiles/$_CRYPTTAB_NAME.key" # preserve mangled name |
| 81 | ;; |
| 82 | *) |
| 83 | if [ "$usage" = rootfs ]; then |
| 84 | cryptsetup_message "WARNING: Skipping root target $CRYPTTAB_NAME: uses a key file" |
| 85 | return 1 |
| 86 | elif [ "$usage" = resume ]; then |
| 87 | cryptsetup_message "WARNING: Resume target $CRYPTTAB_NAME uses a key file" |
| 88 | fi |
| 89 | if [ -L "$CRYPTTAB_KEY" ] && keyfile="$(readlink -- "$CRYPTTAB_KEY")" && |
| 90 | [ "${keyfile#/}" != "$keyfile" ]; then |
| 91 | cryptsetup_message "WARNING: Skipping target $CRYPTTAB_NAME: key file is a symlink with absolute target" |
| 92 | return 1 |
| 93 | elif [ -f "$CRYPTTAB_KEY" ] && [ "$(stat -L -c"%m" -- "$CRYPTTAB_KEY" 2>/dev/null)" != "/" ]; then |
| 94 | cryptsetup_message "WARNING: Skipping target $CRYPTTAB_NAME: key file is not on the root FS" |
| 95 | return 1 |
| 96 | fi |
| 97 | if [ ! -e "$CRYPTTAB_KEY" ]; then |
| 98 | cryptsetup_message "WARNING: Target $CRYPTTAB_NAME has a non-existing key file $CRYPTTAB_KEY" |
| 99 | else |
| 100 | _CRYPTTAB_KEY="/FIXME-initramfs-rootmnt$_CRYPTTAB_KEY" # preserve mangled name |
| 101 | fi |
| 102 | esac |
| 103 | fi |
| 104 | |
| 105 | if [ -n "${CRYPTTAB_OPTION_keyscript+x}" ]; then |
| 106 | copy_exec "$CRYPTTAB_OPTION_keyscript" |
| 107 | elif [ "$CRYPTTAB_KEY" = "none" ]; then |
| 108 | ASKPASS="y" |
| 109 | fi |
| 110 | if [ "${CRYPTTAB_OPTION_keyscript-}" = "/lib/cryptsetup/scripts/decrypt_derived" ]; then |
| 111 | # (recursively) list first the device to derive the key from (so |
| 112 | # the boot scripts unlock it first); since _CRYPTTAB_* are local |
| 113 | # to crypttab_find_and_print_entry() the new value won't |
| 114 | # override the new ones |
| 115 | crypttab_find_and_print_entry "$CRYPTTAB_KEY" |
| 116 | fi |
| 117 | printf '%s %s %s %s\n' \ |
| 118 | "$_CRYPTTAB_NAME" "$_CRYPTTAB_SOURCE" "$_CRYPTTAB_KEY" "$_CRYPTTAB_OPTIONS" >&3 |
| 119 | } |
| 120 | |
| 121 | # get_resume_devno() |
| 122 | # Return the device ID(s) used for system suspend/hibernate. |
| 123 | get_resume_devno() { |
| 124 | local dev filename |
| 125 | |
| 126 | # uswsusp |
| 127 | for filename in /etc/uswsusp.conf /etc/suspend.conf; do |
| 128 | [ -e "$filename" ] || continue |
| 129 | dev="$(sed -nr '/^resume device\s*[:=]\s*/ {s///p;q}' "$filename")" |
| 130 | if [ -n "$dev" ] && [ "$dev" != "<path_to_resume_device_file>" ]; then |
| 131 | # trim quotes |
| 132 | dev="$(printf '%s' "$dev" | sed -re 's/^"(.*)"\s*$/\1/' -e "s/^'(.*)'\\s*$/\\1/")" |
| 133 | _print_devno "$(printf '%b' "$dev")" # unmangle |
| 134 | fi |
| 135 | done |
| 136 | |
| 137 | # regular swsusp |
| 138 | dev="$(sed -nr 's,^(.*\s)?resume=(\S+)(\s.*)?$,\2,p' /proc/cmdline)" |
| 139 | _print_devno "$(printf '%b' "$dev")" # unmangle |
| 140 | |
| 141 | # initramfs-tools >=0.129 |
| 142 | dev="${RESUME:-auto}" |
| 143 | if [ "$dev" != none ]; then |
| 144 | if [ "$dev" = auto ]; then |
| 145 | # next line from /usr/share/initramfs-tools/hooks/resume |
| 146 | dev="$(grep ^/dev/ /proc/swaps | sort -rnk3 | head -n 1 | cut -d " " -f 1)" |
| 147 | fi |
| 148 | _print_devno "$(printf '%b' "$dev")" # unmangle |
| 149 | fi |
| 150 | } |
| 151 | _print_devno() { |
| 152 | local DEV MAJ MIN # locally scope the 3 variables _resolve_device() sets |
| 153 | if [ -n "$1" ] && _resolve_device "$1"; then |
| 154 | printf '%d:%d\n' "$MAJ" "$MIN" |
| 155 | fi |
| 156 | } |
| 157 | |
| 158 | # crypttab_print_initramfs_entry() |
| 159 | # Print a crypttab(5) entry - unless it was already processed - if it |
| 160 | # has the 'initramfs' option set. |
| 161 | crypttab_print_initramfs_entry() { |
| 162 | local usage= |
| 163 | if ! grep -Fxqz -e "$CRYPTTAB_NAME" -- "$DESTDIR/cryptroot/targets" && |
| 164 | crypttab_parse_options --quiet && |
| 165 | [ "${CRYPTTAB_OPTION_initramfs-no}" = "yes" ]; then |
| 166 | printf '%s\0' "$CRYPTTAB_NAME" >>"$DESTDIR/cryptroot/targets" |
| 167 | crypttab_print_entry |
| 168 | fi |
| 169 | } |
| 170 | |
| 171 | # generate_initrd_crypttab() |
| 172 | # Generate the crypttab(5) snippet that is relevant at initramfs |
| 173 | # stage. (Devices that aren't required at initramfs stage are |
| 174 | # ignored.) |
| 175 | generate_initrd_crypttab() { |
| 176 | local devnos usage IFS="$(printf '\t\n ')" |
| 177 | mkdir -- "$DESTDIR/cryptroot" |
| 178 | true >"$DESTDIR/cryptroot/targets" |
| 179 | |
| 180 | { |
| 181 | # add crypttab entries with the 'initramfs' option set |
| 182 | crypttab_foreach_entry crypttab_print_initramfs_entry |
| 183 | |
| 184 | if devnos="$(get_mnt_devno /)"; then |
| 185 | usage=rootfs foreach_cryptdev crypttab_find_and_print_entry $devnos |
| 186 | else |
| 187 | cryptsetup_message "WARNING: Couldn't determine root device" |
| 188 | fi |
| 189 | |
| 190 | if devnos="$(get_resume_devno)"; then |
| 191 | usage=resume foreach_cryptdev crypttab_find_and_print_entry $devnos |
| 192 | fi |
| 193 | |
| 194 | if devnos="$(get_mnt_devno /usr)"; then |
| 195 | usage="" foreach_cryptdev crypttab_find_and_print_entry $devnos |
| 196 | fi |
| 197 | } 3>"$DESTDIR/cryptroot/crypttab" |
| 198 | rm -f "$DESTDIR/cryptroot/targets" |
| 199 | } |
| 200 | |
| 201 | # populate_CRYPTO_HASHES() |
| 202 | # Find out which crypto hashes are required for a crypttab(5) entry, |
| 203 | # and append them to the CRYPTO_HASHES variable. |
| 204 | populate_CRYPTO_HASHES() { |
| 205 | local hash source newline=" |
| 206 | " |
| 207 | |
| 208 | if crypttab_parse_options --quiet && [ -n "${CRYPTTAB_OPTION_header+x}" ]; then |
| 209 | source="$CRYPTTAB_OPTION_header" |
| 210 | else |
| 211 | source="$(_resolve_device_spec "$CRYPTTAB_SOURCE")" || source="" |
| 212 | fi |
| 213 | |
| 214 | if [ ! -e "$source" ]; then |
| 215 | # missing source device or detached header, can't determine hashing function(s) |
| 216 | hash="@@UNKNOWN@@" |
| 217 | elif [ "$CRYPTTAB_TYPE" = "luks" ]; then |
| 218 | # using --dump-json-metadata would be more robust for LUKS2 but |
| 219 | # we also have to support LUKS1 hence have to parse luksDump output |
| 220 | hash="$(/sbin/cryptsetup luksDump -- "$source" | sed -nr 's/^\s*(AF hash|Hash|Hash spec)\s*:\s*//Ip')" |
| 221 | elif [ "$CRYPTTAB_TYPE" = "plain" ]; then |
| 222 | # --hash is being ignored when opening via key file |
| 223 | if [ "$CRYPTTAB_KEY" = "none" ] && [ -z "${CRYPTTAB_OPTION_keyscript+x}" ]; then |
| 224 | hash="${CRYPTTAB_OPTION_hash-sha256}" # default password hashing as of cryptsetup 2.7 |
| 225 | fi |
| 226 | else |
| 227 | hash="" # or hash="@@UNKNOWN@@"? |
| 228 | fi |
| 229 | |
| 230 | if [ -n "$hash" ]; then |
| 231 | CRYPTO_HASHES="${CRYPTO_HASHES:+$CRYPTO_HASHES$newline}$hash" |
| 232 | fi |
| 233 | } |
| 234 | |
| 235 | # populate_CRYPTO_MODULES() |
| 236 | # Find out which crypto modules are required for a crypttab(5) entry, |
| 237 | # and append them to the CRYPTO_MODULES variable. |
| 238 | populate_CRYPTO_MODULES() { |
| 239 | local cipher iv |
| 240 | |
| 241 | # cf. dmsetup(8) and https://gitlab.com/cryptsetup/cryptsetup/wikis/DMCrypt |
| 242 | cipher="$(dmsetup table --target crypt -- "$CRYPTTAB_NAME" | cut -d' ' -f4)" |
| 243 | if [ -z "$cipher" ]; then |
| 244 | cryptsetup_message "WARNING: Couldn't determine cipher modules to load for $CRYPTTAB_NAME" |
| 245 | elif [ "${cipher#capi:}" = "$cipher" ]; then |
| 246 | # direct specification "cipher[:keycount]-chainmode-ivmode[:ivopts]" |
| 247 | CRYPTO_MODULES="${CRYPTO_MODULES:+$CRYPTO_MODULES }${cipher%%[-:]*}" # cipher |
| 248 | cipher="${cipher#"${cipher%%-*}-"}" # chainmode-ivmode[:ivopts]" |
| 249 | CRYPTO_MODULES="${CRYPTO_MODULES:+$CRYPTO_MODULES }${cipher%-*}" # chainmode |
| 250 | iv="${cipher##*-}" # ivmode[:ivopts]" |
| 251 | CRYPTO_MODULES="${CRYPTO_MODULES:+$CRYPTO_MODULES }${iv%%:*}" # ivmode |
| 252 | if [ "${iv#*:}" != "$iv" ]; then |
| 253 | CRYPTO_MODULES="${CRYPTO_MODULES:+$CRYPTO_MODULES }${iv#*:}" # ivopts |
| 254 | fi |
| 255 | else |
| 256 | # kernel crypto API format "capi:cipher_api_spec-ivmode[:ivopts]", since linux 4.12 |
| 257 | cipher="${cipher#capi:}" |
| 258 | cryptsetup_message "WARNING: Couldn't determine cipher modules to load for $CRYPTTAB_NAME" \ |
| 259 | "(kernel crypto API format isn't supported yet)" |
| 260 | fi |
| 261 | } |
| 262 | |
| 263 | # add_modules($glob, $moduledir, [$moduledir ..]) |
| 264 | # Add modules matching under the given $moduledir(s), the name of |
| 265 | # which matching $glob. |
| 266 | # Return 0 if any module was found found, 1 if not. |
| 267 | add_modules() { |
| 268 | local glob="$1" found=n |
| 269 | shift |
| 270 | for mod in $(find -H "$@" -name "$glob.ko*" -type f -printf '%f\n'); do |
| 271 | manual_add_modules "${mod%%.*}" |
| 272 | found=y |
| 273 | done |
| 274 | [ "$found" = y ] && return 0 || return 1 |
| 275 | } |
| 276 | |
| 277 | # add_crypto_modules($name, [$name ..]) |
| 278 | # Determine kernel module name and add to initramfs. |
| 279 | add_crypto_modules() { |
| 280 | local mod |
| 281 | for mod in "$@"; do |
| 282 | # We have several potential sources of modules (in order of preference): |
| 283 | # |
| 284 | # a) /lib/modules/$VERSION/kernel/arch/$ARCH/crypto/$mod-$specific.ko |
| 285 | # b) /lib/modules/$VERSION/kernel/crypto/$mod_generic.ko |
| 286 | # c) /lib/modules/$VERSION/kernel/crypto/$mod.ko |
| 287 | # |
| 288 | # and (currently ignored): |
| 289 | # |
| 290 | # d) /lib/modules/$VERSION/kernel/drivers/crypto/$specific-$mod.ko |
| 291 | add_modules "$mod-*" "$MODULESDIR"/kernel/arch/*/crypto || true |
| 292 | add_modules "${mod}_generic" "$MODULESDIR/kernel/crypto" \ |
| 293 | || add_modules "$mod" "$MODULESDIR/kernel/crypto" \ |
| 294 | || true |
| 295 | done |
| 296 | } |
| 297 | |
| 298 | # copy_libssl_legacy_library() |
| 299 | # Copy ossl-modules/legacy.so (from libssl library) to initramfs if needed. |
| 300 | # OpenSSL 3.0 moved support for some crypto hashes into legacy.so. |
| 301 | # See https://launchpad.net/bugs/1979159 |
| 302 | copy_libssl_legacy_library() { |
| 303 | local libcryptodir CRYPTO_HASHES="" |
| 304 | |
| 305 | libcryptodir="$(env --unset=LD_PRELOAD ldd /sbin/cryptsetup | sed -nr '/.*=>\s*(\S+)\/libcrypto\.so\..*/ {s//\1/p;q}')" |
| 306 | [ -d "$libcryptodir" ] || return |
| 307 | |
| 308 | crypttab_foreach_entry populate_CRYPTO_HASHES |
| 309 | # See https://www.openssl.org/docs/man3.0/man7/OSSL_PROVIDER-legacy.html#Hashing-Algorithms-Message-Digests |
| 310 | if printf '%s\n' "$CRYPTO_HASHES" | grep -Fxq -e @@UNKNOWN@@ -e whirlpool; then |
| 311 | # legacy hashes are used so legacy.so needs to be copied to the initramfs |
| 312 | # (assume ossl-modules/legacy.so is relative to the linked libcrypto.so) |
| 313 | copy_exec "$libcryptodir/ossl-modules/legacy.so" || true |
| 314 | fi |
| 315 | } |
| 316 | |
| 317 | |
| 318 | ####################################################################### |
| 319 | # Begin real processing |
| 320 | |
| 321 | unset -v ASKPASS KEYFILE_PATTERN |
| 322 | ASKPASS="y" |
| 323 | KEYFILE_PATTERN= |
| 324 | |
| 325 | # Load the hook config |
| 326 | if [ -f "/etc/cryptsetup-initramfs/conf-hook" ]; then |
| 327 | . /etc/cryptsetup-initramfs/conf-hook |
| 328 | fi |
| 329 | |
| 330 | if [ -n "$KEYFILE_PATTERN" ]; then |
| 331 | case "${UMASK:-$(umask)}" in |
| 332 | 0[0-7]77) ;; |
| 333 | *) cryptsetup_message "WARNING: Permissive UMASK (${UMASK:-$(umask)})." \ |
| 334 | "Private key material within the initrd might be left unprotected." |
| 335 | ;; |
| 336 | esac |
| 337 | fi |
| 338 | |
| 339 | CRYPTO_MODULES= |
| 340 | if [ -r "$TABFILE" ]; then |
| 341 | generate_initrd_crypttab |
| 342 | TABFILE="$DESTDIR/cryptroot/crypttab" |
| 343 | crypttab_foreach_entry populate_CRYPTO_MODULES |
| 344 | copy_libssl_legacy_library |
| 345 | fi |
| 346 | |
| 347 | # add required components |
| 348 | manual_add_modules dm_mod dm_crypt |
| 349 | |
| 350 | copy_exec /sbin/cryptsetup |
| 351 | copy_exec /sbin/dmsetup |
| 352 | |
| 353 | [ "$ASKPASS" = n ] || copy_exec /lib/cryptsetup/askpass |
| 354 | |
| 355 | # We need sed. Either via busybox or as standalone binary. |
| 356 | if [ "$BUSYBOX" = n ] || [ -z "$BUSYBOXDIR" ]; then |
| 357 | copy_exec /bin/sed |
| 358 | fi |
| 359 | |
| 360 | # detect whether the host CPU has AES-NI support |
| 361 | if grep -Eq '^flags\s*:(.*\s)?aes(\s.*)?$' /proc/cpuinfo; then |
| 362 | CRYPTO_MODULES="${CRYPTO_MODULES:+$CRYPTO_MODULES }aesni" |
| 363 | else |
| 364 | # workaround for #883595/#901884 (xts depends on ecb) |
| 365 | CRYPTO_MODULES="${CRYPTO_MODULES:+$CRYPTO_MODULES }ecb" |
| 366 | fi |
| 367 | |
| 368 | # add userspace crypto module (only required for opening LUKS2 devices |
| 369 | # we add the module unconditionally as it's the default format) |
| 370 | CRYPTO_MODULES="${CRYPTO_MODULES:+$CRYPTO_MODULES }algif_skcipher" |
| 371 | |
| 372 | if [ "$MODULES" = most ]; then |
| 373 | for d in "$MODULESDIR"/kernel/arch/*/crypto; do |
| 374 | copy_modules_dir "${d#"$MODULESDIR/"}" |
| 375 | done |
| 376 | copy_modules_dir "kernel/crypto" |
| 377 | else |
| 378 | if [ "$MODULES" != "dep" ]; then |
| 379 | # with large initramfs, we always add a basic subset of modules |
| 380 | add_crypto_modules aes cbc chainiv cryptomgr krng sha256 xts vmx_crypto |
| 381 | fi |
| 382 | add_crypto_modules $(printf '%s' "${CRYPTO_MODULES-}" | tr ' ' '\n' | sort -u) |
| 383 | fi |
| 384 | copy_file library /lib/cryptsetup/functions /lib/cryptsetup/functions |