https://github.com/python/cpython/commit/daafacf0053e9c329b0f96447258f628dd0bd6f1
commit: daafacf0053e9c329b0f96447258f628dd0bd6f1
branch: main
author: Shamil <[email protected]>
committer: vstinner <[email protected]>
date: 2025-11-18T17:34:58+01:00
summary:

gh-42400: Fix buffer overflow in _Py_wrealpath() for very long paths (#141529)

Co-authored-by: Victor Stinner <[email protected]>

files:
A Misc/NEWS.d/next/Security/2025-11-13-22-31-56.gh-issue-42400.pqB5Kq.rst
M Python/fileutils.c

diff --git 
a/Misc/NEWS.d/next/Security/2025-11-13-22-31-56.gh-issue-42400.pqB5Kq.rst 
b/Misc/NEWS.d/next/Security/2025-11-13-22-31-56.gh-issue-42400.pqB5Kq.rst
new file mode 100644
index 00000000000000..17dc241aef91d6
--- /dev/null
+++ b/Misc/NEWS.d/next/Security/2025-11-13-22-31-56.gh-issue-42400.pqB5Kq.rst
@@ -0,0 +1,3 @@
+Fix buffer overflow in ``_Py_wrealpath()`` for paths exceeding ``MAXPATHLEN`` 
bytes
+by using dynamic memory allocation instead of fixed-size buffer.
+Patch by Shamil Abdulaev.
diff --git a/Python/fileutils.c b/Python/fileutils.c
index 93abd70a34d420..0c1766b8804500 100644
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -2118,7 +2118,6 @@ _Py_wrealpath(const wchar_t *path,
               wchar_t *resolved_path, size_t resolved_path_len)
 {
     char *cpath;
-    char cresolved_path[MAXPATHLEN];
     wchar_t *wresolved_path;
     char *res;
     size_t r;
@@ -2127,12 +2126,14 @@ _Py_wrealpath(const wchar_t *path,
         errno = EINVAL;
         return NULL;
     }
-    res = realpath(cpath, cresolved_path);
+    res = realpath(cpath, NULL);
     PyMem_RawFree(cpath);
     if (res == NULL)
         return NULL;
 
-    wresolved_path = Py_DecodeLocale(cresolved_path, &r);
+    wresolved_path = Py_DecodeLocale(res, &r);
+    free(res);
+
     if (wresolved_path == NULL) {
         errno = EINVAL;
         return NULL;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to