mirror of
https://github.com/python/cpython.git
synced 2025-08-11 12:29:34 +00:00
[3.12] gh-104432: Use memcpy()
to avoid misaligned loads (GH-104433) (#107355)
gh-104432: Use `memcpy()` to avoid misaligned loads (GH-104433)
Fix potential unaligned memory access on C APIs involving returned sequences
of `char *` pointers within the :mod:`grp` and :mod:`socket` modules. These
were revealed using a ``-fsaniziter=alignment`` build on ARM macOS.
(cherry picked from commit f01e4cedba
)
Co-authored-by: Christopher Chavez <chrischavez@gmx.us>
This commit is contained in:
parent
c580527d92
commit
5daf19d763
3 changed files with 29 additions and 7 deletions
|
@ -0,0 +1,4 @@
|
||||||
|
Fix potential unaligned memory access on C APIs involving returned sequences
|
||||||
|
of `char *` pointers within the :mod:`grp` and :mod:`socket` modules. These
|
||||||
|
were revealed using a ``-fsaniziter=alignment`` build on ARM macOS. Patch by
|
||||||
|
Christopher Chavez.
|
|
@ -65,8 +65,14 @@ mkgrent(PyObject *module, struct group *p)
|
||||||
Py_DECREF(v);
|
Py_DECREF(v);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
for (member = p->gr_mem; *member != NULL; member++) {
|
for (member = p->gr_mem; ; member++) {
|
||||||
PyObject *x = PyUnicode_DecodeFSDefault(*member);
|
char *group_member;
|
||||||
|
// member can be misaligned
|
||||||
|
memcpy(&group_member, member, sizeof(group_member));
|
||||||
|
if (group_member == NULL) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
PyObject *x = PyUnicode_DecodeFSDefault(group_member);
|
||||||
if (x == NULL || PyList_Append(w, x) != 0) {
|
if (x == NULL || PyList_Append(w, x) != 0) {
|
||||||
Py_XDECREF(x);
|
Py_XDECREF(x);
|
||||||
Py_DECREF(w);
|
Py_DECREF(w);
|
||||||
|
|
|
@ -5783,9 +5783,15 @@ gethost_common(socket_state *state, struct hostent *h, struct sockaddr *addr,
|
||||||
|
|
||||||
/* SF #1511317: h_aliases can be NULL */
|
/* SF #1511317: h_aliases can be NULL */
|
||||||
if (h->h_aliases) {
|
if (h->h_aliases) {
|
||||||
for (pch = h->h_aliases; *pch != NULL; pch++) {
|
for (pch = h->h_aliases; ; pch++) {
|
||||||
int status;
|
int status;
|
||||||
tmp = PyUnicode_FromString(*pch);
|
char *host_alias;
|
||||||
|
// pch can be misaligned
|
||||||
|
memcpy(&host_alias, pch, sizeof(host_alias));
|
||||||
|
if (host_alias == NULL) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
tmp = PyUnicode_FromString(host_alias);
|
||||||
if (tmp == NULL)
|
if (tmp == NULL)
|
||||||
goto err;
|
goto err;
|
||||||
|
|
||||||
|
@ -5797,8 +5803,14 @@ gethost_common(socket_state *state, struct hostent *h, struct sockaddr *addr,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (pch = h->h_addr_list; *pch != NULL; pch++) {
|
for (pch = h->h_addr_list; ; pch++) {
|
||||||
int status;
|
int status;
|
||||||
|
char *host_address;
|
||||||
|
// pch can be misaligned
|
||||||
|
memcpy(&host_address, pch, sizeof(host_address));
|
||||||
|
if (host_address == NULL) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
switch (af) {
|
switch (af) {
|
||||||
|
|
||||||
|
@ -5810,7 +5822,7 @@ gethost_common(socket_state *state, struct hostent *h, struct sockaddr *addr,
|
||||||
#ifdef HAVE_SOCKADDR_SA_LEN
|
#ifdef HAVE_SOCKADDR_SA_LEN
|
||||||
sin.sin_len = sizeof(sin);
|
sin.sin_len = sizeof(sin);
|
||||||
#endif
|
#endif
|
||||||
memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
|
memcpy(&sin.sin_addr, host_address, sizeof(sin.sin_addr));
|
||||||
tmp = make_ipv4_addr(&sin);
|
tmp = make_ipv4_addr(&sin);
|
||||||
|
|
||||||
if (pch == h->h_addr_list && alen >= sizeof(sin))
|
if (pch == h->h_addr_list && alen >= sizeof(sin))
|
||||||
|
@ -5827,7 +5839,7 @@ gethost_common(socket_state *state, struct hostent *h, struct sockaddr *addr,
|
||||||
#ifdef HAVE_SOCKADDR_SA_LEN
|
#ifdef HAVE_SOCKADDR_SA_LEN
|
||||||
sin6.sin6_len = sizeof(sin6);
|
sin6.sin6_len = sizeof(sin6);
|
||||||
#endif
|
#endif
|
||||||
memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
|
memcpy(&sin6.sin6_addr, host_address, sizeof(sin6.sin6_addr));
|
||||||
tmp = make_ipv6_addr(&sin6);
|
tmp = make_ipv6_addr(&sin6);
|
||||||
|
|
||||||
if (pch == h->h_addr_list && alen >= sizeof(sin6))
|
if (pch == h->h_addr_list && alen >= sizeof(sin6))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue