diff --git a/Kernel/Devices/MemoryDevice.cpp b/Kernel/Devices/MemoryDevice.cpp index d17bce0a9ad..c94667a6019 100644 --- a/Kernel/Devices/MemoryDevice.cpp +++ b/Kernel/Devices/MemoryDevice.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include namespace Kernel { @@ -31,9 +32,17 @@ UNMAP_AFTER_INIT MemoryDevice::~MemoryDevice() { } -ErrorOr MemoryDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) +ErrorOr MemoryDevice::read(OpenFileDescription&, u64 offset, UserOrKernelBuffer& buffer, size_t length) { - TODO(); + if (!MM.is_allowed_to_read_physical_memory_for_userspace(PhysicalAddress(offset), length)) { + dbgln("MemoryDevice: Trying to read physical memory at {} for range of {} bytes failed due to violation of access", PhysicalAddress(offset), length); + return EINVAL; + } + auto mapping = Memory::map_typed(PhysicalAddress(offset), length); + + auto bytes = ReadonlyBytes { mapping.ptr(), length }; + TRY(buffer.write(bytes)); + return length; } ErrorOr MemoryDevice::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared) @@ -41,7 +50,7 @@ ErrorOr MemoryDevice::mmap(Process& process, OpenFileDescriptio auto viewed_address = PhysicalAddress(offset); dbgln("MemoryDevice: Trying to mmap physical memory at {} for range of {} bytes", viewed_address, range.size()); - if (!MM.is_allowed_to_mmap_physical_memory_to_userspace(viewed_address, range)) { + if (!MM.is_allowed_to_read_physical_memory_for_userspace(viewed_address, range.size())) { dbgln("MemoryDevice: Trying to mmap physical memory at {} for range of {} bytes failed due to violation of access", viewed_address, range.size()); return EINVAL; } diff --git a/Kernel/Memory/MemoryManager.cpp b/Kernel/Memory/MemoryManager.cpp index 3e14f503b78..8b06e7732dc 100644 --- a/Kernel/Memory/MemoryManager.cpp +++ b/Kernel/Memory/MemoryManager.cpp @@ -209,13 +209,13 @@ UNMAP_AFTER_INIT void MemoryManager::register_reserved_ranges() m_reserved_memory_ranges.append(ContiguousReservedMemoryRange { range.start, m_physical_memory_ranges.last().start.get() + m_physical_memory_ranges.last().length - range.start.get() }); } -bool MemoryManager::is_allowed_to_mmap_physical_memory_to_userspace(PhysicalAddress start_address, VirtualRange const& range) const +bool MemoryManager::is_allowed_to_read_physical_memory_for_userspace(PhysicalAddress start_address, size_t read_length) const { // Note: Guard against overflow in case someone tries to mmap on the edge of // the RAM - if (start_address.offset_addition_would_overflow(range.size())) + if (start_address.offset_addition_would_overflow(read_length)) return false; - auto end_address = start_address.offset(range.size()); + auto end_address = start_address.offset(read_length); for (auto& current_range : m_reserved_memory_ranges) { if (current_range.start > start_address) continue; diff --git a/Kernel/Memory/MemoryManager.h b/Kernel/Memory/MemoryManager.h index 761419c461d..fb0892d833c 100644 --- a/Kernel/Memory/MemoryManager.h +++ b/Kernel/Memory/MemoryManager.h @@ -230,7 +230,7 @@ public: PageDirectory& kernel_page_directory() { return *m_kernel_page_directory; } Vector const& used_memory_ranges() { return m_used_memory_ranges; } - bool is_allowed_to_mmap_physical_memory_to_userspace(PhysicalAddress, VirtualRange const&) const; + bool is_allowed_to_read_physical_memory_for_userspace(PhysicalAddress, size_t read_length) const; PhysicalPageEntry& get_physical_page_entry(PhysicalAddress); PhysicalAddress get_physical_address(PhysicalPage const&); diff --git a/Tests/Kernel/TestMemoryDeviceMmap.cpp b/Tests/Kernel/TestMemoryDeviceMmap.cpp index 64a8605cb0e..ef4a21ee809 100644 --- a/Tests/Kernel/TestMemoryDeviceMmap.cpp +++ b/Tests/Kernel/TestMemoryDeviceMmap.cpp @@ -9,12 +9,16 @@ #include #include #include +#include #include #include #include #include +#include #include +static u8 read_buffer[0x100000]; + static ALWAYS_INLINE bool mem_chunk(int fd, u64 base, u64 length) { u64 mmoffset = base % sysconf(_SC_PAGESIZE); @@ -22,30 +26,80 @@ static ALWAYS_INLINE bool mem_chunk(int fd, u64 base, u64 length) return mmp != MAP_FAILED; } +enum class ReadResult { + SeekFailure, + ReadFailure, + ReadSuccess, +}; + +static ALWAYS_INLINE ReadResult read_chunk(int fd, u64 base, u64 length) +{ + VERIFY(length <= sizeof(read_buffer)); + auto rs = lseek(fd, base, SEEK_SET); + if (rs < 0) { + fprintf(stderr, "Couldn't seek to offset %" PRIi64 " while verifying: %s\n", base, strerror(errno)); + return ReadResult::SeekFailure; + } + if (read(fd, read_buffer, length) < 0) + return ReadResult::ReadFailure; + return ReadResult::ReadSuccess; +} + +TEST_CASE(test_memory_access_device_read) +{ + int rc = geteuid(); + EXPECT_EQ(rc, 0); + + int fd = open("/dev/mem", O_RDONLY); + EXPECT(fd >= 0); + + // FIXME: This is expected to work on QEMU machines (both 440FX and Q35), + // however, it will be much nicer to have some sort of a node in the ProcFS + // to expose physical memory ranges (e820 memory map). + + auto read_result = read_chunk(fd, 0x0, 0x100000); + EXPECT_EQ(read_result, ReadResult::ReadFailure); + + read_result = read_chunk(fd, 0xe0000, 0x100000 - 0xe0000); + EXPECT_EQ(read_result, ReadResult::ReadSuccess); + + read_result = read_chunk(fd, 0x100000, 0x200000 - 0x100000); + EXPECT_EQ(read_result, ReadResult::ReadFailure); + + read_result = read_chunk(fd, 0xf0000, 70000); + EXPECT_EQ(read_result, ReadResult::ReadFailure); + + read_result = read_chunk(fd, 0xfffc0000, 16384); + EXPECT_EQ(read_result, ReadResult::ReadSuccess); + + read_result = read_chunk(fd, 0xfffc0000, 0x100000); + EXPECT_EQ(read_result, ReadResult::ReadFailure); +} + TEST_CASE(test_memory_access_device_mmap) { int rc = geteuid(); EXPECT_EQ(rc, 0); int fd = open("/dev/mem", O_RDONLY); - EXPECT_EQ(fd < 0, false); + EXPECT(fd >= 0); // FIXME: This is expected to work on QEMU machines (both 440FX and Q35), // however, it will be much nicer to have some sort of a node in the ProcFS // to expose physical memory ranges (e820 memory map). - auto result = mem_chunk(fd, 0xe0000, 0x100000 - 0xe0000); - EXPECT_EQ(result, true); + auto mmap_result = mem_chunk(fd, 0xe0000, 0x100000 - 0xe0000); + EXPECT_EQ(mmap_result, true); - result = mem_chunk(fd, 0x100000, 0x200000 - 0x100000); - EXPECT_EQ(result, false); + mmap_result = mem_chunk(fd, 0x100000, 0x200000 - 0x100000); + EXPECT_EQ(mmap_result, false); - result = mem_chunk(fd, 0xf0000, 70000); - EXPECT_EQ(result, false); + mmap_result = mem_chunk(fd, 0xf0000, 70000); + EXPECT_EQ(mmap_result, false); - result = mem_chunk(fd, 0xfffc0000, 16384); - EXPECT_EQ(result, true); + mmap_result = mem_chunk(fd, 0xfffc0000, 16384); + EXPECT_EQ(mmap_result, true); - result = mem_chunk(fd, 0xfffc0000, 0x100000); - EXPECT_EQ(result, false); + mmap_result = mem_chunk(fd, 0xfffc0000, 0x100000); + EXPECT_EQ(mmap_result, false); }