diff --git a/ChangeLog b/ChangeLog index 84070a4c9..dd2d32331 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ This file details the changelog of Qiling Framework. +[Unreleased] + +- Preserve the host errno for `pread64` failures. + ------------------------------------ [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..0728f30be 100644 --- a/qiling/os/posix/syscall/unistd.py +++ b/qiling/os/posix/syscall/unistd.py @@ -390,8 +390,9 @@ def ql_syscall_pread64(ql: Qiling, fd: int, buf: int, length: int, offt: int): data = f.read(length) f.seek(pos) - except OSError: - regreturn = -1 + except OSError as e: + # Linux syscall errors are returned as negative errno values. + regreturn = -e.errno if e.errno is not None else -1 else: ql.mem.write(buf, data) diff --git a/tests/test_posix.py b/tests/test_posix.py index 067bff84c..46efa4a4b 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_pread64_errno import Pread64ErrnoTests if __name__ == "__main__": unittest.main() diff --git a/tests/test_syscall_pread64_errno.py b/tests/test_syscall_pread64_errno.py new file mode 100644 index 000000000..a8298cde0 --- /dev/null +++ b/tests/test_syscall_pread64_errno.py @@ -0,0 +1,73 @@ +import os +import tempfile +import unittest +from types import SimpleNamespace + +from qiling.const import QL_ARCH +from qiling.os.filestruct import ql_file +from qiling.os.posix.const import EBADF +from qiling.os.posix.syscall.unistd import ql_syscall_pread64 + + +class _Memory: + def __init__(self): + self.write_calls = [] + + @staticmethod + def is_mapped(_address, _length): + return True + + def write(self, address, data): + self.write_calls.append((address, bytes(data))) + + +class _ReadableFile: + def tell(self): + return 9 + + @staticmethod + def seek(_position): + pass + + @staticmethod + def read(_length): + return b"R" + + +class _Qiling: + def __init__(self, file_obj): + fds = [None] * 8 + fds[3] = file_obj + self.os = SimpleNamespace(fd=fds) + self.mem = _Memory() + self.arch = SimpleNamespace(type=QL_ARCH.RISCV64) + + +class Pread64ErrnoTests(unittest.TestCase): + def test_write_only_descriptor_returns_ebadf(self): + with tempfile.TemporaryDirectory() as temp_dir: + path = os.path.join(temp_dir, "probe.bin") + with open(path, "wb") as file: + file.write(b"X") + + file_obj = ql_file.open(path, os.O_WRONLY, 0) + ql = _Qiling(file_obj) + try: + result = ql_syscall_pread64(ql, 3, 0x2000, 1, 0) + finally: + file_obj.close() + + self.assertEqual(result, -EBADF, "host EBADF must remain guest -EBADF") + self.assertEqual(ql.mem.write_calls, [], "failed pread must not copy to guest memory") + + def test_readable_descriptor_control_copies_data(self): + ql = _Qiling(_ReadableFile()) + + result = ql_syscall_pread64(ql, 3, 0x2000, 1, 2) + + self.assertEqual(result, 1, "readable descriptor must report the byte count") + self.assertEqual(ql.mem.write_calls, [(0x2000, b"R")], "successful pread must copy its data") + + +if __name__ == "__main__": + unittest.main()