Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp3764720pxt; Tue, 10 Aug 2021 10:47:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz0GYoIdF1ybWsR8lJYpd1JRsC/Hc6zYHoUDoOJ+JCulGF5+Hn3PC7JQMnGCF2pEB8RZC/3 X-Received: by 2002:a02:b047:: with SMTP id q7mr28544102jah.130.1628617627331; Tue, 10 Aug 2021 10:47:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628617627; cv=none; d=google.com; s=arc-20160816; b=w7FlkdTgVl7iPPfOf+FRwMEE6nAGs61XoT7G9QDKJAC09nkFUk5TPwxueMWb6RaLw6 2lx99RPjhUYBEquP1fnGqtAeI9LYX2EVxVQXaQNr4yQWnTsEFxYpIZawFCPP5NktDqjb HW22wIF9XDhis5jRXLz/YAYUMefuxcDRqA6wJed0U1C/yXjhaCfcP0JxY1XXVI2+zrl3 btlUg+QlJ8W17iEQjxr6DrUwqw2rk1sAgq1BHxtjy02GXMeMa+L01XYz3AV9IFqAztwr D5iN49jFoB8NMGs/w/SMEbXSWfPczA3psrJLoLQkTQn2HLaSB5/tG4Ddw1KfxPLXhBCG vIPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=/cQb3b20wkx2MxkpyGU7raCoJTbcO8K2/JO+OADJquE=; b=ZfOQdKV9/kEf7wR8UvLH9K9asE3YILNnYaEJ/VHLqUWV7VExbO8vElEriypudY2YdX ICUzbt1nvFQBrgvMK/TIFtTIG2SLhIwDQHUPb/VL4cFMJt12rxXISRqt9kKMRJNn08Pi tKS3+nR9LZzZeZ5CH8YO3Hq4eyEE6ugR4T6EGt6Ku0MQFXMexawopEmh6HgG7yf4kl3s hpQRsVHUo6mpRC5kLXQo6qiSmUYGNS6ZC8wyRV3w4xoLsCIIec18X1807zgs8bM2fjd2 LiH9ej++SsY7fQeIbYc60E+G4g2cMAKqydzbrS0ptlOlyJtOgDQ1azYaRUlwMCpqSZKa eKdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ImxRJAou; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d131si32788481iog.11.2021.08.10.10.46.55; Tue, 10 Aug 2021 10:47:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ImxRJAou; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235724AbhHJRq2 (ORCPT + 99 others); Tue, 10 Aug 2021 13:46:28 -0400 Received: from mail.kernel.org ([198.145.29.99]:59558 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234919AbhHJRns (ORCPT ); Tue, 10 Aug 2021 13:43:48 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 37328610F7; Tue, 10 Aug 2021 17:39:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1628617155; bh=aXMTZTT3kSUoVe5ZEbLDNMkbdOrpCxLP548lkwqAPrY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ImxRJAouCQM5LPAZHqCDY1FuwB4qo2e3JA8WF/FD9KRPsteFroPuRX9E55stVVkAO hjl/oEu7dSl4S8oif7zp8zdFQqSPW43fZHZW5bffBz/myaDaY4ruNQzCDqHKNGWqOi Udri6h1559Li9bxIhFITLqfBYbbPlTNZQGLsBUYM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Maxim Devaev , Phil Elwell Subject: [PATCH 5.10 068/135] usb: gadget: f_hid: fixed NULL pointer dereference Date: Tue, 10 Aug 2021 19:30:02 +0200 Message-Id: <20210810172958.022075158@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210810172955.660225700@linuxfoundation.org> References: <20210810172955.660225700@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Phil Elwell commit 2867652e4766360adf14dfda3832455e04964f2a upstream. Disconnecting and reconnecting the USB cable can lead to crashes and a variety of kernel log spam. The problem was found and reproduced on the Raspberry Pi [1] and the original fix was created in Raspberry's own fork [2]. Link: https://github.com/raspberrypi/linux/issues/3870 [1] Link: https://github.com/raspberrypi/linux/commit/a6e47d5f4efbd2ea6a0b6565cd2f9b7bb217ded5 [2] Signed-off-by: Maxim Devaev Signed-off-by: Phil Elwell Cc: stable Link: https://lore.kernel.org/r/20210723155928.210019-1-mdevaev@gmail.com Signed-off-by: Greg Kroah-Hartman --- drivers/usb/gadget/function/f_hid.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) --- a/drivers/usb/gadget/function/f_hid.c +++ b/drivers/usb/gadget/function/f_hid.c @@ -339,6 +339,11 @@ static ssize_t f_hidg_write(struct file spin_lock_irqsave(&hidg->write_spinlock, flags); + if (!hidg->req) { + spin_unlock_irqrestore(&hidg->write_spinlock, flags); + return -ESHUTDOWN; + } + #define WRITE_COND (!hidg->write_pending) try_again: /* write queue */ @@ -359,8 +364,14 @@ try_again: count = min_t(unsigned, count, hidg->report_length); spin_unlock_irqrestore(&hidg->write_spinlock, flags); - status = copy_from_user(req->buf, buffer, count); + if (!req) { + ERROR(hidg->func.config->cdev, "hidg->req is NULL\n"); + status = -ESHUTDOWN; + goto release_write_pending; + } + + status = copy_from_user(req->buf, buffer, count); if (status != 0) { ERROR(hidg->func.config->cdev, "copy_from_user error\n"); @@ -388,14 +399,17 @@ try_again: spin_unlock_irqrestore(&hidg->write_spinlock, flags); + if (!hidg->in_ep->enabled) { + ERROR(hidg->func.config->cdev, "in_ep is disabled\n"); + status = -ESHUTDOWN; + goto release_write_pending; + } + status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC); - if (status < 0) { - ERROR(hidg->func.config->cdev, - "usb_ep_queue error on int endpoint %zd\n", status); + if (status < 0) goto release_write_pending; - } else { + else status = count; - } return status; release_write_pending: