File cryptsetup-Print-warning-early-if-LUKS-container-is-too-small-for-activation.patch of Package cryptsetup.36961
From c31494abc694ffc24bfdd711a5dd1da1026ae0bb Mon Sep 17 00:00:00 2001
From: Milan Broz <gmazyland@gmail.com>
Date: Thu, 17 Nov 2022 22:31:57 +0100
Subject: [PATCH] Print warning early if LUKS container is too small for
activation.
Activation with header only fails too late (in device-mapper
call) while it is clear from the beginning that it cannot succeed.
Just add an early and better worded error.
Ignore this situation for block device (we have to call ioctl to get size).
The most common case is a file container here anyway.
For block devices it fails during activation later.
---
src/cryptsetup.c | 9 +++++++++
1 file changed, 9 insertions(+)
Index: cryptsetup-2.3.7/src/cryptsetup.c
===================================================================
--- cryptsetup-2.3.7.orig/src/cryptsetup.c
+++ cryptsetup-2.3.7/src/cryptsetup.c
@@ -1526,6 +1526,7 @@ static int action_open_luks(void)
int r, keysize, tries;
char *password = NULL;
size_t passwordLen;
+ struct stat st;
if (opt_refresh) {
activated_name = action_argc > 1 ? action_argv[1] : action_argv[0];
@@ -1552,6 +1553,14 @@ static int action_open_luks(void)
r = -EINVAL;
goto out;
}
+
+ if (activated_name && !stat(crypt_get_device_name(cd), &st) && S_ISREG(st.st_mode) &&
+ crypt_get_data_offset(cd) >= ((uint64_t)st.st_size / SECTOR_SIZE)) {
+ log_err(_("LUKS file container %s is too small for activation, there is no remaining space for data."),
+ crypt_get_device_name(cd));
+ r = -EINVAL;
+ goto out;
+ }
}
_set_activation_flags(&activate_flags);