From 450ee9baab7af5f73c4c860dde5b42168e4513bb Mon Sep 17 00:00:00 2001 From: elicn Date: Tue, 9 Nov 2021 17:18:32 +0200 Subject: [PATCH] Fix getpeername and getsockname --- qiling/os/posix/syscall/socket.py | 60 ++++++++++++++++++------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/qiling/os/posix/syscall/socket.py b/qiling/os/posix/syscall/socket.py index 1f207ce14..6d3eac800 100644 --- a/qiling/os/posix/syscall/socket.py +++ b/qiling/os/posix/syscall/socket.py @@ -338,37 +338,49 @@ def ql_syscall_bind(ql: Qiling, bind_fd, bind_addr, bind_addrlen): return regreturn -def ql_syscall_getsockname(ql: Qiling, sockfd, addr, addrlenptr): - if 0 <= sockfd < NR_OPEN and ql.os.fd[sockfd] != 0: - host, port = ql.os.fd[sockfd].getsockname() - data = struct.pack("H", port) - data += ipaddress.ip_address(host).packed - addrlen = ql.mem.read(addrlenptr, 4) - addrlen = ql.unpack32(addrlen) - data = data[:addrlen] - ql.mem.write(addr, data) - regreturn = 0 +def ql_syscall_getsockname(ql: Qiling, sockfd: int, addr: int, addrlenptr: int): + if 0 <= sockfd < NR_OPEN: + socket = ql.os.fd[sockfd] + + if isinstance(socket, ql_socket): + host, port = socket.getpeername() + + data = struct.pack("H", port) + data += ipaddress.ip_address(host).packed + + addrlen = ql.mem.read_ptr(addrlenptr) + + ql.mem.write(addr, data[:addrlen]) + regreturn = 0 + else: + regreturn = -EPERM else: - regreturn = -1 + regreturn = -EPERM ql.log.debug("getsockname(%d, 0x%x, 0x%x) = %d" % (sockfd, addr, addrlenptr, regreturn)) return regreturn -def ql_syscall_getpeername(ql: Qiling, sockfd, addr, addrlenptr): - if 0 <= sockfd < NR_OPEN and ql.os.fd[sockfd] != 0: - host, port = ql.os.fd[sockfd].getpeername() - data = struct.pack("H", port) - data += ipaddress.ip_address(host).packed - addrlen = ql.mem.read(addrlenptr, 4) - addrlen = ql.unpack32(addrlen) - data = data[:addrlen] - ql.mem.write(addr, data) - regreturn = 0 +def ql_syscall_getpeername(ql: Qiling, sockfd: int, addr: int, addrlenptr: int): + if 0 <= sockfd < NR_OPEN: + socket = ql.os.fd[sockfd] + + if isinstance(socket, ql_socket): + host, port = socket.getpeername() + + data = struct.pack("H", port) + data += ipaddress.ip_address(host).packed + + addrlen = ql.mem.read_ptr(addrlenptr) + + ql.mem.write(addr, data[:addrlen]) + regreturn = 0 + else: + regreturn = -EPERM else: - regreturn = -1 + regreturn = -EPERM ql.log.debug("getpeername(%d, 0x%x, 0x%x) = %d" % (sockfd, addr, addrlenptr, regreturn)) return regreturn