ff6046
From a046230cfb7e02938e3ad2ac85515636b319651e Mon Sep 17 00:00:00 2001
ff6046
From: Dimitri John Ledkov <xnox@ubuntu.com>
ff6046
Date: Wed, 29 Aug 2018 15:38:09 +0100
ff6046
Subject: [PATCH] cryptsetup: add support for sector-size= option (#9936)
ff6046
ff6046
Bug-Ubuntu: https://launchpad.net/bugs/1776626
ff6046
ff6046
Closes #8881.
ff6046
ff6046
(cherry picked from commit a9fc640671ef60ac949f1ace6fa687ff242fc233)
ff6046
ff6046
Resolves: #1572563
ff6046
---
ff6046
 man/crypttab.xml            |  9 +++++++++
ff6046
 meson.build                 |  6 ++++++
ff6046
 src/cryptsetup/cryptsetup.c | 30 ++++++++++++++++++++++++++++++
ff6046
 3 files changed, 45 insertions(+)
ff6046
ff6046
diff --git a/man/crypttab.xml b/man/crypttab.xml
ff6046
index dcaf03d2ca..3574ce00da 100644
ff6046
--- a/man/crypttab.xml
ff6046
+++ b/man/crypttab.xml
ff6046
@@ -250,6 +250,15 @@
ff6046
         option.</para></listitem>
ff6046
       </varlistentry>
ff6046
 
ff6046
+      <varlistentry>
ff6046
+        <term><option>sector-size=</option></term>
ff6046
+
ff6046
+        <listitem><para>Specifies the sector size in bytes. See
ff6046
+        <citerefentry project='die-net'><refentrytitle>cryptsetup</refentrytitle><manvolnum>8</manvolnum></citerefentry>
ff6046
+        for possible values and the default value of this
ff6046
+        option.</para></listitem>
ff6046
+      </varlistentry>
ff6046
+
ff6046
       <varlistentry>
ff6046
         <term><option>swap</option></term>
ff6046
 
ff6046
diff --git a/meson.build b/meson.build
ff6046
index a0e7240708..f308db2631 100644
ff6046
--- a/meson.build
ff6046
+++ b/meson.build
ff6046
@@ -927,11 +927,17 @@ if want_libcryptsetup != 'false' and not fuzzer_build
ff6046
                                    version : '>= 1.6.0',
ff6046
                                    required : want_libcryptsetup == 'true')
ff6046
         have = libcryptsetup.found()
ff6046
+        have_sector = cc.has_member(
ff6046
+                    'struct crypt_params_plain',
ff6046
+                    'sector_size',
ff6046
+                    prefix : '#include <libcryptsetup.h>')
ff6046
 else
ff6046
         have = false
ff6046
+        have_sector = false
ff6046
         libcryptsetup = []
ff6046
 endif
ff6046
 conf.set10('HAVE_LIBCRYPTSETUP', have)
ff6046
+conf.set10('HAVE_LIBCRYPTSETUP_SECTOR_SIZE', have_sector)
ff6046
 
ff6046
 want_libcurl = get_option('libcurl')
ff6046
 if want_libcurl != 'false' and not fuzzer_build
ff6046
diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c
ff6046
index 832168184a..87008cb969 100644
ff6046
--- a/src/cryptsetup/cryptsetup.c
ff6046
+++ b/src/cryptsetup/cryptsetup.c
ff6046
@@ -23,10 +23,14 @@
ff6046
 
ff6046
 /* internal helper */
ff6046
 #define ANY_LUKS "LUKS"
ff6046
+/* as in src/cryptsetup.h */
ff6046
+#define CRYPT_SECTOR_SIZE 512
ff6046
+#define CRYPT_MAX_SECTOR_SIZE 4096
ff6046
 
ff6046
 static const char *arg_type = NULL; /* ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT or CRYPT_PLAIN */
ff6046
 static char *arg_cipher = NULL;
ff6046
 static unsigned arg_key_size = 0;
ff6046
+static unsigned arg_sector_size = CRYPT_SECTOR_SIZE;
ff6046
 static int arg_key_slot = CRYPT_ANY_SLOT;
ff6046
 static unsigned arg_keyfile_size = 0;
ff6046
 static uint64_t arg_keyfile_offset = 0;
ff6046
@@ -86,6 +90,29 @@ static int parse_one_option(const char *option) {
ff6046
 
ff6046
                 arg_key_size /= 8;
ff6046
 
ff6046
+        } else if ((val = startswith(option, "sector-size="))) {
ff6046
+
ff6046
+#if HAVE_LIBCRYPTSETUP_SECTOR_SIZE
ff6046
+                r = safe_atou(val, &arg_sector_size);
ff6046
+                if (r < 0) {
ff6046
+                        log_error_errno(r, "Failed to parse %s, ignoring: %m", option);
ff6046
+                        return 0;
ff6046
+                }
ff6046
+
ff6046
+                if (arg_sector_size % 2) {
ff6046
+                        log_error("sector-size= not a multiple of 2, ignoring.");
ff6046
+                        return 0;
ff6046
+                }
ff6046
+
ff6046
+                if (arg_sector_size < CRYPT_SECTOR_SIZE || arg_sector_size > CRYPT_MAX_SECTOR_SIZE) {
ff6046
+                        log_error("sector-size= is outside of %u and %u, ignoring.", CRYPT_SECTOR_SIZE, CRYPT_MAX_SECTOR_SIZE);
ff6046
+                        return 0;
ff6046
+                }
ff6046
+#else
ff6046
+                log_error("sector-size= is not supported, compiled with old libcryptsetup.");
ff6046
+                return 0;
ff6046
+#endif
ff6046
+
ff6046
         } else if ((val = startswith(option, "key-slot="))) {
ff6046
 
ff6046
                 arg_type = ANY_LUKS;
ff6046
@@ -471,6 +498,9 @@ static int attach_luks_or_plain(struct crypt_device *cd,
ff6046
                 struct crypt_params_plain params = {
ff6046
                         .offset = arg_offset,
ff6046
                         .skip = arg_skip,
ff6046
+#if HAVE_LIBCRYPTSETUP_SECTOR_SIZE
ff6046
+                        .sector_size = arg_sector_size,
ff6046
+#endif
ff6046
                 };
ff6046
                 const char *cipher, *cipher_mode;
ff6046
                 _cleanup_free_ char *truncated_cipher = NULL;