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

sockaddr definition #489

Open
fordcars opened this issue Jan 16, 2022 · 3 comments
Open

sockaddr definition #489

fordcars opened this issue Jan 16, 2022 · 3 comments

Comments

@fordcars
Copy link

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!

@mtheall
Copy link
Contributor

mtheall commented Jan 16, 2022

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

@fordcars
Copy link
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!

@mtheall
Copy link
Contributor

mtheall commented Jan 16, 2022

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants