From: Steve Dower Date: Fri, 9 Sep 2016 18:56:34 +0000 (-0700) Subject: Issue #24594: Validates persist parameter when opening MSI database X-Git-Tag: v3.6.0b1~188^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6ceda631af2717c271e0b5b2b05a036463764418;p=python Issue #24594: Validates persist parameter when opening MSI database --- diff --git a/Misc/NEWS b/Misc/NEWS index 86d499b29f..e4061d64d4 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -62,6 +62,8 @@ Core and Builtins Library ------- +- Issue #24594: Validates persist parameter when opening MSI database + - Issue #28047: Fixed calculation of line length used for the base64 CTE in the new email policies. diff --git a/PC/_msi.c b/PC/_msi.c index 86a5943106..9e7e36da71 100644 --- a/PC/_msi.c +++ b/PC/_msi.c @@ -955,6 +955,17 @@ static PyTypeObject msidb_Type = { 0, /*tp_is_gc*/ }; +#define Py_NOT_PERSIST(x, flag) \ + (x != (int)(flag) && \ + x != ((int)(flag) | MSIDBOPEN_PATCHFILE)) + +#define Py_INVALID_PERSIST(x) \ + (Py_NOT_PERSIST(x, MSIDBOPEN_READONLY) && \ + Py_NOT_PERSIST(x, MSIDBOPEN_TRANSACT) && \ + Py_NOT_PERSIST(x, MSIDBOPEN_DIRECT) && \ + Py_NOT_PERSIST(x, MSIDBOPEN_CREATE) && \ + Py_NOT_PERSIST(x, MSIDBOPEN_CREATEDIRECT)) + static PyObject* msiopendb(PyObject *obj, PyObject *args) { int status; @@ -962,11 +973,14 @@ static PyObject* msiopendb(PyObject *obj, PyObject *args) int persist; MSIHANDLE h; msiobj *result; - if (!PyArg_ParseTuple(args, "si:MSIOpenDatabase", &path, &persist)) return NULL; - - status = MsiOpenDatabase(path, (LPCSTR)persist, &h); + /* We need to validate that persist is a valid MSIDBOPEN_* value. Otherwise, + MsiOpenDatabase may treat the value as a pointer, leading to unexpected + behavior. */ + if (Py_INVALID_PERSIST(persist)) + return msierror(ERROR_INVALID_PARAMETER); + status = MsiOpenDatabase(path, (LPCSTR)persist, &h); if (status != ERROR_SUCCESS) return msierror(status);