Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp3761956pxt; Tue, 10 Aug 2021 10:43:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyu9k/WhEC+C+NBPN/NnMnObNMeNtOdz3LWgL7itUJbKK6n3cwf4Dz7UYSho4UlfpZx7/+B X-Received: by 2002:a17:906:53d3:: with SMTP id p19mr4204326ejo.509.1628617397801; Tue, 10 Aug 2021 10:43:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628617397; cv=none; d=google.com; s=arc-20160816; b=OKCg5AddKM0I7pn2Uewqu+79FnsEPI+TkRy2gPsKnrRP+CIbHbgRy1IaXFN/9O88PZ aRFukKTeMsQimu6vtc0Fp+vGRqCBRqpb2kkEtGmO+Q8PdDgt6sPtAla2HjkMz+Fmixhs vsdCIlS+rm1Qhy6mNSrKJXznKX8odNmh+vvHZLZRNr0gvHbEPRKUMFvFVgzTi5ZVvIoH lIWW5CTtCy3XYvV3gGdrNHz7RmdxSBYKrRvZp/uKI1lIM52Cx2dRN7gsTpH4FvY/3lQP wlAJ18Adcx7QE5qaD1FFfqM+tvxdQQhardNdcjfAMAi5MLBp8AavS9oAFF5m+hUynjHZ lyoA== 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=hKTWVso45irPEXscsVD4pyT2kfSvXJO7vmLUDLiccT4=; b=khFRXhUBJ5qKQeEi3T0s6M0bGm2EfgZGIjHi5X6An9jSYZo6WUe2v6qH/7xJMitzgt fxyjCVsr+m+IcWNyFZFA0wJorVSNNl94aTzuSDWyFNZ9nrk9Vx9Lo5P5naqDPbl/MJnX j81mC3DS9EseMO7xSazd/ro3lsiHGFy57AxqaX3zhZe9MYeOTWUEljCZ0SCjdYFyAzNU 5gWA3RK8H4TJJ9P/ov2Gdc2579r+0vvqbTlxAH2d4Xk7aJMGrA/J5s/jly1v/pNaIJAh lb3v7zTjJoWOZC3VoV5YJTYKAYltACHTjksVRUxt1dhYJisqCpVN+dxUhIoufAFdhfA3 Pl3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aBYYqrjB; 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 v2si22944969edc.86.2021.08.10.10.42.52; Tue, 10 Aug 2021 10:43:17 -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=aBYYqrjB; 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 S234535AbhHJRhd (ORCPT + 99 others); Tue, 10 Aug 2021 13:37:33 -0400 Received: from mail.kernel.org ([198.145.29.99]:33340 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233715AbhHJRfm (ORCPT ); Tue, 10 Aug 2021 13:35:42 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 87A6961101; Tue, 10 Aug 2021 17:35:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1628616913; bh=KbIjo1CgdEAh2k2GBQcCXB+34k/yUSrGN+77x1fi/0o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aBYYqrjBtBC5iqNl3p6gXPr3bFjQYpb47XDELU8VQeipAoJ52DPogtCp3IN/1zJUR uC1jkCY6zPqwGMhik25yOGEFQFB9oHE6tnLY8sfZ8ovhHANFW5UJMyeyLyW+ugvk7w v9B6dhemgvttWzKEcafeEpmni+6PaIm1R9JIh4tA= 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.4 46/85] usb: gadget: f_hid: fixed NULL pointer dereference Date: Tue, 10 Aug 2021 19:30:19 +0200 Message-Id: <20210810172949.793384993@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210810172948.192298392@linuxfoundation.org> References: <20210810172948.192298392@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 @@ -345,6 +345,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 */ @@ -365,8 +370,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"); @@ -394,14 +405,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: