From 120db727eff20f6568a0085847ea8e66645bb831 Mon Sep 17 00:00:00 2001 From: carlosqwqqwq <102978772+carlosqwqqwq@users.noreply.github.com> Date: Wed, 25 Nov 2026 13:15:34 +0800 Subject: [PATCH 1/2] Fix zero-length getrandom requests --- qiling/os/posix/syscall/random.py | 3 +++ tests/test_posix.py | 1 + tests/test_syscall_random.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 tests/test_syscall_random.py diff --git a/qiling/os/posix/syscall/random.py b/qiling/os/posix/syscall/random.py index f0f5cc0a0..05f3df444 100644 --- a/qiling/os/posix/syscall/random.py +++ b/qiling/os/posix/syscall/random.py @@ -9,6 +9,9 @@ from qiling.os.posix.const import EFAULT def ql_syscall_getrandom(ql: Qiling, buf: int, buflen: int, flags: int): + if buflen == 0: + return 0 + if not ql.mem.is_mapped(buf, buflen): return -EFAULT diff --git a/tests/test_posix.py b/tests/test_posix.py index 067bff84c..bdd8284cb 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_random import * if __name__ == "__main__": unittest.main() diff --git a/tests/test_syscall_random.py b/tests/test_syscall_random.py new file mode 100644 index 000000000..86fe49770 --- /dev/null +++ b/tests/test_syscall_random.py @@ -0,0 +1,30 @@ +import unittest +from unittest.mock import MagicMock, patch + +from qiling.os.posix.syscall.random import ql_syscall_getrandom + + +class GetRandomZeroLengthTest(unittest.TestCase): + def test_zero_length_request_returns_without_accessing_memory(self): + ql = MagicMock() + + self.assertEqual(ql_syscall_getrandom(ql, 0, 0, 0), 0) + ql.mem.is_mapped.assert_not_called() + ql.mem.write.assert_not_called() + + def test_positive_length_request_still_writes_random_bytes(self): + ql = MagicMock() + ql.mem.is_mapped.return_value = True + + with patch( + "qiling.os.posix.syscall.random.os.urandom", return_value=b"data" + ): + result = ql_syscall_getrandom(ql, 0x1000, 4, 0) + + self.assertEqual(result, 4) + ql.mem.is_mapped.assert_called_once_with(0x1000, 4) + ql.mem.write.assert_called_once_with(0x1000, b"data") + + +if __name__ == "__main__": + unittest.main() From 7e107b88d2c02df293132ea757a81e13416964b6 Mon Sep 17 00:00:00 2001 From: carlosqwqqwq <102978772+carlosqwqqwq@users.noreply.github.com> Date: Wed, 25 Nov 2026 17:21:06 +0800 Subject: [PATCH 2/2] Fix zero-length getrandom flag validation --- qiling/os/posix/syscall/random.py | 12 +++++++++++- tests/test_syscall_random.py | 13 +++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/qiling/os/posix/syscall/random.py b/qiling/os/posix/syscall/random.py index 05f3df444..1c2f477b9 100644 --- a/qiling/os/posix/syscall/random.py +++ b/qiling/os/posix/syscall/random.py @@ -6,9 +6,19 @@ import os from qiling import Qiling -from qiling.os.posix.const import EFAULT +from qiling.os.posix.const import EFAULT, EINVAL + +GRND_NONBLOCK = 0x1 +GRND_RANDOM = 0x2 +GRND_INSECURE = 0x4 + def ql_syscall_getrandom(ql: Qiling, buf: int, buflen: int, flags: int): + if flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE) or ( + flags & (GRND_RANDOM | GRND_INSECURE) + ) == (GRND_RANDOM | GRND_INSECURE): + return -EINVAL + if buflen == 0: return 0 diff --git a/tests/test_syscall_random.py b/tests/test_syscall_random.py index 86fe49770..fe4b5468b 100644 --- a/tests/test_syscall_random.py +++ b/tests/test_syscall_random.py @@ -1,6 +1,7 @@ import unittest from unittest.mock import MagicMock, patch +from qiling.os.posix.const import EINVAL from qiling.os.posix.syscall.random import ql_syscall_getrandom @@ -25,6 +26,18 @@ def test_positive_length_request_still_writes_random_bytes(self): ql.mem.is_mapped.assert_called_once_with(0x1000, 4) ql.mem.write.assert_called_once_with(0x1000, b"data") + def test_invalid_flags_are_rejected_before_accessing_memory(self): + ql = MagicMock() + + for buflen, flags in ((0, 0x8), (0, 0x6), (4, 0x8), (4, 0x6)): + with self.subTest(buflen=buflen, flags=flags): + self.assertEqual( + ql_syscall_getrandom(ql, 0, buflen, flags), -EINVAL + ) + + ql.mem.is_mapped.assert_not_called() + ql.mem.write.assert_not_called() + if __name__ == "__main__": unittest.main()