diff options
author | Liav A <liavalb@gmail.com> | 2021-10-22 12:02:11 +0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-10-22 13:13:00 +0200 |
commit | cf0dbc9069645c966b9dcda3c00acdf506069dc3 (patch) | |
tree | 288a11780e8e146b86eefc76ebda2e30cf9da5da | |
parent | 026687816d65a28bd3a553c7a343d8b3421a9483 (diff) | |
download | serenity-cf0dbc9069645c966b9dcda3c00acdf506069dc3.zip |
Tests: Add a unit test to ensure the /dev/mem device works correctly
To ensure everything works as expected, a unit test was added with
multiple scenarios.
This binary has to have the SetUID flag, and we also bind-mount the
/usr/Tests directory to allow running of SetUID binaries.
-rw-r--r-- | Base/etc/fstab | 1 | ||||
-rwxr-xr-x | Meta/build-root-filesystem.sh | 4 | ||||
-rw-r--r-- | Tests/Kernel/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Tests/Kernel/TestMemoryDeviceMmap.cpp | 51 |
4 files changed, 57 insertions, 0 deletions
diff --git a/Base/etc/fstab b/Base/etc/fstab index 5da278f29d..1ef2e832aa 100644 --- a/Base/etc/fstab +++ b/Base/etc/fstab @@ -8,5 +8,6 @@ /root /root bind bind,nodev,nosuid /var /var bind bind,nodev,nosuid /www /www bind bind,nodev,nosuid +/usr/Tests /usr/Tests bind bind,nodev none /tmp tmp nodev,nosuid diff --git a/Meta/build-root-filesystem.sh b/Meta/build-root-filesystem.sh index 5ee609b39d..e11a7b97c8 100755 --- a/Meta/build-root-filesystem.sh +++ b/Meta/build-root-filesystem.sh @@ -102,6 +102,10 @@ if [ -f mnt/bin/utmpupdate ]; then chown 0:$utmp_gid mnt/bin/utmpupdate chmod 2755 mnt/bin/utmpupdate fi +if [ -f mnt/usr/Tests/Kernel/TestMemoryDeviceMmap ]; then + chown 0:0 mnt/usr/Tests/Kernel/TestMemoryDeviceMmap + chmod 4755 mnt/usr/Tests/Kernel/TestMemoryDeviceMmap +fi chmod 0400 mnt/res/kernel.map chmod 0400 mnt/boot/Kernel diff --git a/Tests/Kernel/CMakeLists.txt b/Tests/Kernel/CMakeLists.txt index 746f195593..18abcba55a 100644 --- a/Tests/Kernel/CMakeLists.txt +++ b/Tests/Kernel/CMakeLists.txt @@ -37,6 +37,7 @@ set(LIBTEST_BASED_SOURCES TestKernelFilePermissions.cpp TestKernelPledge.cpp TestKernelUnveil.cpp + TestMemoryDeviceMmap.cpp TestMunMap.cpp TestProcFS.cpp ) diff --git a/Tests/Kernel/TestMemoryDeviceMmap.cpp b/Tests/Kernel/TestMemoryDeviceMmap.cpp new file mode 100644 index 0000000000..64a8605cb0 --- /dev/null +++ b/Tests/Kernel/TestMemoryDeviceMmap.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <AK/Types.h> +#include <LibTest/TestCase.h> +#include <assert.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/mman.h> +#include <unistd.h> + +static ALWAYS_INLINE bool mem_chunk(int fd, u64 base, u64 length) +{ + u64 mmoffset = base % sysconf(_SC_PAGESIZE); + void* mmp = mmap(NULL, mmoffset + length, PROT_READ, MAP_SHARED, fd, base - mmoffset); + return mmp != MAP_FAILED; +} + +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); + + // 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); + + result = mem_chunk(fd, 0x100000, 0x200000 - 0x100000); + EXPECT_EQ(result, false); + + result = mem_chunk(fd, 0xf0000, 70000); + EXPECT_EQ(result, false); + + result = mem_chunk(fd, 0xfffc0000, 16384); + EXPECT_EQ(result, true); + + result = mem_chunk(fd, 0xfffc0000, 0x100000); + EXPECT_EQ(result, false); +} |