Kernel: Prevent reference to unaligned u32 in MBRPartitionTable init

This is technically UB, so triggers KUBSAN.
This commit is contained in:
Idan Horowitz 2024-05-18 21:16:59 +03:00 committed by Andreas Kling
parent b4cdd6a55c
commit 95b9d77536
Notes: sideshowbarker 2024-07-17 18:38:54 +09:00

View File

@ -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;
}