From 95b9d775360f5b3cb973ee185ccedea6089d350e Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Sat, 18 May 2024 21:16:59 +0300 Subject: [PATCH] Kernel: Prevent reference to unaligned u32 in MBRPartitionTable init This is technically UB, so triggers KUBSAN. --- .../Libraries/LibPartition/MBRPartitionTable.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibPartition/MBRPartitionTable.cpp b/Userland/Libraries/LibPartition/MBRPartitionTable.cpp index 47331d4d8e8..bcb1ec2f080 100644 --- a/Userland/Libraries/LibPartition/MBRPartitionTable.cpp +++ b/Userland/Libraries/LibPartition/MBRPartitionTable.cpp @@ -59,7 +59,11 @@ MBRPartitionTable::MBRPartitionTable(PartitionableDevice device, u32 start_lba) if (entry.offset == 0x00) { continue; } - MUST(m_partitions.try_empend(entry.offset, (entry.offset + entry.length) - 1, entry.type)); + // We have to place these in stack variables, since try_empend will try to take a reference to them, which is UB (since they're gnu::packed and unaligned) + u64 const block_offset = entry.offset; + u64 const block_limit = (entry.offset + entry.length) - 1; + u8 const partition_type = entry.type; + MUST(m_partitions.try_empend(block_offset, block_limit, partition_type)); } m_valid = true; } @@ -78,7 +82,11 @@ MBRPartitionTable::MBRPartitionTable(PartitionableDevice device) if (entry.offset == 0x00) { continue; } - MUST(m_partitions.try_empend(entry.offset, (entry.offset + entry.length) - 1, entry.type)); + // We have to place these in stack variables, since try_empend will try to take a reference to them, which is UB (since they're gnu::packed and unaligned) + u64 const block_offset = entry.offset; + u64 const block_limit = (entry.offset + entry.length) - 1; + u8 const partition_type = entry.type; + MUST(m_partitions.try_empend(block_offset, block_limit, partition_type)); } m_valid = true; }