Skip to content
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
4 changes: 2 additions & 2 deletions src/chomper/os/android/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def set_errno(self, value: int):
self.emu.write_s32(TLS_ADDRESS + 0x10, value)

@staticmethod
def _create_stat(st: os.stat_result) -> bytes:
def _construct_stat(st: os.stat_result) -> bytes:
if sys.platform == "win32":
block_size = 4096

Expand Down Expand Up @@ -92,7 +92,7 @@ def _create_stat(st: os.stat_result) -> bytes:
return struct_to_bytes(st)

@staticmethod
def _create_device_stat() -> bytes:
def _construct_device_stat() -> bytes:
atim = Timespec.from_time_ns(0)
mtim = Timespec.from_time_ns(0)
ctim = Timespec.from_time_ns(0)
Expand Down
1 change: 1 addition & 0 deletions src/chomper/os/android/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@
EFAULT = 14
EEXIST = 17
ENOTDIR = 20
EINVAL = 22
13 changes: 7 additions & 6 deletions src/chomper/os/android/syscall.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
SyscallError.EFAULT: (const.EFAULT, "EFAULT"),
SyscallError.EEXIST: (const.EEXIST, "EEXIST"),
SyscallError.ENOTDIR: (const.ENOTDIR, "ENOTDIR"),
SyscallError.EINVAL: (const.EINVAL, "EINVAL"),
}

syscall_handlers: Dict[int, SyscallHandleCallable] = {}
Expand Down Expand Up @@ -52,6 +53,10 @@ def decorator(emu: Chomper):
error_type = SyscallError.ENOENT
except FileExistsError:
error_type = SyscallError.EEXIST
except UnicodeDecodeError:
error_type = SyscallError.EPERM
except OSError:
error_type = SyscallError.EINVAL
except SystemOperationFailed as e:
error_type = e.error_type

Expand All @@ -73,10 +78,6 @@ def decorator(emu: Chomper):
return wrapper


def raise_permission_denied():
raise SystemOperationFailed("No permission", SyscallError.EPERM)


@register_syscall_handler(const.NR_GETCWD, "NR_getcwd")
def handle_nr_getcwd(emu: Chomper):
buf = emu.get_arg(0)
Expand Down Expand Up @@ -281,7 +282,7 @@ def handle_nr_clock_nanosleep(emu: Chomper):

@register_syscall_handler(const.NR_SETRESGID, "NR_setresgid")
def handle_nr_setresgid(emu: Chomper):
raise_permission_denied()
emu.os.raise_permission_denied()

return 0

Expand All @@ -291,7 +292,7 @@ def handle_nr_getpgid(emu: Chomper):
pid = emu.get_arg(0)

if pid != 0:
raise_permission_denied()
emu.os.raise_permission_denied()

return 1

Expand Down
17 changes: 12 additions & 5 deletions src/chomper/os/handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,26 @@ def validate(self, handle: int) -> bool:
"""Check if a resource handle is valid."""
return handle in self._handles

def set_prop(self, handle: int, prop_name: str, prop_value: Any):
def set_prop(self, handle: int, name: str, value: Any):
"""Set property value for a resource."""
if handle not in self._handles:
return

self._handle_props[handle][prop_name] = prop_value
self._handle_props[handle][name] = value

def get_prop(self, handle: int, prop_name: str) -> Optional[Any]:
def get_prop(self, handle: int, name: str) -> Optional[Any]:
"""Get property value for a resource."""
if handle not in self._handles:
return None

if prop_name not in self._handle_props[handle]:
if name not in self._handle_props[handle]:
return None

return self._handle_props[handle][prop_name]
return self._handle_props[handle][name]

def has_prop(self, handle: int, name: str) -> bool:
"""Check if the property exists."""
if handle not in self._handles:
return False

return name in self._handle_props[handle]
31 changes: 30 additions & 1 deletion src/chomper/os/ios/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# System call numbers

SYS_EXIT = 0x1
SYS_FORK = 0x2
SYS_READ = 0x3
SYS_WRITE = 0x4
SYS_OPEN = 0x5
Expand All @@ -16,6 +17,8 @@
SYS_GETEUID = 0x19
SYS_SENDMSG = 0x1C
SYS_RECVFROM = 0x1D
SYS_GETPEERNAME = 0x1F
SYS_GETSOCKNAME = 0x20
SYS_ACCESS = 0x21
SYS_CHFLAGS = 0x22
SYS_FCHFLAGS = 0x23
Expand All @@ -25,18 +28,25 @@
SYS_GETEGID = 0x2B
SYS_SIGACTION = 0x2E
SYS_SIGPROCMASK = 0x30
SYS_GETLOGIN = 0x31
SYS_SETLOGIN = 0x32
SYS_SIGALTSTACK = 0x35
SYS_IOCTL = 0x36
SYS_REBOOT = 0x37
SYS_SYMLINK = 0x39
SYS_READLINK = 0x3A
SYS_MSYNC = 0x41
SYS_MUNMAP = 0x49
SYS_MPROTECT = 0x4A
SYS_MADVISE = 0x4B
SYS_SETPGID = 0x52
SYS_FCNTL = 0x5C
SYS_SELECT = 0x5D
SYS_FSYNC = 0x5F
SYS_SETPRIORITY = 0x60
SYS_SOCKET = 0x61
SYS_CONNECT = 0x62
SYS_GETPRIORITY = 0x64
SYS_BIND = 0x68
SYS_SETSOCKOPT = 0x69
SYS_SIGSUSPEND = 0x6F
Expand All @@ -55,12 +65,13 @@
SYS_UTIMES = 0x8A
SYS_FUTIMES = 0x8B
SYS_ADJTIME = 0x8C
SYS_GETPGID = 0x97
SYS_PREAD = 0x99
SYS_PWRITE = 0x9A
SYS_QUOTACTL = 0xA5
SYS_CSOPS = 0xA9
SYS_CSOPS_AUDITTOKEN = 0xAA
SYS_RLIMIT = 0xC2
SYS_GETRLIMIT = 0xC2
SYS_SETRLIMIT = 0xC3
SYS_MMAP = 0xC5
SYS_LSEEK = 0xC7
Expand Down Expand Up @@ -234,6 +245,7 @@
EFAULT = 14
EEXIST = 17
ENOTDIR = 20
EINVAL = 22

# Flavors for proc_pidinfo()

Expand All @@ -242,6 +254,23 @@
PROC_PIDT_SHORTBSDINFO = 13
PROC_PIDUNIQIDENTIFIERINFO = 17

# sys/resource.h

# RESOURCE LIMITS

RLIM_INFINITY = (1 << 63) - 1

RLIMIT_CPU = 0
RLIMIT_FSIZE = 1
RLIMIT_DATA = 2
RLIMIT_STACK = 3
RLIMIT_CORE = 4
RLIMIT_AS = 5
RLIMIT_RSS = RLIMIT_AS
RLIMIT_MEMLOCK = 6
RLIMIT_NPROC = 7
RLIMIT_NOFILE = 8

# mach/kern_return.h

KERN_SUCCESS = 0
Expand Down
20 changes: 16 additions & 4 deletions src/chomper/os/ios/ios.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import plistlib
import random
import shutil
import socket
import sys
import time
import uuid
Expand All @@ -19,7 +20,7 @@
from .fixup import SystemModuleFixer
from .hooks import get_hooks
from .mach import MachMsgHandler
from .structs import Dirent, Stat64, Statfs64, Timespec
from .structs import Dirent, Stat64, Statfs64, Timespec, SockaddrIn
from .syscall import get_syscall_handlers, get_syscall_names
from .xpc import XpcMessageHandler

Expand Down Expand Up @@ -186,6 +187,7 @@ def __init__(self, *args, **kwargs):
self.gid = self.uid

self.pid = random.randint(1000, 2000)
self.pgid = self.pid

self.tid = random.randint(10000, 20000)

Expand Down Expand Up @@ -231,7 +233,7 @@ def set_errno(self, value: int):
self.emu.write_s32(errno_ptr, value)

@staticmethod
def _create_stat(st: os.stat_result) -> bytes:
def _construct_stat(st: os.stat_result) -> bytes:
if sys.platform == "win32":
block_size = 4096

Expand Down Expand Up @@ -272,7 +274,7 @@ def _create_stat(st: os.stat_result) -> bytes:
return struct_to_bytes(st)

@staticmethod
def _create_device_stat() -> bytes:
def _construct_device_stat() -> bytes:
atimespec = Timespec.from_time_ns(0)
mtimespec = Timespec.from_time_ns(0)
ctimespec = Timespec.from_time_ns(0)
Expand All @@ -297,7 +299,7 @@ def _create_device_stat() -> bytes:
return struct_to_bytes(st)

@staticmethod
def _create_statfs() -> bytes:
def _construct_statfs() -> bytes:
st = Statfs64(
f_bsize=4096,
f_iosize=1048576,
Expand All @@ -317,6 +319,16 @@ def _create_statfs() -> bytes:
)
return struct_to_bytes(st)

@classmethod
def _construct_sockaddr_in(cls, address: str, port: int) -> bytes:
sa = SockaddrIn(
sin_len=ctypes.sizeof(SockaddrIn),
sin_family=cls.AF_INET,
sin_port=socket.htons(port),
sin_addr=int.from_bytes(socket.inet_aton(address), "little"),
)
return struct_to_bytes(sa)

@log_call
def getdirentries(self, fd: int, offset: int) -> Optional[bytes]:
if not self._is_dir_fd(fd):
Expand Down
31 changes: 31 additions & 0 deletions src/chomper/os/ios/mach.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ def handle_send_and_rcv_msg(self, msg: int, msgh: MachMsgHeader) -> int:
result = self._handle_task_get_special_port(msg, msgh)
elif msgh_id == 3410:
result = self._handle_task_set_special_port(msg, msgh)
elif msgh_id == 3414:
result = self._handle_task_get_exception_ports(msg, msgh)
elif msgh_id == 3418:
result = self._handle_semaphore_create(msg, msgh)
elif msgh_id == 4808:
Expand Down Expand Up @@ -481,6 +483,35 @@ def _handle_task_set_special_port(self, msg: int, msgh: MachMsgHeader) -> int:

return const.KERN_SUCCESS

def _handle_task_get_exception_ports(self, msg: int, msgh: MachMsgHeader) -> int:
msg_header = MachMsgHeader(
msgh_bits=const.MACH_MSGH_BITS_COMPLEX,
msgh_size=36 + 12 * 32 + 4,
msgh_remote_port=0,
msgh_local_port=0,
msgh_voucher_port=0,
msgh_id=(msgh.msgh_id + 100),
)

msg_body = MachMsgBody(
msgh_descriptor_count=32,
)

masks_cnt = 0

self.write_msg(
msg,
msg_header,
msg_body,
int_to_bytes(0, 8),
bytes(12 * 31),
int_to_bytes(0, 4),
int_to_bytes(0, 8),
int_to_bytes(masks_cnt, 4),
)

return const.KERN_SUCCESS

def _handle_semaphore_create(self, msg: int, msgh: MachMsgHeader) -> int:
# policy = self.emu.read_s32(msg_ptr + 0x20)
value = self.emu.read_s32(msg + 0x24)
Expand Down
7 changes: 7 additions & 0 deletions src/chomper/os/ios/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,10 @@ class Utsname(ctypes.Structure):
("version", ctypes.c_char * 256),
("machine", ctypes.c_char * 256),
]


class Rlimit(ctypes.Structure):
_fields_ = [
("rlim_cur", ctypes.c_uint64),
("rlim_max", ctypes.c_uint64),
]
Loading