Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring sockaddr Handling #3

Open
Yaxuan-w opened this issue Sep 11, 2024 · 0 comments
Open

Refactoring sockaddr Handling #3

Yaxuan-w opened this issue Sep 11, 2024 · 0 comments

Comments

@Yaxuan-w
Copy link
Member

Description:

This issue tracks the refactoring of sockaddr handling and addresses the problems encountered with accept_syscall.

Background:

Currently, sockaddr is initialized using the default Unix GenSockaddr borrowed from RustPOSIX, which has the largest memory space. This may cause issues with getpeername, as IPv4/IPv6 shouldn't have a path field.

It's been observed that byte-level operations should be preferred over struct-type checks.

In Linux, there are various sockaddr structures, such as sockaddr_in for IPv4 and sockaddr_in6 for IPv6. However, tracing through the PostgreSQL source code shows that PostgreSQL uses sockaddr_storage, which is cast to sockaddr when necessary. The key difference between sockaddr and sockaddr_storage is size, with sockaddr_storage being large enough to accommodate all sockaddr types.

PostgreSQL's accept syscall passes a sockaddr->family=0 with sock_len=128, making it impossible to determine the family based on size alone.

Problem:

When using accept, recvfrom, getsockname, or getpeername, issues arise because the wrong sockaddr family is being inferred. The system receives a NULL sockaddr, and the copy_out function from RustPOSIX doesn't perform any operations.

Current Patch

I made a temporary modification to accept_syscall to make RawPOSIX work for now:

  • Initialize a default GenSockaddr struct based on the sockaddr family received at the dispatcher stage.
  • Handle the UNIX path conversion within the syscall itself.
  • Additionally, in the copy_out function, the number of bytes to copy will be determined by comparing initaddrlen with the actual length of the structure (taking the minimum value), ensuring that no more than the reserved space is copied.

Proposed Further Solution:

To resolve this, I suggest:

  • Refining the GenSockAddr data structure. Directly allocate a buffer of size 128 bytes for syscalls like accept, recvfrom, getsockname, and getpeername.
  • Implement a new function: Pass pointers to avoid NULL values being sent to syscalls.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant