[riot-notifications] [RIOT-OS/RIOT] POSIX TCP API: bad file descriptor (#11212)

Felipe Lalanne notifications at github.com
Tue Mar 19 22:47:00 CET 2019

please submit it to the security mailing-list security at riot-os.org.

If your issue is a question related to the usage of RIOT, please submit it to
the user mailing-list users at riot-os.org or to the developer mailing-list
devel at riot-os.org.

#### Description

I've been testing the POSIX TCP/IP implementation in RIOT and I've come up with a few problems for the same code depending on the platform.

For testing I've created a simple [echo server/client](https://github.com/niclabs/two/tree/echo-v2) combining the [posix sockets example](https://github.com/RIOT-OS/RIOT/tree/2019.01-branch/examples/posix_sockets) and the [LWIP TCP example](https://github.com/RIOT-OS/RIOT/tree/2019.01-branch/tests/lwip). I tested the code on linux and it runs without issue (see the branch [echo-v2-linux](https://github.com/niclabs/two/tree/echo-v2-linux)).

Example: Cannot build gnrc_networking application for samr21-xpro board.

#### Steps to reproduce the issue
Try to describe as precisely as possible here the steps required to reproduce
the issue. Here you can also describe your hardware configuration, the network
setup, etc.

Testing the code on different platforms I get the following

* On native
$  dist/tools/tapsetup/tapsetup -c 2
$ make all 

# For tap0 
$ PORT=tap0 make term
> ifconfig 
ET_00:  inet6 fe80::d864:daff:fed3:909
> echo server start 1234
Success: started TCP server on port 1234

# For tap1
$ PORT=tap0 make term
> echo send fe80::d864:daff:fed3:909 1234 hello
Success: connected to TCP server on port 1234
client send: Bad file descriptor

# If I enter the wrong port
> echo send fe80::d864:daff:fed3:909 12345 hello
Success: connected to TCP server on port 12345
client send: Bad file descriptor

* On samr21-xpro 
# For both boards
$ BOARD=samr21-xpro make all flash

# On board 1
$ BOARD=samr21-xpro PORT=<tty1> make term
> echo server start
INFO # Success: started TCP server on port 1234

# On board2
> echo send <addr> 1234 hello
INFO # Error in connect: Invalid argument
INFO # errno is unknown error: 22

# Same error with wrong port

* iotlab-m3 I get the same issue that with samr21-xpro

I figured that the error 22 was the result of a an uninitialized netif on the call to [_sockaddr_to_ep](https://github.com/RIOT-OS/RIOT/blob/9876e93b6b601411e0bf40e68e109f67100a284c/sys/posix/sockets/posix_sockets.c#L542) inside `_bind_connect` leading to a NULL return in [_netif_to_bind_addr](https://github.com/RIOT-OS/RIOT/blob/9876e93b6b601411e0bf40e68e109f67100a284c/pkg/lwip/contrib/sock/lwip_sock.c#L98)

However if y change the following code 
diff --git a/sys/posix/sockets/posix_sockets.c b/sys/posix/sockets/posix_sockets.c
index ae7195b32..8c7f16708 100644
--- a/sys/posix/sockets/posix_sockets.c
+++ b/sys/posix/sockets/posix_sockets.c
@@ -222,6 +222,7 @@ static int _sockaddr_to_ep(const struct sockaddr *address, socklen_t address_len
             struct sockaddr_in *in_addr = (struct sockaddr_in *)address;
             out->family = AF_INET;
+            out->netif = SOCK_ADDR_ANY_NETIF;
             out->addr.ipv4_u32 = in_addr->sin_addr.s_addr;
             out->port = ntohs(in_addr->sin_port);
@@ -233,6 +234,7 @@ static int _sockaddr_to_ep(const struct sockaddr *address, socklen_t address_len
             struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)address;
             out->family = AF_INET6;
+            out->netif = SOCK_ADDR_ANY_NETIF;
             memcpy(&out->addr.ipv6, &in6_addr->sin6_addr, sizeof(out->addr.ipv6));
             out->port = ntohs(in6_addr->sin6_port);

I get the "Bad file number" message on iotlab-m3 and a hard fault on samr21-xpro. 

Am I missing something obvious? I still don't have such a good grasp of the lwip API to see what is wrong with the code.

#### Expected results
Example: The gnrc_networking application builds on samr21-xpro.

Similar to the result on [echo-v2-linux](https://github.com/niclabs/two/tree/echo-v2-linux)

# Server
$ gcc echo.c main.c 
$ ./a.out server start 12345
Success: started TCP server on port 12345
TCP Client [::1]: 42963 connected
Received TCP data from client [::1]:42963:
Received: "hello"
Received TCP data from client [::1]:42963:
TCP connection to [::1]:42963 reset, starting to accept again

# Client 
$ ./a.out send ::1 12345 hello
Success: connected to TCP server on port 12345

#### Actual results
Please paste or specifically describe the actual output.
Bad file descriptor on iotlab-m3 and native, and  hardfault on samr21-xpro.

#### Versions
Operating system: Mac OSX, Linux, Vagrant VM
Build environment: GCC, CLang versions (you can run the following command from
the RIOT base directory: ./dist/tools/ci/print_toolchain_versions.sh).

Operating System Environment
       Operating System: Mac OS X 10.14.3
                 Kernel: Darwin 18.2.0 x86_64 i386

Installed compiler toolchains
             native gcc: Apple LLVM version 10.0.0 (clang-1000.10.44.4)
      arm-none-eabi-gcc: arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 8-2018-q4-major) 8.2.1 20181213 (release) [gcc-8-branch revision 267074]
                avr-gcc: missing
       mips-mti-elf-gcc: missing
             msp430-gcc: missing
   riscv-none-embed-gcc: missing
   xtensa-esp32-elf-gcc: missing
   xtensa-lx106-elf-gcc: missing
                  clang: Apple LLVM version 10.0.0 (clang-1000.10.44.4)

Installed compiler libs
   arm-none-eabi-newlib: "3.0.0"
    mips-mti-elf-newlib: missing
riscv-none-embed-newlib: missing
xtensa-esp32-elf-newlib: missing
xtensa-lx106-elf-newlib: missing
               avr-libc: missing (missing)

Installed development tools
                  cmake: missing
               cppcheck: missing
                doxygen: missing
                 flake8: missing
                    git: git version 2.17.2 (Apple Git-113)
                   make: GNU Make 3.81
                openocd: Open On-Chip Debugger 0.10.0+dev-00730-ge2430759 (2019-03-06-17:21)
                 python: Python 3.7.2
                python2: missing
                python3: Python 3.7.2
             coccinelle: missing

<!-- Thanks for contributing! -->

You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20190319/4c6230dc/attachment.html>

More information about the notifications mailing list