Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POSIX syscalls improvements and bug fixes #1448

Merged
merged 19 commits into from
Jul 2, 2024
Merged
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
5 changes: 1 addition & 4 deletions qiling/os/filestruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ def __init__(self, path: AnyStr, fd: int):
def open(cls, path: AnyStr, flags: int, mode: int, dir_fd: Optional[int] = None):
mode &= 0x7fffffff

try:
fd = os.open(path, flags, mode, dir_fd=dir_fd)
except OSError as e:
raise QlSyscallError(e.errno, e.args[1] + ' : ' + e.filename)
fd = os.open(path, flags, mode, dir_fd=dir_fd)

return cls(path, fd)

Expand Down
38 changes: 23 additions & 15 deletions qiling/os/freebsd/syscall.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#!/usr/bin/env python3
#
#
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
#

import ctypes
from datetime import datetime
from math import floor

from qiling import Qiling
from qiling.arch.x86_const import *
from qiling.os.freebsd.const import *
from qiling.os.posix.const import ENOENT
from qiling.os.posix.syscall.unistd import ql_syscall_getcwd
from qiling.os.posix.syscall.stat import ql_syscall_newfstatat
from datetime import datetime
from math import floor
import ctypes

class timespec(ctypes.Structure):
_fields_ = [
Expand Down Expand Up @@ -59,37 +61,43 @@ def ql_syscall___getcwd(ql, path_buff, path_buffsize, *args, **kw):
def ql_syscall_fstatat(ql, newfstatat_dirfd, newfstatat_path, newfstatat_buf_ptr, newfstatat_flag, *args, **kw):
return ql_syscall_newfstatat(ql, newfstatat_dirfd, newfstatat_path, newfstatat_buf_ptr, newfstatat_flag, *args, **kw)

def ql_syscall___sysctl(ql, name, namelen, old, oldlenp, new_arg, newlen):
def ql_syscall___sysctl(ql: Qiling, name: int, namelen: int, old: int, oldlenp: int, new_arg: int, newlen: int):
ql.log.debug("__sysctl(name: 0x%x, namelen: 0x%x, old: 0x%x, oldlenp: 0x%x, new: 0x%x, newlen: 0x%x)" % (
name, namelen, old, oldlenp, new_arg, newlen
))
vecs = []
for i in range(namelen):
vecs.append(ql.unpack32s(ql.mem.read(name + i*4, 4)))

vecs = [ql.mem.read_ptr(name + i * 4, 4, signed=True) for i in range(namelen)]

ql.log.debug(f"__sysctl vectors: {vecs}")
if vecs[0] == CTL_SYSCTL:
if vecs[1] == CTL_SYSCTL_NAME2OID:
# Write oid to old and oldlenp
sysctl_name = ql.mem.string(new_arg)
sysctl_name = ql.os.utils.read_cstring(new_arg)
out_vecs = []
out_len = 0

# TODO: Implement oid<-->name as many as possible from FreeBSD source.
# Search SYSCTL_ADD_NODE etc.
if sysctl_name == "hw.pagesizes":
out_vecs = [CTL_HW, HW_PAGESIZE]
out_len = 2
else:
ql.log.warning("Unknown oid name!")

for i, v in enumerate(out_vecs):
ql.mem.write(old + 4*i, ql.pack32s(v))
ql.mem.write(oldlenp, ql.pack32s(out_len))
return 2 # ENOENT
ql.mem.write_ptr(old + i * 4, v, 32, signed=True)

ql.mem.write_ptr(oldlenp, out_len, 32, signed=True)

return -ENOENT

if vecs[0] == CTL_KERN:
if vecs[1] == KERN_OSRELDATE:
if old == 0 or oldlenp == 0:
if old == 0 or oldlenp == 0:
return -1

# Ignore oldlenp check.
ql.mem.write(old, ql.pack32s(FREEBSD_OSRELDATE))
ql.mem.write_ptr(old, FREEBSD_OSRELDATE, 32, signed=True)

return 0
return 0

24 changes: 18 additions & 6 deletions qiling/os/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,26 +313,32 @@ def read(self, addr: int, size: int) -> bytearray:

return self.ql.uc.mem_read(addr, size)

def read_ptr(self, addr: int, size: int = 0) -> int:
def read_ptr(self, addr: int, size: int = 0, *, signed = False) -> int:
"""Read an integer value from a memory address.
Bytes read will be unpacked using emulated architecture properties.

Args:
addr: memory address to read
size: pointer size (in bytes): either 1, 2, 4, 8, or 0 for arch native size
signed: interpret value as a signed integer (default: False)

Returns: integer value stored at the specified memory address
"""

if not size:
size = self.ql.arch.pointersize

__unpack = {
__unpack = ({
1: self.ql.unpack8s,
2: self.ql.unpack16s,
4: self.ql.unpack32s,
8: self.ql.unpack64s
} if signed else {
1: self.ql.unpack8,
2: self.ql.unpack16,
4: self.ql.unpack32,
8: self.ql.unpack64
}.get(size)
}).get(size)

if __unpack is None:
raise QlErrorStructConversion(f"Unsupported pointer size: {size}")
Expand All @@ -349,25 +355,31 @@ def write(self, addr: int, data: bytes) -> None:

self.ql.uc.mem_write(addr, data)

def write_ptr(self, addr: int, value: int, size: int = 0) -> None:
def write_ptr(self, addr: int, value: int, size: int = 0, *, signed = False) -> None:
"""Write an integer value to a memory address.
Bytes written will be packed using emulated architecture properties.

Args:
addr: target memory address
value: integer value to write
size: pointer size (in bytes): either 1, 2, 4, 8, or 0 for arch native size
signed: interpret value as a signed integer (default: False)
"""

if not size:
size = self.ql.arch.pointersize

__pack = {
__pack = ({
1: self.ql.pack8s,
2: self.ql.pack16s,
4: self.ql.pack32s,
8: self.ql.pack64s
} if signed else {
1: self.ql.pack8,
2: self.ql.pack16,
4: self.ql.pack32,
8: self.ql.pack64
}.get(size)
}).get(size)

if __pack is None:
raise QlErrorStructConversion(f"Unsupported pointer size: {size}")
Expand Down
Loading
Loading