c2dfb7
From 9040e15cd3cba546b47aeae0ea133afa1a6ad292 Mon Sep 17 00:00:00 2001
c2dfb7
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
c2dfb7
Date: Wed, 13 Nov 2019 10:32:30 +0100
c2dfb7
Subject: [PATCH] cryptsetup-generator: guess whether the keyfile argument is
c2dfb7
 two items or one
c2dfb7
c2dfb7
Fixes #13615.
c2dfb7
c2dfb7
See the inline comment for documentation.
c2dfb7
c2dfb7
(cherry picked from commit 32c6237a7c2e697d2fc4f3403319db16858fb8e3)
c2dfb7
c2dfb7
Related: #1763155
c2dfb7
---
c2dfb7
 src/cryptsetup/cryptsetup-generator.c | 45 ++++++++++++++++++---------
c2dfb7
 1 file changed, 30 insertions(+), 15 deletions(-)
c2dfb7
c2dfb7
diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c
c2dfb7
index a09983b576..4117930925 100644
c2dfb7
--- a/src/cryptsetup/cryptsetup-generator.c
c2dfb7
+++ b/src/cryptsetup/cryptsetup-generator.c
c2dfb7
@@ -54,17 +54,36 @@ static int split_keyspec(const char *keyspec, char **ret_keyfile, char **ret_key
c2dfb7
 
c2dfb7
         c = strrchr(keyspec, ':');
c2dfb7
         if (c) {
c2dfb7
-                keyfile = strndup(keyspec, c-keyspec);
c2dfb7
-                keydev = strdup(c + 1);
c2dfb7
-                if (!keyfile || !keydev)
c2dfb7
+                /* The keydev part has to be either an absolute path to device node (/dev/something,
c2dfb7
+                 * /dev/foo/something, or even possibly /dev/foo/something:part), or a fstab device
c2dfb7
+                 * specification starting with LABEL= or similar. The keyfile part has the same syntax.
c2dfb7
+                 *
c2dfb7
+                 * Let's try to guess if the second part looks like a keydev specification, or just part of a
c2dfb7
+                 * filename with a colon. fstab_node_to_udev_node() will convert the fstab device syntax to
c2dfb7
+                 * an absolute path. If we didn't get an absolute path, assume that it is just part of the
c2dfb7
+                 * first keyfile argument. */
c2dfb7
+
c2dfb7
+                keydev = fstab_node_to_udev_node(c + 1);
c2dfb7
+                if (!keydev)
c2dfb7
                         return log_oom();
c2dfb7
-        } else {
c2dfb7
+
c2dfb7
+                if (path_is_absolute(keydev))
c2dfb7
+                        keyfile = strndup(keyspec, c-keyspec);
c2dfb7
+                else {
c2dfb7
+                        log_debug("Keyspec argument contains a colon, but \"%s\" doesn't look like a device specification.\n"
c2dfb7
+                                  "Assuming that \"%s\" is a single device specification.",
c2dfb7
+                                  c + 1, keyspec);
c2dfb7
+                        keydev = mfree(keydev);
c2dfb7
+                        c = NULL;
c2dfb7
+                }
c2dfb7
+        }
c2dfb7
+
c2dfb7
+        if (!c)
c2dfb7
                 /* No keydev specified */
c2dfb7
                 keyfile = strdup(keyspec);
c2dfb7
-                keydev = NULL;
c2dfb7
-                if (!keyfile)
c2dfb7
-                        return log_oom();
c2dfb7
-        }
c2dfb7
+
c2dfb7
+        if (!keyfile)
c2dfb7
+                return log_oom();
c2dfb7
 
c2dfb7
         *ret_keyfile = TAKE_PTR(keyfile);
c2dfb7
         *ret_keydev = TAKE_PTR(keydev);
c2dfb7
@@ -73,7 +92,7 @@ static int split_keyspec(const char *keyspec, char **ret_keyfile, char **ret_key
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_free_ char *u = NULL, *where = NULL, *name_escaped = NULL, *device_unit = NULL;
c2dfb7
         _cleanup_fclose_ FILE *f = NULL;
c2dfb7
         int r;
c2dfb7
         usec_t timeout_us;
c2dfb7
@@ -111,22 +130,18 @@ static int generate_keydev_mount(const char *name, const char *keydev, const cha
c2dfb7
         if (r < 0)
c2dfb7
                 return r;
c2dfb7
 
c2dfb7
-        what = fstab_node_to_udev_node(keydev);
c2dfb7
-        if (!what)
c2dfb7
-                return -ENOMEM;
c2dfb7
-
c2dfb7
         fprintf(f,
c2dfb7
                 "[Unit]\n"
c2dfb7
                 "DefaultDependencies=no\n\n"
c2dfb7
                 "[Mount]\n"
c2dfb7
                 "What=%s\n"
c2dfb7
                 "Where=%s\n"
c2dfb7
-                "Options=ro%s\n", what, where, canfail ? ",nofail" : "");
c2dfb7
+                "Options=ro%s\n", keydev, 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
+                        r = unit_name_from_path(keydev, ".device", &device_unit);
c2dfb7
                         if (r < 0)
c2dfb7
                                 return log_error_errno(r, "Failed to generate unit name: %m");
c2dfb7