aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKrzysztof Opasiak <kopasiak90@gmail.com>2017-01-19 12:55:28 -0500
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2017-03-12 00:41:49 -0500
commitd3acd94c0f79387b0d1855bdd4690f1596eb3f25 (patch)
treea216f6ea6c4eee38a7626a38942f01118211fe8a
parentb6092a57150c1641dffb6bfeebbe2cbde7275d1f (diff)
usb: gadget: f_hid: fix: Prevent accessing released memory
commit aa65d11aa008f4de58a9cee7e121666d9d68505e upstream. When we unlock our spinlock to copy data to user we may get disabled by USB host and free the whole list of completed out requests including the one from which we are copying the data to user memory. To prevent from this let's remove our working element from the list and place it back only if there is sth left when we finish with it. Fixes: 99c515005857 ("usb: gadget: hidg: register OUT INT endpoint for SET_REPORT") Tested-by: David Lechner <david@lechnology.com> Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com> Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--drivers/usb/gadget/function/f_hid.c24
1 files changed, 20 insertions, 4 deletions
diff --git a/drivers/usb/gadget/function/f_hid.c b/drivers/usb/gadget/function/f_hid.c
index ceb1d0cc5c90..b914e5725387 100644
--- a/drivers/usb/gadget/function/f_hid.c
+++ b/drivers/usb/gadget/function/f_hid.c
@@ -223,6 +223,13 @@ static ssize_t f_hidg_read(struct file *file, char __user *buffer,
223 /* pick the first one */ 223 /* pick the first one */
224 list = list_first_entry(&hidg->completed_out_req, 224 list = list_first_entry(&hidg->completed_out_req,
225 struct f_hidg_req_list, list); 225 struct f_hidg_req_list, list);
226
227 /*
228 * Remove this from list to protect it from beign free()
229 * while host disables our function
230 */
231 list_del(&list->list);
232
226 req = list->req; 233 req = list->req;
227 count = min_t(unsigned int, count, req->actual - list->pos); 234 count = min_t(unsigned int, count, req->actual - list->pos);
228 spin_unlock_irqrestore(&hidg->spinlock, flags); 235 spin_unlock_irqrestore(&hidg->spinlock, flags);
@@ -238,15 +245,20 @@ static ssize_t f_hidg_read(struct file *file, char __user *buffer,
238 * call, taking into account its current read position. 245 * call, taking into account its current read position.
239 */ 246 */
240 if (list->pos == req->actual) { 247 if (list->pos == req->actual) {
241 spin_lock_irqsave(&hidg->spinlock, flags);
242 list_del(&list->list);
243 kfree(list); 248 kfree(list);
244 spin_unlock_irqrestore(&hidg->spinlock, flags);
245 249
246 req->length = hidg->report_length; 250 req->length = hidg->report_length;
247 ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL); 251 ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
248 if (ret < 0) 252 if (ret < 0) {
253 free_ep_req(hidg->out_ep, req);
249 return ret; 254 return ret;
255 }
256 } else {
257 spin_lock_irqsave(&hidg->spinlock, flags);
258 list_add(&list->list, &hidg->completed_out_req);
259 spin_unlock_irqrestore(&hidg->spinlock, flags);
260
261 wake_up(&hidg->read_queue);
250 } 262 }
251 263
252 return count; 264 return count;
@@ -506,14 +518,18 @@ static void hidg_disable(struct usb_function *f)
506{ 518{
507 struct f_hidg *hidg = func_to_hidg(f); 519 struct f_hidg *hidg = func_to_hidg(f);
508 struct f_hidg_req_list *list, *next; 520 struct f_hidg_req_list *list, *next;
521 unsigned long flags;
509 522
510 usb_ep_disable(hidg->in_ep); 523 usb_ep_disable(hidg->in_ep);
511 usb_ep_disable(hidg->out_ep); 524 usb_ep_disable(hidg->out_ep);
512 525
526 spin_lock_irqsave(&hidg->spinlock, flags);
513 list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) { 527 list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
528 free_ep_req(hidg->out_ep, list->req);
514 list_del(&list->list); 529 list_del(&list->list);
515 kfree(list); 530 kfree(list);
516 } 531 }
532 spin_unlock_irqrestore(&hidg->spinlock, flags);
517} 533}
518 534
519static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 535static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)