| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3441,7 +3441,7 @@ MODULE__CTYPES_DEPS=$(srcdir)/Modules/_ctypes/ctypes.h | |||
| 3441 | 3441 | MODULE__CTYPES_TEST_DEPS=$(srcdir)/Modules/_ctypes/_ctypes_test_generated.c.h | |
| 3442 | 3442 | MODULE__CTYPES_MALLOC_CLOSURE=@MODULE__CTYPES_MALLOC_CLOSURE@ | |
| 3443 | 3443 | MODULE__ELEMENTTREE_DEPS=$(srcdir)/Modules/pyexpat.c @LIBEXPAT_INTERNAL@ | |
| 3444 | - MODULE__HASHLIB_DEPS=$(srcdir)/Modules/hashlib.h | ||
| 3444 | + MODULE__HASHLIB_DEPS=$(srcdir)/Modules/hashlib.h $(srcdir)/Modules/_openssl_mem.h | ||
| 3445 | 3445 | MODULE__IO_DEPS=$(srcdir)/Modules/_io/_iomodule.h | |
| 3446 | 3446 | MODULE__REMOTE_DEBUGGING_DEPS=$(srcdir)/Modules/_remote_debugging/_remote_debugging.h $(srcdir)/Modules/_remote_debugging/gc_stats.h | |
| 3447 | 3447 | ||
@@ -3460,7 +3460,7 @@ MODULE__HMAC_DEPS=$(srcdir)/Modules/hashlib.h $(LIBHACL_HMAC_HEADERS) $(LIBHACL_ | |||
| 3460 | 3460 | MODULE__HMAC_LDEPS=$(LIBHACL_HMAC_LIB_@LIBHACL_LDEPS_LIBTYPE@) | |
| 3461 | 3461 | ||
| 3462 | 3462 | MODULE__SOCKET_DEPS=$(srcdir)/Modules/socketmodule.h $(srcdir)/Modules/addrinfo.h $(srcdir)/Modules/getaddrinfo.c $(srcdir)/Modules/getnameinfo.c | |
| 3463 | - MODULE__SSL_DEPS=$(srcdir)/Modules/_ssl.h $(srcdir)/Modules/_ssl/cert.c $(srcdir)/Modules/_ssl/debughelpers.c $(srcdir)/Modules/_ssl/misc.c $(srcdir)/Modules/_ssl_data_111.h $(srcdir)/Modules/_ssl_data_300.h $(srcdir)/Modules/socketmodule.h | ||
| 3463 | + MODULE__SSL_DEPS=$(srcdir)/Modules/_ssl.h $(srcdir)/Modules/_openssl_mem.h $(srcdir)/Modules/_ssl/cert.c $(srcdir)/Modules/_ssl/debughelpers.c $(srcdir)/Modules/_ssl/misc.c $(srcdir)/Modules/_ssl_data_111.h $(srcdir)/Modules/_ssl_data_300.h $(srcdir)/Modules/socketmodule.h | ||
| 3464 | 3464 | MODULE__TESTCAPI_DEPS=$(srcdir)/Modules/_testcapi/parts.h $(srcdir)/Modules/_testcapi/util.h | |
| 3465 | 3465 | MODULE__TESTLIMITEDCAPI_DEPS=$(srcdir)/Modules/_testlimitedcapi/testcapi_long.h $(srcdir)/Modules/_testlimitedcapi/parts.h $(srcdir)/Modules/_testlimitedcapi/util.h | |
| 3466 | 3466 | MODULE__TESTINTERNALCAPI_DEPS=$(srcdir)/Modules/_testinternalcapi/parts.h $(srcdir)/Parser/tokenizer/cursor.h $(srcdir)/Parser/tokenizer/source.h $(srcdir)/Python/ceval.h $(srcdir)/Modules/_testinternalcapi/test_targets.h $(srcdir)/Modules/_testinternalcapi/test_cases.c.h | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,4 @@ | |||
| 1 | + The :mod:`ssl` and :mod:`hashlib` modules now route OpenSSL memory | ||
| 2 | + allocations through the Python raw memory allocators, making OpenSSL memory | ||
| 3 | + usage visible to :mod:`tracemalloc` and to custom allocators installed with | ||
| 4 | + :c:func:`PyMem_SetAllocator`. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,6 +27,7 @@ | |||
| 27 | 27 | #include "pycore_strhex.h" // _Py_strhex() | |
| 28 | 28 | #include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_PTR_RELAXED | |
| 29 | 29 | #include "hashlib.h" | |
| 30 | + #include "_openssl_mem.h" | ||
| 30 | 31 | ||
| 31 | 32 | /* EVP is the preferred interface to hashing in OpenSSL */ | |
| 32 | 33 | #include <openssl/evp.h> | |
@@ -2933,5 +2934,6 @@ static struct PyModuleDef _hashlibmodule = { | |||
| 2933 | 2934 | PyMODINIT_FUNC | |
| 2934 | 2935 | PyInit__hashlib(void) | |
| 2935 | 2936 | { | |
| 2937 | + _PyOpenSSL_SetupMemFunctions(); | ||
| 2936 | 2938 | return PyModuleDef_Init(&_hashlibmodule); | |
| 2937 | 2939 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,56 @@ | |||
| 1 | + // Route OpenSSL allocations through the raw memory allocators. | ||
| 2 | + // Shared by the _ssl and _hashlib modules. | ||
| 3 | + | ||
| 4 | + #ifndef Py_OPENSSL_MEM_H | ||
| 5 | + #define Py_OPENSSL_MEM_H | ||
| 6 | + | ||
| 7 | + #include "Python.h" | ||
| 8 | + | ||
| 9 | + #include <openssl/crypto.h> // CRYPTO_set_mem_functions() | ||
| 10 | + | ||
| 11 | + // LibreSSL stubs out CRYPTO_set_mem_functions() and BoringSSL lacks it. | ||
| 12 | + // AWS-LC has it, but unlike OpenSSL it does not refuse to install hooks | ||
| 13 | + // after the first allocation, so earlier size-prefixed allocations would | ||
| 14 | + // be freed with the wrong allocator. | ||
| 15 | + #if !defined(LIBRESSL_VERSION_NUMBER) && !defined(OPENSSL_IS_BORINGSSL) \ | ||
| 16 | + && !defined(OPENSSL_IS_AWSLC) | ||
| 17 | + # define _Py_OPENSSL_CAN_SET_MEM_FUNCTIONS | ||
| 18 | + #endif | ||
| 19 | + | ||
| 20 | + #ifdef _Py_OPENSSL_CAN_SET_MEM_FUNCTIONS | ||
| 21 | + | ||
| 22 | + static void * | ||
| 23 | + _PyOpenSSL_Malloc(size_t size, const char *Py_UNUSED(file), | ||
| 24 | + int Py_UNUSED(line)) | ||
| 25 | + { | ||
| 26 | + return PyMem_RawMalloc(size); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + static void * | ||
| 30 | + _PyOpenSSL_Realloc(void *ptr, size_t size, const char *Py_UNUSED(file), | ||
| 31 | + int Py_UNUSED(line)) | ||
| 32 | + { | ||
| 33 | + return PyMem_RawRealloc(ptr, size); | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + static void | ||
| 37 | + _PyOpenSSL_Free(void *ptr, const char *Py_UNUSED(file), | ||
| 38 | + int Py_UNUSED(line)) | ||
| 39 | + { | ||
| 40 | + PyMem_RawFree(ptr); | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + #endif // _Py_OPENSSL_CAN_SET_MEM_FUNCTIONS | ||
| 44 | + | ||
| 45 | + static void | ||
| 46 | + _PyOpenSSL_SetupMemFunctions(void) | ||
| 47 | + { | ||
| 48 | + #ifdef _Py_OPENSSL_CAN_SET_MEM_FUNCTIONS | ||
| 49 | + // Fails if OpenSSL has already allocated memory (e.g. another | ||
| 50 | + // libcrypto user in the process); it then keeps its current allocator. | ||
| 51 | + (void)CRYPTO_set_mem_functions(_PyOpenSSL_Malloc, _PyOpenSSL_Realloc, | ||
| 52 | + _PyOpenSSL_Free); | ||
| 53 | + #endif | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + #endif // !Py_OPENSSL_MEM_H | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,6 +41,7 @@ | |||
| 41 | 41 | #endif | |
| 42 | 42 | ||
| 43 | 43 | #include "_ssl.h" | |
| 44 | + #include "_openssl_mem.h" | ||
| 44 | 45 | ||
| 45 | 46 | /* Redefined below for Windows debug builds after important #includes */ | |
| 46 | 47 | #define _PySSL_FIX_ERRNO | |
@@ -7447,5 +7448,6 @@ static struct PyModuleDef _sslmodule_def = { | |||
| 7447 | 7448 | PyMODINIT_FUNC | |
| 7448 | 7449 | PyInit__ssl(void) | |
| 7449 | 7450 | { | |
| 7451 | + _PyOpenSSL_SetupMemFunctions(); | ||
| 7450 | 7452 | return PyModuleDef_Init(&_sslmodule_def); | |
| 7451 | 7453 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -97,6 +97,9 @@ | |||
| 97 | 97 | <AdditionalDependencies>ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies> | |
| 98 | 98 | </Link> | |
| 99 | 99 | </ItemDefinitionGroup> | |
| 100 | + <ItemGroup> | ||
| 101 | + <ClInclude Include="..\Modules\_openssl_mem.h" /> | ||
| 102 | + </ItemGroup> | ||
| 100 | 103 | <ItemGroup> | |
| 101 | 104 | <ClCompile Include="..\Modules\_hashopenssl.c" /> | |
| 102 | 105 | </ItemGroup> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,9 @@ | |||
| 4 | 4 | <Filter Include="Source Files"> | |
| 5 | 5 | <UniqueIdentifier>{cc45963d-bd25-4eb8-bdba-a5507090bca4}</UniqueIdentifier> | |
| 6 | 6 | </Filter> | |
| 7 | + <Filter Include="Header Files"> | ||
| 8 | + <UniqueIdentifier>{5abcdd3e-a8bc-4833-949c-9477609092b9}</UniqueIdentifier> | ||
| 9 | + </Filter> | ||
| 7 | 10 | <Filter Include="Resource Files"> | |
| 8 | 11 | <UniqueIdentifier>{67630fa4-76e4-4035-bced-043a6df1e2e0}</UniqueIdentifier> | |
| 9 | 12 | </Filter> | |
@@ -13,6 +16,11 @@ | |||
| 13 | 16 | <Filter>Source Files</Filter> | |
| 14 | 17 | </ClCompile> | |
| 15 | 18 | </ItemGroup> | |
| 19 | + <ItemGroup> | ||
| 20 | + <ClInclude Include="..\Modules\_openssl_mem.h"> | ||
| 21 | + <Filter>Header Files</Filter> | ||
| 22 | + </ClInclude> | ||
| 23 | + </ItemGroup> | ||
| 16 | 24 | <ItemGroup> | |
| 17 | 25 | <ResourceCompile Include="..\PC\python_nt.rc"> | |
| 18 | 26 | <Filter>Resource Files</Filter> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -97,6 +97,9 @@ | |||
| 97 | 97 | <AdditionalDependencies>ws2_32.lib;crypt32.lib;%(AdditionalDependencies)</AdditionalDependencies> | |
| 98 | 98 | </Link> | |
| 99 | 99 | </ItemDefinitionGroup> | |
| 100 | + <ItemGroup> | ||
| 101 | + <ClInclude Include="..\Modules\_openssl_mem.h" /> | ||
| 102 | + </ItemGroup> | ||
| 100 | 103 | <ItemGroup> | |
| 101 | 104 | <ClCompile Include="..\Modules\_ssl.c" /> | |
| 102 | 105 | </ItemGroup> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,9 @@ | |||
| 4 | 4 | <Filter Include="Source Files"> | |
| 5 | 5 | <UniqueIdentifier>{695348f7-e9f6-4fe1-bc03-5f08ffc8095b}</UniqueIdentifier> | |
| 6 | 6 | </Filter> | |
| 7 | + <Filter Include="Header Files"> | ||
| 8 | + <UniqueIdentifier>{7c1bd5da-8912-4107-b6ac-f3930f2d90c7}</UniqueIdentifier> | ||
| 9 | + </Filter> | ||
| 7 | 10 | <Filter Include="Resource Files"> | |
| 8 | 11 | <UniqueIdentifier>{1b18a2e6-040d-46c7-a9ac-ac2ec64fb5d6}</UniqueIdentifier> | |
| 9 | 12 | </Filter> | |
@@ -13,6 +16,11 @@ | |||
| 13 | 16 | <Filter>Source Files</Filter> | |
| 14 | 17 | </ClCompile> | |
| 15 | 18 | </ItemGroup> | |
| 19 | + <ItemGroup> | ||
| 20 | + <ClInclude Include="..\Modules\_openssl_mem.h"> | ||
| 21 | + <Filter>Header Files</Filter> | ||
| 22 | + </ClInclude> | ||
| 23 | + </ItemGroup> | ||
| 16 | 24 | <ItemGroup> | |
| 17 | 25 | <ResourceCompile Include="..\PC\python_nt.rc"> | |
| 18 | 26 | <Filter>Resource Files</Filter> | |
| Back | FazBrowse Home | New Git URL |
0 commit comments