Segfaults on FreeBSD

73 views
Skip to first unread message

Rogier

unread,
May 4, 2021, 2:08:29 PM5/4/21
to Crypto++ Users
Hi,
I keep getting segfaults within cryptopp on freebsd for at least PBKDF DeriveKey(). I used the sample code from the wiki: 
https://www.cryptopp.com/wiki/PKCS12_PBKDF

I used the cryptopp-lib that is available through the binary ports collection (pkg). The reported version is 8.5.0

Any ideas on how I can fix this or at least where to look?

Below all the technical info about versions and traces for a first triage.

- Rogier

$ ./crypto_test
[1]    995 segmentation fault (core dumped)  ./crypto_test

lldb ./crypto_test -c crypto_test.core
(lldb) target create "./crypto_test" --core "crypto_test.core"
Core file '/home/bloem/source/c++/etude/crypto_test.core' (x86_64) was loaded.
(lldb) bt
* thread #1, name = 'crypto_test', stop reason = signal SIGSEGV
  * frame #0: 0x00000008008d64b1 libcryptopp.so.8`___lldb_unnamed_symbol84$$libcryptopp.so.8 + 17
    frame #1: 0x00000008008d72af libcryptopp.so.8`CryptoPP::SHA256::HashMultipleBlocks(unsigned int const*, unsigned long) + 79
    frame #2: 0x0000000800843682 libcryptopp.so.8`CryptoPP::IteratedHashBase<unsigned int, CryptoPP::HashTransformation>::Update(unsigned char const*, unsigned long) + 338
    frame #3: 0x0000000000210771 crypto_test`CryptoPP::HashTransformation::CalculateDigest(unsigned char*, unsigned char const*, unsigned long) + 49
    frame #4: 0x000000000020cf6c crypto_test`CryptoPP::PKCS12_PBKDF<CryptoPP::SHA256>::DeriveKey(unsigned char*, unsigned long, unsigned char, unsigned char const*, unsigned long, unsigned char const*, unsigned long, unsigned int, double) const + 844
    frame #5: 0x000000000020c9be crypto_test`main + 190
    frame #6: 0x000000000020c710 crypto_test`_start(ap=<unavailable>, cleanup=<unavailable>) at crt1.c:76:7

$ c++ -o crypto_test \
-I/usr/local/include/cryptopp \
-L/usr/local/lib/ -lcryptopp \
crypto_test1.cc

$cat crypto_test1.cc
#include <iostream>
#include <string>

#include "cryptlib.h"
#include "pwdbased.h"
#include "sha.h"
#include "hex.h"

int main(int argc, char* argv[])
{
    using namespace CryptoPP;

    byte password[] ="password";
    size_t plen = strlen((const char*)password);

    byte salt[] = "salt";
    size_t slen = strlen((const char*)salt);

    byte derived[SHA256::DIGESTSIZE];

    PKCS12_PBKDF<SHA256> pbkdf;
    byte purpose = 0;

    pbkdf.DeriveKey(derived, sizeof(derived), purpose, password, plen, salt, slen, 1024, 0.0f);

    std::string result;
    HexEncoder encoder(new StringSink(result));

    encoder.Put(derived, sizeof(derived));
    encoder.MessageEnd();

    std::cout << "Derived: " << result << std::endl;

    return 0;
}

$ uname -v
FreeBSD 12.2-RELEASE-p6 GENERIC

$ c++ --version
FreeBSD clang version 10.0.1 (g...@github.com:llvm/llvm-project.git llvmorg-10.0.1-0-gef32c611aa2)
Target: x86_64-unknown-freebsd12.2
Thread model: posix
InstalledDir: /usr/bin

$ pkg info cryptopp
cryptopp-8.5.0
Name           : cryptopp
Version        : 8.5.0
Installed on   : Mon May  3 20:11:11 2021 CEST
Origin         : security/cryptopp
Architecture   : FreeBSD:12:amd64
Prefix         : /usr/local
Categories     : security
Licenses       : BSL
Maintainer     : jhale < a t > FreeBSD.org
WWW            : https://www.cryptopp.com/
Comment        : Free C++ class library of Cryptographic Primitives
Options        :
    DEBUG          : off
    DOCS           : on
    FULL_DEBUG     : off
    SIMD           : off
    TOOLS          : on
Shared Libs provided:
    libcryptopp.so.8
Annotations    :
    FreeBSD_version: 1202000
    cpe            : cpe:2.3:a:cryptopp:crypto++:8.5.0:::::freebsd12:x64
    repo_type      : binary
    repository     : FreeBSD
Flat size      : 28.0MiB
Description    :
Crypto++ Library is a free C++ class library of cryptographic schemes.
One purpose of Crypto++ is to act as a repository of public domain (not
copyrighted) source code. Although the library is copyrighted as a
compilation, the individual files in it (except for a few exceptions listed
in the license) are in the public domain.

WWW: https://www.cryptopp.com/



Sunandan Nandi

unread,
May 5, 2021, 1:12:22 AM5/5/21
to cryptop...@googlegroups.com
Recently I also faced a similar segfault, where I did not find any flaw in my implementation.
I was doing different trials where I have seen if I decrease the optimization level(or no optimization) for the project who use cryptopp..the crash is gone.

It's not the solution but if you have some time...can you try this out.

Regards,
Sunandan

--
You received this message because you are subscribed to the Google Groups "Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cryptopp-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cryptopp-users/62215a2d-3011-489c-ade4-75b6dece16d7n%40googlegroups.com.

Jeffrey Walton

unread,
May 5, 2021, 9:58:24 AM5/5/21
to Crypto++ Users List
On Tue, May 4, 2021 at 2:08 PM Rogier <rogie...@gmail.com> wrote:
>
> I keep getting segfaults within cryptopp on freebsd for at least PBKDF DeriveKey(). I used the sample code from the wiki:
> https://www.cryptopp.com/wiki/PKCS12_PBKDF
>
> I used the cryptopp-lib that is available through the binary ports collection (pkg). The reported version is 8.5.0
>
> Any ideas on how I can fix this or at least where to look?
>
> Below all the technical info about versions and traces for a first triage.

Ugh... I can duplicate this, too using FreeBSD's version of the library.

It looks like the problem is at frame 3:

#3 0x0000000000207739 in CryptoPP::HashTransformation::CalculateDigest (
this=0x7fffffffe820, digest=0x800253000 "", input=<optimized out>,
length=192) at /usr/local/include/cryptopp/cryptlib.h:1189
No locals.

length=192 is wrong, if I am not mistaken.

When I look at frame 4, where 192 is passed to frame 3:

(gdb) f 4
#4 CryptoPP::PKCS12_PBKDF<CryptoPP::SHA256>::DeriveKey (this=<optimized out>,
derived=0x7fffffffe9a0 "\320`\263", derivedLen=32,
purpose=<optimized out>, secret=<optimized out>, secretLen=8,
salt=0x7fffffffeac0 "salt", saltLen=4, iterations=1024, timeInSeconds=0)
at /usr/local/include/cryptopp/pwdbased.h:439
439 hash.CalculateDigest(Ai, buffer, buffer.size());
(gdb) p Ai
$3 = {
m_alloc = {<CryptoPP::AllocatorBase<unsigned char>> = {<No data
fields>}, <No data fields>}, m_mark = 18446744073709551615, m_size =
32,
m_ptr = 0x800253000 ""}

So the size looks OK at frame 4.

On the upside, building Crypto++ 8.5 from sources is OK and tests OK.
I can't duplicate when using Crypto++ 8.5 or Master. It is OK with
both the static lib and shared lib.

I would be interested to know how FreeBSD builds the library. Like are
they using LTO, which is known bad. Every distro I have seen that uses
LTO has problems with it. Also see
https://cryptopp.com/wiki/Link_Time_Optimization.

Is there any chance you can avoid FreeBSD's version, and build
Crypto++ yourself?

Jeff

Rogier Boon

unread,
May 5, 2021, 12:59:09 PM5/5/21
to cryptop...@googlegroups.com
Pfiew, I'm glad you can reproduce this issue. I was starting to question my sanity.

I'll try to build de library myself to see if it improves.

Rogier

--
-- Insanity: doing the same thing over and over again
-- and expecting different results.

Rogier Boon

unread,
May 6, 2021, 3:21:52 AM5/6/21
to cryptop...@googlegroups.com
On Wed, 5 May 2021 18:59:04 +0200
Rogier Boon <rogie...@gmail.com> wrote:

> >
> > On the upside, building Crypto++ 8.5 from sources is OK and tests OK.
> > I can't duplicate when using Crypto++ 8.5 or Master. It is OK with
> > both the static lib and shared lib.
> >
> > I would be interested to know how FreeBSD builds the library. Like are
> > they using LTO, which is known bad. Every distro I have seen that uses
> > LTO has problems with it. Also see
> > https://cryptopp.com/wiki/Link_Time_Optimization.
> >
> > Is there any chance you can avoid FreeBSD's version, and build
> > Crypto++ yourself?
> >
> > Jeff
> >
>
>
> Pfiew, I'm glad you can reproduce this issue. I was starting to question my sanity.
>
> I'll try to build de library myself to see if it improves.
>
> Rogier
>

Compiling from source seems to fix the crash I have using the sample code. For my own code I'm getting different crashes.... so it is back to the debugger for me. At least I'm a step further.

Thanks

Jeffrey Walton

unread,
May 6, 2021, 5:36:43 AM5/6/21
to Crypto++ Users List
On Thu, May 6, 2021 at 3:21 AM Rogier Boon <rogie...@gmail.com> wrote:
> ....
> Compiling from source seems to fix the crash I have using the sample code. For my own code I'm getting different crashes.... so it is back to the debugger for me. At least I'm a step further.

We're going to do something about the LTO problem for the next release.

We also have a wiki with lots of examples at
https://cryptopp.com/wiki/Main_Page.

Jeff
Reply all
Reply to author
Forward
0 new messages