Skip to content

Core: Fix crash (segv) in libusb_init if backend.init failes - #989

Closed
ankurvdev wants to merge 1 commit into
libusb:masterfrom
ankurvdev:libusb_init_crash
Closed

ankurvdev wants to merge 1 commit into
libusb:masterfrom
ankurvdev:libusb_init_crash

Conversation

@ankurvdev

Copy link
Copy Markdown

If backend.init fails. the control goes to err_io_exit which tries to clean up hotplug related lists that arent initialized.

Moving hotplug_init before makes sure the lists are valid so if backend.init fails they get cleaned up without errors.

Also adding mutx_unlock to a few more error scenarios

ankurvdev referenced this pull request in ankurvdev/libusb Sep 2, 2021
@ankurvdev ankurvdev mentioned this pull request Sep 2, 2021
6 of 15 tasks
@mcuee mcuee added the core Related to common codes label Sep 3, 2021
Comment thread libusb/core.c Resolved
@tormodvolden

Copy link
Copy Markdown
Contributor

Note that this pull request conversation and title doesn't get included in the git log, so please add an explanation to the commit message as well.

… clean up hotplug related lists that arent initialized.

Moving hotplug_init before makes sure the lists are valid so if backend.init fails they get cleaned up without errors.
@ankurvdev

Copy link
Copy Markdown
Author

Note that this pull request conversation and title doesn't get included in the git log, so please add an explanation to the commit message as well.

done. squashed with capturing the title as a commit message

@ankurvdev

Copy link
Copy Markdown
Author

@mcuee , @tormodvolden

Anything else needed before this can be merged ?

@tormodvolden

Copy link
Copy Markdown
Contributor

It would be good if one of core developers who actually touched this code would chime in and say, yes, this was an oversight, or, no, this should be dealt with differently. But from the look of it, the suggested patch and explanation, thumbs up from me.

You still have an unfinished commit message though, I would suggest having "Fix segmentation fault in libusb_init() if backend.init fails" on the first line, a second empty line, then the already existing explanation.

@tormodvolden

Copy link
Copy Markdown
Contributor

git blame points to commit 32a2206. @dickens, can you please review this fixup?

@mcuee

mcuee commented Sep 21, 2021

Copy link
Copy Markdown
Member

Chris seems to be busy and not able to review as of now.
@hjelmn -- please help here when you got time. Thanks.

@photron

photron commented Sep 22, 2021

Copy link
Copy Markdown
Contributor

@ankurverma85 are you sure that the init order is wrong here? Maybe the exit/cleanup order is wrong instead. Because your move of the usbi_hotplug_init call before the usbi_backend.init call might not be allowed. Maybe usbi_hotplug_init assumes/requires an initialized backend.

@tormodvolden

Copy link
Copy Markdown
Contributor

The suggested change is at least consistent with how it was before the above-mentioned commit. What's inside usbi_hotplug_init() used to be called before usbi_backend.init(), see 32a2206#diff-c1f9bc250077d41456a3e580fca0ddf5d8c25b741bff6d9b9505990a8b70b254L2296
However, the hotplug_ready flag is new.

@photron

photron commented Sep 22, 2021

Copy link
Copy Markdown
Contributor

@tormodvolden You're correct. I take my question back and agree with the suggested change.

@mcuee mcuee changed the title Fix crash (segv) in libusb_init if backend.init failes Core: Fix crash (segv) in libusb_init if backend.init failes Sep 24, 2021
benzea pushed a commit to benzea/libusb that referenced this pull request Mar 1, 2022
The initial enumeration should not result in hotplug events to be fired.
This is just a convenience though, API users still need to be prepared
to be notified a second time fora device that was plugged in between
libusb_init and libusb_hotplug_register_callback.

This regressed with commit 6929b82 ("Fix segmentation fault in
libusb_init() if usbi_backend.init() fails"). This commit avoids the
mentioned segmentation fault by avoiding to clean up the hotplug code if
it was not yet initialised.

Closes: libusb#1082
Related: libusb#989
benzea pushed a commit to benzea/libusb that referenced this pull request Mar 1, 2022
This tests that the initial device enumeration correctly triggers events
for the existing devices with LIBUSB_HOTPLUG_ENUMERATE, but does not
trigger any events beyond that.

Unfortunately, a regression test for the crash happening in issue libusb#989
is not easily possible, as there is no way to inject an error into the
linux_usbfs op_init function using umockdev.
benzea pushed a commit to benzea/libusb that referenced this pull request Mar 2, 2022
The initial enumeration should not result in hotplug events to be fired.
This is just a convenience though, API users still need to be prepared
to be notified a second time fora device that was plugged in between
libusb_init and libusb_hotplug_register_callback.

This regressed with commit 6929b82 ("Fix segmentation fault in
libusb_init() if usbi_backend.init() fails"). This commit avoids the
mentioned segmentation fault by avoiding to clean up the hotplug code if
it was not yet initialised.

Closes: libusb#1082
Related: libusb#989
benzea pushed a commit to benzea/libusb that referenced this pull request Mar 2, 2022
This tests that the initial device enumeration correctly triggers events
for the existing devices with LIBUSB_HOTPLUG_ENUMERATE, but does not
trigger any events beyond that.

Unfortunately, a regression test for the crash happening in issue libusb#989
is not easily possible, as there is no way to inject an error into the
linux_usbfs op_init function using umockdev.
tormodvolden pushed a commit to tormodvolden/libusb that referenced this pull request Mar 15, 2022
The initial enumeration should not result in hotplug events to be fired.
This is just a convenience though, API users still need to be prepared
to be notified a second time fora device that was plugged in between
libusb_init and libusb_hotplug_register_callback.

This regressed with commit 6929b82 ("Fix segmentation fault in
libusb_init() if usbi_backend.init() fails"). This commit avoids the
mentioned segmentation fault by avoiding to clean up the hotplug code if
it was not yet initialised.

Closes libusb#1082
References libusb#989
benzea pushed a commit to benzea/libusb that referenced this pull request Mar 15, 2022
The initial enumeration should not result in hotplug events to be fired.
This is just a convenience though, API users still need to be prepared
to be notified a second time fora device that was plugged in between
libusb_init and libusb_hotplug_register_callback.

This regressed with commit 6929b82 ("Fix segmentation fault in
libusb_init() if usbi_backend.init() fails"). This commit avoids the
mentioned segmentation fault by avoiding to clean up the hotplug code if
it was not yet initialised.

Closes: libusb#1082
Related: libusb#989
tormodvolden pushed a commit that referenced this pull request Mar 16, 2022
The initial enumeration should not result in hotplug events to be fired.
This is just a convenience though, API users still need to be prepared
to be notified a second time for a device that was plugged in between
libusb_init and libusb_hotplug_register_callback.

This regressed with commit 6929b82 ("Fix segmentation fault in
libusb_init() if usbi_backend.init() fails"). This commit avoids the
mentioned segmentation fault by avoiding to clean up the hotplug code if
it was not yet initialised.

Fixes #1082
Closes #1090
References #989
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Related to common codes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants