daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
923a60
From 063a7a0071a0e6b01426088c8003e24e160937d1 Mon Sep 17 00:00:00 2001
923a60
From: Dimitri John Ledkov <xnox@ubuntu.com>
923a60
Date: Wed, 29 Aug 2018 15:38:09 +0100
923a60
Subject: [PATCH] cryptsetup: add support for sector-size= option (#9936)
923a60
923a60
Bug-Ubuntu: https://launchpad.net/bugs/1776626
923a60
923a60
Closes #8881.
923a60
923a60
(cherry picked from commit a9fc640671ef60ac949f1ace6fa687ff242fc233)
923a60
923a60
Resolves: #1571801
923a60
---
923a60
 configure.ac                |  6 ++++++
923a60
 man/crypttab.xml            |  9 +++++++++
923a60
 src/cryptsetup/cryptsetup.c | 35 ++++++++++++++++++++++++++++++++++-
923a60
 3 files changed, 49 insertions(+), 1 deletion(-)
923a60
923a60
diff --git a/configure.ac b/configure.ac
923a60
index ee147e28eb..19d42602c9 100644
923a60
--- a/configure.ac
923a60
+++ b/configure.ac
923a60
@@ -832,6 +832,12 @@ if test "x$enable_libcryptsetup" != "xno"; then
923a60
         if test "x$have_libcryptsetup" = xno -a "x$enable_libcryptsetup" = xyes; then
923a60
                 AC_MSG_ERROR([*** libcryptsetup support requested but libraries not found])
923a60
         fi
923a60
+        AC_CHECK_MEMBER(
923a60
+                [struct crypt_params_plain.sector_size],
923a60
+                [AC_DEFINE([HAVE_LIBCRYPTSETUP_SECTOR_SIZE], [1], [Define if libcryptsetup supports sector_size param])],
923a60
+                [],
923a60
+                [#include <libcryptsetup.h>]
923a60
+        )
923a60
 fi
923a60
 AM_CONDITIONAL(HAVE_LIBCRYPTSETUP, [test "$have_libcryptsetup" = "yes"])
923a60
 
923a60
diff --git a/man/crypttab.xml b/man/crypttab.xml
923a60
index e4ecab3dcb..df75007072 100644
923a60
--- a/man/crypttab.xml
923a60
+++ b/man/crypttab.xml
923a60
@@ -248,6 +248,15 @@
923a60
         option.</para></listitem>
923a60
       </varlistentry>
923a60
 
923a60
+      <varlistentry>
923a60
+        <term><option>sector-size=</option></term>
923a60
+
923a60
+        <listitem><para>Specifies the sector size in bytes. See
923a60
+        <citerefentry project='die-net'><refentrytitle>cryptsetup</refentrytitle><manvolnum>8</manvolnum></citerefentry>
923a60
+        for possible values and the default value of this
923a60
+        option.</para></listitem>
923a60
+      </varlistentry>
923a60
+
923a60
       <varlistentry>
923a60
         <term><option>swap</option></term>
923a60
 
923a60
diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c
923a60
index 528c36c48b..fd8b9ba9ec 100644
923a60
--- a/src/cryptsetup/cryptsetup.c
923a60
+++ b/src/cryptsetup/cryptsetup.c
923a60
@@ -43,10 +43,14 @@
923a60
 
923a60
 /* internal helper */
923a60
 #define ANY_LUKS "LUKS"
923a60
+/* as in src/cryptsetup.h */
923a60
+#define CRYPT_SECTOR_SIZE 512
923a60
+#define CRYPT_MAX_SECTOR_SIZE 4096
923a60
 
923a60
 static const char *arg_type = NULL; /* ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT or CRYPT_PLAIN */
923a60
 static char *arg_cipher = NULL;
923a60
 static unsigned arg_key_size = 0;
923a60
+static unsigned arg_sector_size = CRYPT_SECTOR_SIZE;
923a60
 static int arg_key_slot = CRYPT_ANY_SLOT;
923a60
 static unsigned arg_keyfile_size = 0;
923a60
 static unsigned arg_keyfile_offset = 0;
923a60
@@ -104,6 +108,31 @@ static int parse_one_option(const char *option) {
923a60
 
923a60
                 arg_key_size /= 8;
923a60
 
923a60
+        } else if (startswith(option, "sector-size=")) {
923a60
+
923a60
+#if HAVE_LIBCRYPTSETUP_SECTOR_SIZE
923a60
+                int r;
923a60
+
923a60
+                r = safe_atou(option+12, &arg_sector_size);
923a60
+                if (r < 0) {
923a60
+                        log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
923a60
+                        return 0;
923a60
+                }
923a60
+
923a60
+                if (arg_sector_size % 2) {
923a60
+                        log_error("sector-size= not a multiple of 2, ignoring.");
923a60
+                        return 0;
923a60
+                }
923a60
+
923a60
+                if (arg_sector_size < CRYPT_SECTOR_SIZE || arg_sector_size > CRYPT_MAX_SECTOR_SIZE) {
923a60
+                        log_error("sector-size= is outside of %u and %u, ignoring.", CRYPT_SECTOR_SIZE, CRYPT_MAX_SECTOR_SIZE);
923a60
+                        return 0;
923a60
+                }
923a60
+#else
923a60
+                log_error("sector-size= is not supported, compiled with old libcryptsetup.");
923a60
+                return 0;
923a60
+#endif
923a60
+
923a60
         } else if (startswith(option, "key-slot=")) {
923a60
 
923a60
                 arg_type = ANY_LUKS;
923a60
@@ -450,7 +479,11 @@ static int attach_luks_or_plain(struct crypt_device *cd,
923a60
         }
923a60
 
923a60
         if ((!arg_type && r < 0) || streq_ptr(arg_type, CRYPT_PLAIN)) {
923a60
-                struct crypt_params_plain params = {};
923a60
+                struct crypt_params_plain params = {
923a60
+#if HAVE_LIBCRYPTSETUP_SECTOR_SIZE
923a60
+                        .sector_size = arg_sector_size,
923a60
+#endif
923a60
+                };
923a60
                 const char *cipher, *cipher_mode;
923a60
                 _cleanup_free_ char *truncated_cipher = NULL;
923a60