diff --git a/kernel/kernel/DeviceManager.cpp b/kernel/kernel/DeviceManager.cpp index 451a79d4d1..422f78e4c3 100644 --- a/kernel/kernel/DeviceManager.cpp +++ b/kernel/kernel/DeviceManager.cpp @@ -43,7 +43,7 @@ namespace Kernel { s_instance->add_device(device); if (auto res = device->initialize_partitions(); res.is_error()) - dprintln("{}: {}", device->name(), res.error()); + dprintln("{}", res.error()); else { for (auto* partition : device->partitions()) diff --git a/kernel/kernel/Storage/StorageDevice.cpp b/kernel/kernel/Storage/StorageDevice.cpp index ef86120cd1..e7dde8cd79 100644 --- a/kernel/kernel/Storage/StorageDevice.cpp +++ b/kernel/kernel/Storage/StorageDevice.cpp @@ -146,23 +146,29 @@ namespace Kernel BAN::ErrorOr StorageDevice::initialize_partitions() { + if (total_size() < sizeof(GPTHeader)) + return BAN::Error::from_format("Disk {} does not have enough space for GPT header", name()); + BAN::Vector lba1(sector_size()); TRY(read_sectors(1, 1, lba1.data())); const GPTHeader& header = *(const GPTHeader*)lba1.data(); if (!is_valid_gpt_header(header, sector_size())) - return BAN::Error::from_c_string("Invalid GPT header"); + return BAN::Error::from_format("Disk {} has invalid GPT header", name()); uint32_t size = header.partition_entry_count * header.partition_entry_size; if (uint32_t remainder = size % sector_size()) size += sector_size() - remainder; + if (total_size() < header.partition_entry_lba * sector_size() + size) + return BAN::Error::from_format("Disk {} has invalid GPT header", name()); + BAN::Vector entry_array; TRY(entry_array.resize(size)); TRY(read_sectors(header.partition_entry_lba, size / sector_size(), entry_array.data())); if (!is_valid_gpt_crc32(header, lba1, entry_array)) - return BAN::Error::from_c_string("Invalid crc3 in the GPT header"); + return BAN::Error::from_format("Disk {} has invalid crc3 in the GPT header", name()); for (uint32_t i = 0; i < header.partition_entry_count; i++) {