Skip to content

The code assumes the memory layout of SocketAddrV{4,6} #119

Closed

Description

@faern

This library casts std::net::SocketAddrV4 (and V6) into libc::sockaddr: https://github.com/rust-lang/socket2-rs/blob/b0f77842b3357dd571bb86b30b354bc55215f693/src/sockaddr.rs#L95-L115

As far as I can tell there are no guarantees from std about the layout of SocketAddrV{4,6}, and this code could silently compile and cause UB elsewhere if the representation changes.

This internals forum thread is where this discussion started: https://internals.rust-lang.org/t/why-are-socketaddrv4-socketaddrv6-based-on-low-level-sockaddr-in-6/13321

mio does the same kind of invalid casting: tokio-rs/mio#1386

Activity

  1. changed the title [-]The code invalidly assumes the memory layout of SocketAddrV{4,6}[/-] [+]The code assumes the memory layout of SocketAddrV{4,6}[/+] on Nov 6, 2020
  2. Thomasdezeeuw commented on Nov 6, 2020

    @Thomasdezeeuw
    Collaborator

    @Nemo157 made me aware of this in tokio-rs/mio#1386. I didn't know about rust-lang/rust#78802, but if that will proceed than I'll fix both Mio and socket2 in the same way.

  3. faern commented on Nov 6, 2020

    @faern
    ContributorAuthor

    I think this needs to be fixed no matter what happens with rust-lang/rust#78802. Because both these crates still rely on a non specified memory layout. Having these assumptions in commonly used crates makes it more troublesome for the standard library to evolve and change internal details that should not matter to the outside world.

  4. sfackler commented on Nov 6, 2020

    @sfackler
    Member

    For reference, this assumption was made because Alex wrote the implementation of std's SocketAddr and then also wrote the implementation of this library.

  5. faern commented on Nov 8, 2020

    @faern
    ContributorAuthor

    Fixed in #120

  6. faern commented on Nov 11, 2020

    @faern
    ContributorAuthor

    @Thomasdezeeuw Thanks for the fast and smooth collaboration on getting this fixed in all the crates you maintain! I know you said you wanted to get something more merged before releasing mio, but is there anything blocking releasing a patch release of socket2 0.3.x with this fix?

  7. de-vri-es commented on Nov 11, 2020

    @de-vri-es
    Contributor

    I think #117 needs to be merged before releasing, or Socket::new() would change to not setting the CLOEXEC flag. But that has some requests for changes and it needs to be rebased on #124 first, which itself isn't merged yet.

  8. Thomasdezeeuw commented on Nov 11, 2020

    @Thomasdezeeuw
    Collaborator

    @de-vri-es #117 and #124 are for the master branch which will become v0.4.x. @faern I'll prepare a release.

  9. Thomasdezeeuw commented on Nov 11, 2020

    @Thomasdezeeuw
    Collaborator

    @faern version v0.3.16 is released.

  10. added a commit that references this issue on Jan 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions