Skip to content

Commit 40ed9d7

Browse files
benzeamartinpitt
authored andcommitted
Add locking to ioctl emulation (#92)
When e. g. using libgusb (GLib libusb wrapper), libusb has its mainloop running in one thread while the main thread might be submitting urbs directly using another ioctl. This can cause the wrong URB to be returned. Fix this issue by adding locking to ioctl_emulate.
1 parent 2136ec5 commit 40ed9d7

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/libumockdev-preload.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,15 @@ path_exists(const char *path)
120120
/* multi-thread locking for trap_path users */
121121
pthread_mutex_t trap_path_lock = PTHREAD_MUTEX_INITIALIZER;
122122

123+
/* multi-thread locking for ioctls */
124+
pthread_mutex_t ioctl_lock = PTHREAD_MUTEX_INITIALIZER;
125+
123126
#define TRAP_PATH_LOCK pthread_mutex_lock (&trap_path_lock)
124127
#define TRAP_PATH_UNLOCK pthread_mutex_unlock (&trap_path_lock)
125128

129+
#define IOCTL_LOCK pthread_mutex_lock (&ioctl_lock)
130+
#define IOCTL_UNLOCK pthread_mutex_unlock (&ioctl_lock)
131+
126132
static size_t trap_path_prefix_len = 0;
127133

128134
static const char *
@@ -645,6 +651,8 @@ ioctl_emulate(int fd, IOCTL_REQUEST_TYPE request, void *arg)
645651
int orig_errno;
646652
struct ioctl_fd_info *fdinfo;
647653

654+
IOCTL_LOCK;
655+
648656
if (fd_map_get(&ioctl_wrapped_fds, fd, (const void **)&fdinfo)) {
649657
/* we default to erroring and an appropriate error code before
650658
* tree_execute, as handlers might change errno; if they succeed, we
@@ -668,6 +676,8 @@ ioctl_emulate(int fd, IOCTL_REQUEST_TYPE request, void *arg)
668676
ioctl_result = UNHANDLED;
669677
}
670678

679+
IOCTL_UNLOCK;
680+
671681
return ioctl_result;
672682
}
673683

0 commit comments

Comments
 (0)