| 1 |
from ptrace.cpu_info import CPU_WORD_SIZE |
| 2 |
from ptrace.ctypes_tools import uint2int, formatWordHex, formatAddress |
| 3 |
from ptrace.signames import signalName |
| 4 |
from ctypes import c_int |
| 5 |
from ptrace.error import PTRACE_ERRORS, writeError |
| 6 |
from logging import getLogger, INFO |
| 7 |
from ptrace.func_arg import FunctionArgument |
| 8 |
from ptrace.syscall.posix_arg import ( |
| 9 |
formatMmapProt, formatAccessMode, formatOpenFlags, formatCloneFlags, formatDirFd, formatOpenMode) |
| 10 |
from ptrace.func_call import FunctionCall |
| 11 |
from ptrace.syscall.socketcall import (setupSocketCall, |
| 12 |
formatOptVal, formatSockaddr, formatSockaddrInStruct, formatSockaddrIn6Struct) |
| 13 |
from ptrace.syscall.socketcall_constants import SOCKETCALL |
| 14 |
import os |
| 15 |
import re |
| 16 |
|
| 17 |
from ptrace.os_tools import RUNNING_LINUX, RUNNING_FREEBSD |
| 18 |
from ptrace.syscall import FILENAME_ARGUMENTS |
| 19 |
from ptrace.syscall.socketcall_constants import formatSocketType |
| 20 |
if RUNNING_LINUX: |
| 21 |
from ptrace.syscall.linux_struct import ( |
| 22 |
timeval, timespec, pollfd, rlimit, new_utsname, user_desc) |
| 23 |
from ptrace.syscall.linux_constants import SYSCALL_ARG_DICT, FD_SETSIZE |
| 24 |
elif RUNNING_FREEBSD: |
| 25 |
from ptrace.syscall.freebsd_constants import SYSCALL_ARG_DICT |
| 26 |
else: |
| 27 |
SYSCALL_ARG_DICT = {} |
| 28 |
|
| 29 |
|
| 30 |
KNOWN_STRUCTS = [] |
| 31 |
if RUNNING_LINUX: |
| 32 |
KNOWN_STRUCTS.extend( |
| 33 |
(timeval, timespec, pollfd, rlimit, new_utsname, user_desc)) |
| 34 |
KNOWN_STRUCTS = dict((struct.__name__, struct) for struct in KNOWN_STRUCTS) |
| 35 |
|
| 36 |
ARGUMENT_CALLBACK = { |
| 37 |
# Prototype: callback(argument) -> str |
| 38 |
"access": {"mode": formatAccessMode}, |
| 39 |
"open": {"flags": formatOpenFlags, "mode": formatOpenMode}, |
| 40 |
"openat": {"dirfd": formatDirFd, "flags": formatOpenFlags, "mode": formatOpenMode}, |
| 41 |
"name_to_handle_at": {"dirfd": formatDirFd}, |
| 42 |
"mmap": {"prot": formatMmapProt}, |
| 43 |
"mmap2": {"prot": formatMmapProt}, |
| 44 |
"clone": {"flags": formatCloneFlags}, |
| 45 |
"socket": {"type": formatSocketType}, |
| 46 |
"setsockopt": {"optval": formatOptVal}, |
| 47 |
} |
| 48 |
|
| 49 |
POINTER_CALLBACK = { |
| 50 |
# Prototype: callback(argument, argtype) -> str |
| 51 |
"sockaddr": formatSockaddr, |
| 52 |
} |
| 53 |
|
| 54 |
STRUCT_CALLBACK = { |
| 55 |
# Prototype: callback(argument, attr_name, attr_value) -> str |
| 56 |
"sockaddr_in": formatSockaddrInStruct, |
| 57 |
"sockaddr_in6": formatSockaddrIn6Struct, |
| 58 |
} |
| 59 |
|
| 60 |
INTEGER_TYPES = set(( |
| 61 |
"int", "size_t", "clockid_t", "long", |
| 62 |
"socklen_t", "pid_t", "uid_t", "gid_t", |
| 63 |
)) |
| 64 |
|
| 65 |
|
| 66 |
def iterBits(data): |
| 67 |
for char in data: |
| 68 |
byte = ord(chr(char)) |
| 69 |
for index in range(8): |
| 70 |
yield ((byte >> index) & 1) == 1 |
| 71 |
|
| 72 |
|
| 73 |
class SyscallArgument(FunctionArgument): |
| 74 |
|
| 75 |
def createText(self): |
| 76 |
value = self.value |
| 77 |
argtype = self.type |
| 78 |
name = self.name |
| 79 |
if not argtype or not name: |
| 80 |
return formatWordHex(self.value) |
| 81 |
|
| 82 |
syscall = self.function.name |
| 83 |
|
| 84 |
# Special cases |
| 85 |
try: |
| 86 |
return SYSCALL_ARG_DICT[syscall][name][value] |
| 87 |
except KeyError: |
| 88 |
pass |
| 89 |
try: |
| 90 |
callback = ARGUMENT_CALLBACK[syscall][name] |
| 91 |
except KeyError: |
| 92 |
callback = None |
| 93 |
if callback: |
| 94 |
return callback(self) |
| 95 |
if syscall == "execve": |
| 96 |
if name in ("argv", "envp"): |
| 97 |
return self.readCStringArray(value) |
| 98 |
if syscall == "socketcall": |
| 99 |
if name == "call": |
| 100 |
try: |
| 101 |
return SOCKETCALL[value] |
| 102 |
except KeyError: |
| 103 |
return str(value) |
| 104 |
if name == "args": |
| 105 |
func_call = FunctionCall("socketcall", self.options) |
| 106 |
setupSocketCall(func_call, self.function.process, |
| 107 |
self.function[0], self.value) |
| 108 |
text = "<%s>" % func_call.format() |
| 109 |
return self.formatPointer(text, self.value) |
| 110 |
if syscall == "write" and name == "buf": |
| 111 |
fd = self.function[0].value |
| 112 |
if fd < 3: |
| 113 |
length = self.function[2].value |
| 114 |
return self.readString(value, length) |
| 115 |
if name == "signum": |
| 116 |
return signalName(value) |
| 117 |
|
| 118 |
# Remove "const " prefix |
| 119 |
if argtype.startswith("const "): |
| 120 |
argtype = argtype[6:] |
| 121 |
|
| 122 |
if name in FILENAME_ARGUMENTS and argtype == "char *": |
| 123 |
return self.readCString(value) |
| 124 |
|
| 125 |
# Format depending on the type |
| 126 |
if argtype.endswith("*"): |
| 127 |
try: |
| 128 |
# Strip in case there is a space between the name and '*' |
| 129 |
text = self.formatValuePointer(argtype[:-1].strip()) |
| 130 |
if text: |
| 131 |
return text |
| 132 |
except PTRACE_ERRORS as err: |
| 133 |
writeError( |
| 134 |
getLogger(), err, "Warning: Format %r value error" % self, log_level=INFO) |
| 135 |
return formatAddress(self.value) |
| 136 |
|
| 137 |
# Array like "int[2]" |
| 138 |
match = re.match(r"(.*)\[([0-9])+\]", argtype) |
| 139 |
if match: |
| 140 |
basetype = match.group(1) |
| 141 |
count = int(match.group(2)) |
| 142 |
if basetype == "int": |
| 143 |
return self.readArray(self.value, c_int, count) |
| 144 |
|
| 145 |
# Simple types |
| 146 |
if argtype in ("unsigned int", "unsigned long", "u32"): |
| 147 |
return str(self.value) |
| 148 |
if argtype in INTEGER_TYPES: |
| 149 |
return str(uint2int(self.value)) |
| 150 |
|
| 151 |
# Default formatter: hexadecimal |
| 152 |
return formatWordHex(self.value) |
| 153 |
|
| 154 |
def formatValuePointer(self, argtype): |
| 155 |
address = self.value |
| 156 |
|
| 157 |
if not address: |
| 158 |
return "NULL" |
| 159 |
if argtype.startswith("struct "): |
| 160 |
argtype = argtype[7:] |
| 161 |
|
| 162 |
# Try a callback |
| 163 |
try: |
| 164 |
callback = POINTER_CALLBACK[argtype] |
| 165 |
except KeyError: |
| 166 |
callback = None |
| 167 |
if callback: |
| 168 |
return callback(self, argtype) |
| 169 |
|
| 170 |
if argtype == "int": |
| 171 |
pointee = self.function.process.readStruct(address, c_int) |
| 172 |
return self.formatPointer("<%s>" % pointee, address) |
| 173 |
if argtype in KNOWN_STRUCTS: |
| 174 |
struct = KNOWN_STRUCTS[argtype] |
| 175 |
return self.readStruct(address, struct) |
| 176 |
if RUNNING_LINUX and argtype == "fd_set": |
| 177 |
# The function is either select or pselect, so arg[0] is nfds |
| 178 |
nfds = self.function.arguments[0].value |
| 179 |
fd_set = filter(lambda x: int(x) < nfds, self.readBits(address, FD_SETSIZE)) |
| 180 |
return self.formatPointer("[%s]" % " ".join(fd_set), address) |
| 181 |
|
| 182 |
syscall = self.function.name |
| 183 |
if syscall == "rt_sigprocmask" and argtype == "sigset_t": |
| 184 |
size = self.function["sigsetsize"].value * 8 |
| 185 |
|
| 186 |
def formatter(key): |
| 187 |
key += 1 |
| 188 |
return signalName(key) |
| 189 |
fd_set = self.readBits(address, size, format=formatter) |
| 190 |
return self.formatPointer("<sigset=(%s)>" % " ".join(fd_set), address) |
| 191 |
return None |
| 192 |
|
| 193 |
def readBits(self, address, count, format=str): |
| 194 |
bytes = self.function.process.readBytes(address, count // 8) |
| 195 |
fd_set = [format(index) |
| 196 |
for index, bit in enumerate(iterBits(bytes)) if bit] |
| 197 |
return fd_set |
| 198 |
|
| 199 |
def readCString(self, address): |
| 200 |
if address: |
| 201 |
max_size = self.options.string_max_length |
| 202 |
data, truncated = self.function.process.readCString( |
| 203 |
address, max_size) |
| 204 |
text = os.fsdecode(data) |
| 205 |
text = repr(text) |
| 206 |
if truncated: |
| 207 |
text += "..." |
| 208 |
else: |
| 209 |
text = "NULL" |
| 210 |
return self.formatPointer(text, address) |
| 211 |
|
| 212 |
def readString(self, address, size): |
| 213 |
if address: |
| 214 |
max_len = self.options.string_max_length |
| 215 |
truncated = (max_len < size) |
| 216 |
size = min(size, max_len) |
| 217 |
data = self.function.process.readBytes(address, size) |
| 218 |
text = os.fsdecode(data) |
| 219 |
text = repr(text) |
| 220 |
if truncated: |
| 221 |
text += "..." |
| 222 |
else: |
| 223 |
text = "NULL" |
| 224 |
return self.formatPointer(text, address) |
| 225 |
|
| 226 |
def readCStringArray(self, address): |
| 227 |
if not address: |
| 228 |
return "NULL" |
| 229 |
address0 = address |
| 230 |
max_count = self.options.max_array_count |
| 231 |
text = [] |
| 232 |
while True: |
| 233 |
str_addr = self.function.process.readWord(address) |
| 234 |
address += CPU_WORD_SIZE |
| 235 |
text.append(self.readCString(str_addr)) |
| 236 |
if not str_addr: |
| 237 |
break |
| 238 |
if max_count <= len(text): |
| 239 |
text.append("(... more than %s strings ...)" % max_count) |
| 240 |
break |
| 241 |
text = "<(%s)>" % ", ".join(text) |
| 242 |
return self.formatPointer(text, address0) |
| 243 |
|
| 244 |
def formatStructValue(self, struct, name, value): |
| 245 |
if struct in STRUCT_CALLBACK: |
| 246 |
callback = STRUCT_CALLBACK[struct] |
| 247 |
return callback(self, name, value) |
| 248 |
return None |