Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 9 additions & 0 deletions qiling/os/posix/syscall/mman.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions tests/test_posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
67 changes: 67 additions & 0 deletions tests/test_syscall_mman.py
Original file line number Diff line number Diff line change
@@ -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()