Received: by 10.213.65.68 with SMTP id h4csp742246imn; Fri, 6 Apr 2018 08:09:20 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+uybJOYYaV7doWNpIGdnd7ty+fupB+f/hcy9ZJRiqC0vfnOiPOLBzYtVym6/QFZMDo+Ru5 X-Received: by 10.98.141.78 with SMTP id z75mr20915533pfd.174.1523027360279; Fri, 06 Apr 2018 08:09:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523027360; cv=none; d=google.com; s=arc-20160816; b=KK+U8iAJx8fLIWqzwxM9EXiFI7DqD5L2TvGKZzQmIZOlqMQ/0bBzM5QvU5cdFuoNFR T+g0vyc5pXOPhVwc9NRKRBltwSAXjwii5vSOXqBt/Kc0sSezUVNLpL1tVzeaXY84aqyV VAC1Hlrf60xPWZgtSgbLC6w1+QQxV+PwM2nojvFJaWgddo+ipW+yTHgHM5ER6KTiJlHY 2M3R/G6LfHMKXJf0zDdU9h5ySui2HTeM/MoiVJm7LhOpVyiUrKN/IteOztyBziOg1wyA dpJLHQW+vuNnc/Y5WK7ZTR0kZjbe6Vfkqw3ZNyC6vHpZ7IZj3Gd5iCO2GrhCBKzGylUb zn3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=JTmIQ6kcR6Ql4yBy77dO7jwVRl7B+6/kOa6A+eQysWA=; b=RXEpWypkdRtWtb4wfteJscvRiIp8gaanTUSz5KQ4tOTNzy9Jc7wvfK6wGW+NPRbNa1 Subkq4UnEMF+WOOsRWpyPPgwuX6KArEOJItOY4H5Wx28aTuXmZOMdBW6HJUaTmn8IPUR rhku2+mjtLRlmn/tZWYXy6sQA2nI/n2L8zQVxWDn13fchoPrBAcrCurYZVoD8cbrqTYc F4sPvGXtAMmODyIHjbu3Bh/KNqd3aBiKhNGfEdpDuOMkwPbbb1ZXcVHCFjvtea6bNdre LlppkA+NisARpWs5gTZnKDwXK3RhoK0ewOZEClr6os+CDdR6NCUPaozmbyz8NY/bv+Ie i06Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u8-v6si8446032plr.50.2018.04.06.08.09.06; Fri, 06 Apr 2018 08:09:20 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755247AbeDFPHR (ORCPT + 99 others); Fri, 6 Apr 2018 11:07:17 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:55924 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754703AbeDFN3q (ORCPT ); Fri, 6 Apr 2018 09:29:46 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 0B6E6D48; Fri, 6 Apr 2018 13:29:45 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, David Lechner , Krzysztof Opasiak , Felipe Balbi , Jerry Zhang Subject: [PATCH 4.4 10/72] usb: gadget: f_hid: fix: Prevent accessing released memory Date: Fri, 6 Apr 2018 15:23:11 +0200 Message-Id: <20180406084305.924231914@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180406084305.210085169@linuxfoundation.org> References: <20180406084305.210085169@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Krzysztof Opasiak 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") Cc: stable@vger.kernel.org Tested-by: David Lechner Signed-off-by: Krzysztof Opasiak Signed-off-by: Felipe Balbi Cc: Jerry Zhang Signed-off-by: Greg Kroah-Hartman --- drivers/usb/gadget/function/f_hid.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) --- 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 * /* pick the first one */ list = list_first_entry(&hidg->completed_out_req, struct f_hidg_req_list, list); + + /* + * Remove this from list to protect it from beign free() + * while host disables our function + */ + list_del(&list->list); + req = list->req; count = min_t(unsigned int, count, req->actual - list->pos); spin_unlock_irqrestore(&hidg->spinlock, flags); @@ -238,15 +245,20 @@ static ssize_t f_hidg_read(struct file * * call, taking into account its current read position. */ if (list->pos == req->actual) { - spin_lock_irqsave(&hidg->spinlock, flags); - list_del(&list->list); kfree(list); - spin_unlock_irqrestore(&hidg->spinlock, flags); req->length = hidg->report_length; ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL); - if (ret < 0) + if (ret < 0) { + free_ep_req(hidg->out_ep, req); return ret; + } + } else { + spin_lock_irqsave(&hidg->spinlock, flags); + list_add(&list->list, &hidg->completed_out_req); + spin_unlock_irqrestore(&hidg->spinlock, flags); + + wake_up(&hidg->read_queue); } return count; @@ -490,14 +502,18 @@ static void hidg_disable(struct usb_func { struct f_hidg *hidg = func_to_hidg(f); struct f_hidg_req_list *list, *next; + unsigned long flags; usb_ep_disable(hidg->in_ep); usb_ep_disable(hidg->out_ep); + spin_lock_irqsave(&hidg->spinlock, flags); list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) { + free_ep_req(hidg->out_ep, list->req); list_del(&list->list); kfree(list); } + spin_unlock_irqrestore(&hidg->spinlock, flags); } static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)