Skip to content

Allocate the crypt_gensalt_ra result with malloc - #306

Open
yahonda wants to merge 1 commit into
bcrypt-ruby:masterfrom
yahonda:malloc-crypt-gensalt-ra
Open

yahonda wants to merge 1 commit into
bcrypt-ruby:masterfrom
yahonda:malloc-crypt-gensalt-ra

Conversation

@yahonda

@yahonda yahonda commented Nov 17, 2026 •

Copy link
Copy Markdown

This pull request fixes BCrypt::Engine.generate_salt aborting with free(): invalid pointer when Ruby is not linked to libcrypt and is built with RUBY_DEBUG from ruby/ruby@91619f0230 or later.

ext/mri/wrapper.c includes ruby/util.h, which redefines strdup as ruby_strdup, so the bundled crypt_gensalt_ra allocates its result with ruby_xmalloc, while bc_salt releases it with free. When Ruby is linked to libcrypt, crypt_gensalt_ra is resolved to the one in libxcrypt, which uses malloc, and the bundled one is not used.

It can be reproduced with a rubylang/ruby:master-debug image built before ruby/docker-images#195 .

  • Without this commit
$ docker run --rm rubylang/ruby:master-debug-20261030 bash -c 'apt-get update && apt-get install -y build-essential && gem install bcrypt -v 3.1.22 && ruby -e "require %q(bcrypt); BCrypt::Password.create(%q(secret))"'
... snip ...
free(): invalid pointer
/usr/local/lib/ruby/gems/4.1.0+6/gems/bcrypt-3.1.22/lib/bcrypt/engine.rb:90: [BUG] Aborted at 0x0000000000000001
... snip ...
  • With this commit
$ docker run --rm rubylang/ruby:master-debug-20261030 bash -c 'apt-get update && apt-get install -y build-essential git && git clone --depth 1 -b malloc-crypt-gensalt-ra https://github.com/yahonda/bcrypt-ruby && cd bcrypt-ruby && gem build bcrypt.gemspec && gem install bcrypt-3.1.22.gem && ruby -e "require %q(bcrypt); p BCrypt::Password.create(%q(secret)) == %q(secret)"'
... snip ...
true

Fix #305

`BCrypt::Engine.generate_salt` aborts with `free(): invalid pointer`
when Ruby is not linked to libcrypt and is built with `RUBY_DEBUG`
from ruby/ruby@91619f0230 or later.

`ext/mri/wrapper.c` includes `ruby/util.h`, which redefines `strdup`
as `ruby_strdup`, so the bundled `crypt_gensalt_ra` allocates its
result with `ruby_xmalloc`, while `bc_salt` releases it with `free`.
Since ruby/ruby@91619f0230, `ruby_xmalloc` in a `RUBY_DEBUG` build
returns a pointer after its own header, which `free` rejects.

When Ruby is linked to libcrypt, `crypt_gensalt_ra` is resolved to the
one in libxcrypt, which uses `malloc`, so `free` is correct there.

This commit allocates the copy with `malloc` and `memcpy` instead of
`strdup`, and stops including `ruby/util.h`. Not calling `strdup` also
keeps avoiding the problem fixed by f48c58f.

Fix bcrypt-ruby#305

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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 this pull request may close these issues.

free(): invalid pointer on Ruby built with RUBY_DEBUG: the bundled crypt_gensalt_ra allocates with ruby_strdup but the result is released with free

1 participant