Skip to content

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 #305

Open

Description

@yahonda

Steps to reproduce

rubylang/ruby:master-debug is Ruby built with -DRUBY_DEBUG=1, based on Ubuntu 26.04 since ruby/docker-images#192.

docker run --rm rubylang/ruby:master-debug 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))"'

Expected behavior

BCrypt::Password.create returns a password hash, as it does on rubylang/ruby:master-debug-noble with the same Ruby revision.

Actual behavior

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 0x00000000000003da
ruby 4.1.0dev (2026-10-30T13:52:04Z HEAD 99fb833467) +PRISM [x86_64-linux]

The same happens on the bcrypt-ruby master branch when the test suite runs in a rubylang/ruby:master-debug container:
https://github.com/yahonda/bcrypt-ruby/actions/runs/41144804860

Cause

ext/mri/wrapper.c includes ruby/util.h, which redefines strdup to ruby_strdup, so __crypt_gensalt_ra allocates its result with ruby_xmalloc. bc_salt in ext/mri/bcrypt_ext.c then releases it with free(salt).

When Ruby is built with RUBY_DEBUG, ruby_xmalloc puts a header in front of the returned pointer, so free receives a pointer that malloc did not return. Without RUBY_DEBUG, ruby_xmalloc returns what malloc returned, so the mismatch does not show up.

The bundled crypt_gensalt_ra is only used when Ruby is not linked to libcrypt. Otherwise the symbol resolves to libxcrypt's crypt_gensalt_ra, which allocates with malloc. On Ubuntu 26.04, libc6-dev no longer depends on libcrypt-dev, so Ruby in rubylang/ruby:master-debug is not linked to libcrypt.

Possible fix

Allocate the result with malloc so that it matches the free in bc_salt and the libxcrypt implementation:

--- a/ext/mri/wrapper.c
+++ b/ext/mri/wrapper.c
@@ -17,9 +17,6 @@
 #include <stdlib.h>
 #include <string.h>
 
-/* Redefine strdup to ruby_strdup in case string.h doesn't export it. */
-#include <ruby/util.h>
-
 #include <errno.h>
 #ifndef __set_errno
 #define __set_errno(val) errno = (val)
@@ -246,12 +243,16 @@
 		input, size, output, sizeof(output));
 
 	if (retval) {
-		retval = strdup(retval);
+		size_t len = strlen(retval) + 1;
+		char *copy = malloc(len);
+		if (copy)
+			memcpy(copy, retval, len);
 #ifndef __GLIBC__
-		/* strdup(3) on glibc sets errno, so we don't need to bother */
-		if (!retval)
+		/* malloc(3) on glibc sets errno, so we don't need to bother */
+		else
 			__set_errno(ENOMEM);
 #endif
+		retval = copy;
 	}
 
 	return retval;

With this change, the test suite passes in the same container:
https://github.com/yahonda/bcrypt-ruby/actions/runs/41144804661

Activity

  1. added a commit that references this issue on Nov 16, 2026
  2. yahonda commented on Nov 16, 2026

    @yahonda
    Author

    The steps above no longer reproduce with the current rubylang/ruby:master-debug. ruby/docker-images#195 installs libcrypt-dev when Ruby is built, so Ruby in the image is linked to libcrypt again and libxcrypt's crypt_gensalt_ra is used.

    The mismatch in ext/mri/wrapper.c is still there. It can be reproduced with an image built before that change:

    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))"'
    

    It is not specific to Ubuntu 26.04. Ruby master built with -DRUBY_DEBUG=1 and ac_cv_lib_crypt_crypt=no on Ubuntu 24.04, which is not linked to libcrypt either, aborts in the same way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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