From a07cb7d404582f9c0bfaedb9dd07f93848aa91c6 Mon Sep 17 00:00:00 2001 From: David Tardon Date: Thu, 30 May 2024 10:44:36 +0200 Subject: [PATCH 1/4] cryptsetup-generator: refactor add_crypttab_devices() Move the processing of a crypttab entry to a separate function. No functional changes, just refactoring. --- src/cryptsetup/cryptsetup-generator.c | 86 +++++++++++++++------------ 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c index 4db25d362f..d8065d8b93 100644 --- a/src/cryptsetup/cryptsetup-generator.c +++ b/src/cryptsetup/cryptsetup-generator.c @@ -795,6 +795,52 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat return 0; } +static int add_crypttab_device(const char *name, const char *device, const char *keyspec, const char *options) { + _cleanup_free_ char *keyfile = NULL, *keydev = NULL, *headerdev = NULL, *filtered_header = NULL; + crypto_device *d = NULL; + char *uuid; + int r; + + uuid = startswith(device, "UUID="); + if (!uuid) + uuid = path_startswith(device, "/dev/disk/by-uuid/"); + if (!uuid) + uuid = startswith(name, "luks-"); + if (uuid) + d = hashmap_get(arg_disks, uuid); + + if (arg_allow_list && !d) { + log_info("Not creating device '%s' because it was not specified on the kernel command line.", name); + return 0; + } + + r = split_locationspec(keyspec, &keyfile, &keydev); + if (r < 0) + return r; + + if (options && (!d || !d->options)) { + r = filter_header_device(options, &headerdev, &filtered_header); + if (r < 0) + return r; + options = filtered_header; + } + + r = create_disk(name, + device, + keyfile, + keydev, + (d && d->options) ? d->headerdev : headerdev, + (d && d->options) ? d->options : options, + arg_crypttab); + if (r < 0) + return r; + + if (d) + d->create = false; + + return 0; +} + static int add_crypttab_devices(void) { _cleanup_fclose_ FILE *f = NULL; unsigned crypttab_line = 0; @@ -811,10 +857,7 @@ static int add_crypttab_devices(void) { } for (;;) { - _cleanup_free_ char *line = NULL, *name = NULL, *device = NULL, *keyspec = NULL, *options = NULL, - *keyfile = NULL, *keydev = NULL, *headerdev = NULL, *filtered_header = NULL; - crypto_device *d = NULL; - char *uuid; + _cleanup_free_ char *line = NULL, *name = NULL, *device = NULL, *keyspec = NULL, *options = NULL; int k; r = read_stripped_line(f, LONG_LINE_MAX, &line); @@ -834,42 +877,9 @@ static int add_crypttab_devices(void) { continue; } - uuid = startswith(device, "UUID="); - if (!uuid) - uuid = path_startswith(device, "/dev/disk/by-uuid/"); - if (!uuid) - uuid = startswith(name, "luks-"); - if (uuid) - d = hashmap_get(arg_disks, uuid); - - if (arg_allow_list && !d) { - log_info("Not creating device '%s' because it was not specified on the kernel command line.", name); - continue; - } - - r = split_locationspec(keyspec, &keyfile, &keydev); + r = add_crypttab_device(name, device, keyspec, options); if (r < 0) return r; - - if (options && (!d || !d->options)) { - r = filter_header_device(options, &headerdev, &filtered_header); - if (r < 0) - return r; - free_and_replace(options, filtered_header); - } - - r = create_disk(name, - device, - keyfile, - keydev, - (d && d->options) ? d->headerdev : headerdev, - (d && d->options) ? d->options : options, - arg_crypttab); - if (r < 0) - return r; - - if (d) - d->create = false; } return 0; From 83813bae7ae471862ff84b038b5e4eaefae41c98 Mon Sep 17 00:00:00 2001 From: David Tardon Date: Thu, 30 May 2024 10:46:13 +0200 Subject: [PATCH 2/4] cryptsetup-generator: continue parsing after error Let's make the crypttab parser more robust and continue even if parsing of a line failed. --- src/cryptsetup/cryptsetup-generator.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c index d8065d8b93..6016e7580b 100644 --- a/src/cryptsetup/cryptsetup-generator.c +++ b/src/cryptsetup/cryptsetup-generator.c @@ -844,7 +844,7 @@ static int add_crypttab_device(const char *name, const char *device, const char static int add_crypttab_devices(void) { _cleanup_fclose_ FILE *f = NULL; unsigned crypttab_line = 0; - int r; + int r, ret = 0; if (!arg_read_crypttab) return 0; @@ -877,12 +877,10 @@ static int add_crypttab_devices(void) { continue; } - r = add_crypttab_device(name, device, keyspec, options); - if (r < 0) - return r; + RET_GATHER(ret, add_crypttab_device(name, device, keyspec, options)); } - return 0; + return ret; } static int add_proc_cmdline_devices(void) { From 47c703d949e84997d11d657fade68064c04a46c8 Mon Sep 17 00:00:00 2001 From: David Tardon Date: Thu, 30 May 2024 13:32:20 +0200 Subject: [PATCH 3/4] cryptsetup-generator: parse all cmdline devices too --- src/cryptsetup/cryptsetup-generator.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c index 6016e7580b..56350b9b12 100644 --- a/src/cryptsetup/cryptsetup-generator.c +++ b/src/cryptsetup/cryptsetup-generator.c @@ -884,7 +884,7 @@ static int add_crypttab_devices(void) { } static int add_proc_cmdline_devices(void) { - int r; + int r, ret = 0; crypto_device *d; HASHMAP_FOREACH(d, arg_disks) { @@ -910,11 +910,10 @@ static int add_proc_cmdline_devices(void) { d->headerdev, d->options ?: arg_default_options, "/proc/cmdline"); - if (r < 0) - return r; + RET_GATHER(ret, r); } - return 0; + return ret; } DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(crypt_device_hash_ops, char, string_hash_func, string_compare_func, From d181939e2e382631d9b067e0b4cfbf11b709a297 Mon Sep 17 00:00:00 2001 From: David Tardon Date: Thu, 30 May 2024 13:33:57 +0200 Subject: [PATCH 4/4] cryptsetup-generator: always process cmdline devices --- src/cryptsetup/cryptsetup-generator.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c index 56350b9b12..6a3ff961fc 100644 --- a/src/cryptsetup/cryptsetup-generator.c +++ b/src/cryptsetup/cryptsetup-generator.c @@ -939,14 +939,9 @@ static int run(const char *dest, const char *dest_early, const char *dest_late) return 0; r = add_crypttab_devices(); - if (r < 0) - return r; + RET_GATHER(r, add_proc_cmdline_devices()); - r = add_proc_cmdline_devices(); - if (r < 0) - return r; - - return 0; + return r; } DEFINE_MAIN_GENERATOR_FUNCTION(run);