[GH-ISSUE #1245] Memory leak in gpg format #784

Closed
opened 2026-10-14 03:23:50 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @frank-dittrich on GitHub (Apr 29, 2015).
Original GitHub issue: https://github.com/openwall/john/issues/1245

This is on Fedora 22 Beta 3, a non-OMP build without ASan and memdbg:

$ valgrind --leak-check=full ../run/john --test=0 --format=gpg
==16400== Memcheck, a memory error detector
==16400== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==16400== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==16400== Command: ../run/john --test=0 --format=gpg
==16400== 
Testing: gpg, OpenPGP / GnuPG Secret Key [32/64]... PASS
==16400== 
==16400== HEAP SUMMARY:
==16400==     in use at exit: 560 bytes in 4 blocks
==16400==   total heap usage: 345 allocs, 341 frees, 3,551,668 bytes allocated
==16400== 
==16400== 560 (48 direct, 512 indirect) bytes in 2 blocks are definitely lost in loss record 2 of 2
==16400==    at 0x4C28C0F: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==16400==    by 0x510B3C7: CRYPTO_malloc (in /usr/lib64/libcrypto.so.1.0.1k)
==16400==    by 0x5145F49: BN_new (in /usr/lib64/libcrypto.so.1.0.1k)
==16400==    by 0x51463F7: BN_bin2bn (in /usr/lib64/libcrypto.so.1.0.1k)
==16400==    by 0x50C928: check (gpg_fmt_plug.c:1184)
==16400==    by 0x50FC08: crypt_all (gpg_fmt_plug.c:1265)
==16400==    by 0x5DDD1D: fmt_self_test_body (formats.c:541)
==16400==    by 0x5DDD1D: fmt_self_test (formats.c:691)
==16400==    by 0x5D2F24: benchmark_format.part.1 (bench.c:248)
==16400==    by 0x5D3CC3: benchmark_all (bench.c:660)
==16400==    by 0x5E50EA: john_run (john.c:1319)
==16400==    by 0x5E5CD3: main (john.c:1697)
==16400== 
==16400== LEAK SUMMARY:
==16400==    definitely lost: 48 bytes in 2 blocks
==16400==    indirectly lost: 512 bytes in 2 blocks
==16400==      possibly lost: 0 bytes in 0 blocks
==16400==    still reachable: 0 bytes in 0 blocks
==16400==         suppressed: 0 bytes in 0 blocks
==16400== 
==16400== For counts of detected and suppressed errors, rerun with: -v
==16400== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Apparently, in line 1185 of gpg_fmt_plug.c, b points to memory allocated in BN_bin2bn() which doesn't get released.
But the GPG logic is quite complex (different algorithms, a large number of return 0 which might need to be replaced with some goto and appropriate free()...), so there are probably several more places which need to be checked.

Originally created by @frank-dittrich on GitHub (Apr 29, 2015). Original GitHub issue: https://github.com/openwall/john/issues/1245 This is on Fedora 22 Beta 3, a non-OMP build without ASan and memdbg: ``` $ valgrind --leak-check=full ../run/john --test=0 --format=gpg ==16400== Memcheck, a memory error detector ==16400== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==16400== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info ==16400== Command: ../run/john --test=0 --format=gpg ==16400== Testing: gpg, OpenPGP / GnuPG Secret Key [32/64]... PASS ==16400== ==16400== HEAP SUMMARY: ==16400== in use at exit: 560 bytes in 4 blocks ==16400== total heap usage: 345 allocs, 341 frees, 3,551,668 bytes allocated ==16400== ==16400== 560 (48 direct, 512 indirect) bytes in 2 blocks are definitely lost in loss record 2 of 2 ==16400== at 0x4C28C0F: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) ==16400== by 0x510B3C7: CRYPTO_malloc (in /usr/lib64/libcrypto.so.1.0.1k) ==16400== by 0x5145F49: BN_new (in /usr/lib64/libcrypto.so.1.0.1k) ==16400== by 0x51463F7: BN_bin2bn (in /usr/lib64/libcrypto.so.1.0.1k) ==16400== by 0x50C928: check (gpg_fmt_plug.c:1184) ==16400== by 0x50FC08: crypt_all (gpg_fmt_plug.c:1265) ==16400== by 0x5DDD1D: fmt_self_test_body (formats.c:541) ==16400== by 0x5DDD1D: fmt_self_test (formats.c:691) ==16400== by 0x5D2F24: benchmark_format.part.1 (bench.c:248) ==16400== by 0x5D3CC3: benchmark_all (bench.c:660) ==16400== by 0x5E50EA: john_run (john.c:1319) ==16400== by 0x5E5CD3: main (john.c:1697) ==16400== ==16400== LEAK SUMMARY: ==16400== definitely lost: 48 bytes in 2 blocks ==16400== indirectly lost: 512 bytes in 2 blocks ==16400== possibly lost: 0 bytes in 0 blocks ==16400== still reachable: 0 bytes in 0 blocks ==16400== suppressed: 0 bytes in 0 blocks ==16400== ==16400== For counts of detected and suppressed errors, rerun with: -v ==16400== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) ``` Apparently, in line 1185 of gpg_fmt_plug.c, `b` points to memory allocated in `BN_bin2bn()` which doesn't get released. But the GPG logic is quite complex (different algorithms, a large number of `return 0` which might need to be replaced with some goto and appropriate free()...), so there are probably several more places which need to be checked.
bsakaguchi 2026-10-14 03:23:50 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@frank-dittrich commented on GitHub (Apr 29, 2015):

BTW: Asan on my Fedora 22 system shows for the same --test=0 --format=gpg (but for a different build):

=================================================================
==31268==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 120 byte(s) in 5 object(s) allocated from:
    #0 0x7f053f623a0a in malloc (/lib64/libasan.so.2+0x98a0a)
    #1 0x7f053ef9f3c7 in CRYPTO_malloc (/lib64/libcrypto.so.10+0x693c7)

Indirect leak of 1280 byte(s) in 5 object(s) allocated from:
    #0 0x7f053f623a0a in malloc (/lib64/libasan.so.2+0x98a0a)
    #1 0x7f053ef9f3c7 in CRYPTO_malloc (/lib64/libcrypto.so.10+0x693c7)


SUMMARY: AddressSanitizer: 1400 byte(s) leaked in 10 allocation(s).
<!-- gh-comment-id:97594724 --> @frank-dittrich commented on GitHub (Apr 29, 2015): BTW: Asan on my Fedora 22 system shows for the same --test=0 --format=gpg (but for a different build): ``` ================================================================= ==31268==ERROR: LeakSanitizer: detected memory leaks Direct leak of 120 byte(s) in 5 object(s) allocated from: #0 0x7f053f623a0a in malloc (/lib64/libasan.so.2+0x98a0a) #1 0x7f053ef9f3c7 in CRYPTO_malloc (/lib64/libcrypto.so.10+0x693c7) Indirect leak of 1280 byte(s) in 5 object(s) allocated from: #0 0x7f053f623a0a in malloc (/lib64/libasan.so.2+0x98a0a) #1 0x7f053ef9f3c7 in CRYPTO_malloc (/lib64/libcrypto.so.10+0x693c7) SUMMARY: AddressSanitizer: 1400 byte(s) leaked in 10 allocation(s). ```
Author
Owner

@jfoug commented on GitHub (Jun 26, 2015):

@frank-dittrich please test. (test after 028de10 )

<!-- gh-comment-id:115874349 --> @jfoug commented on GitHub (Jun 26, 2015): @frank-dittrich please test. (test after 028de10 )
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openwall/john#784
No description provided.