bpo-31927: Fix bugs in socketmodule.c on NetBSD and other issues. (#4235)

* 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.
This commit is contained in:
Serhiy Storchaka 2017-11-09 18:00:38 +02:00 committed by GitHub
parent 0a2ff23fe6
commit d3187158c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 65 deletions

View File

@ -0,0 +1,3 @@
Fixed compilation of the socket module on NetBSD 8. Fixed assertion failure
or reading arbitrary data when parse a AF_BLUETOOTH address on NetBSD and
DragonFly BSD.

View File

@ -227,7 +227,7 @@ http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/net/getaddrinfo.c.diff?r1=1.82&
#endif
#if defined(__APPLE__) || defined(__CYGWIN__)
#if defined(__APPLE__) || defined(__CYGWIN__) || defined(__NetBSD__)
# include <sys/ioctl.h>
#endif
@ -1232,7 +1232,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:
@ -1271,11 +1271,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__)
@ -1284,20 +1284,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) {
@ -1313,7 +1313,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:
@ -1346,13 +1346,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) {
@ -1370,7 +1370,7 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
a->can_addr.tp.rx_id,
a->can_addr.tp.tx_id);
}
#endif
#endif /* CAN_ISOTP */
default:
{
return Py_BuildValue("O&", PyUnicode_DecodeFSDefault,
@ -1378,7 +1378,7 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
}
}
}
#endif
#endif /* AF_CAN && SIOCGIFNAME */
#ifdef PF_SYSTEM
case PF_SYSTEM:
@ -1389,13 +1389,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:
@ -1411,7 +1411,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... */
@ -1577,7 +1577,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
*len_ret = sizeof(*addr);
return 1;
}
#endif
#endif /* AF_NETLINK */
#if defined(AF_VSOCK)
case AF_VSOCK:
@ -1602,13 +1602,13 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
*len_ret = sizeof(*addr);
return 1;
}
#endif
#endif /* AF_VSOCK */
#ifdef AF_RDS
case AF_RDS:
/* RDS sockets use sockaddr_in: fall-through */
#endif
#endif /* AF_RDS */
case AF_INET:
{
@ -1690,7 +1690,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
*len_ret = sizeof *addr;
return 1;
}
#endif
#endif /* ENABLE_IPV6 */
#ifdef USE_BLUETOOTH
case AF_BLUETOOTH:
@ -1699,7 +1699,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));
@ -1719,7 +1719,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;
@ -1739,24 +1739,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;
}
@ -1764,7 +1764,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;
@ -1780,20 +1780,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;
@ -1847,7 +1847,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:
@ -1897,16 +1897,19 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
return 1;
}
#endif
#endif /* HAVE_LINUX_TIPC_H */
#ifdef AF_CAN
#if defined(CAN_RAW) && defined(CAN_BCM)
#if defined(AF_CAN) && defined(SIOCGIFINDEX)
case AF_CAN:
switch (s->sock_proto) {
#ifdef CAN_RAW
case CAN_RAW:
/* fall-through */
#endif
#ifdef CAN_BCM
case CAN_BCM:
#endif
#if defined(CAN_RAW) || defined(CAN_BCM)
{
struct sockaddr_can *addr;
PyObject *interfaceName;
@ -1944,7 +1947,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
Py_DECREF(interfaceName);
return 1;
}
#endif
#endif /* CAN_RAW || CAN_BCM */
#ifdef CAN_ISOTP
case CAN_ISOTP:
@ -1991,13 +1994,13 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
Py_DECREF(interfaceName);
return 1;
}
#endif
#endif /* CAN_ISOTP */
default:
PyErr_SetString(PyExc_OSError,
"getsockaddrarg: unsupported CAN protocol");
return 0;
}
#endif
#endif /* AF_CAN && SIOCGIFINDEX */
#ifdef PF_SYSTEM
case PF_SYSTEM:
@ -2049,19 +2052,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));
@ -2085,7 +2088,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
*len_ret = sizeof(*sa);
return 1;
}
#endif
#endif /* HAVE_SOCKADDR_ALG */
/* More cases here... */
@ -2120,7 +2123,7 @@ getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
*len_ret = sizeof (struct sockaddr_nl);
return 1;
}
#endif
#endif /* AF_NETLINK */
#if defined(AF_VSOCK)
case AF_VSOCK:
@ -2128,12 +2131,12 @@ getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
*len_ret = sizeof (struct sockaddr_vm);
return 1;
}
#endif
#endif /* AF_VSOCK */
#ifdef AF_RDS
case AF_RDS:
/* RDS sockets use sockaddr_in: fall-through */
#endif
#endif /* AF_RDS */
case AF_INET:
{
@ -2147,7 +2150,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:
@ -2168,7 +2171,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");
@ -2176,7 +2179,7 @@ getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
}
}
#endif
#endif /* USE_BLUETOOTH */
#ifdef HAVE_NETPACKET_PACKET_H
case AF_PACKET:
@ -2184,7 +2187,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:
@ -2192,7 +2195,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:
@ -2200,7 +2203,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:
@ -2209,20 +2212,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... */
@ -5288,7 +5291,7 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
int result;
#endif
#endif /* HAVE_GETHOSTBYNAME_R */
char *ap;
const char *ap;
int al;
int af;
@ -5361,7 +5364,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;
@ -5392,7 +5395,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;
@ -5427,7 +5430,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;
@ -5753,7 +5756,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;
@ -5846,7 +5849,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))];
@ -6076,7 +6079,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];

View File

@ -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