Skip to content

Commit d5528d5

Browse files
nmerinovIngo Molnar
authored andcommitted
partitions/efi: Fix partition name parsing in GUID partition entry
GUID partition entry defined to have a partition name as 36 UTF-16LE code units. This means that on big-endian platforms ASCII symbols would be read with 0xXX00 efi_char16_t character code. In order to correctly extract ASCII characters from a partition name field we should be converted from 16LE to CPU architecture. The problem exists on all big endian platforms. [ mingo: Minor edits. ] Fixes: eec7ecf ("genhd, efi: add efi partition metadata to hd_structs") Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Nikolai Merinov <[email protected]> Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Link: https://lore.kernel.org/r/[email protected] Link: https://lore.kernel.org/r/797777312.1324734.1582544319435.JavaMail.zimbra@inango-systems.com/
1 parent 0347d8c commit d5528d5

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

block/partitions/efi.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,31 @@ static int find_valid_gpt(struct parsed_partitions *state, gpt_header **gpt,
656656
return 0;
657657
}
658658

659+
/**
660+
* utf16_le_to_7bit(): Naively converts a UTF-16LE string to 7-bit ASCII characters
661+
* @in: input UTF-16LE string
662+
* @size: size of the input string
663+
* @out: output string ptr, should be capable to store @size+1 characters
664+
*
665+
* Description: Converts @size UTF16-LE symbols from @in string to 7-bit
666+
* ASCII characters and stores them to @out. Adds trailing zero to @out array.
667+
*/
668+
static void utf16_le_to_7bit(const __le16 *in, unsigned int size, u8 *out)
669+
{
670+
unsigned int i = 0;
671+
672+
out[size] = 0;
673+
674+
while (i < size) {
675+
u8 c = le16_to_cpu(in[i]) & 0xff;
676+
677+
if (c && !isprint(c))
678+
c = '!';
679+
out[i] = c;
680+
i++;
681+
}
682+
}
683+
659684
/**
660685
* efi_partition(struct parsed_partitions *state)
661686
* @state: disk parsed partitions
@@ -692,7 +717,6 @@ int efi_partition(struct parsed_partitions *state)
692717

693718
for (i = 0; i < le32_to_cpu(gpt->num_partition_entries) && i < state->limit-1; i++) {
694719
struct partition_meta_info *info;
695-
unsigned label_count = 0;
696720
unsigned label_max;
697721
u64 start = le64_to_cpu(ptes[i].starting_lba);
698722
u64 size = le64_to_cpu(ptes[i].ending_lba) -
@@ -713,14 +737,7 @@ int efi_partition(struct parsed_partitions *state)
713737
/* Naively convert UTF16-LE to 7 bits. */
714738
label_max = min(ARRAY_SIZE(info->volname) - 1,
715739
ARRAY_SIZE(ptes[i].partition_name));
716-
info->volname[label_max] = 0;
717-
while (label_count < label_max) {
718-
u8 c = ptes[i].partition_name[label_count] & 0xff;
719-
if (c && !isprint(c))
720-
c = '!';
721-
info->volname[label_count] = c;
722-
label_count++;
723-
}
740+
utf16_le_to_7bit(ptes[i].partition_name, label_max, info->volname);
724741
state->parts[i + 1].has_info = true;
725742
}
726743
kfree(ptes);

block/partitions/efi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ typedef struct _gpt_entry {
8888
__le64 starting_lba;
8989
__le64 ending_lba;
9090
gpt_entry_attributes attributes;
91-
efi_char16_t partition_name[72 / sizeof (efi_char16_t)];
91+
__le16 partition_name[72/sizeof(__le16)];
9292
} __packed gpt_entry;
9393

9494
typedef struct _gpt_mbr_record {

0 commit comments

Comments
 (0)