[erlang-questions] unix domain sockets with abstract namespace: can't use all 108 bytes

Kenneth Lakin kennethlakin@REDACTED
Wed Apr 26 21:50:05 CEST 2017

Disclaimer: I've only read the man pages, I've not actually worked with
this stuff.

On 04/26/2017 01:55 AM, Raimo Niskanen wrote:
> Well, it is just because on all other Unix-like OS:es
> the address is (loosly/de-facto) defined to be zero
> terminated...

unix(7) on my Linux system [0] agrees that sockaddr_un.sun_path is
typically null-terminated. (Though, it sounds like abstract socket
addresses are always 108 bytes long, as embedded NULLs other than the
very first one are not treated specially.)

The BUGS section claims to describe two different portable methods to
retrieve the specified socket address when there might be exactly 108
bytes of address. The man page makes it really sound like Linux will Do
The Right Thing, as long as sun_path is no longer than 108 bytes.

So, just make sure that the Erlang string or binary containing the
socket address isn't longer than 108 bytes, fill sun_path (or the entire
sockaddr_un) with 0 before you copy the address in, and you should be fine?

I'm _fairly_ certain the second paragraph in the BUGS section is talking
about the behavior of non-Linux systems, and can be ignored when writing
Linux-specific code. Someone _please_ correct me if I'm wrong about that.

> Nobody has so far found a way to figure out how to use
> configure to detect if a size larger than the string length
> is OK or might crash the program some day.

As mentioned above, it _looks_ like sun_paths up to 108 bytes are A-OK.

> I can not find (in Posix) any clue to which address_len you shall use with
> a sockaddr_un structure. Is it the size of the whole structure[?]

Yeah, it _looks_ like it is the size of the whole structure.

The "Pathname sockets" section of the unix(7) man page suggests:

"or, more simply, addrlen can be specified as
 sizeof(struct sockaddr_un)."

The EXAMPLE section in bind(2) [1] as well as the EXAMPLE section in
unix(7) indicates that passing sizeof(struct sockaddr_un) as bind's
third argument is the correct way to specify addrlen to bind a Unix
domain socket.

_Additionally_, in the POSIX reference that you linked to in your
message, the man page for bind(2) [2] agrees with the Linux man page
that sizeof(struct sockaddr_un) is the correct thing to pass to bind's
third argument.

Or did I misunderstand, and you're talking about a different address
length parameter?

[0] http://man7.org/linux/man-pages/man7/unix.7.html
[1] http://man7.org/linux/man-pages/man2/bind.2.html
[2] http://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170426/dd318009/attachment.bin>

More information about the erlang-questions mailing list