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

SIGSEGV using OpenSSL 3 PKCS11 provider with SoftHSM2 + Botan #722

Open
blackbird1 opened this issue Aug 30, 2023 · 1 comment
Open

SIGSEGV using OpenSSL 3 PKCS11 provider with SoftHSM2 + Botan #722

blackbird1 opened this issue Aug 30, 2023 · 1 comment

Comments

@blackbird1
Copy link

OS: Docker (Ubuntu 22.04.2 LTS)
Version: SoftHSM2 2.6.1
OpenSSL 3 with PKCS11 provider: https://github.com/latchset/pkcs11-provider

Steps to reproduce:

  1. Compile SoftHSM2 with Botan and install in /usr/local. (OpenSSL 3 crypto backend option has major problems with deadlocks)
  2. Compile PKCS11 provider with OpenSSL 3 and make sure it gets installed in /lib/ossl-modules
  3. export PKCS11_PROVIDER_MODULE=/usr/local/lib/softhsm/libsofthsm2.so
  4. export PKCS11_PROVIDER_DEBUG=file:/dev/stderr,level:2
  5. softhsm2-util --init-token --slot 0 --label "My Crypto Token" --pin abc123 --so-pin abc123
  6. pkcs11-tool --module $PKCS11_PROVIDER_MODULE -l --keypairgen --key-type rsa:2048 --id 100 --label my_crypto_masterkey --pin abc123
  7. pkcs11-tool --module $PKCS11_MODULE -r --id 100 --type pubkey > pubkey.der
  8. openssl rsa -inform DER -outform PEM -in pubkey.der -pubin > pubkey.pem
  9. echo "hello world" | openssl pkeyutl -encrypt -pubin -inkey pubkey.pem > data.txt.enc
  10. openssl pkeyutl -provider pkcs11 -decrypt -inkey "pkcs11:object=my_crypto_masterkey;type=private" -in data.txt.enc

This crashes the openssl command line using pkeyutl command as well as my own library. Both are using the private key from PKCS11 provider to decrypt. When the app exits I get the following:

`
[ OK ] CAPI_StorageTest.LargeBinaryData (8976 ms)
[----------] 2 tests from CAPI_StorageTest (9018 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test suite ran. (9018 ms total)
[ PASSED ] 2 tests.
Process 10739 stopped

  • thread misc changes #1, name = 'tests', stop reason = signal SIGSEGV: invalid address (fault address: 0xfffff7ff2080)
    frame #0: 0x0000fffff7559018 libc.so.6___lldb_unnamed_symbol3416 + 88 libc.so.6___lldb_unnamed_symbol3416:
    -> 0xfffff7559018 <+88>: str q0, [x0]
    0xfffff755901c <+92>: tbnz w2, #0x6, 0xfffff7559040 ; <+128>
    0xfffff7559020 <+96>: stur q0, [x4, #-0x10]
    0xfffff7559024 <+100>: tbz w2, #0x5, 0xfffff7559030 ; <+112>
    (lldb) up
    frame misc changes #1: 0x0000fffff706a4d8 libbotan-2.so.19Botan::deallocate_memory(void*, unsigned long, unsigned long) + 40 libbotan-2.so.19Botan::deallocate_memory:
    -> 0xfffff706a4d8 <+40>: bl 0xfffff7069280 ; Botan::mlock_allocator::instance()
    0xfffff706a4dc <+44>: mov x3, x21
    0xfffff706a4e0 <+48>: mov x2, x20
    0xfffff706a4e4 <+52>: mov x1, x19
    (lldb) up
    frame add HMAC Sign/Verify support #2: 0x0000fffff7282bf8 libsofthsm2.soBotanSymmetricAlgorithm::~BotanSymmetricAlgorithm() [inlined] Botan::secure_allocator<unsigned long>::deallocate(n=<unavailable>, p=<unavailable>, this=<unavailable>) at secmem.h:53:27 50 51 void deallocate(T* p, std::size_t n) 52 { -> 53 deallocate_memory(p, n, sizeof(T)); 54 } 55 }; 56 (lldb) up frame #3: 0x0000fffff7282bf0 libsofthsm2.soBotanSymmetricAlgorithm::~BotanSymmetricAlgorithm() [inlined] std::allocator_traits<Botan::secure_allocator >::deallocate(__n=, __p=, __a=) at alloc_traits.h:345:23
    342 /
    343 static _GLIBCXX20_CONSTEXPR void
    344 deallocate(_Alloc& __a, pointer __p, size_type __n)
    -> 345 { __a.deallocate(__p, __n); }
    346
    347 /
    *
    348 * @brief Construct an object of type _Tp
    (lldb) up
    frame ECDSA support #4: 0x0000fffff7282bf0 libsofthsm2.soBotanSymmetricAlgorithm::~BotanSymmetricAlgorithm() at stl_vector.h:354:19 351 { 352 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tr; 353 if (__p) -> 354 _Tr::deallocate(_M_impl, __p, __n); 355 } 356 357 protected: (lldb) up frame #5: 0x0000fffff7282bf0 libsofthsm2.soBotanSymmetricAlgorithm::BotanSymmetricAlgorithm() [inlined] std::_Vector_base<unsigned long, Botan::secure_allocator >::_Vector_base(this=, __in_chrg=) at stl_vector.h:335:15
    332
    333 ~_Vector_base() _GLIBCXX_NOEXCEPT
    334 {
    -> 335 _M_deallocate(_M_impl._M_start,
    336 _M_impl._M_end_of_storage - _M_impl._M_start);
    337 }
    338
    (lldb) up
    Traceback (most recent call last):
    File "", line 1, in
    NameError: name 'run_one_line' is not defined
    Traceback (most recent call last):
    File "", line 1, in
    NameError: name 'lldb' is not defined
    frame Add ECDH support (+HMAC/DSA/ECDSA/DH) #6: 0x0000fffff7282bdc libsofthsm2.soBotanSymmetricAlgorithm::~BotanSymmetricAlgorithm() [inlined] std::vector<unsigned long, Botan::secure_allocator<unsigned long> >::~vector(this=<unavailable>, __in_chrg=<unavailable>) at stl_vector.h:683:7 680 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish, 681 _M_get_Tp_allocator()); 682 _GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC; -> 683 } 684 685 /** 686 * @brief %Vector assignment operator. (lldb) up frame #7: 0x0000fffff7282bdc libsofthsm2.soBotanSymmetricAlgorithm::~BotanSymmetricAlgorithm() [inlined] Botan::BigInt::Data::~Data(this=, __in_chrg=) at bigint.h:917:12
    914
    915 private:
    916
    -> 917 class Data
    918 {
    919 public:
    920 word* mutable_data()
    (lldb) up
    frame Gost #8: 0x0000fffff7282bdc libsofthsm2.soBotanSymmetricAlgorithm::~BotanSymmetricAlgorithm() [inlined] Botan::BigInt::~BigInt(this=<unavailable>, __in_chrg=<unavailable>) at bigint.h:139:41 136 this->swap(other); 137 } 138 -> 139 ~BigInt() { const_time_unpoison(); } 140 141 /** 142 * Move assignment (lldb) up frame #9: 0x0000fffff7282bdc libsofthsm2.soBotanSymmetricAlgorithm::~BotanSymmetricAlgorithm(this=0x0000aaaaaac92380) at BotanSymmetricAlgorithm.cpp:92:1
    89 {
    90 delete cryption;
    91 cryption = NULL;
    -> 92 }
    93
    94 // Encryption functions
    95 bool BotanSymmetricAlgorithm::encryptInit(const SymmetricKey* key, const SymMode::Type mode /* = SymMode:CBC /, const ByteString& IV / = ByteString()/, bool padding / = true /, size_t counterBits / = 0 /, const ByteString& aad / = ByteString() /, size_t tagBytes / = 0 /)
    (lldb) up
    frame tentative port to WIN32 #10: 0x0000fffff72609f0 libsofthsm2.soBotanAES::~BotanAES() [inlined] BotanAES::~BotanAES(this=<unavailable>, __in_chrg=<unavailable>) at BotanAES.h:44:24 41 { 42 public: 43 // Destructor -> 44 virtual ~BotanAES() { } 45 46 // Wrap/Unwrap keys 47 virtual bool wrapKey(const SymmetricKey* key, const SymWrap::Type mode, const ByteString& in, ByteString& out); (lldb) up frame #11: 0x0000fffff72609e8 libsofthsm2.soBotanAES::~BotanAES(this=0x0000aaaaaac92380) at BotanAES.h:44:24
    41 {
    42 public:
    43 // Destructor
    -> 44 virtual ~BotanAES() { }
    45
    46 // Wrap/Unwrap keys
    47 virtual bool wrapKey(const SymmetricKey
    key, const SymWrap::Type mode, const ByteString& in, ByteString& out);
    (lldb) up
    frame OS X experiments #12: 0x0000fffff72874a8 libsofthsm2.soSecureDataManager::~SecureDataManager(this=0x0000aaaaaac90f80) at SecureDataManager.cpp:96:47 93 SecureDataManager::~SecureDataManager() 94 { 95 // Recycle the AES instance -> 96 CryptoFactory::i()->recycleSymmetricAlgorithm(aes); 97 98 // Clean up the mask 99 delete mask; (lldb) up frame #13: 0x0000fffff7287648 libsofthsm2.soSecureDataManager::~SecureDataManager(this=0x0000aaaaaac90f80) at SecureDataManager.cpp:102:1
    99 delete mask;
    100
    101 MutexFactory::i()->recycleMutex(dataMgrMutex);
    -> 102 }
    103
    104 // Generic function for creating an encrypted version of the key from the specified passphrase
    105 bool SecureDataManager::pbeEncryptKey(const ByteString& passphrase, ByteString& encryptedKey)
    (lldb) up
    frame Ldl #14: 0x0000fffff72a1780 libsofthsm2.soToken::~Token() [inlined] Token::~Token(this=0x0000aaaaaac89b70, __in_chrg=<unavailable>) at Token.cpp:69:26 66 // Destructor 67 Token::~Token() 68 { -> 69 if (sdm != NULL) delete sdm; 70 71 MutexFactory::i()->recycleMutex(tokenMutex); 72 } (lldb) up frame #15: 0x0000fffff72a1768 libsofthsm2.soToken::~Token(this=0x0000aaaaaac89b70) at Token.cpp:72:1
    69 if (sdm != NULL) delete sdm;
    70
    71 MutexFactory::i()->recycleMutex(tokenMutex);
    -> 72 }
    73
    74 // Check if the token is still valid
    75 bool Token::isValid()
    (lldb) up
    frame Transaction #16: 0x0000fffff72a12c0 libsofthsm2.soSlot::~Slot() [inlined] Slot::~Slot(this=<unavailable>, __in_chrg=<unavailable>) at Slot.cpp:61:9 58 // Destructor 59 Slot::~Slot() 60 { -> 61 delete token; 62 } 63 64 // Retrieve the token in the slot (lldb) up frame #17: 0x0000fffff72a12a8 libsofthsm2.soSlot::~Slot(this=0x0000aaaaaac89b40) at Slot.cpp:62:1
    59 Slot::~Slot()
    60 {
    61 delete token;
    -> 62 }
    63
    64 // Retrieve the token in the slot
    65 Token* Slot::getToken()
    (lldb) up
    frame add dump object file tool #18: 0x0000fffff72a0964 libsofthsm2.soSlotManager::~SlotManager(this=0x0000aaaaaac88fc0) at SlotManager.cpp:93:13 90 91 for (SlotMap::iterator i = toDelete.begin(); i != toDelete.end(); i++) 92 { -> 93 delete i->second; 94 } 95 } 96 (lldb) up frame #19: 0x0000fffff72a0a14 libsofthsm2.soSlotManager::~SlotManager(this=0x0000aaaaaac88fc0) at SlotManager.cpp:95:1
    92 {
    93 delete i->second;
    94 }
    -> 95 }
    96
    97 // Get the slot list
    98 CK_RV SlotManager::getSlotList(ObjectStore* objectStore, CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount)
    (lldb) up
    frame Replace semaphore counters by generation counters #20: 0x0000fffff722e618 libsofthsm2.soSoftHSM::~SoftHSM(this=0x0000aaaaaabffbb0) at SoftHSM.cpp:397:34 394 handleManager = NULL; 395 if (sessionManager != NULL) delete sessionManager; 396 sessionManager = NULL; -> 397 if (slotManager != NULL) delete slotManager; 398 slotManager = NULL; 399 if (objectStore != NULL) delete objectStore; 400 objectStore = NULL; (lldb) up frame #21: 0x0000fffff7252f24 libsofthsm2.sostd::unique_ptr<SoftHSM, std::default_delete >::~unique_ptr() [inlined] SoftHSM::~SoftHSM(this=0x0000aaaaaabffbb0, __in_chrg=) at SoftHSM.cpp:410:1
    407 isInitialised = false;
    408
    409 resetMutexFactoryCallbacks();
    -> 410 }
    411
    412 /*****************************************************************************
    413 Implementation of PKCS add AES/DES higher level support #11 functions
    (lldb) up
    frame Botan 1.11 / C++11 support #22: 0x0000fffff7252f18 libsofthsm2.sostd::unique_ptr<SoftHSM, std::default_delete<SoftHSM> >::~unique_ptr() [inlined] std::default_delete<SoftHSM>::operator(__ptr=0x0000aaaaaabffbb0, this=<unavailable>)(SoftHSM*) const at unique_ptr.h:85:2 82 "can't delete pointer to incomplete type"); 83 static_assert(sizeof(_Tp)>0, 84 "can't delete pointer to incomplete type"); -> 85 delete __ptr; 86 } 87 }; 88 (lldb) up frame #23: 0x0000fffff7252f04 libsofthsm2.sostd::unique_ptr<SoftHSM, std::default_delete >::~unique_ptr(this=) at unique_ptr.h:361:17
    358 "unique_ptr's deleter must be invocable with a pointer");
    359 auto& __ptr = _M_t._M_ptr();
    360 if (__ptr != nullptr)
    -> 361 get_deleter()(std::move(__ptr));
    362 __ptr = pointer();
    363 }
    364
    (lldb) up
    frame wrap long lines #24: 0x0000fffff74fcde8 libc.so.6___lldb_unnamed_symbol3054 + 312 libc.so.6___lldb_unnamed_symbol3054:
    -> 0xfffff74fcde8 <+312>: b 0xfffff74fcd88 ; <+216>
    0xfffff74fcdec <+316>: add x2, x19, x0
    0xfffff74fcdf0 <+320>: mov x1, x24
    0xfffff74fcdf4 <+324>: ldr x3, [x25]
    (lldb)

`

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

No branches or pull requests

1 participant