]> xenbits.xensource.com Git - unikraft/libs/lwip.git/commitdiff
sockets.c: fix crash when handling invalid/unsupported FDs
authorHugo Lefeuvre <hugo.lefeuvre@manchester.ac.uk>
Fri, 26 Nov 2021 14:23:11 +0000 (15:23 +0100)
committerUnikraft <monkey@unikraft.io>
Mon, 29 Nov 2021 16:28:59 +0000 (16:28 +0000)
aee924b [0] introduce a bug where any return value of sock_net_file_get
that is an error code but *not -EBADF* will return in a crash.

Assume a situation where sock_net_file_get(i) returns an error-encoded
file that is not -EBADF (e.g., -EINVAL): that file will go through the

if (PTR2ERR(file) == -EBADF)

check and pass it, moving on to

if (maxfd < file->sock_fd)

where the error-encoded pointer is dereferenced, resulting in a crash.

[0] https://github.com/hlef/lib-lwip/commit/aee924ba854034f8b085a4298

Signed-off-by: Hugo Lefeuvre <hugo.lefeuvre@manchester.ac.uk>
Reviewed-by: Cezar Craciunoiu <cezar.craciunoiu@gmail.com>
Approved-by: Simon Kuenzer <simon.kuenzer@neclab.eu>
Tested-by: Unikraft CI <monkey@unikraft.io>
GitHub-Pull-Request: #14

sockets.c

index 8b00711e823cf3137f1d67ec2926962b44b960cc..7db82c7fdccac0b56d8a5c8de028e5985876baa9 100644 (file)
--- a/sockets.c
+++ b/sockets.c
@@ -538,14 +538,13 @@ int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
                                        FD_CLR(i, readfds);
                                        continue;
                                }
-#else
+#endif
                                LWIP_DEBUGF(SOCKETS_DEBUG,
                                            ("failed to identify socket descriptor\n"));
                                ret = -1;
                                /* Setting the errno */
                                SOCK_NET_SET_ERRNO(PTR2ERR(file));
                                goto EXIT;
-#endif
                        }
                        if (maxfd < file->sock_fd)
                                maxfd = file->sock_fd;
@@ -561,14 +560,13 @@ int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
                                        FD_CLR(i, writefds);
                                        continue;
                                }
-#else
+#endif
                                LWIP_DEBUGF(SOCKETS_DEBUG,
                                            ("failed to identify socket descriptor\n"));
                                ret = -1;
                                /* Setting the errno */
                                SOCK_NET_SET_ERRNO(PTR2ERR(file));
                                goto EXIT;
-#endif
                        }
                        if (maxfd < file->sock_fd)
                                maxfd = file->sock_fd;
@@ -584,14 +582,13 @@ int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
                                        FD_CLR(i, exceptfds);
                                        continue;
                                }
-#else
+#endif
                                LWIP_DEBUGF(SOCKETS_DEBUG,
                                            ("failed to identify socket descriptor\n"));
                                ret = -1;
                                /* Setting the errno */
                                SOCK_NET_SET_ERRNO(PTR2ERR(file));
                                goto EXIT;
-#endif
                        }
                        if (maxfd < file->sock_fd)
                                maxfd = file->sock_fd;