ff6046
From 2a4d58bb2ab9ba5487785cc167932440a4f0c13d Mon Sep 17 00:00:00 2001
ff6046
From: Michal Sekletar <msekleta@redhat.com>
ff6046
Date: Tue, 4 Sep 2018 20:03:34 +0200
ff6046
Subject: [PATCH] cryptsetup-generator: allow whitespace characters in keydev
ff6046
 specification
ff6046
ff6046
For example, <luks.uuid>=/keyfile:LABEL="KEYFILE FS" previously wouldn't
ff6046
work, because we truncated label at the first whitespace character,
ff6046
i.e. LABEL="KEYFILE".
ff6046
ff6046
(cherry-picked from commit 7949dfa73a44ae6524779689483d12243dfbcfdf)
ff6046
ff6046
Related: #1656869
ff6046
---
ff6046
 src/cryptsetup/cryptsetup-generator.c | 64 ++++++++++++++++++---------
ff6046
 1 file changed, 43 insertions(+), 21 deletions(-)
ff6046
ff6046
diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c
ff6046
index 03c513c26e..52c1262728 100644
ff6046
--- a/src/cryptsetup/cryptsetup-generator.c
ff6046
+++ b/src/cryptsetup/cryptsetup-generator.c
ff6046
@@ -5,11 +5,13 @@
ff6046
 
ff6046
 #include "alloc-util.h"
ff6046
 #include "dropin.h"
ff6046
+#include "escape.h"
ff6046
 #include "fd-util.h"
ff6046
 #include "fileio.h"
ff6046
 #include "fstab-util.h"
ff6046
 #include "generator.h"
ff6046
 #include "hashmap.h"
ff6046
+#include "id128-util.h"
ff6046
 #include "log.h"
ff6046
 #include "mkdir.h"
ff6046
 #include "parse-util.h"
ff6046
@@ -39,7 +41,7 @@ static char *arg_default_options = NULL;
ff6046
 static char *arg_default_keyfile = NULL;
ff6046
 
ff6046
 static int generate_keydev_mount(const char *name, const char *keydev, char **unit, char **mount) {
ff6046
-        _cleanup_free_ char *u = NULL, *what = NULL, *where = NULL;
ff6046
+        _cleanup_free_ char *u = NULL, *what = NULL, *where = NULL, *name_escaped = NULL;
ff6046
         _cleanup_fclose_ FILE *f = NULL;
ff6046
         int r;
ff6046
 
ff6046
@@ -56,7 +58,11 @@ static int generate_keydev_mount(const char *name, const char *keydev, char **un
ff6046
         if (r < 0 && errno != EEXIST)
ff6046
                 return -errno;
ff6046
 
ff6046
-        where = strjoin("/run/systemd/cryptsetup/keydev-", name);
ff6046
+        name_escaped = cescape(name);
ff6046
+        if (!name_escaped)
ff6046
+                return -ENOMEM;
ff6046
+
ff6046
+        where = strjoin("/run/systemd/cryptsetup/keydev-", name_escaped);
ff6046
         if (!where)
ff6046
                 return -ENOMEM;
ff6046
 
ff6046
@@ -386,36 +392,52 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
ff6046
                         return log_oom();
ff6046
 
ff6046
         } else if (streq(key, "luks.key")) {
ff6046
+                size_t n;
ff6046
+                _cleanup_free_ char *keyfile = NULL, *keydev = NULL;
ff6046
+                char *c;
ff6046
+                const char *keyspec;
ff6046
 
ff6046
                 if (proc_cmdline_value_missing(key, value))
ff6046
                         return 0;
ff6046
 
ff6046
-                r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value);
ff6046
-                if (r == 2) {
ff6046
-                        char *c;
ff6046
-                        _cleanup_free_ char *keyfile = NULL, *keydev = NULL;
ff6046
+                n = strspn(value, LETTERS DIGITS "-");
ff6046
+                if (value[n] != '=') {
ff6046
+                        if (free_and_strdup(&arg_default_keyfile, value) < 0)
ff6046
+                                 return log_oom();
ff6046
+                        return 0;
ff6046
+                }
ff6046
 
ff6046
-                        d = get_crypto_device(uuid);
ff6046
-                        if (!d)
ff6046
-                                return log_oom();
ff6046
+                uuid = strndup(value, n);
ff6046
+                if (!uuid)
ff6046
+                        return log_oom();
ff6046
 
ff6046
-                        c = strrchr(uuid_value, ':');
ff6046
-                        if (!c)
ff6046
-                                /* No keydev specified */
ff6046
-                                return free_and_replace(d->keyfile, uuid_value);
ff6046
+                if (!id128_is_valid(uuid)) {
ff6046
+                        log_warning("Failed to parse luks.key= kernel command line switch. UUID is invalid, ignoring.");
ff6046
+                        return 0;
ff6046
+                }
ff6046
+
ff6046
+                d = get_crypto_device(uuid);
ff6046
+                if (!d)
ff6046
+                        return log_oom();
ff6046
 
ff6046
-                        *c = '\0';
ff6046
-                        keyfile = strdup(uuid_value);
ff6046
-                        keydev = strdup(++c);
ff6046
+                keyspec = value + n + 1;
ff6046
+                c = strrchr(keyspec, ':');
ff6046
+                if (c) {
ff6046
+                         *c = '\0';
ff6046
+                        keyfile = strdup(keyspec);
ff6046
+                        keydev = strdup(c + 1);
ff6046
 
ff6046
                         if (!keyfile || !keydev)
ff6046
                                 return log_oom();
ff6046
+                } else {
ff6046
+                        /* No keydev specified */
ff6046
+                        keyfile = strdup(keyspec);
ff6046
+                        if (!keyfile)
ff6046
+                                return log_oom();
ff6046
+                }
ff6046
 
ff6046
-                        free_and_replace(d->keyfile, keyfile);
ff6046
-                        free_and_replace(d->keydev, keydev);
ff6046
-                } else if (free_and_strdup(&arg_default_keyfile, value) < 0)
ff6046
-                        return log_oom();
ff6046
-
ff6046
+                free_and_replace(d->keyfile, keyfile);
ff6046
+                free_and_replace(d->keydev, keydev);
ff6046
         } else if (streq(key, "luks.name")) {
ff6046
 
ff6046
                 if (proc_cmdline_value_missing(key, value))