Skip to content

Commit c7d9976

Browse files
[3.11] gh-104432: Use memcpy() to avoid misaligned loads (GH-104433) (#107356)
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 f01e4ce) Co-authored-by: Christopher Chavez <[email protected]>
1 parent 50d2613 commit c7d9976

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix potential unaligned memory access on C APIs involving returned sequences
2+
of ``char *`` pointers within the :mod:`grp` and :mod:`socket` modules. These
3+
were revealed using a ``-fsaniziter=alignment`` build on ARM macOS. Patch by
4+
Christopher Chavez.

Modules/grpmodule.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,14 @@ mkgrent(PyObject *module, struct group *p)
6565
Py_DECREF(v);
6666
return NULL;
6767
}
68-
for (member = p->gr_mem; *member != NULL; member++) {
69-
PyObject *x = PyUnicode_DecodeFSDefault(*member);
68+
for (member = p->gr_mem; ; member++) {
69+
char *group_member;
70+
// member can be misaligned
71+
memcpy(&group_member, member, sizeof(group_member));
72+
if (group_member == NULL) {
73+
break;
74+
}
75+
PyObject *x = PyUnicode_DecodeFSDefault(group_member);
7076
if (x == NULL || PyList_Append(w, x) != 0) {
7177
Py_XDECREF(x);
7278
Py_DECREF(w);

Modules/socketmodule.c

+17-5
Original file line numberDiff line numberDiff line change
@@ -5657,9 +5657,15 @@ gethost_common(struct hostent *h, struct sockaddr *addr, size_t alen, int af)
56575657

56585658
/* SF #1511317: h_aliases can be NULL */
56595659
if (h->h_aliases) {
5660-
for (pch = h->h_aliases; *pch != NULL; pch++) {
5660+
for (pch = h->h_aliases; ; pch++) {
56615661
int status;
5662-
tmp = PyUnicode_FromString(*pch);
5662+
char *host_alias;
5663+
// pch can be misaligned
5664+
memcpy(&host_alias, pch, sizeof(host_alias));
5665+
if (host_alias == NULL) {
5666+
break;
5667+
}
5668+
tmp = PyUnicode_FromString(host_alias);
56635669
if (tmp == NULL)
56645670
goto err;
56655671

@@ -5671,8 +5677,14 @@ gethost_common(struct hostent *h, struct sockaddr *addr, size_t alen, int af)
56715677
}
56725678
}
56735679

5674-
for (pch = h->h_addr_list; *pch != NULL; pch++) {
5680+
for (pch = h->h_addr_list; ; pch++) {
56755681
int status;
5682+
char *host_address;
5683+
// pch can be misaligned
5684+
memcpy(&host_address, pch, sizeof(host_address));
5685+
if (host_address == NULL) {
5686+
break;
5687+
}
56765688

56775689
switch (af) {
56785690

@@ -5684,7 +5696,7 @@ gethost_common(struct hostent *h, struct sockaddr *addr, size_t alen, int af)
56845696
#ifdef HAVE_SOCKADDR_SA_LEN
56855697
sin.sin_len = sizeof(sin);
56865698
#endif
5687-
memcpy(&sin.sin_addr, *pch, sizeof(sin.sin_addr));
5699+
memcpy(&sin.sin_addr, host_address, sizeof(sin.sin_addr));
56885700
tmp = make_ipv4_addr(&sin);
56895701

56905702
if (pch == h->h_addr_list && alen >= sizeof(sin))
@@ -5701,7 +5713,7 @@ gethost_common(struct hostent *h, struct sockaddr *addr, size_t alen, int af)
57015713
#ifdef HAVE_SOCKADDR_SA_LEN
57025714
sin6.sin6_len = sizeof(sin6);
57035715
#endif
5704-
memcpy(&sin6.sin6_addr, *pch, sizeof(sin6.sin6_addr));
5716+
memcpy(&sin6.sin6_addr, host_address, sizeof(sin6.sin6_addr));
57055717
tmp = make_ipv6_addr(&sin6);
57065718

57075719
if (pch == h->h_addr_list && alen >= sizeof(sin6))

0 commit comments

Comments
 (0)