[Python-checkins] gh-100795: Don't call freeaddrinfo on failure. (GH-101252)

miss-islington webhook-mailer at python.org
Mon Jan 23 18:30:25 EST 2023


https://github.com/python/cpython/commit/5964b1282919b4f82fbe7f4afd28624a4564dc04
commit: 5964b1282919b4f82fbe7f4afd28624a4564dc04
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2023-01-23T15:30:19-08:00
summary:

gh-100795: Don't call freeaddrinfo on failure. (GH-101252)


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 b724ac2fe7fbb5a7a33d639cad8e748f17b325e0)

Co-authored-by: Gregory P. Smith <greg at krypto.org>
Co-authored-by: Sergey G. Brester <github at sebres.de>
Co-authored-by: Oleg Iarygin <dralife at yandex.ru>

files:
A Misc/NEWS.d/next/Library/2023-01-21-16-50-22.gh-issue-100795.NPMZf7.rst
M Modules/socketmodule.c

diff --git a/Misc/NEWS.d/next/Library/2023-01-21-16-50-22.gh-issue-100795.NPMZf7.rst b/Misc/NEWS.d/next/Library/2023-01-21-16-50-22.gh-issue-100795.NPMZf7.rst
new file mode 100644
index 000000000000..4cb56ea0f0e5
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-01-21-16-50-22.gh-issue-100795.NPMZf7.rst
@@ -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.
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 14af496a45f4..0e319a4cfd38 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -1078,6 +1078,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;
         }
@@ -1188,6 +1189,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;
     }
@@ -6608,6 +6610,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;
     }
@@ -6704,6 +6707,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;
     }



More information about the Python-checkins mailing list