Fix use-after-free by mrb_gc_unregistor()
#6390
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Calling
mrb_gc_unregistor()frommrb_data_type::dfreecaused a use-after-free deep insidemrb_close().The impetus to investigate was #6342 (review).
Currently, when
mrb_close()is called, all objects are destroyed first.The process is done heap page by heap page, and when all objects belonging to a heap page are destroyed, the heap page is released.
If the next heap page contains
RDataobjects, themrb_gc_unregistor()function may be called from themrb_data_type::dfreefunction.At this time, the
mrb_gc_unregistor()function gets an array object from a Ruby global variable.If the array object belongs to a freed heap page, use-after-free is established by referencing this array object.
About the fixes.
First of all, there is the fact that the
mrb_gv_get()function returnsnilifmrb->globalsisNULL.Therefore, before destroying all objects, free
mrb->globalsand setmrb->globalstoNULLat the same time.Now the
mrb_gv_get()function will returnnilto the callingmrb_gc_unregistor()function andmrb_gc_unregistor()will do nothing more.ref. #4618