diff --git a/ChangeLog b/ChangeLog index 84070a4c9..82b8b4942 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,11 @@ This file details the changelog of Qiling Framework. ------------------------------------ +[Version 1.4.12.dev0]: Unreleased + +## What's Changed +* Reject mprotect ranges beyond the guest address space. + [Version 1.4.6]: August 4th, 2023 ## What's Changed diff --git a/qiling/os/posix/syscall/mman.py b/qiling/os/posix/syscall/mman.py index a89f98d39..fa5052d1a 100755 --- a/qiling/os/posix/syscall/mman.py +++ b/qiling/os/posix/syscall/mman.py @@ -63,6 +63,15 @@ def ql_syscall_madvise(ql: Qiling, addr: int, length: int, advice: int): def ql_syscall_mprotect(ql: Qiling, start: int, mlen: int, prot: int): + # max_mem_addr is inclusive; mprotect uses an exclusive range end. + if ( + ql.os.type == QL_OS.LINUX + and mlen > 0 + and start % ql.mem.pagesize == 0 + and start + mlen > ql.mem.max_mem_addr + 1 + ): + return -ENOMEM + try: ql.mem.protect(start, mlen, prot) except Exception as e: diff --git a/tests/test_posix.py b/tests/test_posix.py index 067bff84c..990b3fa57 100644 --- a/tests/test_posix.py +++ b/tests/test_posix.py @@ -8,6 +8,7 @@ from test_elf import * from test_riscv import * from test_qltool import * +from test_syscall_mman import MprotectRangeOverflowTests if __name__ == "__main__": unittest.main() diff --git a/tests/test_syscall_mman.py b/tests/test_syscall_mman.py new file mode 100644 index 000000000..e89ad7b6f --- /dev/null +++ b/tests/test_syscall_mman.py @@ -0,0 +1,67 @@ +import unittest +from types import SimpleNamespace +from unittest.mock import Mock + +from qiling.const import QL_OS +from qiling.os.posix.const import ENOMEM +from qiling.os.posix.syscall.mman import ql_syscall_mprotect + + +class MprotectRangeOverflowTests(unittest.TestCase): + @staticmethod + def _ql(os_type=QL_OS.LINUX, max_mem_addr=(1 << 64) - 1): + memory = SimpleNamespace( + pagesize=4096, + max_mem_addr=max_mem_addr, + protect=Mock(), + ) + return SimpleNamespace(os=SimpleNamespace(type=os_type), mem=memory) + + def test_linux_overflow_returns_enomem_before_protection(self): + ql = self._ql() + result = ql_syscall_mprotect(ql, 0x1000, (1 << 64) - 1, 1) + self.assertEqual(result, -ENOMEM) + ql.mem.protect.assert_not_called() + + def test_one_page_control_reaches_memory_manager(self): + ql = self._ql() + result = ql_syscall_mprotect(ql, 0x1000, 4096, 1) + self.assertEqual(result, 0) + ql.mem.protect.assert_called_once_with(0x1000, 4096, 1) + + def test_range_including_last_guest_address_is_not_overflow(self): + ql = self._ql() + start = ql.mem.max_mem_addr + 1 - ql.mem.pagesize + result = ql_syscall_mprotect(ql, start, ql.mem.pagesize, 1) + self.assertEqual(result, 0) + ql.mem.protect.assert_called_once_with(start, ql.mem.pagesize, 1) + + def test_start_beyond_address_space_returns_enomem(self): + ql = self._ql() + start = ql.mem.max_mem_addr + 1 + result = ql_syscall_mprotect(ql, start, ql.mem.pagesize, 1) + self.assertEqual(result, -ENOMEM) + ql.mem.protect.assert_not_called() + + def test_range_uses_guest_address_width(self): + ql = self._ql(max_mem_addr=(1 << 32) - 1) + result = ql_syscall_mprotect(ql, 0x1000, (1 << 32) - 1, 1) + self.assertEqual(result, -ENOMEM) + ql.mem.protect.assert_not_called() + + def test_zero_length_keeps_existing_path(self): + ql = self._ql() + result = ql_syscall_mprotect(ql, 0x1000, 0, 1) + self.assertEqual(result, 0) + ql.mem.protect.assert_called_once_with(0x1000, 0, 1) + + def test_non_linux_personality_keeps_existing_path(self): + ql = self._ql(QL_OS.FREEBSD) + length = (1 << 64) - 1 + result = ql_syscall_mprotect(ql, 0x1000, length, 1) + self.assertEqual(result, 0) + ql.mem.protect.assert_called_once_with(0x1000, length, 1) + + +if __name__ == "__main__": + unittest.main()