teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0297-cryptsetup-add-keyfile-timeout-to-allow-a-keydev-tim.patch

b9a53a
From 0f7a4f49a7ce95e87061afe03ac40662a1eb0e2d Mon Sep 17 00:00:00 2001
b9a53a
From: shinygold <10763595+shinygold@users.noreply.github.com>
b9a53a
Date: Tue, 16 Jul 2019 13:06:16 +0200
b9a53a
Subject: [PATCH] cryptsetup: add keyfile-timeout to allow a keydev timeout and
b9a53a
 allow to fallback to a password if it fails.
b9a53a
b9a53a
(cherry picked from commit 50d2eba27b9bfc77ef6b40e5721713846815418b)
b9a53a
b9a53a
Resolves: #1763155
b9a53a
---
b9a53a
 src/cryptsetup/cryptsetup-generator.c | 119 ++++++++++++++++++--------
b9a53a
 src/cryptsetup/cryptsetup.c           |   5 +-
b9a53a
 2 files changed, 89 insertions(+), 35 deletions(-)
b9a53a
b9a53a
diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c
b9a53a
index 52c1262728..1e8e3ba00d 100644
b9a53a
--- a/src/cryptsetup/cryptsetup-generator.c
b9a53a
+++ b/src/cryptsetup/cryptsetup-generator.c
b9a53a
@@ -40,10 +40,39 @@ static Hashmap *arg_disks = NULL;
b9a53a
 static char *arg_default_options = NULL;
b9a53a
 static char *arg_default_keyfile = NULL;
b9a53a
 
b9a53a
-static int generate_keydev_mount(const char *name, const char *keydev, char **unit, char **mount) {
b9a53a
-        _cleanup_free_ char *u = NULL, *what = NULL, *where = NULL, *name_escaped = NULL;
b9a53a
+static int split_keyspec(const char *keyspec, char **keyfile, char **keydev) {
b9a53a
+        _cleanup_free_ char *kfile = NULL, *kdev = NULL;
b9a53a
+        char *c;
b9a53a
+
b9a53a
+        assert(keyspec);
b9a53a
+        assert(keyfile);
b9a53a
+        assert(keydev);
b9a53a
+
b9a53a
+        c = strrchr(keyspec, ':');
b9a53a
+        if (c) {
b9a53a
+                kfile = strndup(keyspec, c-keyspec);
b9a53a
+                kdev = strdup(c + 1);
b9a53a
+                if (!*kfile || !*kdev)
b9a53a
+                        return log_oom();
b9a53a
+        } else {
b9a53a
+                /* No keydev specified */
b9a53a
+                kfile = strdup(keyspec);
b9a53a
+                kdev = NULL;
b9a53a
+                if (!*kfile)
b9a53a
+                        return log_oom();
b9a53a
+        }
b9a53a
+
b9a53a
+        *keyfile = TAKE_PTR(kfile);
b9a53a
+        *keydev = TAKE_PTR(kdev);
b9a53a
+
b9a53a
+        return 0;
b9a53a
+}
b9a53a
+
b9a53a
+static int generate_keydev_mount(const char *name, const char *keydev, const char *keydev_timeout, bool canfail, char **unit, char **mount) {
b9a53a
+        _cleanup_free_ char *u = NULL, *what = NULL, *where = NULL, *name_escaped = NULL, *device_unit = NULL;
b9a53a
         _cleanup_fclose_ FILE *f = NULL;
b9a53a
         int r;
b9a53a
+        usec_t timeout_us;
b9a53a
 
b9a53a
         assert(name);
b9a53a
         assert(keydev);
b9a53a
@@ -88,7 +117,25 @@ static int generate_keydev_mount(const char *name, const char *keydev, char **un
b9a53a
                 "[Mount]\n"
b9a53a
                 "What=%s\n"
b9a53a
                 "Where=%s\n"
b9a53a
-                "Options=ro\n", what, where);
b9a53a
+                "Options=ro%s\n", what, where, canfail ? ",nofail" : "");
b9a53a
+
b9a53a
+        if (keydev_timeout) {
b9a53a
+                r = parse_sec_fix_0(keydev_timeout, &timeout_us);
b9a53a
+                if (r >= 0) {
b9a53a
+                        r = unit_name_from_path(what, ".device", &device_unit);
b9a53a
+                        if (r < 0)
b9a53a
+                                return log_error_errno(r, "Failed to generate unit name: %m");
b9a53a
+
b9a53a
+                        r = write_drop_in_format(arg_dest, device_unit, 90, "device-timeout",
b9a53a
+                                "# Automatically generated by systemd-cryptsetup-generator \n\n"
b9a53a
+                                "[Unit]\nJobRunningTimeoutSec=%s", keydev_timeout);
b9a53a
+                        if (r < 0)
b9a53a
+                                return log_error_errno(r, "Failed to write device drop-in: %m");
b9a53a
+
b9a53a
+                } else
b9a53a
+                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", keydev_timeout);
b9a53a
+
b9a53a
+        }
b9a53a
 
b9a53a
         r = fflush_and_check(f);
b9a53a
         if (r < 0)
b9a53a
@@ -103,16 +150,17 @@ static int generate_keydev_mount(const char *name, const char *keydev, char **un
b9a53a
 static int create_disk(
b9a53a
                 const char *name,
b9a53a
                 const char *device,
b9a53a
-                const char *keydev,
b9a53a
                 const char *password,
b9a53a
+                const char *keydev,
b9a53a
                 const char *options) {
b9a53a
 
b9a53a
         _cleanup_free_ char *n = NULL, *d = NULL, *u = NULL, *e = NULL,
b9a53a
-                *filtered = NULL, *u_escaped = NULL, *password_escaped = NULL, *filtered_escaped = NULL, *name_escaped = NULL, *keydev_mount = NULL;
b9a53a
+                *keydev_mount = NULL, *keyfile_timeout_value = NULL, *password_escaped = NULL,
b9a53a
+                *filtered = NULL, *u_escaped = NULL, *filtered_escaped = NULL, *name_escaped = NULL;
b9a53a
         _cleanup_fclose_ FILE *f = NULL;
b9a53a
         const char *dmname;
b9a53a
         bool noauto, nofail, tmp, swap, netdev;
b9a53a
-        int r;
b9a53a
+        int r, keyfile_can_timeout;
b9a53a
 
b9a53a
         assert(name);
b9a53a
         assert(device);
b9a53a
@@ -123,6 +171,10 @@ static int create_disk(
b9a53a
         swap = fstab_test_option(options, "swap\0");
b9a53a
         netdev = fstab_test_option(options, "_netdev\0");
b9a53a
 
b9a53a
+        keyfile_can_timeout = fstab_filter_options(options, "keyfile-timeout\0", NULL, &keyfile_timeout_value, NULL);
b9a53a
+        if (keyfile_can_timeout < 0)
b9a53a
+                return log_error_errno(keyfile_can_timeout, "Failed to parse keyfile-timeout= option value: %m");
b9a53a
+
b9a53a
         if (tmp && swap) {
b9a53a
                 log_error("Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", name);
b9a53a
                 return -EINVAL;
b9a53a
@@ -152,12 +204,6 @@ static int create_disk(
b9a53a
         if (r < 0)
b9a53a
                 return log_error_errno(r, "Failed to generate unit name: %m");
b9a53a
 
b9a53a
-        if (password) {
b9a53a
-                password_escaped = specifier_escape(password);
b9a53a
-                if (!password_escaped)
b9a53a
-                        return log_oom();
b9a53a
-        }
b9a53a
-
b9a53a
         if (keydev && !password) {
b9a53a
                 log_error("Key device is specified, but path to the password file is missing.");
b9a53a
                 return -EINVAL;
b9a53a
@@ -178,10 +224,16 @@ static int create_disk(
b9a53a
                 "After=%s\n",
b9a53a
                 netdev ? "remote-fs-pre.target" : "cryptsetup-pre.target");
b9a53a
 
b9a53a
+        if (password) {
b9a53a
+                password_escaped = specifier_escape(password);
b9a53a
+                if (!password_escaped)
b9a53a
+                        return log_oom();
b9a53a
+        }
b9a53a
+
b9a53a
         if (keydev) {
b9a53a
                 _cleanup_free_ char *unit = NULL, *p = NULL;
b9a53a
 
b9a53a
-                r = generate_keydev_mount(name, keydev, &unit, &keydev_mount);
b9a53a
+                r = generate_keydev_mount(name, keydev, keyfile_timeout_value, keyfile_can_timeout > 0, &unit, &keydev_mount);
b9a53a
                 if (r < 0)
b9a53a
                         return log_error_errno(r, "Failed to generate keydev mount unit: %m");
b9a53a
 
b9a53a
@@ -190,6 +242,12 @@ static int create_disk(
b9a53a
                         return log_oom();
b9a53a
 
b9a53a
                 free_and_replace(password_escaped, p);
b9a53a
+
b9a53a
+                fprintf(f, "After=%s\n", unit);
b9a53a
+                if (keyfile_can_timeout > 0)
b9a53a
+                        fprintf(f, "Wants=%s\n", unit);
b9a53a
+                else
b9a53a
+                        fprintf(f, "Requires=%s\n", unit);
b9a53a
         }
b9a53a
 
b9a53a
         if (!nofail)
b9a53a
@@ -197,7 +255,7 @@ static int create_disk(
b9a53a
                         "Before=%s\n",
b9a53a
                         netdev ? "remote-cryptsetup.target" : "cryptsetup.target");
b9a53a
 
b9a53a
-        if (password) {
b9a53a
+        if (password && !keydev) {
b9a53a
                 if (STR_IN_SET(password, "/dev/urandom", "/dev/random", "/dev/hw_random"))
b9a53a
                         fputs("After=systemd-random-seed.service\n", f);
b9a53a
                 else if (!STR_IN_SET(password, "-", "none")) {
b9a53a
@@ -271,7 +329,7 @@ static int create_disk(
b9a53a
 
b9a53a
         if (keydev)
b9a53a
                 fprintf(f,
b9a53a
-                        "ExecStartPost=" UMOUNT_PATH " %s\n\n",
b9a53a
+                        "ExecStartPost=-" UMOUNT_PATH " %s\n\n",
b9a53a
                         keydev_mount);
b9a53a
 
b9a53a
         r = fflush_and_check(f);
b9a53a
@@ -394,7 +452,6 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
b9a53a
         } else if (streq(key, "luks.key")) {
b9a53a
                 size_t n;
b9a53a
                 _cleanup_free_ char *keyfile = NULL, *keydev = NULL;
b9a53a
-                char *c;
b9a53a
                 const char *keyspec;
b9a53a
 
b9a53a
                 if (proc_cmdline_value_missing(key, value))
b9a53a
@@ -421,23 +478,13 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
b9a53a
                         return log_oom();
b9a53a
 
b9a53a
                 keyspec = value + n + 1;
b9a53a
-                c = strrchr(keyspec, ':');
b9a53a
-                if (c) {
b9a53a
-                         *c = '\0';
b9a53a
-                        keyfile = strdup(keyspec);
b9a53a
-                        keydev = strdup(c + 1);
b9a53a
-
b9a53a
-                        if (!keyfile || !keydev)
b9a53a
-                                return log_oom();
b9a53a
-                } else {
b9a53a
-                        /* No keydev specified */
b9a53a
-                        keyfile = strdup(keyspec);
b9a53a
-                        if (!keyfile)
b9a53a
-                                return log_oom();
b9a53a
-                }
b9a53a
+                r = split_keyspec(keyspec, &keyfile, &keydev);
b9a53a
+                if (r < 0)
b9a53a
+                        return r;
b9a53a
 
b9a53a
                 free_and_replace(d->keyfile, keyfile);
b9a53a
                 free_and_replace(d->keydev, keydev);
b9a53a
+
b9a53a
         } else if (streq(key, "luks.name")) {
b9a53a
 
b9a53a
                 if (proc_cmdline_value_missing(key, value))
b9a53a
@@ -485,7 +532,7 @@ static int add_crypttab_devices(void) {
b9a53a
                 int r, k;
b9a53a
                 char line[LINE_MAX], *l, *uuid;
b9a53a
                 crypto_device *d = NULL;
b9a53a
-                _cleanup_free_ char *name = NULL, *device = NULL, *keyfile = NULL, *options = NULL;
b9a53a
+                _cleanup_free_ char *name = NULL, *device = NULL, *keydev = NULL, *keyfile = NULL, *keyspec = NULL, *options = NULL;
b9a53a
 
b9a53a
                 if (!fgets(line, sizeof(line), f))
b9a53a
                         break;
b9a53a
@@ -496,7 +543,7 @@ static int add_crypttab_devices(void) {
b9a53a
                 if (IN_SET(*l, 0, '#'))
b9a53a
                         continue;
b9a53a
 
b9a53a
-                k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &keyfile, &options);
b9a53a
+                k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &keyspec, &options);
b9a53a
                 if (k < 2 || k > 4) {
b9a53a
                         log_error("Failed to parse /etc/crypttab:%u, ignoring.", crypttab_line);
b9a53a
                         continue;
b9a53a
@@ -515,7 +562,11 @@ static int add_crypttab_devices(void) {
b9a53a
                         continue;
b9a53a
                 }
b9a53a
 
b9a53a
-                r = create_disk(name, device, NULL, keyfile, (d && d->options) ? d->options : options);
b9a53a
+                r = split_keyspec(keyspec, &keyfile, &keydev);
b9a53a
+                if (r < 0)
b9a53a
+                        return r;
b9a53a
+
b9a53a
+                r = create_disk(name, device, keyfile, keydev, (d && d->options) ? d->options : options);
b9a53a
                 if (r < 0)
b9a53a
                         return r;
b9a53a
 
b9a53a
@@ -555,7 +606,7 @@ static int add_proc_cmdline_devices(void) {
b9a53a
                 else
b9a53a
                         options = "timeout=0";
b9a53a
 
b9a53a
-                r = create_disk(d->name, device, d->keydev, d->keyfile ?: arg_default_keyfile, options);
b9a53a
+                r = create_disk(d->name, device, d->keyfile ?: arg_default_keyfile, d->keydev, options);
b9a53a
                 if (r < 0)
b9a53a
                         return r;
b9a53a
         }
b9a53a
diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c
b9a53a
index 9071126c2e..0881aea915 100644
b9a53a
--- a/src/cryptsetup/cryptsetup.c
b9a53a
+++ b/src/cryptsetup/cryptsetup.c
b9a53a
@@ -69,7 +69,10 @@ static int parse_one_option(const char *option) {
b9a53a
         assert(option);
b9a53a
 
b9a53a
         /* Handled outside of this tool */
b9a53a
-        if (STR_IN_SET(option, "noauto", "auto", "nofail", "fail", "_netdev"))
b9a53a
+        if (STR_IN_SET(option, "noauto", "auto", "nofail", "fail", "_netdev", "keyfile-timeout"))
b9a53a
+                return 0;
b9a53a
+
b9a53a
+        if (startswith(option, "keyfile-timeout="))
b9a53a
                 return 0;
b9a53a
 
b9a53a
         if ((val = startswith(option, "cipher="))) {