-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-111178: Fix getsockaddrarg() undefined behavior #131668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
88fc5c2
1aebf4c
9eb932c
533a478
9968c1e
a95b22d
18ed09e
99dcf45
c0d7d03
7178cfd
94f7b6f
7455bf5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
:mod:`socket`: Fix code parsing AF_BLUETOOTH socket addresses. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1521,11 +1521,15 @@ 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 /* __NetBSD__ || __DragonFly__ */ | ||
#elif defined(__FreeBSD__) | ||
char *node = _BT_HCI_MEMB(a, node); | ||
size_t len = strnlen(node, sizeof(_BT_HCI_MEMB(a, node))); | ||
return PyBytes_FromStringAndSize(node, (Py_ssize_t)len); | ||
#else | ||
PyObject *ret = NULL; | ||
ret = Py_BuildValue("i", _BT_HCI_MEMB(a, dev)); | ||
return ret; | ||
#endif /* !(__NetBSD__ || __DragonFly__) */ | ||
#endif | ||
} | ||
|
||
#if !defined(__FreeBSD__) | ||
|
@@ -2044,15 +2048,21 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args, | |
struct sockaddr_l2 *addr = &addrbuf->bt_l2; | ||
memset(addr, 0, sizeof(struct sockaddr_l2)); | ||
_BT_L2_MEMB(addr, family) = AF_BLUETOOTH; | ||
_BT_L2_MEMB(addr, bdaddr_type) = BDADDR_BREDR; | ||
if (!PyArg_ParseTuple(args, "si|iB", &straddr, | ||
&_BT_L2_MEMB(addr, psm), | ||
&_BT_L2_MEMB(addr, cid), | ||
&_BT_L2_MEMB(addr, bdaddr_type))) { | ||
unsigned short psm; | ||
unsigned short cid = 0; | ||
unsigned char bdaddr_type = BDADDR_BREDR; | ||
if (!PyArg_ParseTuple(args, "sH|HB", &straddr, | ||
&psm, | ||
&cid, | ||
&bdaddr_type)) { | ||
PyErr_Format(PyExc_OSError, | ||
"%s(): wrong format", caller); | ||
return 0; | ||
} | ||
_BT_L2_MEMB(addr, psm) = psm; | ||
_BT_L2_MEMB(addr, cid) = cid; | ||
_BT_L2_MEMB(addr, bdaddr_type) = bdaddr_type; | ||
|
||
if (setbdaddr(straddr, &_BT_L2_MEMB(addr, bdaddr)) < 0) | ||
return 0; | ||
|
||
|
@@ -2065,12 +2075,21 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args, | |
const char *straddr; | ||
struct sockaddr_rc *addr = &addrbuf->bt_rc; | ||
_BT_RC_MEMB(addr, family) = AF_BLUETOOTH; | ||
if (!PyArg_ParseTuple(args, "si", &straddr, | ||
&_BT_RC_MEMB(addr, channel))) { | ||
PyErr_Format(PyExc_OSError, | ||
"%s(): wrong format", caller); | ||
#ifdef MS_WINDOWS | ||
unsigned long channel = _BT_RC_MEMB(addr, channel); | ||
# define FORMAT_CHANNEL "k" | ||
#else | ||
unsigned char channel = _BT_RC_MEMB(addr, channel); | ||
# define FORMAT_CHANNEL "B" | ||
#endif | ||
if (!PyArg_ParseTuple(args, "s" FORMAT_CHANNEL, | ||
&straddr, &channel)) { | ||
PyErr_Format(PyExc_OSError, "%s(): wrong format", caller); | ||
return 0; | ||
} | ||
#undef FORMAT_CHANNEL | ||
_BT_RC_MEMB(addr, channel) = channel; | ||
|
||
if (setbdaddr(straddr, &_BT_RC_MEMB(addr, bdaddr)) < 0) | ||
return 0; | ||
|
||
|
@@ -2092,13 +2111,36 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args, | |
straddr = PyBytes_AS_STRING(args); | ||
if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0) | ||
return 0; | ||
#elif defined(__FreeBSD__) | ||
_BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; | ||
if (!PyBytes_Check(args)) { | ||
PyErr_Format(PyExc_OSError, "%s: " | ||
"wrong node format", caller); | ||
return 0; | ||
} | ||
const char *straddr = PyBytes_AS_STRING(args); | ||
size_t len = PyBytes_GET_SIZE(args); | ||
if (strlen(straddr) != len) { | ||
PyErr_Format(PyExc_OSError, "%s: " | ||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"node contains embedded null character", caller); | ||
return 0; | ||
} | ||
if (len > sizeof(_BT_HCI_MEMB(addr, node))) { | ||
PyErr_Format(PyExc_OSError, "%s: " | ||
"node too long", caller); | ||
return 0; | ||
} | ||
strncpy(_BT_HCI_MEMB(addr, node), straddr, | ||
sizeof(_BT_HCI_MEMB(addr, node))); | ||
#else /* __NetBSD__ || __DragonFly__ */ | ||
_BT_HCI_MEMB(addr, family) = AF_BLUETOOTH; | ||
if (!PyArg_ParseTuple(args, "i", &_BT_HCI_MEMB(addr, dev))) { | ||
unsigned short dev = _BT_HCI_MEMB(addr, dev); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is it defined? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a bunch of #if (defined(HAVE_BLUETOOTH_H) || defined(HAVE_BLUETOOTH_BLUETOOTH_H)) \
&& !defined(__NetBSD__) && !defined(__DragonFly__)
#define USE_BLUETOOTH 1
#if defined(__FreeBSD__)
...
#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
...
#elif defined(__NetBSD__) || defined(__DragonFly__) // <- unreachable
...
#define _BT_HCI_MEMB(sa, memb) ((sa)->bt_##memb)
#else
...
#define _BT_HCI_MEMB(sa, memb) ((sa)->hci_##memb)
...
#endif
#endif But AFAICT, the I think we should remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know about |
||
if (!PyArg_ParseTuple(args, "H", &dev)) { | ||
PyErr_Format(PyExc_OSError, | ||
"%s(): wrong format", caller); | ||
return 0; | ||
} | ||
_BT_HCI_MEMB(addr, dev) = dev; | ||
#endif /* !(__NetBSD__ || __DragonFly__) */ | ||
*len_ret = sizeof *addr; | ||
return 1; | ||
|
Uh oh!
There was an error while loading. Please reload this page.