Skip to content

Commit b420601

Browse files
committed
Issue #24594: Validates persist parameter when opening MSI database
1 parent fc7e430 commit b420601

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎Misc/NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Core and Builtins
4242
Library
4343
-------
4444

45+
- Issue #24594: Validates persist parameter when opening MSI database
46+
4547
- Issue #27570: Avoid zero-length memcpy() etc calls with null source
4648
pointers in the "ctypes" and "array" modules.
4749

‎PC/_msi.c‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -938,18 +938,32 @@ static PyTypeObject msidb_Type = {
938938
0, /*tp_is_gc*/
939939
};
940940

941+
#define Py_NOT_PERSIST(x, flag) \
942+
(x != (int)(flag) && \
943+
x != ((int)(flag) | MSIDBOPEN_PATCHFILE))
944+
945+
#define Py_INVALID_PERSIST(x) \
946+
(Py_NOT_PERSIST(x, MSIDBOPEN_READONLY) && \
947+
Py_NOT_PERSIST(x, MSIDBOPEN_TRANSACT) && \
948+
Py_NOT_PERSIST(x, MSIDBOPEN_DIRECT) && \
949+
Py_NOT_PERSIST(x, MSIDBOPEN_CREATE) && \
950+
Py_NOT_PERSIST(x, MSIDBOPEN_CREATEDIRECT))
951+
941952
static PyObject* msiopendb(PyObject *obj, PyObject *args)
942953
{
943954
int status;
944955
char *path;
945956
int persist;
946957
MSIHANDLE h;
947958
msiobj *result;
948-
949959
if (!PyArg_ParseTuple(args, "si:MSIOpenDatabase", &path, &persist))
950960
return NULL;
951-
952-
status = MsiOpenDatabase(path, (LPCSTR)persist, &h);
961+
/* We need to validate that persist is a valid MSIDBOPEN_* value. Otherwise,
962+
MsiOpenDatabase may treat the value as a pointer, leading to unexpected
963+
behavior. */
964+
if (Py_INVALID_PERSIST(persist))
965+
return msierror(ERROR_INVALID_PARAMETER);
966+
status = MsiOpenDatabase(path, (LPCSTR)persist, &h);
953967
if (status != ERROR_SUCCESS)
954968
return msierror(status);
955969

0 commit comments

Comments
 (0)