Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in OpenSSL relocation patch #11552

Closed
ashie opened this issue Apr 24, 2022 · 2 comments · Fixed by #11555
Closed

Memory leak in OpenSSL relocation patch #11552

ashie opened this issue Apr 24, 2022 · 2 comments · Fixed by #11555

Comments

@ashie
Copy link
Contributor

ashie commented Apr 24, 2022

I found a memory leak in MINGW's OpenSSL.
It causes every time just only calling X509_STORE_set_default_paths().

Here is the sample code:

#include <openssl/x509v3.h>

int
main(int argc, char *argv[])
{
  X509_STORE *store = X509_STORE_new();
  X509_STORE_set_default_paths(store);
  X509_STORE_free(store);
  return 0;
}

And here is the analyzed log by Dr. Memory:

> "C:\Program Files (x86)\Dr. Memory\bin\drmemory.exe" -leaks_only -- a.exe
Dr. Memory version 2.5.0 build 0 built on Oct 18 2021 03:01:22
Windows version: WinVer=105;Rel=2009;Build=19044;Edition=Professional
Dr. Memory results for pid 20624: "a.exe"
Application cmdline: "a.exe"
Recorded 124 suppression(s) from default C:\Program Files (x86)\Dr. Memory\bin64\suppress-default.txt

Error #1: LEAK 121 direct bytes 0x0000022a61f90600-0x0000022a61f90679 + 0 indirect bytes
# 0 replace_malloc                             [d:\a\drmemory\drmemory\common\alloc_replace.c:2580]
# 1 libcrypto-1_1-x64.dll!simplify_path
# 2 libcrypto-1_1-x64.dll!malloc_copy_string
# 3 libcrypto-1_1-x64.dll!get_relative_path
# 4 libcrypto-1_1-x64.dll!openssl_relocation
# 5 libcrypto-1_1-x64.dll!X509_get_default_cert_file
# 6 libcrypto-1_1-x64.dll!by_file_ctrl
# 7 libcrypto-1_1-x64.dll!X509_LOOKUP_ctrl
# 8 libcrypto-1_1-x64.dll!X509_STORE_set_default_paths
# 9 main

Error #2: LEAK 313 direct bytes 0x0000022a61f906a0-0x0000022a61f907d9 + 0 indirect bytes
# 0 replace_malloc                             [d:\a\drmemory\drmemory\common\alloc_replace.c:2580]
# 1 libcrypto-1_1-x64.dll!simplify_path
# 2 libcrypto-1_1-x64.dll!malloc_copy_string
# 3 libcrypto-1_1-x64.dll!openssl_relocation
# 4 libcrypto-1_1-x64.dll!X509_get_default_cert_file
# 5 libcrypto-1_1-x64.dll!by_file_ctrl
# 6 libcrypto-1_1-x64.dll!X509_LOOKUP_ctrl
# 7 libcrypto-1_1-x64.dll!X509_STORE_set_default_paths
# 8 main

Error #3: LEAK 97 direct bytes 0x0000022a61fbef20-0x0000022a61fbef81 + 0 indirect bytes
# 0 replace_malloc                                   [d:\a\drmemory\drmemory\common\alloc_replace.c:2580]
# 1 KERNEL32.dll!GetPhysicallyInstalledSystemMemory +0x22bf   (0x00007fff6bce3530 <KERNEL32.dll+0x3530>)
# 2 libcrypto-1_1-x64.dll!simplify_path
# 3 libcrypto-1_1-x64.dll!malloc_copy_string
# 4 libcrypto-1_1-x64.dll!get_relative_path
# 5 libcrypto-1_1-x64.dll!openssl_relocation
# 6 libcrypto-1_1-x64.dll!X509_get_default_cert_dir
# 7 libcrypto-1_1-x64.dll!dir_ctrl
# 8 libcrypto-1_1-x64.dll!X509_LOOKUP_ctrl
# 9 libcrypto-1_1-x64.dll!X509_STORE_set_default_paths
#10 main

Error #4: LEAK 289 direct bytes 0x0000022a61fbefb0-0x0000022a61fbf0d1 + 0 indirect bytes
# 0 replace_malloc                             [d:\a\drmemory\drmemory\common\alloc_replace.c:2580]
# 1 libcrypto-1_1-x64.dll!simplify_path
# 2 libcrypto-1_1-x64.dll!malloc_copy_string
# 3 libcrypto-1_1-x64.dll!openssl_relocation
# 4 libcrypto-1_1-x64.dll!X509_get_default_cert_dir
# 5 libcrypto-1_1-x64.dll!dir_ctrl
# 6 libcrypto-1_1-x64.dll!X509_LOOKUP_ctrl
# 7 libcrypto-1_1-x64.dll!X509_STORE_set_default_paths
# 8 main

===========================================================================
FINAL SUMMARY:

DUPLICATE ERROR COUNTS:

SUPPRESSIONS USED:

ERRORS FOUND:
      0 unique,     0 total invalid heap argument(s)
      0 unique,     0 total warning(s)
      4 unique,     4 total,    820 byte(s) of leak(s)
      0 unique,     0 total,      0 byte(s) of possible leak(s)
ERRORS IGNORED:
      1 potential leak(s) (suspected false positives)
         (details: C:\Users\aho\AppData\Roaming\Dr. Memory\DrMemory-a.exe.20624.000\potential_errors.txt)
      7 unique,     7 total,    550 byte(s) of still-reachable allocation(s)
         (re-run with "-show_reachable" for details)

It seems that it's caused by the MINGW's patch:

+char * openssl_relocation(const char *path)
+{
+ char exe_path[PATH_MAX];
+ get_executable_path (NULL, &exe_path[0], sizeof(exe_path)/sizeof(exe_path[0]));
+ if (strrchr (exe_path, '/') != NULL)
+ {
+ strrchr (exe_path, '/')[1] = '\0';
+ }
+ char * rel_to_datadir = get_relative_path (OPENSSLBIN, path);
+ strcat (exe_path, rel_to_datadir);
+ simplify_path (&exe_path[0]);
+ return malloc_copy_string(exe_path);
+}
+

const char *X509_get_default_cert_dir(void)
{
- return X509_CERT_DIR;
+ return openssl_relocation(X509_CERT_DIR);
}
const char *X509_get_default_cert_file(void)
{
- return X509_CERT_FILE;
+ return openssl_relocation(X509_CERT_FILE);
}

I think these default paths should be stored in static variables and shouldn't be reallocated.

See also: fluent/fluent-package-builder#374

@lazka
Copy link
Member

lazka commented Apr 24, 2022

thanks

I guess the easiest fix is to make it static and only relocate and leak once.

@lazka lazka changed the title Memory leak in OpenSSL Memory leak in OpenSSL relocation patch Apr 24, 2022
lazka added a commit to lazka/MINGW-packages that referenced this issue Apr 24, 2022
…times

Instead of relocating every time the getters are called,
relocate only on the first call for each path.

Fixes msys2#11552
@lazka
Copy link
Member

lazka commented Apr 24, 2022

See #11555

lazka added a commit to lazka/MINGW-packages that referenced this issue May 6, 2022
…times

Instead of relocating every time the getters are called,
relocate only on the first call for each path.

Fixes msys2#11552
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants