Inconsistencies/possible bugs in if_indextoname.c, if_nametoindex.c under libc/network/

20 views
Skip to first unread message

Waldek Kozaczuk

unread,
Aug 13, 2020, 12:10:25 PM8/13/20
to OSv Development
As I have been working on trying to reduce files copied from musl, specifically those that use syscall by using the macro approach I described earlier, I have come across 3 files that use syscall but also use socket() call differently than musl.

And I think we have a problem in 2 of those.

In this commit https://github.com/cloudius-systems/osv/commit/c4df1043285b7597709f5199d619658909a22794, Nadav copied if_nameindex.c from musl into libc/network but also changed socket call like this:

<       int s = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
---
>       int s = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0);

because AF_UNIX domain is not supported in OSv (read the commit).

Now 2 other files - if_indextoname.c, if_nametoindex.c - have socket() call that uses AF_UNIX which most likely (we do not have unit tests for these) fails:

socket(AF_UNIX, SOCK_DGRAM, 0))

So I think we should change AF_UNIX to AF_INET in those as well, right?

Also it seems these 2 files were copied from older musl and new musl adds protocol option  SOCK_CLOEXEC and there is no reason we should have it different - see this diff between OSv and musl:

DIFF: [/network/if_indextoname.c]
6c6
< #include "syscall.h"
---
> #include "unistd.h"
13c13
<       if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0;
---
>       if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) return 0;
16c16
<       __syscall(SYS_close, fd);
---
>       close(fd);

Finally, I think we can solve the socket call changes (AF_UNIX->AF_INET) with --include libc/network/socket_mod.h option where this header looks like so:

#include <sys/socket.h>
#undef socket
#define socket(domain,type,protocol) socket(AF_INET,type,protocol)

We should also enhance existing tests/tst-ifaddrs.cc to test if_indextoname and if_nametoindex.

Waldek

Waldek Kozaczuk

unread,
Aug 13, 2020, 12:18:20 PM8/13/20
to OSv Development

Waldek Kozaczuk

unread,
Aug 15, 2020, 12:39:37 AM8/15/20
to OSv Development
Replacing AF_UNIX with AF_INET in those 2 files is not enough.

Firstly, if_nametoindex behaves weirdly:

--> (1st call)  Index of eth0: 1082589186, errno:2
--> (2nd call) Index of eth0: 2, errno:2

So it seems to fail in 2 consecutive calls each time and sets errno to ENENT but each time returns different index. Or maybe it does succeed but why return different index each time.

Now calling if_indextoname fails for both indexes and I think it happens because we do not handle SIOCGIFNAME in bsd/sys/compat/linux/linux_ioctl.cc, do we? 

Waldek

Reply all
Reply to author
Forward
0 new messages