From 17580212b4483d32a2ce093ee2170d08dff979e8 Mon Sep 17 00:00:00 2001 From: David van Moolenbroek Date: Sat, 9 Jan 2016 13:34:13 +0000 Subject: [PATCH] libc: check raw IP socket type before using it Previously, the libc sendto(3) and recvfrom(3) implementations would blindly assume that any unrecognized socket is a raw-IP socket. This is not only inconsistent but also messes with returned error codes. Change-Id: Id0328f04ea8ca0968a4e8636bc441caa0c3579b7 --- minix/lib/libc/sys/recvfrom.c | 8 ++++++++ minix/lib/libc/sys/sendto.c | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/minix/lib/libc/sys/recvfrom.c b/minix/lib/libc/sys/recvfrom.c index 05a711839..b66e0a720 100644 --- a/minix/lib/libc/sys/recvfrom.c +++ b/minix/lib/libc/sys/recvfrom.c @@ -19,6 +19,7 @@ #include #include +#include #define DEBUG 0 @@ -42,6 +43,7 @@ ssize_t recvfrom(int sock, void *__restrict buffer, size_t length, int r; nwio_tcpconf_t tcpconf; nwio_udpopt_t udpopt; + nwio_ipopt_t ipopt; struct sockaddr_un uds_addr; int uds_sotype = -1; @@ -85,11 +87,17 @@ ssize_t recvfrom(int sock, void *__restrict buffer, size_t length, } } + r= ioctl(sock, NWIOGIPOPT, &ipopt); + if (r != -1 || errno != ENOTTY) { ip_hdr_t *ip_hdr; int rd; struct sockaddr_in sin; + if (r == -1) { + return r; + } + rd = read(sock, buffer, length); if(rd < 0) return rd; diff --git a/minix/lib/libc/sys/sendto.c b/minix/lib/libc/sys/sendto.c index ddfa8ae10..18d484df8 100644 --- a/minix/lib/libc/sys/sendto.c +++ b/minix/lib/libc/sys/sendto.c @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -37,6 +38,7 @@ ssize_t sendto(int sock, const void *message, size_t length, int flags, int r; nwio_tcpopt_t tcpopt; nwio_udpopt_t udpopt; + nwio_ipopt_t ipopt; int uds_sotype = -1; r= ioctl(sock, NWIOGTCPOPT, &tcpopt); @@ -75,12 +77,18 @@ ssize_t sendto(int sock, const void *message, size_t length, int flags, } } + r= ioctl(sock, NWIOGIPOPT, &ipopt); + if (r != -1 || errno != ENOTTY) { ip_hdr_t *ip_hdr; const struct sockaddr_in *sinp; ssize_t retval; int saved_errno; + if (r == -1) { + return r; + } + sinp = (const struct sockaddr_in *)dest_addr; if (sinp->sin_family != AF_INET) { -- 2.44.0