Blame SOURCES/rear-bz1832394.patch

9602f5
diff -up rear-2.4/usr/share/rear/layout/prepare/GNU/Linux/160_include_luks_code.sh.orig rear-2.4/usr/share/rear/layout/prepare/GNU/Linux/160_include_luks_code.sh
9602f5
--- rear-2.4/usr/share/rear/layout/prepare/GNU/Linux/160_include_luks_code.sh.orig	2018-06-21 10:40:53.000000000 +0200
9602f5
+++ rear-2.4/usr/share/rear/layout/prepare/GNU/Linux/160_include_luks_code.sh	2020-11-25 09:21:55.186716041 +0100
9602f5
@@ -1,35 +1,74 @@
9602f5
 # Code to recreate LUKS volumes.
9602f5
 
9602f5
 create_crypt() {
9602f5
+    # See the create_device() function in lib/layout-functions.sh what "device type" means:
9602f5
+    local device_type="$1"
9602f5
+    if ! grep -q "^crypt $device_type " "$LAYOUT_FILE" ; then
9602f5
+        LogPrintError "Skip recreating LUKS volume $device_type (no 'crypt $device_type' entry in $LAYOUT_FILE)"
9602f5
+        # FIXME: The return code is ignored in the create_device() function in lib/layout-functions.sh:
9602f5
+        return 1
9602f5
+    fi
9602f5
+    
9602f5
     local crypt target_device source_device options
9602f5
-    read crypt target_device source_device options < <(grep "^crypt $1 " "$LAYOUT_FILE")
9602f5
+    local mapping_name option key value
9602f5
+    local cryptsetup_options="" keyfile="" password=""
9602f5
 
9602f5
-    local target_name=${target_device#/dev/mapper/}
9602f5
+    read crypt target_device source_device options < <( grep "^crypt $device_type " "$LAYOUT_FILE" )
9602f5
+
9602f5
+    # Careful! One cannot 'test -b $source_device' here at the time when this code is run
9602f5
+    # because the source device is usually a disk partition block device like /dev/sda2
9602f5
+    # but disk partition block devices usually do not yet exist (in particular not on a new clean disk)
9602f5
+    # because partitions are actually created later when the diskrestore.sh script is run
9602f5
+    # but not here when this code is run which only generates the diskrestore.sh script:
9602f5
+    if ! test $source_device ; then
9602f5
+        LogPrintError "Skip recreating LUKS volume $device_type: No source device (see the 'crypt $device_type' entry in $LAYOUT_FILE)"
9602f5
+        # FIXME: The return code is ignored in the create_device() function in lib/layout-functions.sh:
9602f5
+        return 1
9602f5
+    fi
9602f5
+
9602f5
+    mapping_name=${target_device#/dev/mapper/}
9602f5
+    if ! test $mapping_name ; then
9602f5
+        LogPrintError "Skip recreating LUKS volume $device_type on $source_device: No /dev/mapper/... mapping name (see the 'crypt $device_type' entry in $LAYOUT_FILE)"
9602f5
+        # FIXME: The return code is ignored in the create_device() function in lib/layout-functions.sh:
9602f5
+        return 1
9602f5
+    fi
9602f5
 
9602f5
-    local cryptsetup_options="" keyfile="" password=""
9602f5
-    local option key value
9602f5
     for option in $options ; do
9602f5
-        key=${option%=*}
9602f5
+        # $option is of the form keyword=value and
9602f5
+        # we assume keyword has no '=' character but value could be anything that may have a '=' character
9602f5
+        # so we split keyword=value at the leftmost '=' character so that
9602f5
+        # e.g. keyword=foo=bar gets split into key="keyword" and value="foo=bar":
9602f5
+        key=${option%%=*}
9602f5
         value=${option#*=}
9602f5
-
9602f5
+        # The "cryptseup luksFormat" command does not require any of the type, cipher, key-size, hash, uuid option values
9602f5
+        # because if omitted a cryptseup default value is used so we treat those values as optional.
9602f5
+        # Using plain test to ensure the value is a single non empty and non blank word
9602f5
+        # without quoting because test " " would return zero exit code
9602f5
+        # cf. "Beware of the emptiness" in https://github.com/rear/rear/wiki/Coding-Style
9602f5
         case "$key" in
9602f5
-            cipher)
9602f5
-                cryptsetup_options+=" --cipher $value"
9602f5
+            (type)
9602f5
+                test $value && cryptsetup_options+=" --type $value"
9602f5
+                ;;
9602f5
+            (cipher)
9602f5
+                test $value && cryptsetup_options+=" --cipher $value"
9602f5
                 ;;
9602f5
-            key_size)
9602f5
-                cryptsetup_options+=" --key-size $value"
9602f5
+            (key_size)
9602f5
+                test $value && cryptsetup_options+=" --key-size $value"
9602f5
                 ;;
9602f5
-            hash)
9602f5
-                cryptsetup_options+=" --hash $value"
9602f5
+            (hash)
9602f5
+                test $value && cryptsetup_options+=" --hash $value"
9602f5
                 ;;
9602f5
-            uuid)
9602f5
-                cryptsetup_options+=" --uuid $value"
9602f5
+            (uuid)
9602f5
+                test $value && cryptsetup_options+=" --uuid $value"
9602f5
                 ;;
9602f5
-            keyfile)
9602f5
-                keyfile=$value
9602f5
+            (keyfile)
9602f5
+                test $value && keyfile=$value
9602f5
                 ;;
9602f5
-            password)
9602f5
-                password=$value
9602f5
+            (password)
9602f5
+                test $value && password=$value
9602f5
+                ;;
9602f5
+            (*)
9602f5
+                LogPrintError "Skipping unsupported LUKS cryptsetup option '$key' in 'crypt $target_device $source_device' entry in $LAYOUT_FILE"
9602f5
                 ;;
9602f5
         esac
9602f5
     done
9602f5
@@ -37,26 +76,25 @@ create_crypt() {
9602f5
     cryptsetup_options+=" $LUKS_CRYPTSETUP_OPTIONS"
9602f5
 
9602f5
     (
9602f5
-    echo "Log \"Creating LUKS device $target_name on $source_device\""
9602f5
+    echo "LogPrint \"Creating LUKS volume $mapping_name on $source_device\""
9602f5
     if [ -n "$keyfile" ] ; then
9602f5
         # Assign a temporary keyfile at this stage so that original keyfiles do not leak onto the rescue medium.
9602f5
         # The original keyfile will be restored from the backup and then re-assigned to the LUKS device in the
9602f5
         # 'finalize' stage.
9602f5
         # The scheme for generating a temporary keyfile path must be the same here and in the 'finalize' stage.
9602f5
-        keyfile="${TMPDIR:-/tmp}/LUKS-keyfile-$target_name"
9602f5
+        keyfile="$TMP_DIR/LUKS-keyfile-$mapping_name"
9602f5
         dd bs=512 count=4 if=/dev/urandom of="$keyfile"
9602f5
         chmod u=rw,go=- "$keyfile"
9602f5
-
9602f5
         echo "cryptsetup luksFormat --batch-mode $cryptsetup_options $source_device $keyfile"
9602f5
-        echo "cryptsetup luksOpen --key-file $keyfile $source_device $target_name"
9602f5
+        echo "cryptsetup luksOpen --key-file $keyfile $source_device $mapping_name"
9602f5
     elif [ -n "$password" ] ; then
9602f5
         echo "echo \"$password\" | cryptsetup luksFormat --batch-mode $cryptsetup_options $source_device"
9602f5
-        echo "echo \"$password\" | cryptsetup luksOpen $source_device $target_name"
9602f5
+        echo "echo \"$password\" | cryptsetup luksOpen $source_device $mapping_name"
9602f5
     else
9602f5
-        echo "LogPrint \"Please enter the password for LUKS device $target_name ($source_device):\""
9602f5
+        echo "LogUserOutput \"Set the password for LUKS volume $mapping_name (for 'cryptsetup luksFormat' on $source_device):\""
9602f5
         echo "cryptsetup luksFormat --batch-mode $cryptsetup_options $source_device"
9602f5
-        echo "LogPrint \"Please re-enter the password for LUKS device $target_name ($source_device):\""
9602f5
-        echo "cryptsetup luksOpen $source_device $target_name"
9602f5
+        echo "LogUserOutput \"Enter the password for LUKS volume $mapping_name (for 'cryptsetup luksOpen' on $source_device):\""
9602f5
+        echo "cryptsetup luksOpen $source_device $mapping_name"
9602f5
     fi
9602f5
     echo ""
9602f5
     ) >> "$LAYOUT_CODE"
9602f5
diff -up rear-2.4/usr/share/rear/layout/save/GNU/Linux/260_crypt_layout.sh.orig rear-2.4/usr/share/rear/layout/save/GNU/Linux/260_crypt_layout.sh
9602f5
--- rear-2.4/usr/share/rear/layout/save/GNU/Linux/260_crypt_layout.sh.orig	2018-06-21 10:40:53.000000000 +0200
9602f5
+++ rear-2.4/usr/share/rear/layout/save/GNU/Linux/260_crypt_layout.sh	2020-11-25 09:19:31.406669210 +0100
9602f5
@@ -9,6 +9,8 @@ Log "Saving Encrypted volumes."
9602f5
 REQUIRED_PROGS=( "${REQUIRED_PROGS[@]}" cryptsetup dmsetup )
9602f5
 COPY_AS_IS=( "${COPY_AS_IS[@]}" /usr/share/cracklib/\* /etc/security/pwquality.conf )
9602f5
 
9602f5
+local invalid_cryptsetup_option_value="no"
9602f5
+
9602f5
 while read target_name junk ; do
9602f5
     # find the target device we're mapping
9602f5
     if ! [ -e /dev/mapper/$target_name ] ; then
9602f5
@@ -30,17 +32,96 @@ while read target_name junk ; do
9602f5
         source_device="$(get_device_name ${slave##*/})"
9602f5
     done
9602f5
 
9602f5
-    if ! cryptsetup isLuks $source_device >/dev/null 2>&1; then
9602f5
+    if ! blkid -p -o export $source_device >$TMP_DIR/blkid.output ; then
9602f5
+        LogPrintError "Error: Cannot get attributes for $target_name ('blkid -p -o export $source_device' failed)"
9602f5
+        continue
9602f5
+    fi
9602f5
+
9602f5
+    if ! grep -q "TYPE=crypto_LUKS" $TMP_DIR/blkid.output ; then
9602f5
+        Log "Skipping $target_name (no 'TYPE=crypto_LUKS' in 'blkid -p -o export $source_device' output)"
9602f5
+        continue
9602f5
+    fi
9602f5
+
9602f5
+    # Detect LUKS version:
9602f5
+    # Remove all non-digits in particular to avoid leading or trailing spaces in the version string
9602f5
+    # cf. "Beware of the emptiness" in https://github.com/rear/rear/wiki/Coding-Style
9602f5
+    # that could happen if the blkid output contains "VERSION = 2" so that 'cut -d= -f2' results " 2".
9602f5
+    version=$( grep "VERSION" $TMP_DIR/blkid.output | cut -d= -f2 | tr -c -d '[:digit:]' )
9602f5
+    if ! test "$version" = "1" -o "$version" = "2" ; then
9602f5
+        LogPrintError "Error: Unsupported LUKS version for $target_name ('blkid -p -o export $source_device' shows 'VERSION=$version')"
9602f5
+        continue
9602f5
+    fi
9602f5
+    luks_type=luks$version
9602f5
+
9602f5
+    # Gather crypt information:
9602f5
+    if ! cryptsetup luksDump $source_device >$TMP_DIR/cryptsetup.luksDump ; then
9602f5
+        LogPrintError "Error: Cannot get LUKS$version values for $target_name ('cryptsetup luksDump $source_device' failed)"
9602f5
         continue
9602f5
     fi
9602f5
+    uuid=$( grep "UUID" $TMP_DIR/cryptsetup.luksDump | sed -r 's/^.+:\s*(.+)$/\1/' )
9602f5
+    keyfile_option=$( [ -f /etc/crypttab ] && awk '$1 == "'"$target_name"'" && $3 != "none" && $3 != "-" && $3 != "" { print "keyfile=" $3; }' /etc/crypttab )
9602f5
+    if test $luks_type = "luks1" ; then
9602f5
+        cipher_name=$( grep "Cipher name" $TMP_DIR/cryptsetup.luksDump | sed -r 's/^.+:\s*(.+)$/\1/' )
9602f5
+        cipher_mode=$( grep "Cipher mode" $TMP_DIR/cryptsetup.luksDump | cut -d: -f2- | awk '{printf("%s",$1)};' )
9602f5
+        cipher=$cipher_name-$cipher_mode
9602f5
+        key_size=$( grep "MK bits" $TMP_DIR/cryptsetup.luksDump | sed -r 's/^.+:\s*(.+)$/\1/' )
9602f5
+        hash=$( grep "Hash spec" $TMP_DIR/cryptsetup.luksDump | sed -r 's/^.+:\s*(.+)$/\1/' )
9602f5
+    elif test $luks_type = "luks2" ; then
9602f5
+        cipher=$( grep "cipher:" $TMP_DIR/cryptsetup.luksDump | sed -r 's/^.+:\s*(.+)$/\1/' )
9602f5
+        # More than one keyslot may be defined - use key_size from the first slot.
9602f5
+        # Depending on the version the "cryptsetup luksDump" command outputs the key_size value
9602f5
+        # as a line like
9602f5
+        #         Key:        512 bits
9602f5
+        # and/or as a line like
9602f5
+        #         Cipher key: 512 bits
9602f5
+        # cf. https://github.com/rear/rear/pull/2504#issuecomment-718729198 and subsequent comments
9602f5
+        # so we grep for both lines but use only the first match from the first slot:
9602f5
+        key_size=$( egrep -m 1 "Key:|Cipher key:" $TMP_DIR/cryptsetup.luksDump | sed -r 's/^.+:\s*(.+) bits$/\1/' )
9602f5
+        hash=$( grep "Hash" $TMP_DIR/cryptsetup.luksDump | sed -r 's/^.+:\s*(.+)$/\1/' )
9602f5
+    fi
9602f5
 
9602f5
-    # gather crypt information
9602f5
-    cipher=$(cryptsetup luksDump $source_device | grep "Cipher name" | sed -r 's/^.+:\s*(.+)$/\1/')
9602f5
-    mode=$(cryptsetup luksDump $source_device | grep "Cipher mode" | cut -d: -f2- | awk '{printf("%s",$1)};')
9602f5
-    key_size=$(cryptsetup luksDump $source_device | grep "MK bits" | sed -r 's/^.+:\s*(.+)$/\1/')
9602f5
-    hash=$(cryptsetup luksDump $source_device | grep "Hash spec" | sed -r 's/^.+:\s*(.+)$/\1/')
9602f5
-    uuid=$(cryptsetup luksDump $source_device | grep "UUID" | sed -r 's/^.+:\s*(.+)$/\1/')
9602f5
-    keyfile_option=$([ -f /etc/crypttab ] && awk '$1 == "'"$target_name"'" && $3 != "none" && $3 != "-" { print "keyfile=" $3; }' /etc/crypttab)
9602f5
+    # Basic checks that the cipher key_size hash uuid values exist
9602f5
+    # cf. https://github.com/rear/rear/pull/2504#issuecomment-718729198
9602f5
+    # because some values are needed during "rear recover"
9602f5
+    # to set cryptsetup options in layout/prepare/GNU/Linux/160_include_luks_code.sh
9602f5
+    # and it seems cryptsetup fails when options with empty values are specified
9602f5
+    # cf. https://github.com/rear/rear/pull/2504#issuecomment-719479724
9602f5
+    # For example a LUKS1 crypt entry in disklayout.conf looks like
9602f5
+    # crypt /dev/mapper/luks1test /dev/sda7 type=luks1 cipher=aes-xts-plain64 key_size=256 hash=sha256 uuid=1b4198c9-d9b0-4c57-b9a3-3433e391e706 
9602f5
+    # and a LUKS1 crypt entry in disklayout.conf looks like
9602f5
+    # crypt /dev/mapper/luks2test /dev/sda8 type=luks2 cipher=aes-xts-plain64 key_size=256 hash=sha256 uuid=3e874a28-7415-4f8c-9757-b3f28a96c4d2 
9602f5
+    # Only the keyfile_option value is optional and the luks_type value is already tested above.
9602f5
+    # Using plain test to ensure a value is a single non empty and non blank word
9602f5
+    # without quoting because test " " would return zero exit code
9602f5
+    # cf. "Beware of the emptiness" in https://github.com/rear/rear/wiki/Coding-Style
9602f5
+    # Do not error out instantly here but only report errors here so the user can see all messages
9602f5
+    # and actually error out at the end of this script if there was one actually invalid value:
9602f5
+    if ! test $cipher ; then
9602f5
+        LogPrint "No 'cipher' value for LUKS$version volume $target_name in $source_device"
9602f5
+    fi
9602f5
+    if test $key_size ; then
9602f5
+        if ! is_positive_integer $key_size ; then
9602f5
+            LogPrintError "Error: 'key_size=$key_size' is no positive integer for LUKS$version volume $target_name in $source_device"
9602f5
+            invalid_cryptsetup_option_value="yes"
9602f5
+        fi
9602f5
+    else
9602f5
+        LogPrint "No 'key_size' value for LUKS$version volume $target_name in $source_device"
9602f5
+    fi
9602f5
+    if ! test $hash ; then
9602f5
+        LogPrint "No 'hash' value for LUKS$version volume $target_name in $source_device"
9602f5
+    fi
9602f5
+    if ! test $uuid ; then
9602f5
+        # Report a missig uuid value as an error to have the user informed
9602f5
+        # but do not error out here because things can be fixed manually during "rear recover"
9602f5
+        # cf. https://github.com/rear/rear/pull/2506#issuecomment-721757810
9602f5
+        # and https://github.com/rear/rear/pull/2506#issuecomment-722315498
9602f5
+        # and https://github.com/rear/rear/issues/2509
9602f5
+        LogPrintError "Error: No 'uuid' value for LUKS$version volume $target_name in $source_device (mounting it or booting the recreated system may fail)"
9602f5
+    fi
9602f5
+
9602f5
+    echo "crypt /dev/mapper/$target_name $source_device type=$luks_type cipher=$cipher key_size=$key_size hash=$hash uuid=$uuid $keyfile_option" >> $DISKLAYOUT_FILE
9602f5
 
9602f5
-    echo "crypt /dev/mapper/$target_name $source_device cipher=$cipher-$mode key_size=$key_size hash=$hash uuid=$uuid $keyfile_option" >> $DISKLAYOUT_FILE
9602f5
 done < <( dmsetup ls --target crypt )
9602f5
+
9602f5
+# Let this script return successfully when invalid_cryptsetup_option_value is not true:
9602f5
+is_true $invalid_cryptsetup_option_value && Error "Invalid or empty LUKS cryptsetup option value(s) in $DISKLAYOUT_FILE" || true