Closed jhawthorn closed 3 years ago
Hi,
Thanks for this patch! I'm a little surprised that Ruby's garbage collector doesn't treat global variables as GC roots.
Does Nokogiri have the same issue? E.g., here.
Is this a security issue? E.g., if one of these gets garbage collected, can subsequent calls be exploited to do anything other than crash?
Does Nokogiri have the same issue? E.g., here.
rb_define_module_under
implicitly marks the object. So constants defined by C are usually GC roots, but the same isn't true for Ruby objects. I suspect only Nokogiri::HTML5::Document
really needs this, but I figured we should mark any constants we got from rb_const_get
(since this gem didn't create them we shouldn't assume).
Makes sense.
Thanks so much for this fix!
Thank you!
I just released a new version with your fix. It's available on rubygems. Thanks again!
Previously it was possible for these objects to be garbage collected (the constants could be unassigned in Ruby) or moved as part of GC compaction (reproducible with
GC.verify_compaction_references
).This commit marks all the global variables in
nokogumbo.c
usingrb_gc_register_mark_object
to ensure that they can't be moved.To test this, I added
GC.verify_compaction_references(toward: :empty, double_heap: true)
after the requires intest/test_nokogumbo.rb
. This guarantees that as many objects are moved as possible.Before
After