diff options
author | Frank Li <Frank.Li@nxp.com> | 2019-01-11 11:56:47 -0600 |
---|---|---|
committer | Nathan Hjelm <hjelmn@me.com> | 2019-01-30 22:38:32 -0700 |
commit | c20bb681db3fd2d80cbca4477eaf0d3a502ed4c6 (patch) | |
tree | a0b84b7871c47f54d686bfbba6cc7acf90f5f9a8 /libusb | |
parent | 91a49831f3b05f6739c4af21aa6ff802cfe3ac71 (diff) | |
download | libusb-c20bb681db3fd2d80cbca4477eaf0d3a502ed4c6.tar.gz |
fix race condition at event_handles
event_handles supposed just run at a thread.
There are re-entry check at begin.
1: if (usbi_handling_events(ctx))
2: return LIBUSB_ERROR_BUSY;
3: usbi_stat_event_handle(ctx);
this code is hold any lock
it is possible two thread check 1 at the same time, then
go through to 3. So two threads will run event_handles.
above 3 line code should hold event_data_lock to avoid above
race condition.
1: usbi_mutex_lock($ctx->event_data_lock);
2: r = 0;
3: if (usbi_handling_events(ctx))
4: r = LIBUSB_ERROR_BUSY;
5: else
6: usbi_start_event_handling(ctx);
7: usbi_mutex_unlock($ctx->event_data_lock);
8: if(r)
9: return r;
check and set in an atomic operations.
Closes #520
Signed-off-by: Frank Li <Frank.Li@nxp.com>
Signed-off-by: Nathan Hjelm <hjelmn@me.com>
Diffstat (limited to 'libusb')
-rw-r--r-- | libusb/io.c | 11 | ||||
-rw-r--r-- | libusb/version_nano.h | 2 |
2 files changed, 10 insertions, 3 deletions
diff --git a/libusb/io.c b/libusb/io.c index 8c2b02a..6051d7f 100644 --- a/libusb/io.c +++ b/libusb/io.c @@ -2081,9 +2081,16 @@ static int handle_events(struct libusb_context *ctx, struct timeval *tv) /* prevent attempts to recursively handle events (e.g. calling into * libusb_handle_events() from within a hotplug or transfer callback) */ + usbi_mutex_lock(&ctx->event_data_lock); + r = 0; if (usbi_handling_events(ctx)) - return LIBUSB_ERROR_BUSY; - usbi_start_event_handling(ctx); + r = LIBUSB_ERROR_BUSY; + else + usbi_start_event_handling(ctx); + usbi_mutex_unlock(&ctx->event_data_lock); + + if (r) + return r; /* there are certain fds that libusb uses internally, currently: * diff --git a/libusb/version_nano.h b/libusb/version_nano.h index 6d522b6..da4558f 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 11342 +#define LIBUSB_NANO 11343 |