Skip to content

Commit 3b20d34

Browse files
izbyshevzooba
authored andcommitted
bpo-33016: Fix potential use of uninitialized memory in nt._getfinalpathname (#6010)
1 parent 3c7ac7e commit 3b20d34

File tree

2 files changed

+33
-42
lines changed

2 files changed

+33
-42
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix potential use of uninitialized memory in nt._getfinalpathname

Modules/posixmodule.c

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,6 @@ extern int lstat(const char *, struct stat *);
303303
#ifdef HAVE_PROCESS_H
304304
#include <process.h>
305305
#endif
306-
#ifndef VOLUME_NAME_DOS
307-
#define VOLUME_NAME_DOS 0x0
308-
#endif
309-
#ifndef VOLUME_NAME_NT
310-
#define VOLUME_NAME_NT 0x2
311-
#endif
312306
#ifndef IO_REPARSE_TAG_SYMLINK
313307
#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
314308
#endif
@@ -3731,11 +3725,10 @@ os__getfinalpathname_impl(PyObject *module, path_t *path)
37313725
/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
37323726
{
37333727
HANDLE hFile;
3734-
int buf_size;
3735-
wchar_t *target_path;
3728+
wchar_t buf[MAXPATHLEN], *target_path = buf;
3729+
int buf_size = Py_ARRAY_LENGTH(buf);
37363730
int result_length;
37373731
PyObject *result;
3738-
const char *err = NULL;
37393732

37403733
Py_BEGIN_ALLOW_THREADS
37413734
hFile = CreateFileW(
@@ -3747,55 +3740,52 @@ os__getfinalpathname_impl(PyObject *module, path_t *path)
37473740
/* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
37483741
FILE_FLAG_BACKUP_SEMANTICS,
37493742
NULL);
3743+
Py_END_ALLOW_THREADS
37503744

37513745
if (hFile == INVALID_HANDLE_VALUE) {
3752-
err = "CreateFileW";
3753-
goto done1;
3746+
return win32_error_object("CreateFileW", path->object);
37543747
}
37553748

37563749
/* We have a good handle to the target, use it to determine the
37573750
target path name. */
3758-
buf_size = GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
3751+
while (1) {
3752+
Py_BEGIN_ALLOW_THREADS
3753+
result_length = GetFinalPathNameByHandleW(hFile, target_path,
3754+
buf_size, VOLUME_NAME_DOS);
3755+
Py_END_ALLOW_THREADS
37593756

3760-
if (!buf_size) {
3761-
err = "GetFinalPathNameByHandle";
3762-
goto done1;
3763-
}
3764-
done1:
3765-
Py_END_ALLOW_THREADS
3766-
if (err)
3767-
return win32_error_object(err, path->object);
3757+
if (!result_length) {
3758+
result = win32_error_object("GetFinalPathNameByHandleW",
3759+
path->object);
3760+
goto cleanup;
3761+
}
37683762

3769-
target_path = PyMem_New(wchar_t, buf_size+1);
3770-
if(!target_path)
3771-
return PyErr_NoMemory();
3763+
if (result_length < buf_size) {
3764+
break;
3765+
}
37723766

3773-
Py_BEGIN_ALLOW_THREADS
3774-
result_length = GetFinalPathNameByHandleW(hFile, target_path,
3775-
buf_size, VOLUME_NAME_DOS);
3776-
if (!result_length) {
3777-
err = "GetFinalPathNameByHandle";
3778-
goto done2;
3779-
}
3767+
wchar_t *tmp;
3768+
tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
3769+
result_length * sizeof(*tmp));
3770+
if (!tmp) {
3771+
result = PyErr_NoMemory();
3772+
goto cleanup;
3773+
}
37803774

3781-
if (!CloseHandle(hFile)) {
3782-
err = "CloseHandle";
3783-
goto done2;
3784-
}
3785-
done2:
3786-
Py_END_ALLOW_THREADS
3787-
if (err) {
3788-
PyMem_Free(target_path);
3789-
return win32_error_object(err, path->object);
3775+
buf_size = result_length;
3776+
target_path = tmp;
37903777
}
37913778

3792-
target_path[result_length] = 0;
37933779
result = PyUnicode_FromWideChar(target_path, result_length);
3794-
PyMem_Free(target_path);
37953780
if (path->narrow)
37963781
Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
3797-
return result;
37983782

3783+
cleanup:
3784+
if (target_path != buf) {
3785+
PyMem_Free(target_path);
3786+
}
3787+
CloseHandle(hFile);
3788+
return result;
37993789
}
38003790

38013791
/*[clinic input]

0 commit comments

Comments
 (0)