Skip to content

Agent apply reports a digest-crc C patch applied and vex attests it, but the gem's Rakefile build is a no-op and Ruby keeps loading the unpatched extension #2105

Closed

Description

[agent] Found by the scheduled Bundler (RubyGems) bug-hunt routine (ledger #316).

Summary

digest-crc (a dependency of google-cloud-storage) builds its extensions with a Rakefile, in the gem's own directory, and loads them from there (require_paths = ["ext", "lib"]). For an agent-mode patch to one of its C sources, apply writes the source, runs the gem's build in the installed tree and reports the patch applied. The build does nothing: the Rakefile's file task for crc32/crc32_ext.so depends only on crc32/Makefile, the old .so is still there, so rake considers it up to date and never runs make.

The result is a patched .c beside the unpatched .so that Ruby loads. apply exits 0 with "1 of 1 targeted patch applied", apply --check prints "Patches are in sync", and vex emits not_affected for the advisory.

Impact

A VEX statement for a fix that is not running. The user has every signal that the vulnerability is closed (apply, check and vex agree) while the app executes the original native code. The marker records no artifact ("extArtifacts": {}, "libArtifacts": {}), so nothing can notice that the build produced nothing.

Repro

Linux, Ruby 3.3.6, a compiler toolchain. No Socket API needed: the record is written by hand.

mkdir crc-repro && cd crc-repro
printf 'source "https://rubygems.org"\ngem "digest-crc", "0.6.5"\n' > Gemfile
bundle config set --local path vendor/bundle && bundle install
G=vendor/bundle/ruby/3.3.0/gems/digest-crc-0.6.5
cat > app.rb <<'RB'
require "bundler/setup"
require "digest/crc32"
puts "crc32=#{Digest::CRC32.hexdigest("hello")} ext_loaded=#{$LOADED_FEATURES.grep(/crc32_ext/).size} patched=#{Object.const_defined?(:SOCKET_PATCHED)}"
RB
bundle exec ruby app.rb          # crc32=3610a686 ext_loaded=1 patched=false

# mkrec.py: write an agent-mode record (.socket/manifest.json + blobs) for one changed file
cat > mkrec.py <<'PY'
import sys, json, hashlib, os
purl, key, before, after = sys.argv[1:5]
os.makedirs(".socket/blobs", exist_ok=True)
def gh(b): return hashlib.sha256(b"blob %d\0" % len(b) + b).hexdigest()
b, a = open(before, "rb").read(), open(after, "rb").read()
for x in (b, a): open(".socket/blobs/" + gh(x), "wb").write(x)
json.dump({"patches": {purl: {"uuid": "77777777-7777-4777-8777-777777777772",
  "exportedAt": "2026-01-01T00:00:00Z",
  "files": {key: {"beforeHash": gh(b), "afterHash": gh(a)}},
  "vulnerabilities": {"GHSA-aaaa-bbbb-cccc": {"cves": ["CVE-2026-0001"], "summary": "s", "severity": "HIGH", "description": "d"}},
  "description": "synthetic test patch", "license": "MIT", "tier": "free"}}},
  open(".socket/manifest.json", "w"), indent=2)
PY
cp $G/ext/digest/crc32/crc32_ext.c before.c
sed 's/^\trb_undef_method(cCRC32, "update");/\trb_define_global_const("SOCKET_PATCHED", Qtrue);\n\trb_undef_method(cCRC32, "update");/' before.c > after.c
python3 mkrec.py pkg:gem/digest-crc@0.6.5 ext/digest/crc32/crc32_ext.c before.c after.c

socket-patch apply               # exit 0, "1 of 1 targeted patch applied"
bundle exec ruby app.rb          # patched=false   <- the extension Ruby loads is the old one
socket-patch apply --check       # "Patches are in sync (1 patch checked)."
socket-patch vex --product pkg:generic/x@1 | grep '"status"'   # "not_affected"
sha256sum $G/ext/digest/crc32/crc32_ext.so                     # same hash as before apply
cat $G/.socket-patch-native-build.json                         # "extArtifacts": {}, "libArtifacts": {}

gem_make.out of the rebuild shows rake starting and no compiler line. The gem's ext/digest/Rakefile:

file "#{crc}/#{crc_ext_lib}" => "#{crc}/Makefile" do   # no dependency on the .c files
  Dir.chdir(crc) { sh 'make', 'clean'; sh 'make' }
end

Expected

docs/ecosystems.md, "Ruby gems: native extensions": "The extension is rebuilt [...] with the patch laid over it [...] and the build is put where Ruby loads it", and "vex attests the patch only while that record matches the installed binary". The instructions for this routine name "a VEX statement for a patch that isn't applied" as a bug.

Either the rebuild produces a library built from the patched source (for example by removing the gem's stale in-tree build products first, as a fresh gem install starts without them), or the package fails with native_extension_build_failed / native_extensions_unsupported, the source is put back, and vex omits it.

Actual

Patched source, original binary, exit 0, check in sync, not_affected in the VEX document. rollback afterwards is byte-exact (nothing was built).

Versions

socket-patch Ruby Bundler Result
main 3b14afc 3.3.6 4.0.18 reproduces
main 3b14afc 3.3.6 2.5.22 reproduces
5.1.0 release 3.3.6 4.0.18 reproduces

Does not reproduce for scrypt 3.0.8 (its ffi-compiler Rakefile tracks the sources and rebuilds; that gem has a different defect, filed separately) or for gems RubyGems builds into the extension dir.

Linux only was tested; the cause is in the gem's Rakefile and the missing artifact check, so macOS should behave the same.

First bad version

Present in 5.1.0 (#1998). Not a regression of #2098.

Suspect code

crates/socket-patch-core/src/patch/gem_ext.rs:2372 (rebuild): the in-place rebuild trusts the builder's exit status. The marker written at gem_ext.rs:2326-2346 hashes only artifact_rels(extension_dir), which is empty for this gem, and verify_native_build then has nothing to compare, so a build that changed no loadable file passes. A check that at least one loadable file in the tree (see in_tree_products, gem_ext.rs:3010) changed after a rebuild of a changed build input would catch it.


Generated by Claude Code

Activity

  1. added
    bug
    bughunt
    pm:bundler
    on Nov 17, 2026
  2. mikolalysenko commented on Nov 17, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Claiming this issue. Branch: agent/fix-gem-in-tree-build-products.


    Generated by Claude Code

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

    agent:triagedbugbughuntpm:bundlerpriority:p1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions