Skip to content

Commit 3641d49

Browse files
committed
Write back the address in recvmsg
If the guest was requesting the address, it was never given!
1 parent 33ec008 commit 3641d49

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

lib/tinykvm/linux/system_calls.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1492,7 +1492,7 @@ void Machine::setup_linux_system_calls(bool unsafe_syscalls)
14921492
buffers, g_base, g_len);
14931493
bufcount += this_bufcount;
14941494
}
1495-
struct sockaddr addr {};
1495+
struct sockaddr_storage addr {};
14961496
struct msghdr msg_recv {};
14971497
msg_recv.msg_name = &addr;
14981498
msg_recv.msg_namelen = sizeof(addr);
@@ -1516,6 +1516,18 @@ void Machine::setup_linux_system_calls(bool unsafe_syscalls)
15161516
if (UNLIKELY(result < 0)) {
15171517
regs.rax = -errno;
15181518
} else {
1519+
if (msg.msg_name != nullptr && msg.msg_namelen > 0) {
1520+
// Write back the address if there is space
1521+
socklen_t& guest_addrlen = *cpu.machine().writable_memarray<socklen_t>(
1522+
g_msg + offsetof(struct msghdr, msg_namelen));
1523+
const address_t g_addr = (uintptr_t)msg.msg_name;
1524+
// Set/truncate the address length
1525+
guest_addrlen = std::min(guest_addrlen, msg_recv.msg_namelen);
1526+
if (g_addr != 0x0 && guest_addrlen > 0) {
1527+
// Write back the address
1528+
cpu.machine().copy_to_guest(g_addr, &addr, guest_addrlen);
1529+
}
1530+
}
15191531
regs.rax = result;
15201532
}
15211533
} catch (const std::exception& e) {

0 commit comments

Comments
 (0)