Skip to content

Conversation

@ihexon
Copy link
Contributor

@ihexon ihexon commented Feb 11, 2026

BSD and Linux have different sockaddr_in layouts:

// LINUX-style
struct sockaddr_in {
    sa_family_t     sin_family;
    in_port_t       sin_port;
    struct in_addr  sin_addr;
};
// BSD-style
struct sockaddr_in {
    uint8_t	sin_len;
    sa_family_t	sin_family;
    in_port_t	sin_port;
    struct	in_addr sin_addr;
    char	sin_zero[8];
};

Without converting to LINUX-style, the sa_len field present in macOS(BSD-style) sockaddr is misinterpreted as part of sa_family by the guest kernel, causing accept() to return an invalid address family.

This crashes any guest program that inspects the peer address after accept():

$ strace /.bin/dropbearmulti dropbear -D /run/dropbear -p 2222 -r /run/dropbear/private.key -F -s -P /run/dropbear/dropbear.pid  

crash log (sa_family=0x210 which is invalid length):

  accept(3, {sa_family=0x210 /* AF_??? */, sa_data="\370\352\177\0\0\1\0\0\0\0\0\0\0\0"}, [128 => 16]) = 6                                                                                                                                                                                                             
  getpid()                                = 270                                                                                                                                                                                                                                                                        
  writev(2, [{iov_base="[270] Feb 11 03:42:36 ", iov_len=22}, {iov_base="Early exit: Failed lookup: Unrec"..., iov_len=72}], 2[270] Feb 11 03:42:36 Early exit: Failed lookup: Unrecognized address family or invalid length) = 94                                                                                     
  writev(2, [{iov_base="\n", iov_len=1}, {iov_base=NULL, iov_len=0}], 2                                                                                                                                                                                                                                                
  ) = 1                                                                                                                                                                                                                                                                                                                
  exit_group(1)                           = ?                                                                                                                                                                                                                                                                          
  +++ exited with 1 +++                           

This PR converts BSD sockaddr to Linux wire format to fix this issue

@ihexon ihexon changed the title libkrun: converts BSD sockaddr to Linux wire format to prevent invalid sa_family in guests TSI: converts BSD sockaddr to Linux wire format to prevent invalid sa_family in guests Feb 11, 2026
@ihexon ihexon changed the title TSI: converts BSD sockaddr to Linux wire format to prevent invalid sa_family in guests vsock/tsi: converts BSD sockaddr to Linux wire format to prevent invalid sa_family in guests Feb 11, 2026
Copy link
Collaborator

@mtjhrc mtjhrc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work, this actually fixes the test tsi-tcp-guest-listen on macOS (tests enabled in #524 ) Thanks!

The code is a bit messy, but the whole packet.rs file and TSI protocol is - we should probably manage the structs more explicitly.

@slp slp merged commit d4c0e9e into containers:main Feb 11, 2026
11 checks passed
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

Successfully merging this pull request may close these issues.

3 participants