Skip to content

Commit 0eaaa45

Browse files
committed
Fix handling of too long label and subsystem fields
These LUKS2 labels are stored in the binary header area that has limited size. While we have been silently truncating strings here, it is something that is not expected, as the final label is then different than expected. Let's fix the code to explicitly print and return error here. Also remove the comment about duplicate check. It is incorrect optimization, as some users will expect a real write on disk, we should no skip it. Fixes: #958
1 parent 3a8feb8 commit 0eaaa45

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

lib/luks2/luks2_json_metadata.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,11 @@ int LUKS2_hdr_uuid(struct crypt_device *cd, struct luks2_hdr *hdr, const char *u
12721272
int LUKS2_hdr_labels(struct crypt_device *cd, struct luks2_hdr *hdr,
12731273
const char *label, const char *subsystem, int commit)
12741274
{
1275-
//FIXME: check if the labels are the same and skip this.
1275+
if ((label && strlen(label) >= LUKS2_LABEL_L) ||
1276+
(subsystem && strlen(subsystem) >= LUKS2_LABEL_L)) {
1277+
log_err(cd, _("Label is too long."));
1278+
return -EINVAL;
1279+
}
12761280

12771281
memset(hdr->label, 0, LUKS2_LABEL_L);
12781282
if (label)

tests/api-test-2.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4059,6 +4059,7 @@ static void Luks2Refresh(void)
40594059
static void Luks2Flags(void)
40604060
{
40614061
uint32_t flags = 42;
4062+
const char *longlabel = "0123456789abcedf0123456789abcedf0123456789abcedf";
40624063

40634064
OK_(crypt_init(&cd, DEVICE_1));
40644065
OK_(crypt_load(cd, CRYPT_LUKS2, NULL));
@@ -4089,6 +4090,9 @@ static void Luks2Flags(void)
40894090
OK_(strcmp("", crypt_get_label(cd)));
40904091
OK_(strcmp("", crypt_get_subsystem(cd)));
40914092

4093+
FAIL_(crypt_set_label(cd, longlabel, NULL), "long label");
4094+
FAIL_(crypt_set_label(cd, NULL, longlabel), "long subsystem");
4095+
40924096
CRYPT_FREE(cd);
40934097
}
40944098

tests/compat-test2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,8 @@ $CRYPTSETUP luksDump $LOOPDEV | grep "Label:" | grep -q "(no label)" || fail
12781278
$CRYPTSETUP config $LOOPDEV --subsystem SatelliteThree --label TheLabel
12791279
$CRYPTSETUP luksDump $LOOPDEV | grep "Subsystem:" | grep -q "SatelliteThree" || fail
12801280
$CRYPTSETUP luksDump $LOOPDEV | grep "Label:" | grep -q "TheLabel" || fail
1281+
$CRYPTSETUP config $LOOPDEV --label 0123456789abcdef0123456789abcdef0123456789abcdef 2>/dev/null && fail
1282+
$CRYPTSETUP config $LOOPDEV --subsystem 0123456789abcdef0123456789abcdef0123456789abcdef 2>/dev/null && fail
12811283

12821284
prepare "[36] LUKS PBKDF setting" wipe
12831285
echo $PWD1 | $CRYPTSETUP luksFormat --type luks2 --pbkdf bla $LOOPDEV >/dev/null 2>&1 && fail

0 commit comments

Comments
 (0)