diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-11-09 19:40:29 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-09 19:40:29 (GMT) |
commit | 596286f8f3c8e53ef010d6298464775dc900a515 (patch) | |
tree | 7781deaa28ae6e2bd0cbcff14920279871b82c5c /Modules | |
parent | 726467616f6810a76d903683c0a734c1897ceae2 (diff) | |
download | cpython-596286f8f3c8e53ef010d6298464775dc900a515.zip cpython-596286f8f3c8e53ef010d6298464775dc900a515.tar.gz cpython-596286f8f3c8e53ef010d6298464775dc900a515.tar.bz2 |
[3.6] bpo-31927: Fix bugs in socketmodule.c on NetBSD and other issues. (GH-4235) (#4352)
* Fix compilation of the socket module on NetBSD 8.
* Fix the assertion failure or reading arbitrary data when parse
a AF_BLUETOOTH address on NetBSD and DragonFly BSD.
* Fix other potential errors and make the code more reliable.
(cherry picked from commit d3187158c09cf899e9849f335bdff10594209167)
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/socketmodule.c | 114 | ||||
-rw-r--r-- | Modules/socketmodule.h | 5 |
2 files changed, 61 insertions, 58 deletions
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index da84291..968dad0 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -240,7 +240,7 @@ http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/net/getaddrinfo.c.diff?r1=1.82& # include <ctype.h> #endif -#ifdef __APPLE__ +#if defined(__APPLE__) || defined(__NetBSD__) # include <sys/ioctl.h> #endif @@ -1250,7 +1250,7 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto) } return ret; } -#endif +#endif /* ENABLE_IPV6 */ #ifdef USE_BLUETOOTH case AF_BLUETOOTH: @@ -1289,11 +1289,11 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto) struct sockaddr_hci *a = (struct sockaddr_hci *) addr; #if defined(__NetBSD__) || defined(__DragonFly__) return makebdaddr(&_BT_HCI_MEMB(a, bdaddr)); -#else +#else /* __NetBSD__ || __DragonFly__ */ PyObject *ret = NULL; ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); return ret; -#endif +#endif /* !(__NetBSD__ || __DragonFly__) */ } #if !defined(__FreeBSD__) @@ -1302,20 +1302,20 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto) struct sockaddr_sco *a = (struct sockaddr_sco *) addr; return makebdaddr(&_BT_SCO_MEMB(a, bdaddr)); } -#endif +#endif /* !__FreeBSD__ */ default: PyErr_SetString(PyExc_ValueError, "Unknown Bluetooth protocol"); return NULL; } -#endif +#endif /* USE_BLUETOOTH */ #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFNAME) case AF_PACKET: { struct sockaddr_ll *a = (struct sockaddr_ll *)addr; - char *ifname = ""; + const char *ifname = ""; struct ifreq ifr; /* need to look up interface name give index */ if (a->sll_ifindex) { @@ -1331,7 +1331,7 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto) a->sll_addr, a->sll_halen); } -#endif +#endif /* HAVE_NETPACKET_PACKET_H && SIOCGIFNAME */ #ifdef HAVE_LINUX_TIPC_H case AF_TIPC: @@ -1364,13 +1364,13 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto) return NULL; } } -#endif +#endif /* HAVE_LINUX_TIPC_H */ -#ifdef AF_CAN +#if defined(AF_CAN) && defined(SIOCGIFNAME) case AF_CAN: { struct sockaddr_can *a = (struct sockaddr_can *)addr; - char *ifname = ""; + const char *ifname = ""; struct ifreq ifr; /* need to look up interface name given index */ if (a->can_ifindex) { @@ -1383,7 +1383,7 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto) ifname, a->can_family); } -#endif +#endif /* AF_CAN && SIOCGIFNAME */ #ifdef PF_SYSTEM case PF_SYSTEM: @@ -1394,13 +1394,13 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto) struct sockaddr_ctl *a = (struct sockaddr_ctl *)addr; return Py_BuildValue("(II)", a->sc_id, a->sc_unit); } -#endif +#endif /* SYSPROTO_CONTROL */ default: PyErr_SetString(PyExc_ValueError, "Invalid address type"); return 0; } -#endif +#endif /* PF_SYSTEM */ #ifdef HAVE_SOCKADDR_ALG case AF_ALG: @@ -1416,7 +1416,7 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto) a->salg_feat, a->salg_mask); } -#endif +#endif /* HAVE_SOCKADDR_ALG */ /* More cases here... */ @@ -1582,12 +1582,12 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args, *len_ret = sizeof(*addr); return 1; } -#endif +#endif /* AF_NETLINK */ #ifdef AF_RDS case AF_RDS: /* RDS sockets use sockaddr_in: fall-through */ -#endif +#endif /* AF_RDS */ case AF_INET: { @@ -1669,7 +1669,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args, *len_ret = sizeof *addr; return 1; } -#endif +#endif /* ENABLE_IPV6 */ #ifdef USE_BLUETOOTH case AF_BLUETOOTH: @@ -1678,7 +1678,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args, case BTPROTO_L2CAP: { struct sockaddr_l2 *addr; - char *straddr; + const char *straddr; addr = (struct sockaddr_l2 *)addr_ret; memset(addr, 0, sizeof(struct sockaddr_l2)); @@ -1698,7 +1698,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args, case BTPROTO_RFCOMM: { struct sockaddr_rc *addr; - char *straddr; + const char *straddr; addr = (struct sockaddr_rc *)addr_ret; _BT_RC_MEMB(addr, family) = AF_BLUETOOTH; @@ -1718,24 +1718,24 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args, { struct sockaddr_hci *addr = (struct sockaddr_hci *)addr_ret; #if defined(__NetBSD__) || defined(__DragonFly__) - char *straddr = PyBytes_AS_STRING(args); - - _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; - if (straddr == NULL) { + const char *straddr; + _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; + if (!PyBytes_Check(args)) { PyErr_SetString(PyExc_OSError, "getsockaddrarg: " "wrong format"); return 0; } + straddr = PyBytes_AS_STRING(args); if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0) return 0; -#else +#else /* __NetBSD__ || __DragonFly__ */ _BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { PyErr_SetString(PyExc_OSError, "getsockaddrarg: " "wrong format"); return 0; } -#endif +#endif /* !(__NetBSD__ || __DragonFly__) */ *len_ret = sizeof *addr; return 1; } @@ -1743,7 +1743,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args, case BTPROTO_SCO: { struct sockaddr_sco *addr; - char *straddr; + const char *straddr; addr = (struct sockaddr_sco *)addr_ret; _BT_SCO_MEMB(addr, family) = AF_BLUETOOTH; @@ -1759,20 +1759,20 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args, *len_ret = sizeof *addr; return 1; } -#endif +#endif /* !__FreeBSD__ */ default: PyErr_SetString(PyExc_OSError, "getsockaddrarg: unknown Bluetooth protocol"); return 0; } } -#endif +#endif /* USE_BLUETOOTH */ #if defined(HAVE_NETPACKET_PACKET_H) && defined(SIOCGIFINDEX) case AF_PACKET: { struct sockaddr_ll* addr; struct ifreq ifr; - char *interfaceName; + const char *interfaceName; int protoNumber; int hatype = 0; int pkttype = 0; @@ -1826,7 +1826,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args, PyBuffer_Release(&haddr); return 1; } -#endif +#endif /* HAVE_NETPACKET_PACKET_H && SIOCGIFINDEX */ #ifdef HAVE_LINUX_TIPC_H case AF_TIPC: @@ -1876,9 +1876,9 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args, return 1; } -#endif +#endif /* HAVE_LINUX_TIPC_H */ -#if defined(AF_CAN) && defined(CAN_RAW) && defined(CAN_BCM) +#if defined(AF_CAN) && defined(CAN_RAW) && defined(CAN_BCM) && defined(SIOCGIFINDEX) case AF_CAN: switch (s->sock_proto) { case CAN_RAW: @@ -1927,7 +1927,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args, "getsockaddrarg: unsupported CAN protocol"); return 0; } -#endif +#endif /* AF_CAN && CAN_RAW && CAN_BCM && SIOCGIFINDEX */ #ifdef PF_SYSTEM case PF_SYSTEM: @@ -1979,19 +1979,19 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args, *len_ret = sizeof(*addr); return 1; } -#endif +#endif /* SYSPROTO_CONTROL */ default: PyErr_SetString(PyExc_OSError, "getsockaddrarg: unsupported PF_SYSTEM protocol"); return 0; } -#endif +#endif /* PF_SYSTEM */ #ifdef HAVE_SOCKADDR_ALG case AF_ALG: { struct sockaddr_alg *sa; - char *type; - char *name; + const char *type; + const char *name; sa = (struct sockaddr_alg *)addr_ret; memset(sa, 0, sizeof(*sa)); @@ -2015,7 +2015,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args, *len_ret = sizeof(*sa); return 1; } -#endif +#endif /* HAVE_SOCKADDR_ALG */ /* More cases here... */ @@ -2050,12 +2050,12 @@ getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) *len_ret = sizeof (struct sockaddr_nl); return 1; } -#endif +#endif /* AF_NETLINK */ #ifdef AF_RDS case AF_RDS: /* RDS sockets use sockaddr_in: fall-through */ -#endif +#endif /* AF_RDS */ case AF_INET: { @@ -2069,7 +2069,7 @@ getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) *len_ret = sizeof (struct sockaddr_in6); return 1; } -#endif +#endif /* ENABLE_IPV6 */ #ifdef USE_BLUETOOTH case AF_BLUETOOTH: @@ -2090,7 +2090,7 @@ getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) case BTPROTO_SCO: *len_ret = sizeof (struct sockaddr_sco); return 1; -#endif +#endif /* !__FreeBSD__ */ default: PyErr_SetString(PyExc_OSError, "getsockaddrlen: " "unknown BT protocol"); @@ -2098,7 +2098,7 @@ getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) } } -#endif +#endif /* USE_BLUETOOTH */ #ifdef HAVE_NETPACKET_PACKET_H case AF_PACKET: @@ -2106,7 +2106,7 @@ getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) *len_ret = sizeof (struct sockaddr_ll); return 1; } -#endif +#endif /* HAVE_NETPACKET_PACKET_H */ #ifdef HAVE_LINUX_TIPC_H case AF_TIPC: @@ -2114,7 +2114,7 @@ getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) *len_ret = sizeof (struct sockaddr_tipc); return 1; } -#endif +#endif /* HAVE_LINUX_TIPC_H */ #ifdef AF_CAN case AF_CAN: @@ -2122,7 +2122,7 @@ getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) *len_ret = sizeof (struct sockaddr_can); return 1; } -#endif +#endif /* AF_CAN */ #ifdef PF_SYSTEM case PF_SYSTEM: @@ -2131,20 +2131,20 @@ getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret) case SYSPROTO_CONTROL: *len_ret = sizeof (struct sockaddr_ctl); return 1; -#endif +#endif /* SYSPROTO_CONTROL */ default: PyErr_SetString(PyExc_OSError, "getsockaddrlen: " "unknown PF_SYSTEM protocol"); return 0; } -#endif +#endif /* PF_SYSTEM */ #ifdef HAVE_SOCKADDR_ALG case AF_ALG: { *len_ret = sizeof (struct sockaddr_alg); return 1; } -#endif +#endif /* HAVE_SOCKADDR_ALG */ /* More cases here... */ @@ -5178,7 +5178,7 @@ socket_gethostbyaddr(PyObject *self, PyObject *args) int result; #endif #endif /* HAVE_GETHOSTBYNAME_R */ - char *ap; + const char *ap; int al; int af; @@ -5250,7 +5250,7 @@ for a host. The host argument is a string giving a host name or IP number."); static PyObject * socket_getservbyname(PyObject *self, PyObject *args) { - char *name, *proto=NULL; + const char *name, *proto=NULL; struct servent *sp; if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto)) return NULL; @@ -5281,7 +5281,7 @@ static PyObject * socket_getservbyport(PyObject *self, PyObject *args) { int port; - char *proto=NULL; + const char *proto=NULL; struct servent *sp; if (!PyArg_ParseTuple(args, "i|s:getservbyport", &port, &proto)) return NULL; @@ -5316,7 +5316,7 @@ otherwise any protocol will match."); static PyObject * socket_getprotobyname(PyObject *self, PyObject *args) { - char *name; + const char *name; struct protoent *sp; if (!PyArg_ParseTuple(args, "s:getprotobyname", &name)) return NULL; @@ -5614,7 +5614,7 @@ socket_inet_aton(PyObject *self, PyObject *args) /* Have to use inet_addr() instead */ unsigned int packed_addr; #endif - char *ip_addr; + const char *ip_addr; if (!PyArg_ParseTuple(args, "s:inet_aton", &ip_addr)) return NULL; @@ -5709,7 +5709,7 @@ static PyObject * socket_inet_pton(PyObject *self, PyObject *args) { int af; - char* ip; + const char* ip; int retval; #ifdef ENABLE_IPV6 char packed[Py_MAX(sizeof(struct in_addr), sizeof(struct in6_addr))]; @@ -6046,7 +6046,7 @@ socket_getnameinfo(PyObject *self, PyObject *args) { PyObject *sa = (PyObject *)NULL; int flags; - char *hostp; + const char *hostp; int port; unsigned int flowinfo, scope_id; char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; diff --git a/Modules/socketmodule.h b/Modules/socketmodule.h index 03f982b..af7c36a 100644 --- a/Modules/socketmodule.h +++ b/Modules/socketmodule.h @@ -80,7 +80,10 @@ typedef int socklen_t; #endif #ifdef HAVE_LINUX_CAN_H -#include <linux/can.h> +# include <linux/can.h> +#else +# undef AF_CAN +# undef PF_CAN #endif #ifdef HAVE_LINUX_CAN_RAW_H |