daandemeyer / rpms / systemd

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