Skip to content

sockaddr definition #489

Description

@carrotcreamsoup

In socket.h, sockaddr is defined as follows:

struct sockaddr {
	sa_family_t sa_family;
	char        sa_data[];
};

However, according to Linux man pages, it should be defined as follows:

struct sockaddr {
	sa_family_t sa_family;
	char        sa_data[14];
  }

This can cause out-of-bounds memory access when casting between sockaddr and sockaddr_in, which is often done. For reference, here is how sockaddr_in is defined in libctru in.h :

struct sockaddr_in {
	sa_family_t     sin_family;
	in_port_t       sin_port;
	struct in_addr  sin_addr;
	unsigned char   sin_zero[8];
};

I am no socket expert, so I'm not sure if this is intended or not.
Thank you!

Activity

  1. mtheall commented on Jan 16, 2022

    @mtheall
    Contributor

    You should never use sockaddr for storage; it's only for interface. Use sockaddr_in or sockaddr_storage.

  2. carrotcreamsoup commented on Jan 16, 2022

    @carrotcreamsoup
    Author

    You should never use sockaddr for storage; it's only for interface. Use sockaddr_in or sockaddr_storage.

    I think you are correct. I am currently porting a rather large networking library, and sockaddr is used as storage everywhere, so I wasn't sure. However, as you mention, the man pages say:

    The only purpose of this structure is to cast the structure pointer passed in addr in order to avoid compiler warnings.

    I guess the original authors wrongly used sockaddr as storage. However, this library has been used for at least a decade on a wide variety of systems and consoles; was it once considered acceptable to use sockaddr as storage?

    Thanks!

  3. mtheall commented on Jan 16, 2022

    @mtheall
    Contributor

    was it once considered acceptable to use sockaddr as storage?

    No. That's the whole purpose of sockaddr_storage. It's large enough to hold any type that socket functions which take a sockaddr* as a parameter.

    It would be reasonable libctru to make sockaddr as large as sockaddr_in since that's the only socket type it supports.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions