gh-135532: fortify DEBUG checks when fetching HACL*-based module state by picnixz · Pull Request #135844 · python/cpython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions Modules/blake2module.c
30 changes: 29 additions & 1 deletion Modules/hashlib.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
/* Common code for use by all hashlib related modules. */

#include "pycore_lock.h" // PyMutex
#include "pycore_lock.h" // PyMutex
#include "pycore_moduleobject.h" // _PyModule_GetDef()

#ifndef NDEBUG
/*
* Assert that a type cannot be subclassed and that
* its associated module definition matches 'moddef'.
*
* Use this helper to ensure that _PyType_GetModuleState() can be safely used.
*/
static inline void
_Py_hashlib_check_exported_type(PyTypeObject *type, PyModuleDef *moddef)
{
assert(type != NULL);
assert(moddef != NULL);
/* ensure that the type is a final heap type */
assert(PyType_Check(type));
assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
assert(!(type->tp_flags & Py_TPFLAGS_BASETYPE));
/* ensure that the associated module definition matches 'moddef' */
PyHeapTypeObject *ht = (PyHeapTypeObject *)type;
assert(ht->ht_module != NULL);
PyModuleDef *ht_moddef = _PyModule_GetDef(ht->ht_module);
assert(ht_moddef != NULL);
assert(ht_moddef == moddef);
}
#else
#define _Py_hashlib_check_exported_type(_TYPE, _MODDEF)
#endif

/*
* Given a PyObject* obj, fill in the Py_buffer* viewp with the result
Expand Down
19 changes: 11 additions & 8 deletions Modules/hmacmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

#include "Python.h"
#include "pycore_hashtable.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_strhex.h" // _Py_strhex()
#include "pycore_typeobject.h" // _PyType_GetModuleState()

/*
* Taken from blake2module.c. In the future, detection of SIMD support
Expand Down Expand Up @@ -250,6 +252,8 @@ typedef struct py_hmac_hinfo {

// --- HMAC module state ------------------------------------------------------

static struct PyModuleDef hmacmodule_def;

typedef struct hmacmodule_state {
_Py_hashtable_t *hinfo_table;
PyObject *unknown_hash_error;
Expand All @@ -265,15 +269,16 @@ typedef struct hmacmodule_state {
static inline hmacmodule_state *
get_hmacmodule_state(PyObject *module)
{
void *state = PyModule_GetState(module);
void *state = _PyModule_GetState(module);
assert(state != NULL);
return (hmacmodule_state *)state;
}

static inline hmacmodule_state *
get_hmacmodule_state_by_cls(PyTypeObject *cls)
{
void *state = PyType_GetModuleState(cls);
_Py_hashlib_check_exported_type(cls, &hmacmodule_def);
void *state = _PyType_GetModuleState(cls);
assert(state != NULL);
return (hmacmodule_state *)state;
}
Expand Down Expand Up @@ -301,13 +306,11 @@ typedef struct HMACObject {

/*[clinic input]
module _hmac
class _hmac.HMAC "HMACObject *" "clinic_state()->hmac_type"
class _hmac.HMAC "HMACObject *" "&PyType_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=c8bab73fde49ba8a]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=72bc06d6dc634770]*/

#define clinic_state() (get_hmacmodule_state_by_cls(Py_TYPE(self)))
#include "clinic/hmacmodule.c.h"
#undef clinic_state

// --- Helpers ----------------------------------------------------------------
//
Expand Down Expand Up @@ -1683,7 +1686,7 @@ static struct PyModuleDef_Slot hmacmodule_slots[] = {
{0, NULL} /* sentinel */
};

static struct PyModuleDef _hmacmodule = {
static struct PyModuleDef hmacmodule_def = {
PyModuleDef_HEAD_INIT,
.m_name = "_hmac",
.m_size = sizeof(hmacmodule_state),
Expand All @@ -1697,5 +1700,5 @@ static struct PyModuleDef _hmacmodule = {
PyMODINIT_FUNC
PyInit__hmac(void)
{
return PyModuleDef_Init(&_hmacmodule);
return PyModuleDef_Init(&hmacmodule_def);
}
45 changes: 29 additions & 16 deletions Modules/md5module.c
Loading