Skip to content

Commit

Permalink
pythongh-100795: Don't call freeaddrinfo on failure. (pythonGH-101252)
Browse files Browse the repository at this point in the history
When getaddrinfo returns an error, the output pointer is in an unknown state
Don't call freeaddrinfo on it.  See the issue for discussion and details with
links to reasoning.  _Most_ libc getaddrinfo implementations never modify the
output pointer unless they are returning success.

(cherry picked from commit b724ac2)

Co-authored-by: Gregory P. Smith <greg@krypto.org>
Co-authored-by: Sergey G. Brester <github@sebres.de>
Co-authored-by: Oleg Iarygin <dralife@yandex.ru>
  • Loading branch information
3 people authored and miss-islington committed Jan 23, 2023
1 parent e24c73e commit a28aa6e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Avoid potential unexpected ``freeaddrinfo`` call (double free) in :mod:`socket`
when when a libc ``getaddrinfo()`` implementation leaves garbage in an output
pointer when returning an error. Original patch by Sergey G. Brester.
4 changes: 4 additions & 0 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,7 @@ setipaddr(const char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int
subsequent call to getaddrinfo() does not destroy the
outcome of the first call. */
if (error) {
res = NULL; // no-op, remind us that it is invalid; gh-100795
set_gaierror(error);
return -1;
}
Expand Down Expand Up @@ -1163,6 +1164,7 @@ setipaddr(const char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int
#endif
Py_END_ALLOW_THREADS
if (error) {
res = NULL; // no-op, remind us that it is invalid; gh-100795
set_gaierror(error);
return -1;
}
Expand Down Expand Up @@ -6514,6 +6516,7 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
error = getaddrinfo(hptr, pptr, &hints, &res0);
Py_END_ALLOW_THREADS
if (error) {
res0 = NULL; // gh-100795
set_gaierror(error);
goto err;
}
Expand Down Expand Up @@ -6608,6 +6611,7 @@ socket_getnameinfo(PyObject *self, PyObject *args)
error = getaddrinfo(hostp, pbuf, &hints, &res);
Py_END_ALLOW_THREADS
if (error) {
res = NULL; // gh-100795
set_gaierror(error);
goto fail;
}
Expand Down

0 comments on commit a28aa6e

Please sign in to comment.