Skip to content

lib/openssl/hmac.rb:55:in `initialize': EVP_PKEY_new_mac_key: malloc failure (OpenSSL::HMACError) with -Werror flag #628

@junaruga

Description

@junaruga

On the current master branch 22e601a, I got an error. I found this error on the Ubuntu 22.04 Ruby head, 3.2 and 3.1 cases on GitHub Actions. But the error didn't happen on Ubuntu 22.04 Ruby 3.0 and 2.7 cases. The CI result on my fork repository is here, and 7 pending tests too. The 7 pending test didn't happen in Ubuntu 22.04 Ruby 3.0 case.

$ gcc --version
gcc (GCC) 12.2.1 20221121 (Red Hat 12.2.1-4)
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ which ruby
~/.local/ruby-30b960ba34-debug/bin/ruby

$ ruby -v
ruby 3.3.0dev (2023-05-30T12:39:26Z master 30b960ba34) [x86_64-linux]

$ LD_LIBRARY_PATH=$HOME/.local/openssl-3.0.8-debug/lib/ \
  $HOME/.local/openssl-3.0.8-debug/bin/openssl version 
OpenSSL 3.0.8 7 Feb 2023 (Library: OpenSSL 3.0.8 7 Feb 2023)

$ MAKEFLAGS="V=1" \
  bundle exec rake compile -- \
  --with-openssl-dir=$HOME/.local/openssl-3.0.8-debug \
  --with-cflags="-Wall -Wextra -Wdeprecated-declarations -Wdiv-by-zero -Wduplicated-cond -Wimplicit-function-declaration -Wimplicit-int -Wmisleading-indentation -Wpointer-arith -Wwrite-strings -Wold-style-definition -Wimplicit-fallthrough=0 -Wmissing-noreturn -Wno-cast-function-type -Wno-constant-logical-operand -Wno-long-long -Wno-missing-field-initializers -Wno-overlength-strings -Wno-packed-bitfield-compat -Wno-parentheses-equality -Wno-self-assign -Wno-tautological-compare -Wno-unused-parameter -Wno-unused-value -Wsuggest-attribute=format -Wsuggest-attribute=noreturn -Wunused-variable -Wundef -Werror"

$ ruby -I lib -ropenssl -e 'OpenSSL::HMAC.hexdigest("SHA256", "", "test")'
/home/jaruga/var/git/ruby/openssl/lib/openssl/hmac.rb:55:in `initialize': EVP_PKEY_new_mac_key: malloc failure (OpenSSL::HMACError)
	from /home/jaruga/var/git/ruby/openssl/lib/openssl/hmac.rb:55:in `new'
	from /home/jaruga/var/git/ruby/openssl/lib/openssl/hmac.rb:55:in `hexdigest'
	from -e:1:in `<main>'

This error didn't happen without the -Werror. So, I assume that one of the warning flags cause this error.

$ rm -rf tmp/ lib/openssl.so

$ MAKEFLAGS="V=1" \
  bundle exec rake compile -- \
  --with-openssl-dir=$HOME/.local/openssl-3.0.8-debug \
  --with-cflags="-Wall -Wextra -Wdeprecated-declarations -Wdiv-by-zero -Wduplicated-cond -Wimplicit-function-declaration -Wimplicit-int -Wmisleading-indentation -Wpointer-arith -Wwrite-strings -Wold-style-definition -Wimplicit-fallthrough=0 -Wmissing-noreturn -Wno-cast-function-type -Wno-constant-logical-operand -Wno-long-long -Wno-missing-field-initializers -Wno-overlength-strings -Wno-packed-bitfield-compat -Wno-parentheses-equality -Wno-self-assign -Wno-tautological-compare -Wno-unused-parameter -Wno-unused-value -Wsuggest-attribute=format -Wsuggest-attribute=noreturn -Wunused-variable -Wundef"

$ ruby -I lib -ropenssl -e 'OpenSSL::HMAC.hexdigest("SHA256", "", "test")'

$ echo $?
0

Debug

I debugged with debug flags with GDB.

$ rm -rf tmp/ lib/openssl.so

MAKEFLAGS="V=1" \
  bundle exec rake compile -- \
  --with-openssl-dir=$HOME/.local/openssl-3.0.8-debug \
  --with-cflags="-Wall -Wextra -Wdeprecated-declarations -Wdiv-by-zero -Wduplicated-cond -Wimplicit-function-declaration -Wimplicit-int -Wmisleading-indentation -Wpointer-arith -Wwrite-strings -Wold-style-definition -Wimplicit-fallthrough=0 -Wmissing-noreturn -Wno-cast-function-type -Wno-constant-logical-operand -Wno-long-long -Wno-missing-field-initializers -Wno-overlength-strings -Wno-packed-bitfield-compat -Wno-parentheses-equality -Wno-self-assign -Wno-tautological-compare -Wno-unused-parameter -Wno-unused-value -Wsuggest-attribute=format -Wsuggest-attribute=noreturn -Wunused-variable -Wundef -Werror -O0 -g3 -ggdb3 -gdwarf-5"

$ ruby -I lib -ropenssl -e 'OpenSSL::HMAC.hexdigest("SHA256", "", "test")'
/home/jaruga/var/git/ruby/openssl/lib/openssl/hmac.rb:55:in `initialize': EVP_PKEY_new_mac_key: malloc failure (OpenSSL::HMACError)
	from /home/jaruga/var/git/ruby/openssl/lib/openssl/hmac.rb:55:in `new'
	from /home/jaruga/var/git/ruby/openssl/lib/openssl/hmac.rb:55:in `hexdigest'
	from -e:1:in `<main>'

The EVP_PKEY_new_mac_key returns NULL
in the ossl_hmac_initialize in ext/openssl/ossl_hmac.c:107.

$ gdb --args ruby -I lib -ropenssl -e 'OpenSSL::HMAC.hexdigest("SHA256", "", "test")'
...<snip>...
(gdb) l
105	        ossl_raise(eHMACError, "EVP_PKEY_new_raw_private_key");
106	#else
107	    pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
108	                                (unsigned char *)RSTRING_PTR(key),
109	                                RSTRING_LENINT(key));
110	    if (!pkey)
111	        ossl_raise(eHMACError, "EVP_PKEY_new_mac_key");
112	#endif
113	    if (EVP_DigestSignInit(ctx, NULL, ossl_evp_get_digestbyname(digest),
114	                           NULL, pkey) != 1) {
(gdb) f
#0  ossl_hmac_initialize (self=140737045325080, key=140737045325360, digest=140737045325440) at ../../../../ext/openssl/ossl_hmac.c:110
110	    if (!pkey)
(gdb) p pkey
$4 = (EVP_PKEY *) 0x0
(gdb) p (unsigned char *)RSTRING_PTR(key)
$5 = (unsigned char *) 0x7fffe597e648 ""
(gdb) p RSTRING_LENINT(key)
$6 = 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions