diff options
author | Steve Dower <steve.dower@python.org> | 2019-11-18 19:32:46 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-18 19:32:46 (GMT) |
commit | 00923c63995e34cdc25d699478f113de99a69df9 (patch) | |
tree | 597e8346b14d890b99ddcd6ee8c3ef659f349dd2 /Modules | |
parent | 476e76f7cf78598146140c3bda48d53cda50707c (diff) | |
download | cpython-00923c63995e34cdc25d699478f113de99a69df9.zip cpython-00923c63995e34cdc25d699478f113de99a69df9.tar.gz cpython-00923c63995e34cdc25d699478f113de99a69df9.tar.bz2 |
bpo-38622: Add missing audit events for ctypes module (GH-17158)
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_ctypes/_ctypes.c | 21 | ||||
-rw-r--r-- | Modules/_ctypes/callproc.c | 51 |
2 files changed, 65 insertions, 7 deletions
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index b4a3fc4..d38d892 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -641,6 +641,12 @@ CDataType_from_buffer(PyObject *type, PyObject *args) return NULL; } + if (PySys_Audit("ctypes.cdata/buffer", "nnn", + (Py_ssize_t)buffer->buf, buffer->len, offset) < 0) { + Py_DECREF(mv); + return NULL; + } + result = PyCData_AtAddress(type, (char *)buffer->buf + offset); if (result == NULL) { Py_DECREF(mv); @@ -691,6 +697,12 @@ CDataType_from_buffer_copy(PyObject *type, PyObject *args) return NULL; } + if (PySys_Audit("ctypes.cdata/buffer", "nnn", + (Py_ssize_t)buffer.buf, buffer.len, offset) < 0) { + PyBuffer_Release(&buffer); + return NULL; + } + result = GenericPyCData_new((PyTypeObject *)type, NULL, NULL); if (result != NULL) { memcpy(((CDataObject *)result)->b_ptr, @@ -714,6 +726,9 @@ CDataType_in_dll(PyObject *type, PyObject *args) if (!PyArg_ParseTuple(args, "Os:in_dll", &dll, &name)) return NULL; + if (PySys_Audit("ctypes.dlsym", "O", args) < 0) { + return NULL; + } obj = PyObject_GetAttrString(dll, "_handle"); if (!obj) @@ -5535,6 +5550,9 @@ create_comerror(void) static PyObject * string_at(const char *ptr, int size) { + if (PySys_Audit("ctypes.string_at", "ni", (Py_ssize_t)ptr, size) < 0) { + return NULL; + } if (size == -1) return PyBytes_FromStringAndSize(ptr, strlen(ptr)); return PyBytes_FromStringAndSize(ptr, size); @@ -5626,6 +5644,9 @@ static PyObject * wstring_at(const wchar_t *ptr, int size) { Py_ssize_t ssize = size; + if (PySys_Audit("ctypes.wstring_at", "nn", (Py_ssize_t)ptr, ssize) < 0) { + return NULL; + } if (ssize == -1) ssize = wcslen(ptr); return PyUnicode_FromWideChar(ptr, ssize); diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c index a13c89f..7106014 100644 --- a/Modules/_ctypes/callproc.c +++ b/Modules/_ctypes/callproc.c @@ -199,8 +199,9 @@ set_error_internal(PyObject *self, PyObject *args, int index) PyObject *errobj; int *space; - if (!PyArg_ParseTuple(args, "i", &new_errno)) + if (!PyArg_ParseTuple(args, "i", &new_errno)) { return NULL; + } errobj = _ctypes_get_errobj(&space); if (errobj == NULL) return NULL; @@ -213,12 +214,18 @@ set_error_internal(PyObject *self, PyObject *args, int index) static PyObject * get_errno(PyObject *self, PyObject *args) { + if (PySys_Audit("ctypes.get_errno", NULL) < 0) { + return NULL; + } return get_error_internal(self, args, 0); } static PyObject * set_errno(PyObject *self, PyObject *args) { + if (PySys_Audit("ctypes.set_errno", "O", args) < 0) { + return NULL; + } return set_error_internal(self, args, 0); } @@ -227,12 +234,18 @@ set_errno(PyObject *self, PyObject *args) static PyObject * get_last_error(PyObject *self, PyObject *args) { + if (PySys_Audit("ctypes.get_last_error", NULL) < 0) { + return NULL; + } return get_error_internal(self, args, 1); } static PyObject * set_last_error(PyObject *self, PyObject *args) { + if (PySys_Audit("ctypes.set_last_error", "O", args) < 0) { + return NULL; + } return set_error_internal(self, args, 1); } @@ -262,6 +275,11 @@ static WCHAR *FormatError(DWORD code) #ifndef DONT_USE_SEH static void SetException(DWORD code, EXCEPTION_RECORD *pr) { + if (PySys_Audit("ctypes.seh_exception", "I", code) < 0) { + /* An exception was set by the audit hook */ + return; + } + /* The 'code' is a normal win32 error code so it could be handled by PyErr_SetFromWindowsErr(). However, for some errors, we have additional information not included in the error code. We handle those here and @@ -1427,6 +1445,9 @@ static PyObject *py_dl_sym(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "O&s:dlsym", &_parse_voidp, &handle, &name)) return NULL; + if (PySys_Audit("ctypes.dlsym/handle", "O", args) < 0) { + return NULL; + } ptr = ctypes_dlsym((void*)handle, name); if (!ptr) { PyErr_SetString(PyExc_OSError, @@ -1454,6 +1475,10 @@ call_function(PyObject *self, PyObject *args) &_parse_voidp, &func, &PyTuple_Type, &arguments)) return NULL; + if (PySys_Audit("ctypes.call_function", "nO", + (Py_ssize_t)func, arguments) < 0) { + return NULL; + } result = _ctypes_callproc((PPROC)func, arguments, @@ -1485,6 +1510,10 @@ call_cdeclfunction(PyObject *self, PyObject *args) &_parse_voidp, &func, &PyTuple_Type, &arguments)) return NULL; + if (PySys_Audit("ctypes.call_function", "nO", + (Py_ssize_t)func, arguments) < 0) { + return NULL; + } result = _ctypes_callproc((PPROC)func, arguments, @@ -1597,11 +1626,15 @@ static const char addressof_doc[] = static PyObject * addressof(PyObject *self, PyObject *obj) { - if (CDataObject_Check(obj)) - return PyLong_FromVoidPtr(((CDataObject *)obj)->b_ptr); - PyErr_SetString(PyExc_TypeError, - "invalid type"); - return NULL; + if (!CDataObject_Check(obj)) { + PyErr_SetString(PyExc_TypeError, + "invalid type"); + return NULL; + } + if (PySys_Audit("ctypes.addressof", "O", obj) < 0) { + return NULL; + } + return PyLong_FromVoidPtr(((CDataObject *)obj)->b_ptr); } static int @@ -1615,8 +1648,12 @@ static PyObject * My_PyObj_FromPtr(PyObject *self, PyObject *args) { PyObject *ob; - if (!PyArg_ParseTuple(args, "O&:PyObj_FromPtr", converter, &ob)) + if (!PyArg_ParseTuple(args, "O&:PyObj_FromPtr", converter, &ob)) { return NULL; + } + if (PySys_Audit("ctypes.PyObj_FromPtr", "O", ob) < 0) { + return NULL; + } Py_INCREF(ob); return ob; } |