From c2cbd5a3edc23d8cdfb565a04ec875545f3d2a6d Mon Sep 17 00:00:00 2001 From: carlosqwqqwq <102978772+carlosqwqqwq@users.noreply.github.com> Date: Tue, 24 Nov 2026 05:07:37 +0800 Subject: [PATCH] Return EBADF for invalid fstatfs descriptors --- ChangeLog | 5 +++++ qiling/os/posix/syscall/stat.py | 6 ++++++ tests/test_fstatfs.py | 29 +++++++++++++++++++++++++++++ tests/test_posix.py | 1 + 4 files changed, 41 insertions(+) create mode 100644 tests/test_fstatfs.py diff --git a/ChangeLog b/ChangeLog index 84070a4c9..7050d9761 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ This file details the changelog of Qiling Framework. +------------------------------------ +[Unreleased] + +- Validate fstatfs descriptors using the guest ABI width. + ------------------------------------ [Version 1.4.6]: August 4th, 2023 diff --git a/qiling/os/posix/syscall/stat.py b/qiling/os/posix/syscall/stat.py index 87530d50b..5f41e6821 100644 --- a/qiling/os/posix/syscall/stat.py +++ b/qiling/os/posix/syscall/stat.py @@ -1560,6 +1560,12 @@ def ql_syscall_rmdir(ql: Qiling, pathname: int): def ql_syscall_fstatfs(ql: Qiling, fd: int, buf: int): + # The guest syscall ABI passes fstatfs descriptors as 32-bit integers. + fd &= 0xFFFFFFFF + + if fd not in range(NR_OPEN) or ql.os.fd[fd] is None: + return -EBADF + data = b"0" * (12 * 8) # for now, just return 0s regreturn = 0 diff --git a/tests/test_fstatfs.py b/tests/test_fstatfs.py new file mode 100644 index 000000000..de7abbe3f --- /dev/null +++ b/tests/test_fstatfs.py @@ -0,0 +1,29 @@ +import unittest +from unittest.mock import Mock + +from qiling.os.posix.const import EBADF, NR_OPEN +from qiling.os.posix.syscall.stat import ql_syscall_fstatfs + + +class FstatfsTest(unittest.TestCase): + def setUp(self): + self.ql = Mock() + self.ql.os.fd = [None] * NR_OPEN + self.ql.os.fd[0] = object() + + def test_invalid_fd_returns_ebadf_without_copyout(self): + for fd in (-1, 3, NR_OPEN, 0xFFFFFFFFFFFFFFFF): + with self.subTest(fd=fd): + self.assertEqual( + -EBADF, + ql_syscall_fstatfs(self.ql, fd, 0x1000), + ) + self.ql.mem.write.assert_not_called() + + def test_valid_fd_preserves_existing_copyout(self): + self.assertEqual(0, ql_syscall_fstatfs(self.ql, 0, 0x1000)) + self.ql.mem.write.assert_called_once_with(0x1000, b"0" * (12 * 8)) + + def test_riscv64_ignores_bits_above_the_32_bit_fd(self): + self.assertEqual(0, ql_syscall_fstatfs(self.ql, 0x100000000, 0x1000)) + self.ql.mem.write.assert_called_once_with(0x1000, b"0" * (12 * 8)) diff --git a/tests/test_posix.py b/tests/test_posix.py index 067bff84c..f2451b184 100644 --- a/tests/test_posix.py +++ b/tests/test_posix.py @@ -6,6 +6,7 @@ import unittest from test_elf import * +from test_fstatfs import * from test_riscv import * from test_qltool import *