Skip to content

Commit 46613c9

Browse files
shuahkhgregkh
authored andcommitted
usbip: fix vudc usbip_sockfd_store races leading to gpf
usbip_sockfd_store() is invoked when user requests attach (import) detach (unimport) usb gadget device from usbip host. vhci_hcd sends import request and usbip_sockfd_store() exports the device if it is free for export. Export and unexport are governed by local state and shared state - Shared state (usbip device status, sockfd) - sockfd and Device status are used to determine if stub should be brought up or shut down. Device status is shared between host and client. - Local state (tcp_socket, rx and tx thread task_struct ptrs) A valid tcp_socket controls rx and tx thread operations while the device is in exported state. - While the device is exported, device status is marked used and socket, sockfd, and thread pointers are valid. Export sequence (stub-up) includes validating the socket and creating receive (rx) and transmit (tx) threads to talk to the client to provide access to the exported device. rx and tx threads depends on local and shared state to be correct and in sync. Unexport (stub-down) sequence shuts the socket down and stops the rx and tx threads. Stub-down sequence relies on local and shared states to be in sync. There are races in updating the local and shared status in the current stub-up sequence resulting in crashes. These stem from starting rx and tx threads before local and global state is updated correctly to be in sync. 1. Doesn't handle kthread_create() error and saves invalid ptr in local state that drives rx and tx threads. 2. Updates tcp_socket and sockfd, starts stub_rx and stub_tx threads before updating usbip_device status to SDEV_ST_USED. This opens up a race condition between the threads and usbip_sockfd_store() stub up and down handling. Fix the above problems: - Stop using kthread_get_run() macro to create/start threads. - Create threads and get task struct reference. - Add kthread_create() failure handling and bail out. - Hold usbip_device lock to update local and shared states after creating rx and tx threads. - Update usbip_device status to SDEV_ST_USED. - Update usbip_device tcp_socket, sockfd, tcp_rx, and tcp_tx - Start threads after usbip_device (tcp_socket, sockfd, tcp_rx, tcp_tx, and status) is complete. Credit goes to syzbot and Tetsuo Handa for finding and root-causing the kthread_get_run() improper error handling problem and others. This is a hard problem to find and debug since the races aren't seen in a normal case. Fuzzing forces the race window to be small enough for the kthread_get_run() error path bug and starting threads before updating the local and shared state bug in the stub-up sequence. Fixes: 9720b4b ("staging/usbip: convert to kthread") Cc: stable@vger.kernel.org Reported-by: syzbot <syzbot+a93fba6d384346a761e3@syzkaller.appspotmail.com> Reported-by: syzbot <syzbot+bf1a360e305ee719e364@syzkaller.appspotmail.com> Reported-by: syzbot <syzbot+95ce4b142579611ef0a9@syzkaller.appspotmail.com> Reported-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org> Link: https://lore.kernel.org/r/b1c08b983ffa185449c9f0f7d1021dc8c8454b60.1615171203.git.skhan@linuxfoundation.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 718ad96 commit 46613c9

1 file changed

Lines changed: 34 additions & 8 deletions

File tree

drivers/usb/usbip/vudc_sysfs.c

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ static ssize_t dev_desc_read(struct file *file, struct kobject *kobj,
9090
}
9191
static BIN_ATTR_RO(dev_desc, sizeof(struct usb_device_descriptor));
9292

93-
static ssize_t usbip_sockfd_store(struct device *dev, struct device_attribute *attr,
94-
const char *in, size_t count)
93+
static ssize_t usbip_sockfd_store(struct device *dev,
94+
struct device_attribute *attr,
95+
const char *in, size_t count)
9596
{
9697
struct vudc *udc = (struct vudc *) dev_get_drvdata(dev);
9798
int rv;
@@ -100,6 +101,8 @@ static ssize_t usbip_sockfd_store(struct device *dev, struct device_attribute *a
100101
struct socket *socket;
101102
unsigned long flags;
102103
int ret;
104+
struct task_struct *tcp_rx = NULL;
105+
struct task_struct *tcp_tx = NULL;
103106

104107
rv = kstrtoint(in, 0, &sockfd);
105108
if (rv != 0)
@@ -145,24 +148,47 @@ static ssize_t usbip_sockfd_store(struct device *dev, struct device_attribute *a
145148
goto sock_err;
146149
}
147150

148-
udc->ud.tcp_socket = socket;
149-
151+
/* unlock and create threads and get tasks */
150152
spin_unlock_irq(&udc->ud.lock);
151153
spin_unlock_irqrestore(&udc->lock, flags);
152154

153-
udc->ud.tcp_rx = kthread_get_run(&v_rx_loop,
154-
&udc->ud, "vudc_rx");
155-
udc->ud.tcp_tx = kthread_get_run(&v_tx_loop,
156-
&udc->ud, "vudc_tx");
155+
tcp_rx = kthread_create(&v_rx_loop, &udc->ud, "vudc_rx");
156+
if (IS_ERR(tcp_rx)) {
157+
sockfd_put(socket);
158+
return -EINVAL;
159+
}
160+
tcp_tx = kthread_create(&v_tx_loop, &udc->ud, "vudc_tx");
161+
if (IS_ERR(tcp_tx)) {
162+
kthread_stop(tcp_rx);
163+
sockfd_put(socket);
164+
return -EINVAL;
165+
}
166+
167+
/* get task structs now */
168+
get_task_struct(tcp_rx);
169+
get_task_struct(tcp_tx);
157170

171+
/* lock and update udc->ud state */
158172
spin_lock_irqsave(&udc->lock, flags);
159173
spin_lock_irq(&udc->ud.lock);
174+
175+
udc->ud.tcp_socket = socket;
176+
udc->ud.tcp_rx = tcp_rx;
177+
udc->ud.tcp_rx = tcp_tx;
160178
udc->ud.status = SDEV_ST_USED;
179+
161180
spin_unlock_irq(&udc->ud.lock);
162181

163182
ktime_get_ts64(&udc->start_time);
164183
v_start_timer(udc);
165184
udc->connected = 1;
185+
186+
spin_unlock_irqrestore(&udc->lock, flags);
187+
188+
wake_up_process(udc->ud.tcp_rx);
189+
wake_up_process(udc->ud.tcp_tx);
190+
return count;
191+
166192
} else {
167193
if (!udc->connected) {
168194
dev_err(dev, "Device not connected");

0 commit comments

Comments
 (0)