Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent TypeError in read_central_directory_entries and use Zip::Error #381

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

zelivans
Copy link
Contributor

Bug found by fuzzing rubyzip.

In the #read_central_directory_entries method, @cdir_offset can be nil resulting in the call to io.seek raising an unhandled TypeError exception. It appears @cdir_offset is set in #read_e_o_c_d or #read_64_e_o_c_d, but I am not sure that it is considered a failure for it to be set to nil there, so in my fix I threw a Zip::Error exception just before trying to use the non-existent cdir_offset.

Raw crash

/home/ariel/afl-kisaten/private/sandbox/rubyzip/rubyzip/lib/zip/central_directory.rb:121:in `seek': no implicit conversion from nil to integer (TypeError)
	from /home/ariel/afl-kisaten/private/sandbox/rubyzip/rubyzip/lib/zip/central_directory.rb:121:in `read_central_directory_entries'
	from /home/ariel/afl-kisaten/private/sandbox/rubyzip/rubyzip/lib/zip/central_directory.rb:138:in `read_from_stream'
	from /home/ariel/afl-kisaten/private/sandbox/rubyzip/rubyzip/lib/zip/file.rb:76:in `block in initialize'
	from /home/ariel/afl-kisaten/private/sandbox/rubyzip/rubyzip/lib/zip/file.rb:75:in `open'
	from /home/ariel/afl-kisaten/private/sandbox/rubyzip/rubyzip/lib/zip/file.rb:75:in `initialize'
	from /home/ariel/afl-kisaten/private/sandbox/rubyzip/rubyzip/lib/zip/file.rb:97:in `new'
	from /home/ariel/afl-kisaten/private/sandbox/rubyzip/rubyzip/lib/zip/file.rb:97:in `open'
	from zip.rb:3:in `<main>'

Reproduce with this file

@coveralls
Copy link

Coverage Status

Coverage increased (+4.5%) to 99.84% when pulling 0b1b650 on zelivans:fix_read_2 into d07b13a on rubyzip:master.

@hainesr
Copy link
Member

hainesr commented Jun 20, 2021

Thanks for the PR.
I'm not sure that @cdir_offest can be nil, can it? It might end up with a wildly wrong value - which we handle a bit better in the current code than before - but once the two EOCD records are read it must be something other than nil I think. (The code has been updated quite a but since this PR.)

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.

None yet

3 participants