ryantimwilson / rpms / systemd

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