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
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -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

Expand Down
13 changes: 12 additions & 1 deletion qiling/os/posix/syscall/unistd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')

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_ftruncate_readonly_fd import *

if __name__ == "__main__":
unittest.main()
74 changes: 74 additions & 0 deletions tests/test_syscall_ftruncate_readonly_fd.py
Original file line number Diff line number Diff line change
@@ -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()