From 7ec5b4217c325cac82da6a94f8f88135fbbf6baa Mon Sep 17 00:00:00 2001 From: carlosqwqqwq <102978772+carlosqwqqwq@users.noreply.github.com> Date: Tue, 24 Nov 2026 05:55:23 +0800 Subject: [PATCH] fix linux ftruncate read-only guest descriptors --- ChangeLog | 6 ++ qiling/os/posix/syscall/unistd.py | 13 +++- tests/test_posix.py | 1 + tests/test_syscall_ftruncate_readonly_fd.py | 74 +++++++++++++++++++++ 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 tests/test_syscall_ftruncate_readonly_fd.py diff --git a/ChangeLog b/ChangeLog index 84070a4c9..a6190b52d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ This file details the changelog of Qiling Framework. +------------------------------------ +[Unreleased] + +## Bug fixes +- Preserve guest file descriptor access mode in ftruncate. + ------------------------------------ [Version 1.4.6]: August 4th, 2023 diff --git a/qiling/os/posix/syscall/unistd.py b/qiling/os/posix/syscall/unistd.py index 62cb6f61a..73d1ddbbd 100644 --- a/qiling/os/posix/syscall/unistd.py +++ b/qiling/os/posix/syscall/unistd.py @@ -801,7 +801,18 @@ def ql_syscall_truncate(ql: Qiling, path: int, length: int): def ql_syscall_ftruncate(ql: Qiling, fd: int, length: int): f = get_opened_fd(ql.os, fd) - regreturn = -EBADF if f is None else __do_truncate(ql, f.name, length) + if f is None: + regreturn = -EBADF + else: + try: + # ftruncate(2) applies access-mode checks to the open descriptor. + os.ftruncate(f.fileno(), length) + except OSError as ex: + regreturn = -(ex.errno or EINVAL) + except (AttributeError, TypeError, ValueError): + regreturn = -EINVAL + else: + regreturn = 0 ql.log.debug(f'ftruncate({fd}, {length:#x}) = {regreturn}') diff --git a/tests/test_posix.py b/tests/test_posix.py index 067bff84c..847545253 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_ftruncate_readonly_fd import * if __name__ == "__main__": unittest.main() diff --git a/tests/test_syscall_ftruncate_readonly_fd.py b/tests/test_syscall_ftruncate_readonly_fd.py new file mode 100644 index 000000000..edc2aea0f --- /dev/null +++ b/tests/test_syscall_ftruncate_readonly_fd.py @@ -0,0 +1,74 @@ +import errno +import os +import tempfile +import unittest +from pathlib import Path +from types import SimpleNamespace +from unittest.mock import Mock + +from qiling.os.filestruct import ql_file +from qiling.os.posix.const import NR_OPEN +from qiling.os.posix.syscall.unistd import ql_syscall_ftruncate + + +DATA = b"RV64-FTRUNCATE-CONTROL\n" + + +def make_ql(file_obj=None, fd=3): + descriptors = [None] * NR_OPEN + if file_obj is not None: + descriptors[fd] = file_obj + return SimpleNamespace( + os=SimpleNamespace(fd=descriptors), + log=SimpleNamespace(debug=Mock()), + ) + + +class TestFtruncateReadonlyFd(unittest.TestCase): + def test_readonly_descriptor_fails_without_changing_file(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "target.bin" + path.write_bytes(DATA) + file_obj = ql_file.open(str(path), os.O_RDONLY, 0) + try: + result = ql_syscall_ftruncate(make_ql(file_obj), 3, 0) + self.assertIn(result, (-errno.EBADF, -errno.EINVAL)) + self.assertEqual(path.read_bytes(), DATA) + finally: + file_obj.close() + + def test_writable_descriptor_truncates_and_extends(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "target.bin" + path.write_bytes(DATA) + file_obj = ql_file.open(str(path), os.O_RDWR, 0) + try: + ql = make_ql(file_obj) + self.assertEqual(ql_syscall_ftruncate(ql, 3, 5), 0) + self.assertEqual(path.read_bytes(), DATA[:5]) + self.assertEqual(ql_syscall_ftruncate(ql, 3, 31), 0) + self.assertEqual(path.read_bytes(), DATA[:5] + bytes(26)) + finally: + file_obj.close() + + def test_negative_length_returns_einval_without_mutation(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "target.bin" + path.write_bytes(DATA) + file_obj = ql_file.open(str(path), os.O_RDWR, 0) + try: + result = ql_syscall_ftruncate(make_ql(file_obj), 3, -1) + self.assertEqual(result, -errno.EINVAL) + self.assertEqual(path.read_bytes(), DATA) + finally: + file_obj.close() + + def test_invalid_guest_fd_returns_ebadf(self): + self.assertEqual(ql_syscall_ftruncate(make_ql(), 3, 0), -errno.EBADF) + + def test_file_object_without_host_descriptor_returns_einval(self): + self.assertEqual(ql_syscall_ftruncate(make_ql(object()), 3, 0), -errno.EINVAL) + + +if __name__ == "__main__": + unittest.main()