Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial LUKS2 support #2504

Merged
merged 6 commits into from Oct 28, 2020
Merged

Add initial LUKS2 support #2504

merged 6 commits into from Oct 28, 2020

Conversation

vcrhonek
Copy link
Contributor

Relax-and-Recover (ReaR) Pull Request Template

Please fill in the following items before submitting a new pull request:

Pull Request Details:
  • Type: Bug Fix / New Feature / Enhancement / Other?
    Enhancement.

  • Impact: Low / Normal / High / Critical / Urgent
    Normal.

  • Reference to related issue (URL):
    Failed to recover LUKS version 2 #2204

  • How was this pull request tested?
    Quite thoroughly. I've run many backup followed by recover tests on Fedora 32, RHEL 8.2 and Fedora 25 (because there is LUKS1 by default on f25). I didn't notice any issues when I used default encryption during install of the system. I've also tried various ciphers/key-sizes/hash variants to ensure that values given by luksDump parsing on LUKS2 are meaningful and to recreating LUKS1 on system where LUKS2 is default and vice versa.

  • Brief description of the changes in this pull request:
    a) it adds new parameter 'type' to 'crypt' keyword used in disklayout.conf. Using this parameter allows to recreate the same version of LUKS that was on the system
    b) it adds LUKS version detection, parsing depending on version and usage of 'type' parameter have been added to
    /usr/share/rear/layout/save/GNU/Linux/260_crypt_layout.sh

I'm aware that this is useful mainly for basic LUKS2 setup (let's say similar to LUKS1), but I believe that even so it could be sufficient for many users.

And I wonder... wouldn't be better to backup and restore complete LUKS header instead of taking values from luksDump and creating new one? That would simplify things a lot and, for example, users won't lose keys from additional keyslots, ...

This is needed for recreation of the same version of LUKS that was
on the system. Without it, default version of LUKS will be created
depending on version of cryptsetup. Default header format is LUKS1
with cryptsetup < 2.1.0 and LUKS2 with cryptsetup >= 2.1.0.
Use 'blkid' to check that it's LUKS and to find its version. It should
be prefered over 'cryptsetup' according to LUKS maintainers.
@jsmeix jsmeix requested a review from a team October 20, 2020 12:33
@jsmeix jsmeix added the enhancement Adaptions and new features label Oct 20, 2020
@jsmeix jsmeix added this to the ReaR v2.7 milestone Oct 20, 2020
@jsmeix
Copy link
Member

jsmeix commented Oct 20, 2020

@vcrhonek
wow! Thank you!
We will have a look...

@@ -13,6 +13,9 @@ create_crypt() {
value=${option#*=}

case "$key" in
type)
cryptsetup_options+=" --type $value"
Copy link
Member

@jsmeix jsmeix Oct 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just off the top of my head:
I think the --type option is not supported by older cryptsetup versions
that is provided in older Linux operating systems
that are perhaps still supported by ReaR, cf.
https://github.com/rear/rear/blob/master/doc/rear-release-notes.txt#L2513

ReaR-2.6 is supported on the following Linux based operating systems:
  o Fedora 29, 30, 31, and 32
  o RHEL 6, 7, and 8
  o CentOS 6, 7, and 8
  o Scientific Linux 6 and 7
  o SLES 12 and 15
  o openSUSE Leap 15.x
  o Debian 8, and 9
  o Ubuntu 16, 17, and 18

So some backward compatibility code could be required here.
See in particular the sections
"Try hard to care about possible errors" and
"Maintain backward compatibility" and
"Dirty hacks welcome" in
https://github.com/rear/rear/wiki/Coding-Style

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found it:
#2432 (comment)

So for all supported SLES versions cryptsetup supports the --type option.

@vcrhonek
did you check that for all supported RHEL and the other Red Hat based operating systems
cryptsetup supports the --type option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I will do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not supported on all systems, at least not at RHEL 6.

Copy link
Member

@jsmeix jsmeix Oct 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vcrhonek
if you do no longer need RHEL 6 support in current ReaR upstream master code
we can drop support for RHEL 6 and other Red Hat based operating systems
that are basically same for next ReaR version 2.7
cf. what I wrote about older operating systems in our release notes
https://github.com/rear/rear/blob/master/doc/rear-release-notes.txt#L2556
so we could drop support for RHEL 6 and related operating systems
with a note what is known to no longer work
i.e. LUKS support (also LUKS 1 for RHEL 6) in this case.
When no LUKS is used dmsetup ls --target crypt should not output anything
so that usr/share/rear/layout/save/GNU/Linux/260_crypt_layout.sh
results nothing about LUKS in disklayout.conf.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've checked all Red Hat based operating systems and type option is supported on all of them except RHEL 6 and CentOS 6. I've also discovered that blkid -p -o export returns VERSION=256 for LUKS 1 on RHEL 6 so it won't get through LUKS version check anyway.

@gdha
Copy link
Member

gdha commented Oct 21, 2020

@vcrhonek @jsmeix Do not bother with RHEL/CentOS 6 as it is End-of-Life (EOL) anyhow. In Rear 2.7 we will mark it that way.

Log "Skipping $target_name (its $source_device is not a LUKS device)"
continue
fi

# Detect LUKS version
version=$( grep "VERSION" $TMP_DIR/blkid.output | cut -d= -f2 )
if !( test $version = "1" || test $version = "2") ; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition !( ...) will not work as intended
because "man bash" reads

If the extglob shell option is enabled
...
 !(pattern-list)
 Matches anything except one of the given patterns

and usr/sbin/rear has the extglob shell option enabled
https://github.com/rear/rear/blob/master/usr/sbin/rear#L314

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sigh - I needed three commits to now hopefully have that fixed.
Sometimes bash scripting is so weird.

Fixed version test, cf. https://github.com/rear/rear/pull/2504/files#r510856646
plus more explanatory (error) messages.
Fixed how the LUKS version is determined
@jsmeix
Copy link
Member

jsmeix commented Oct 23, 2020

@gdha
please could you add a new initial release-notes-2-7.md to
https://github.com/rear/rear.github.com/tree/master/documentation
so that I could adapt the "Supported and Unsupported Operating Systems" section
for ReaR 2.7 right now?

@jsmeix
Copy link
Member

jsmeix commented Oct 23, 2020

@vcrhonek
I committed some fixes to your code that hopefully do what I intend.
Could you please test if things still work OK for you with my changes?

@vcrhonek
Copy link
Contributor Author

@jsmeix
Thank you for improvement of the code, it looks good. Sure, I'll do some tests and let you know.

If more than one keyslot is used in LUKS header the 'grep' command
looking for 'Cipher key' would return more than one line.
But we need just one value. Use 'key_size' from the first found key.
@vcrhonek
Copy link
Contributor Author

vcrhonek commented Oct 26, 2020

I've tested and your changes and it works fine, no problem there. But I've found different issue that emerged when multiple keys were defined in LUKS header (this is not done by system installer by default, that's main reason why I've missed it before). I fixed that with additional commit.

@jsmeix
Copy link
Member

jsmeix commented Oct 26, 2020

@vcrhonek
thank you for testing it!

@jsmeix
Copy link
Member

jsmeix commented Oct 26, 2020

@rear/contributors
if there are no objections I would like to merge it tomorrow afternoon
so that users who use our current GitHub master code could test it
and provide early feedback if issues appear for them.

@jsmeix
Copy link
Member

jsmeix commented Oct 28, 2020

Yesterday I found no time to merge is so I will merge it today afternoon.

@jsmeix jsmeix merged commit 8dfd1c1 into rear:master Oct 28, 2020
@jsmeix
Copy link
Member

jsmeix commented Oct 28, 2020

@vcrhonek
thank you for adding initial LUKS2 support to ReaR!

Regarding your question in your initial entry (excerpts)

backup and restore complete LUKS header
users won't lose keys from additional keyslots

In general the ReaR recovery system must be free of secrets
except the user has explicitly configured something different,
cf. the reasoning about SSH_UNPROTECTED_PRIVATE_KEYS in default.conf
https://github.com/rear/rear/blob/master/usr/share/rear/conf/default.conf#L1523

I don't know about LUKS internals so I don't know if complete LUKS headers
in the ReaR recovery system could contain secrets or could be used
by someone to extract keys relatively easily (e.g. like getting /etc/shadow
from a host so one could do long running cracking attempts to get passwords).

@vcrhonek
Copy link
Contributor Author

@jsmeix
You're welcome, thank you for your review and improvements!

I see. I don't know much about LUKS internals either. Most of the header is encrypted, but I'm afraid that if someone has copy of it, keys from the header could be used to access the data even if these keys are no longer valid (were removed in more recent version of header). I can ask LUKS experts.

I was thinking to introduce LUKS_HEADER_RESTORE variable that would (when explicitly configured by user) cause usage of luksHeaderBackup/Restore instead of calling luksFormat. I did few tests and it seems to work fine. Please let me know if you think that it would be beneficial, I can contribute the code.

@vcrhonek vcrhonek deleted the luks2 branch October 29, 2020 11:35
@jsmeix
Copy link
Member

jsmeix commented Oct 29, 2020

And this example is a good reason why cryptsetup luksHeaderBackup / luksHeaderRestore
has advantages (for the price of being possibly less secure depending on the user's environment).

@jsmeix
Copy link
Member

jsmeix commented Oct 30, 2020

I verified that things fail for me as I described above in
#2504 (comment)

With current GitHub master code I get
for rear -D recover this terminal output (excerpts)

Please enter the password for LUKS device luks2test (/dev/sdb2):
...
The disk layout recreation script failed

This is the matching excerpt from the log file
about the actual failure in /var/lib/rear/layout/diskrestore.sh
(long lines shown wrapped here):

+++ Print 'Please enter the password for LUKS device luks2test (/dev/sdb2):'
+++ cryptsetup luksFormat --batch-mode --type luks2 --cipher aes-xts-plain64
 --key-size --hash sha256 --uuid 30376f43-60fd-4fc7-af0c-fad8063d5a1a
 --iter-time 2000 --use-random --force-password /dev/sdb2
Usage: cryptsetup [-?vyrq] [-?|--help] [--usage] [--version] [-v|--verbose]
        [--debug] [-c|--cipher=STRING] [-h|--hash=STRING]
        [-y|--verify-passphrase] [-d|--key-file=STRING]
        [--master-key-file=STRING] [--dump-master-key] [-s|--key-size=BITS]
        [-l|--keyfile-size=bytes] [--keyfile-offset=bytes]
        [--new-keyfile-size=bytes] [--new-keyfile-offset=bytes]
        [-S|--key-slot=INT] [-b|--size=SECTORS] [-o|--offset=SECTORS]
        [-p|--skip=SECTORS] [-r|--readonly] [-q|--batch-mode]
        [-t|--timeout=secs] [--progress-frequency=secs] [-T|--tries=INT]
        [--align-payload=SECTORS] [--header-backup-file=STRING]
        [--use-random] [--use-urandom] [--shared] [--uuid=STRING]
        [--allow-discards] [--header=STRING] [--test-passphrase]
        [--tcrypt-hidden] [--tcrypt-system] [--tcrypt-backup] [--veracrypt]
        [--veracrypt-pim=INT] [--veracrypt-query-pim] [-M|--type=STRING]
        [--force-password] [--perf-same_cpu_crypt]
        [--perf-submit_from_crypt_cpus] [--deferred] [-i|--iter-time=msecs]
        [--pbkdf=STRING] [--pbkdf-memory=kilobytes]
        [--pbkdf-parallel=threads] [--pbkdf-force-iterations=LONG]
        [--priority=STRING] [--disable-locks] [--disable-keyring]
        [-I|--integrity=STRING] [--integrity-no-journal]
        [--integrity-no-wipe] [--token-only] [--token-id=INT]
        [--key-description=STRING] [--sector-size=INT] [--persistent]
        [--label=STRING] [--subsystem=STRING] [--unbound]
        [--json-file=STRING] [OPTION...] <action> <action-specific>
--hash: invalid numeric value

I have LUKS_CRYPTSETUP_OPTIONS+=" --force-password" in my etc/rear/local.conf
(for the reason see the LUKS_CRYPTSETUP_OPTIONS comments in default.conf).

/var/lib/rear/layout/diskrestore.sh runs with set -e - cf. the log (excerpts)

+ source /usr/share/rear/layout/recreate/default/200_run_layout_code.sh
...
++ source /var/lib/rear/layout/diskrestore.sh
+++ LogPrint 'Start system layout restoration.'
...
+++ set -e
+++ set -x

so any non-zero exit code in diskrestore.sh results
that The disk layout recreation script failed.

/var/lib/rear/layout/diskrestore.sh is generated only during rear recover by this script:
https://github.com/rear/rear/blob/master/usr/share/rear/layout/prepare/default/540_generate_device_code.sh

jsmeix added a commit that referenced this pull request Oct 30, 2020
In layout/save/GNU/Linux/260_crypt_layout.sh
grep for "Key:" (instead of "Cipher key") to get the key_size value
from the 'cryptsetup luksDump' output in case of LUKS2, see
#2504 (comment)
and subsequent comments therein.
@jsmeix
Copy link
Member

jsmeix commented Oct 30, 2020

With b49a4fc
LUKS2 support does now also work with cryptsetup version 2.0.6
(at least it works now for me on openSUSE Leap 15.1).

According to
#2504 (comment)
it should still work for the systems that @vcrhonek has.

@vcrhonek
please verify that current GitHub master code still works for your systems.
Cf. "Testing current ReaR upstream GitHub master code" in
https://en.opensuse.org/SDB:Disaster_Recovery

@jsmeix
Copy link
Member

jsmeix commented Oct 30, 2020

@mannp @adatum
I would appreciate it when you could test if our LUKS2 support
in our current GitHub master code also works on your systems
cf. "Testing current ReaR upstream GitHub master code" in
https://en.opensuse.org/SDB:Disaster_Recovery
and provide feedback here whether or not things work for you.

@mannp
Copy link

mannp commented Oct 30, 2020

@mannp @adatum
I would appreciate it when you could test if our LUKS2 support
in our current GitHub master code also works on your systems
cf. "Testing current ReaR upstream GitHub master code" in
https://en.opensuse.org/SDB:Disaster_Recovery
and provide feedback here whether or not things work for you.

@jsmeix I was very happy to see the merge for LUKS2 support and installed the git version of rear to give it a try yesterday.

Got sidetracked, but will set aside some time to give it a try :)

@jsmeix
Copy link
Member

jsmeix commented Oct 30, 2020

@mannp
depending on what cryptsetup version you run you may need
b49a4fc
which is in the GitHub master code of today.

So I would recommend to better update again to our current GitHub master code.

Perhaps you even use a cryptsetup version where my
b49a4fc
does not work and then we would like to know such issues better sooner than later
so that we could make LUKS2 support working with all usual cryptsetup versions.

jsmeix added a commit that referenced this pull request Oct 30, 2020
In layout/save/GNU/Linux/260_crypt_layout.sh
added basic checks that the cipher key_size hash uuid values exist
cf. #2504 (comment)
@jsmeix
Copy link
Member

jsmeix commented Oct 30, 2020

With
d25a1a0
I added basic checks to layout/save/GNU/Linux/260_crypt_layout.sh
that the cipher key_size hash uuid values exist.

For me things still work well with that checks.

With artificial causing such errors via

    fi

    key_size=""
    uuid="  "

    if ! test $cipher ; then

in usr/share/rear/layout/save/GNU/Linux/260_crypt_layout.sh
"rear -D mkrescue" fails for me with this terminal output:

# usr/sbin/rear -D mkrescue
...
Creating disk layout
Overwriting existing disk layout file /root/rear.github.master/var/lib/rear/layout/disklayout.conf
Error: No 'key_size' value for LUKS volume cr_ata-TOSHIBA_MQ01ABF050_Y2PLP02CT-part3 in /dev/sda3
Error: No 'uuid' value for LUKS volume cr_ata-TOSHIBA_MQ01ABF050_Y2PLP02CT-part3 in /dev/sda3
Error: No 'key_size' value for LUKS volume luks1test in /dev/sda7
Error: No 'uuid' value for LUKS volume luks1test in /dev/sda7
Error: No 'key_size' value for LUKS volume cr_ata-TOSHIBA_MQ01ABF050_Y2PLP02CT-part2 in /dev/sda2
Error: No 'uuid' value for LUKS volume cr_ata-TOSHIBA_MQ01ABF050_Y2PLP02CT-part2 in /dev/sda2
Error: No 'key_size' value for LUKS volume luks2test in /dev/sda8
Error: No 'uuid' value for LUKS volume luks2test in /dev/sda8
ERROR: Missing LUKS cryptsetup option value in /root/rear.github.master/var/lib/rear/layout/disklayout.conf
Some latest log messages since the last called script 260_crypt_layout.sh:
  2020-10-30 15:27:24.633752720 Error: No 'key_size' value for LUKS volume cr_ata-TOSHIBA_MQ01ABF050_Y2PLP02CT-part3 in /dev/sda3
  2020-10-30 15:27:24.635879887 Error: No 'uuid' value for LUKS volume cr_ata-TOSHIBA_MQ01ABF050_Y2PLP02CT-part3 in /dev/sda3
  2020-10-30 15:27:24.715283297 Error: No 'key_size' value for LUKS volume luks1test in /dev/sda7
  2020-10-30 15:27:24.718042920 Error: No 'uuid' value for LUKS volume luks1test in /dev/sda7
  2020-10-30 15:27:24.800718783 Error: No 'key_size' value for LUKS volume cr_ata-TOSHIBA_MQ01ABF050_Y2PLP02CT-part2 in /dev/sda2
  2020-10-30 15:27:24.804179315 Error: No 'uuid' value for LUKS volume cr_ata-TOSHIBA_MQ01ABF050_Y2PLP02CT-part2 in /dev/sda2
  2020-10-30 15:27:24.877678147 Error: No 'key_size' value for LUKS volume luks2test in /dev/sda8
  2020-10-30 15:27:24.880293752 Error: No 'uuid' value for LUKS volume luks2test in /dev/sda8
Aborting due to an error, check /root/rear.github.master/var/log/rear/rear-linux-h9wr.log for details

@jsmeix
Copy link
Member

jsmeix commented Oct 30, 2020

And on top of that via
d3e5d15
I made nicer error message texts that contain the LUKS version (i.e. 'LUKS1' or 'LUKS2').
Why not provide info to the user when it is there in ReaR.
It can help him to better distinguish possibly various LUKS volumes that he may have.

With that my above artificial causing such errors lets "rear -D mkrescue" fail
with the following messages on the terminal:

Error: No 'key_size' value for LUKS1 volume cr_ata-TOSHIBA_MQ01ABF050_Y2PLP02CT-part3 in /dev/sda3
Error: No 'uuid' value for LUKS1 volume cr_ata-TOSHIBA_MQ01ABF050_Y2PLP02CT-part3 in /dev/sda3
Error: No 'key_size' value for LUKS1 volume luks1test in /dev/sda7
Error: No 'uuid' value for LUKS1 volume luks1test in /dev/sda7
Error: No 'key_size' value for LUKS1 volume cr_ata-TOSHIBA_MQ01ABF050_Y2PLP02CT-part2 in /dev/sda2
Error: No 'uuid' value for LUKS1 volume cr_ata-TOSHIBA_MQ01ABF050_Y2PLP02CT-part2 in /dev/sda2
Error: No 'key_size' value for LUKS2 volume luks2test in /dev/sda8
Error: No 'uuid' value for LUKS2 volume luks2test in /dev/sda8
ERROR: Missing LUKS cryptsetup option value(s) in /root/rear.github.master/var/lib/rear/layout/disklayout.conf

@mannp
Copy link

mannp commented Oct 30, 2020

@jsmeix I have not been using rear for a long time so need to configure and test on a spare machine, so it will take me some time to get together :)

I previously was using this on my main machines -> https://aur.archlinux.org/packages/relax-and-recover-git/
Relax-and-Recover 2.6 / Git
Not sure if this pulls in your changes.

@vcrhonek
Copy link
Contributor Author

vcrhonek commented Nov 2, 2020

@jsmeix I've looked on recent updates and it looks good and works fine on my test machines, thanks!

Have you considered omitting missing values instead of reporting error? Because luksFormat doesn't require any of {cipher, key-size, hash} and if omitted default value is used. Maybe it could omit missing option value and print warning? I'm not saying that I think it would be better, I guess that depends on situation and user preferences.

I was also thinking about 'Key:'/'Cipher key:" issue for second time. If only 'Cipher key:' was present in luksDump, parsing would fail again. I have no idea whether that can happen, to be honest. If that happens, we can match both and use first found like grep -m1 -i "key:" | sed -r 's/^.+:\s*(.+) bits$/\1/'.

But overall, it looks good to me now and I'm looking forward to see results on other setups and distributions.

@jsmeix
Copy link
Member

jsmeix commented Nov 2, 2020

@mannp
to test the current GitHub master code on your system see
"Testing current ReaR upstream GitHub master code" in
https://en.opensuse.org/SDB:Disaster_Recovery

That's what I do myself.

I don't know about whatever third-party ReaR packages.
You would have to ask the third-party what their package provides.

@jsmeix
Copy link
Member

jsmeix commented Nov 2, 2020

@vcrhonek
thank you for your sugestions how to further improve the LUKS code in ReaR!

Currently LUKS support (both LUKS1 and LUKS2) is under step by step enhancement
i.e. none of my changes is meant to be an ultimate final state.

I just proceed little step by little step where each little step should keep things working
i.e. I try to not introduce regressions that let things fail that had somehow worked before.

Because at any time a more important issue (e.g. a severe security issue with SUSE
software that I maintain) could appear that gets in my way to move forward with ReaR
so that I can enhance ReaR only little setp by little step where I can pause at any time.

E.g. as long as the LUKS recreation code in usr/share/rear/layout/prepare/
and usr/share/rear/layout/recreate/ depends on cryptsetup option values
we must error out when the usr/share/rear/layout/save/ code fails to get such values.

When the LUKS recreation code in layout/prepare/ was enhanced as a precondition
that it does no longer fail when certain optional cryptsetup option values are missing
then we could no longer error out when the layout/save/ code fails to get such values.

Currently I have time to enhance the LUKS code so I will do some more enhancements
based on your suggestions (but again: only little step by little step).

@jsmeix
Copy link
Member

jsmeix commented Nov 2, 2020

Right now I see the documentation in
https://github.com/rear/rear/blob/master/doc/user-guide/06-layout-configuration.adoc
in the section about "Disk layout file syntax" the "LUKS Devices" part is plain wrong
(excerpts - long lines wrapped here):

Square brackets "[" and "]" indicate an optional parameter.
They can be excluded when hand-crafting a layout file line.
...
LUKS Devices

crypt /dev/mapper/<name> <device> [type=<type>]
 [cipher=<cipher>] [key_size=<key size>] [hash=<hash function>]
 [uuid=<uuid>] [keyfile=<keyfile>] [password=<password>]

Actually with the current LUKS recreation code in
layout/prepare/GNU/Linux/160_include_luks_code.sh
only keyfile=<keyfile> and password=<password> are optional.

Fortunately nobody reads documenation so such errors are
totally unimportant in real world practice out there ;-)

jsmeix added a commit that referenced this pull request Nov 2, 2020
The "cryptseup luksFormat" command does not require
any of the cipher, key-size, hash option values
because if omitted a cryptseup default value is used, cf.
#2504 (comment)
@jsmeix
Copy link
Member

jsmeix commented Nov 2, 2020

#2506
is my first initial currently untested attempt towards making
recreating LUKS volumes work with optional cryptsetup options

jsmeix added a commit that referenced this pull request Nov 2, 2020
Depending on the version the "cryptsetup luksDump" command
outputs the key_size value as a line like "    Key:        512 bits"
and/or as a line like "    Cipher key: 512 bits"
cf. #2504 (comment)
and subsequent comments
so we grep for both lines but use only the first match.
@jsmeix
Copy link
Member

jsmeix commented Nov 3, 2020

@vcrhonek
regarding your #2504 (comment)
therein (excerpt)

luksFormat doesn't require any of {cipher, key-size, hash}
and if omitted default value is used

Accordingly currently in #2506
the cryptsetup options type=<type> and uuid=<uuid>
are still mandatory but I wonder if that is really needed.

How I did set up a LUKS2 volume on a test VM was using plain

cryptsetup luksFormat --type luks2 --force-password /dev/sdb2

cryptsetup luksOpen /dev/sdb2 luks2test

mkfs.ext4 /dev/mapper/luks2test

mkdir /luks2test

mount /dev/mapper/luks2test /luks2test

I need --force-password only because I use weak test passwords.

According to "man cryptsetup"

    luksFormat <device> [<key file>]

the <device> is the only mandatory parameter for cryptseup luksFormat.

So I think we should handle all options optionally in crypt entries in disklayout.conf
except /dev/mapper/<name> and <device>
because <device> is mandatory for cryptsetup
and /dev/mapper/<name> is mandatory for mkfs.

@vcrhonek
Copy link
Contributor Author

vcrhonek commented Nov 3, 2020

@jsmeix Yes, I agree that type can be optional (still, we probably want to pass it to crypt as we
should be able to get it in layout/save and lately with rear recover we want to recreate the same version of LUKS
that actually was on the machine). But from the point of crypt and cryptsetup luksFormat it is optional.

I'm not sure about uuid though. I didn't try to omit it. It's true that it's not needed by cryptestup luksFormat itself,
but I really don't know ReaR code enough to be sure that it's not needed because of something else (for example, this particular
uuid is referred somewhere else, in some config file or something...). I'm sure you know better:)

I did try to edit disklayout.conf just before I ran rear recover yesterday and replaced
crypt /dev/mapper/luks-d3...5b /dev/vda2 type=luks2 cipher=aes-xts-plain64 key_size=512 hash=sha256 uuid=d3..5b
with crypt /dev/mapper/luks-d3...5b /dev/vda2 uuid=d3...5b (using original code in layout/prepare, without your latest changes applied). The system recovered as expected.

I'll try to save some time tomorrow and look at your recent updates.

@jsmeix
Copy link
Member

jsmeix commented Nov 3, 2020

In #2506
I will change the code so that typeand uuid are optional
and try out how things behave for me without any of the optional options.

My primary intent behind is not that "all will just work well"
even without any of the optional options, in particular I think
to recreate a system as if was before all those options should be there.

My primary intent is to not let "rear recover" needlessly fail
if it could have somehow succeed to set up things with default/fallback values.
I think such a "graceful degradation" behaviour could help the user
in case of an emergency disaster recovery more in real world practice.

@vcrhonek
Copy link
Contributor Author

vcrhonek commented Nov 3, 2020

In #2506

My primary intent is to not let "rear recover" needlessly fail
if it could have somehow succeed to set up things with default/fallback values.
I think such a "graceful degradation" behaviour could help the user
in case of an emergency disaster recovery more in real world practice.

I absolutely agree with that and I had it on mind in first part of #2504 (comment)

jsmeix added a commit that referenced this pull request Nov 6, 2020
…onal_values

Make recreating LUKS volumes work with optional cryptsetup options:
The "cryptseup luksFormat" command does not require
any of the type, cipher, key-size, hash, uuid option values
because if omitted a cryptseup default value is used, cf.
#2504 (comment)
Right UUID values are mantatory for LUKS volumes
that will be mounted during startup of the recreated system.
But this does not mean ReaR should error out when there is
no cryptsetup uuid value because it is possible to run "rear recover"
with enforced MIGRATION_MODE and manually correct the
restored /mnt/local/etc/crypttab file to use the new UUIDs
before the initrd is recreated and the bootloader is (re)-installed
cf. #2509
pcahyna pushed a commit to pcahyna/rear that referenced this pull request Feb 17, 2023
In layout/save/GNU/Linux/260_crypt_layout.sh
grep for "Key:" (instead of "Cipher key") to get the key_size value
from the 'cryptsetup luksDump' output in case of LUKS2, see
rear#2504 (comment)
and subsequent comments therein.
pcahyna pushed a commit to pcahyna/rear that referenced this pull request Feb 17, 2023
In layout/save/GNU/Linux/260_crypt_layout.sh
added basic checks that the cipher key_size hash uuid values exist
cf. rear#2504 (comment)
pcahyna pushed a commit to pcahyna/rear that referenced this pull request Feb 17, 2023
The "cryptseup luksFormat" command does not require
any of the cipher, key-size, hash option values
because if omitted a cryptseup default value is used, cf.
rear#2504 (comment)
pcahyna pushed a commit to pcahyna/rear that referenced this pull request Feb 17, 2023
Depending on the version the "cryptsetup luksDump" command
outputs the key_size value as a line like "    Key:        512 bits"
and/or as a line like "    Cipher key: 512 bits"
cf. rear#2504 (comment)
and subsequent comments
so we grep for both lines but use only the first match.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Adaptions and new features fixed / solved / done
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants