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