From 18820c1f534438a717efab37964ef7156694bb1d Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sat, 8 Apr 2023 21:59:53 +0200 Subject: [PATCH 1/3] gh-83004: Harden msvcrt init --- PC/msvcrtmodule.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c index face4d03af9d4f..f06dc44cc4d1f6 100644 --- a/PC/msvcrtmodule.c +++ b/PC/msvcrtmodule.c @@ -609,7 +609,7 @@ PyMODINIT_FUNC PyInit_msvcrt(void) { int st; - PyObject *d, *version; + PyObject *d; PyObject *m = PyModule_Create(&msvcrtmodule); if (m == NULL) return NULL; @@ -659,11 +659,16 @@ PyInit_msvcrt(void) /* constants for the 2010 crt versions */ #if defined(_VC_CRT_MAJOR_VERSION) && defined (_VC_CRT_MINOR_VERSION) && defined(_VC_CRT_BUILD_VERSION) && defined(_VC_CRT_RBUILD_VERSION) - version = PyUnicode_FromFormat("%d.%d.%d.%d", _VC_CRT_MAJOR_VERSION, - _VC_CRT_MINOR_VERSION, - _VC_CRT_BUILD_VERSION, - _VC_CRT_RBUILD_VERSION); - st = PyModule_AddObject(m, "CRT_ASSEMBLY_VERSION", version); + PyObject *version = PyUnicode_FromFormat("%d.%d.%d.%d", + _VC_CRT_MAJOR_VERSION, + _VC_CRT_MINOR_VERSION, + _VC_CRT_BUILD_VERSION, + _VC_CRT_RBUILD_VERSION); + if (version == NULL) { + return NULL; + } + st = PyModule_AddObjectRef(m, "CRT_ASSEMBLY_VERSION", version); + Py_DECREF(version); if (st < 0) return NULL; #endif /* make compiler warning quiet if st is unused */ From 528221f5600a146024e71cdf97d42ea86603d4c6 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sun, 9 Apr 2023 00:27:11 +0200 Subject: [PATCH 2/3] Don't leak module object on error --- PC/msvcrtmodule.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c index f06dc44cc4d1f6..7ca452ed8f36bb 100644 --- a/PC/msvcrtmodule.c +++ b/PC/msvcrtmodule.c @@ -608,12 +608,11 @@ insertptr(PyObject *d, char *name, void *value) PyMODINIT_FUNC PyInit_msvcrt(void) { - int st; - PyObject *d; PyObject *m = PyModule_Create(&msvcrtmodule); - if (m == NULL) + if (m == NULL) { return NULL; - d = PyModule_GetDict(m); + } + PyObject *d = PyModule_GetDict(m); // Borrowed ref. /* constants for the locking() function's mode argument */ insertint(d, "LK_LOCK", _LK_LOCK); @@ -642,19 +641,25 @@ PyInit_msvcrt(void) /* constants for the crt versions */ #ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN - st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN", - _VC_ASSEMBLY_PUBLICKEYTOKEN); - if (st < 0) return NULL; + int st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN", + _VC_ASSEMBLY_PUBLICKEYTOKEN); + if (st < 0) { + goto error; + } #endif #ifdef _CRT_ASSEMBLY_VERSION st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION", _CRT_ASSEMBLY_VERSION); - if (st < 0) return NULL; + if (st < 0) { + goto error; + } #endif #ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX", __LIBRARIES_ASSEMBLY_NAME_PREFIX); - if (st < 0) return NULL; + if (st < 0) { + goto error; + } #endif /* constants for the 2010 crt versions */ @@ -665,14 +670,20 @@ PyInit_msvcrt(void) _VC_CRT_BUILD_VERSION, _VC_CRT_RBUILD_VERSION); if (version == NULL) { - return NULL; + goto error; } st = PyModule_AddObjectRef(m, "CRT_ASSEMBLY_VERSION", version); Py_DECREF(version); - if (st < 0) return NULL; + if (st < 0) { + goto error; + } #endif /* make compiler warning quiet if st is unused */ (void)st; return m; + +error: + Py_DECREF(m); + return NULL; } From ad93c2a9dd01b1e17334722497ec2a83b05c7550 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sun, 9 Apr 2023 00:36:48 +0200 Subject: [PATCH 3/3] Revert moving 'st' --- PC/msvcrtmodule.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c index 7ca452ed8f36bb..6e8b423c3839a9 100644 --- a/PC/msvcrtmodule.c +++ b/PC/msvcrtmodule.c @@ -608,6 +608,7 @@ insertptr(PyObject *d, char *name, void *value) PyMODINIT_FUNC PyInit_msvcrt(void) { + int st; PyObject *m = PyModule_Create(&msvcrtmodule); if (m == NULL) { return NULL; @@ -641,8 +642,8 @@ PyInit_msvcrt(void) /* constants for the crt versions */ #ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN - int st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN", - _VC_ASSEMBLY_PUBLICKEYTOKEN); + st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN", + _VC_ASSEMBLY_PUBLICKEYTOKEN); if (st < 0) { goto error; }