Skip to content

Commit

Permalink
Fix overwriting of the second argument (address) to connect(2) withou…
Browse files Browse the repository at this point in the history
…t restoring it, close #70

The code in connect_pre_handle() was overwriting the address parameter passed to the connect(2) system call, but it was not restoring the original value before the call returned. This could lead to issues if the caller of connect(2) was relying on the original address value.

To fix this, the code now saves the original address value in the socket_info struct, and then restores it in the connect_exiting_handle() function before the system call returns.
  • Loading branch information
hmgle committed Jul 30, 2024
1 parent 93fbbe9 commit 52d79e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
22 changes: 20 additions & 2 deletions graftcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,15 @@ void connect_pre_handle(struct proc_info *pinfp)
return;
}

if (dest_sa.sin_family == AF_INET) /* IPv4 */
if (dest_sa.sin_family == AF_INET) { /* IPv4 */
memcpy(si->dest_addr, &dest_sa, sizeof(dest_sa));
si->dest_addr_len = sizeof(dest_sa);
putdata(pinfp->pid, addr, (char *)&PROXY_SA, sizeof(PROXY_SA));
else /* IPv6 */
} else { /* IPv6 */
memcpy(si->dest_addr, &dest_sa6, sizeof(dest_sa6));
si->dest_addr_len = sizeof(dest_sa6);
putdata(pinfp->pid, addr, (char *)&PROXY_SA6, sizeof(PROXY_SA6));
}

char buf[1024] = { 0 };
strcpy(buf, dest_ip_addr_str);
Expand Down Expand Up @@ -306,6 +311,16 @@ void socket_exiting_handle(struct proc_info *pinfp, int fd)
add_socket_info(si);
}

void connect_exiting_handle(struct proc_info *pinfp)
{
int socket_fd = get_syscall_arg(pinfp->pid, 0);
struct socket_info *si = find_socket_info((socket_fd << 31) + pinfp->pid);
if (si == NULL || si->dest_addr_len == 0)
return;
long addr = get_syscall_arg(pinfp->pid, 1);
putdata(pinfp->pid, addr, si->dest_addr, si->dest_addr_len);
}

void do_child(struct graftcp_conf *conf, int argc, char **argv)
{
char *args[argc + 1];
Expand Down Expand Up @@ -409,6 +424,9 @@ int trace_syscall_exiting(struct proc_info *pinfp)
}
socket_exiting_handle(pinfp, child_ret);
break;
case SYS_connect:
connect_exiting_handle(pinfp);
break;
}
end:
pinfp->flags &= ~FLAG_INSYSCALL;
Expand Down
2 changes: 2 additions & 0 deletions graftcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ struct socket_info {
uint64_t magic_fd;
int domain;
int type;
size_t dest_addr_len;
char dest_addr[sizeof(struct sockaddr_in6)];
struct timeval conn_ti;
UT_hash_handle hh; /* makes this structure hashable */
};
Expand Down

0 comments on commit 52d79e2

Please sign in to comment.