Skip to content

Commit e59de4e

Browse files
robbat2kawasaki
authored andcommitted
block/partitions: detect LUKS-formatted disks
If an entire device is formatted as LUKS, there is a small chance it maybe be detected as an Atari/AHDI disk - causing the kernel to create partitions, and confusing other systems. Detect the LUKS header before the Atari partition table to prevent this from creating partitions on top of the LUKS volume. Link: https://unix.stackexchange.com/questions/561745/what-are-the-md-partitions-under-a-mdadm-array Link: rook/rook#7940 Link: https://bugs.launchpad.net/ubuntu/+source/util-linux/+bug/1531404 Signed-off-by: Robin H. Johnson <[email protected]>
1 parent aeddbbb commit e59de4e

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

block/partitions/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ config AMIGA_PARTITION
9696
Say Y here if you would like to use hard disks under Linux which
9797
were partitioned under AmigaOS.
9898

99+
config LUKS_PARTITION
100+
bool "LUKS partition support" if PARTITION_ADVANCED
101+
default y if ATARI_PARTITION
102+
help
103+
Say Y here to detect hard disks which have a LUKS header instead of a
104+
partition table. This is valuable as LUKS header may also be wrongly
105+
detected as other partition types.
106+
99107
config ATARI_PARTITION
100108
bool "Atari partition table support" if PARTITION_ADVANCED
101109
default y if ATARI

block/partitions/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ obj-$(CONFIG_AIX_PARTITION) += aix.o
1111
obj-$(CONFIG_CMDLINE_PARTITION) += cmdline.o
1212
obj-$(CONFIG_MAC_PARTITION) += mac.o
1313
obj-$(CONFIG_LDM_PARTITION) += ldm.o
14+
obj-$(CONFIG_LUKS_PARTITION) += luks.o
1415
obj-$(CONFIG_MSDOS_PARTITION) += msdos.o
1516
obj-$(CONFIG_OF_PARTITION) += of.o
1617
obj-$(CONFIG_OSF_PARTITION) += osf.o

block/partitions/check.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ int efi_partition(struct parsed_partitions *state);
6060
int ibm_partition(struct parsed_partitions *);
6161
int karma_partition(struct parsed_partitions *state);
6262
int ldm_partition(struct parsed_partitions *state);
63+
int luks_partition(struct parsed_partitions *state);
6364
int mac_partition(struct parsed_partitions *state);
6465
int msdos_partition(struct parsed_partitions *state);
6566
int of_partition(struct parsed_partitions *state);

block/partitions/core.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ static int (*const check_part[])(struct parsed_partitions *) = {
6767
#ifdef CONFIG_AMIGA_PARTITION
6868
amiga_partition,
6969
#endif
70+
#ifdef CONFIG_LUKS_PARTITION
71+
luks_partition, /* this must come before atari */
72+
#endif
7073
#ifdef CONFIG_ATARI_PARTITION
7174
atari_partition,
7275
#endif

block/partitions/luks.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* fs/partitions/luks.c
4+
* LUKS on raw partition; this is important because a LUKS volume may detected
5+
* as a valid Atari partition table, breaking other detection.
6+
*
7+
* Copyright (C) 2025 Robin H. Johnson ([email protected])
8+
*
9+
* Reference: https://gitlab.com/cryptsetup/LUKS2-docs/blob/master/luks2_doc_wip.pdf
10+
* Page 5, Figure 2: LUKS2 binary header on-disk structure
11+
* This only looks for the Magic & version; and NOT a UUID that starts at
12+
* offset 0xA8.
13+
*/
14+
15+
#include <linux/ctype.h>
16+
#include <linux/compiler.h>
17+
#include "check.h"
18+
19+
#define LUKS_MAGIC_1ST_V1 "LUKS\xba\xbe\x00\x01"
20+
#define LUKS_MAGIC_1ST_V2 "LUKS\xba\xbe\x00\x02"
21+
#define LUKS_MAGIC_2ND_V1 "SKUL\xba\xbe\x00\x01"
22+
#define LUKS_MAGIC_2ND_V2 "SKUL\xba\xbe\x00\x02"
23+
24+
int luks_partition(struct parsed_partitions *state)
25+
{
26+
Sector sect;
27+
int ret = 0;
28+
unsigned char *data;
29+
30+
data = read_part_sector(state, 0, &sect);
31+
32+
if (!data)
33+
return -1;
34+
35+
if (memcmp(data, LUKS_MAGIC_1ST_V1, 8) == 0
36+
|| memcmp(data, LUKS_MAGIC_2ND_V1, 8) == 0) {
37+
strlcat(state->pp_buf, "LUKSv1\n", PAGE_SIZE);
38+
ret = 1;
39+
} else if (memcmp(data, LUKS_MAGIC_1ST_V2, 8) == 0
40+
|| memcmp(data, LUKS_MAGIC_2ND_V2, 8) == 0) {
41+
strlcat(state->pp_buf, "LUKSv2\n", PAGE_SIZE);
42+
ret = 1;
43+
}
44+
put_dev_sector(sect);
45+
return ret;
46+
}

0 commit comments

Comments
 (0)