Skip to content

Commit

Permalink
pythongh-83004: Harden msvcrt further
Browse files Browse the repository at this point in the history
  • Loading branch information
erlend-aasland committed Apr 10, 2023
1 parent c3cd3d1 commit 88bb327
Showing 1 changed file with 52 additions and 66 deletions.
118 changes: 52 additions & 66 deletions PC/msvcrtmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -577,92 +577,80 @@ static struct PyModuleDef msvcrtmodule = {
NULL
};

static void
insertint(PyObject *d, char *name, int value)
{
PyObject *v = PyLong_FromLong((long) value);
if (v == NULL) {
/* Don't bother reporting this error */
PyErr_Clear();
}
else {
PyDict_SetItemString(d, name, v);
Py_DECREF(v);
}
}

static void
insertptr(PyObject *d, char *name, void *value)
{
PyObject *v = PyLong_FromVoidPtr(value);
if (v == NULL) {
/* Don't bother reporting this error */
PyErr_Clear();
}
else {
PyDict_SetItemString(d, name, v);
Py_DECREF(v);
}
}
#define INSERTINT(MOD, NAME, VAL) do { \
if (PyModule_AddIntConstant(MOD, NAME, VAL) < 0) { \
goto error; \
} \
} while (0)

#define INSERTPTR(MOD, NAME, PTR) do { \
PyObject *v = PyLong_FromVoidPtr(PTR); \
if (v == NULL) { \
goto error; \
} \
int rc = PyModule_AddObjectRef(MOD, NAME, v); \
Py_DECREF(v); \
if (rc < 0) { \
goto error; \
} \
} while (0)

#define INSERTSTR(MOD, NAME, CONST) do { \
if (PyModule_AddStringConstant(MOD, NAME, CONST) < 0) { \
goto error; \
} \
} while (0)

PyMODINIT_FUNC
PyInit_msvcrt(void)
{
int st;
PyObject *m = PyModule_Create(&msvcrtmodule);
if (m == NULL) {
return NULL;
}
PyObject *d = PyModule_GetDict(m); // Borrowed ref.

/* constants for the locking() function's mode argument */
insertint(d, "LK_LOCK", _LK_LOCK);
insertint(d, "LK_NBLCK", _LK_NBLCK);
insertint(d, "LK_NBRLCK", _LK_NBRLCK);
insertint(d, "LK_RLCK", _LK_RLCK);
insertint(d, "LK_UNLCK", _LK_UNLCK);
INSERTINT(m, "LK_LOCK", _LK_LOCK);
INSERTINT(m, "LK_NBLCK", _LK_NBLCK);
INSERTINT(m, "LK_NBRLCK", _LK_NBRLCK);
INSERTINT(m, "LK_RLCK", _LK_RLCK);
INSERTINT(m, "LK_UNLCK", _LK_UNLCK);
#ifdef MS_WINDOWS_DESKTOP
insertint(d, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS);
insertint(d, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT);
insertint(d, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX);
insertint(d, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX);
INSERTINT(m, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS);
INSERTINT(m, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT);
INSERTINT(m, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX);
INSERTINT(m, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX);
#endif
#ifdef _DEBUG
insertint(d, "CRT_WARN", _CRT_WARN);
insertint(d, "CRT_ERROR", _CRT_ERROR);
insertint(d, "CRT_ASSERT", _CRT_ASSERT);
insertint(d, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG);
insertint(d, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE);
insertint(d, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW);
insertint(d, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE);
insertptr(d, "CRTDBG_FILE_STDERR", _CRTDBG_FILE_STDERR);
insertptr(d, "CRTDBG_FILE_STDOUT", _CRTDBG_FILE_STDOUT);
insertptr(d, "CRTDBG_REPORT_FILE", _CRTDBG_REPORT_FILE);
INSERTINT(m, "CRT_WARN", _CRT_WARN);
INSERTINT(m, "CRT_ERROR", _CRT_ERROR);
INSERTINT(m, "CRT_ASSERT", _CRT_ASSERT);
INSERTINT(m, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG);
INSERTINT(m, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE);
INSERTINT(m, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW);
INSERTINT(m, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE);
INSERTPTR(m, "CRTDBG_FILE_STDERR", _CRTDBG_FILE_STDERR);
INSERTPTR(m, "CRTDBG_FILE_STDOUT", _CRTDBG_FILE_STDOUT);
INSERTPTR(m, "CRTDBG_REPORT_FILE", _CRTDBG_REPORT_FILE);
#endif

#undef INSERTINT
#undef INSERTPTR

/* constants for the crt versions */
#ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
_VC_ASSEMBLY_PUBLICKEYTOKEN);
if (st < 0) {
goto error;
}
INSERTSTR(m, "VC_ASSEMBLY_PUBLICKEYTOKEN", _VC_ASSEMBLY_PUBLICKEYTOKEN);
#endif
#ifdef _CRT_ASSEMBLY_VERSION
st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
_CRT_ASSEMBLY_VERSION);
if (st < 0) {
goto error;
}
INSERTSTR(m, "CRT_ASSEMBLY_VERSION", _CRT_ASSEMBLY_VERSION);
#endif
#ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
__LIBRARIES_ASSEMBLY_NAME_PREFIX);
if (st < 0) {
goto error;
}
INSERTSTR(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
__LIBRARIES_ASSEMBLY_NAME_PREFIX);
#endif

#undef INSERTSTR

/* 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)
PyObject *version = PyUnicode_FromFormat("%d.%d.%d.%d",
Expand All @@ -673,14 +661,12 @@ PyInit_msvcrt(void)
if (version == NULL) {
goto error;
}
st = PyModule_AddObjectRef(m, "CRT_ASSEMBLY_VERSION", version);
int st = PyModule_AddObjectRef(m, "CRT_ASSEMBLY_VERSION", version);
Py_DECREF(version);
if (st < 0) {
goto error;
}
#endif
/* make compiler warning quiet if st is unused */
(void)st;

return m;

Expand Down

0 comments on commit 88bb327

Please sign in to comment.