Re: struct sockaddr_storage, union (was: Improve getsockname)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 1/19/23 22:38, Bastien Roucariès wrote:
Le jeudi 19 janvier 2023, 21:19:49 UTC Alejandro Colomar a écrit :

On 1/19/23 22:00, Bastien Roucariès wrote:
[...]

<https://inbox.sourceware.org/libc-alpha/0f25d60f-f183-b518-b6c1-6d46aa63ee57@xxxxxxxxx/T/>

I do not believe it is broken by design. It should be used with care and warning.

BTW if we go to the anonymous union way could we add at the end a _null_reserved_field. It will help for unix socket and the infamous sun_path could not end with null...
May be it is too late from an ABI point of view, but for me the posix contract from an ABI point of view is that I said in the note  sockaddr_storage  could grow but not be reduced.

Yes, many types have seen such additions at the end of it over time.  In the
Linux man-pages, I try to document all structures as "having at least these
members", but may grow over time.

In fact it is not needed and it is the best argument of struct sockaddr_storage
  printf("%li %li",sizeof(struct sockaddr_storage),sizeof(struct sockaddr_un));
give me 128 vs 110...

So if correctly documented and aliasing solved it will be the best of the world...

Moreover kernel expect it https://elixir.bootlin.com/linux/latest/source/net/unix/af_unix.c#L293

However... Considering that most APIs use `struct sockaddr *`, this wouldn't allow the internal libc implementation of functions like getnameinfo(3) to be free of UB.

Maybe a better thing would be to do the following:


struct sockaddr {
	union {
		struct {
			sa_family_t  sa_family;
			char         sa_data[];
		};
		struct sockaddr_in   sin;
		struct sockaddr_in6  sin6;
		struct sockaddr_un   sun;
	};
};

struct sockaddr_storage {
	union {
		sa_family_t          ss_family;
		struct sockaddr      sa;
	};
};


This makes sockaddr and sockaddr_storage have the same size, and also both can alias any of the children types through the unions, so one can use either of them for the same purpose.

I'll be sending a patch soon for discussion.

Cheers,

Alex




   struct sockaddr_storage {
	union {
   		sa_family_t          ss_family;
   		struct sockaddr      sa;
   		struct sockaddr_in   sin;
   		struct sockaddr_in6  sin6;
   		struct sockaddr_un   sun;
   	};
                         char __reserved_null;


--
<http://www.alejandro-colomar.es/>

Attachment: OpenPGP_signature
Description: OpenPGP digital signature


[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux