From ebfd0920752d12132301d329a3f94e97bdc227cf Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 3 Apr 2023 09:55:49 +0300 Subject: [PATCH] Kernel: Cleaner partition parsing errors --- kernel/kernel/DeviceManager.cpp | 2 +- kernel/kernel/Storage/StorageDevice.cpp | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/kernel/kernel/DeviceManager.cpp b/kernel/kernel/DeviceManager.cpp index 451a79d4..422f78e4 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 ef86120c..e7dde8cd 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++) {