Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp3756668pxt; Tue, 10 Aug 2021 10:35:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJysPSlsADKpTiZB6lWZJwfDpYu5KYzNbq+/37m20QMf+9RofjY57yiUxnNmcybxLaQj00cF X-Received: by 2002:a17:906:584b:: with SMTP id h11mr26872418ejs.209.1628616923997; Tue, 10 Aug 2021 10:35:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628616923; cv=none; d=google.com; s=arc-20160816; b=KGH92imLH7Q6hpSusw3hlQ2yNoSPLQ6R9RAB9ih8xipm4sqABgfjdO9fproANuLQw6 9p74tMaOrM2WpoG2GJ/mmMMKUV6bxWbyT3TquZm+LkvRF5Wx5gfiZhwnFGTWWbtJPsxV f0IVErWDVN3oix1kAcB7Fwd0v5iZsxz6BzN1clHaCcF7q9emmWS6SNqxdvt0P2obw733 Y0+ypeYqZU4yPyit6sEXIcqsx+3eDzQKdu24/ONHobdnRml2C5tjhDsPMl1JqiPYk5+l JrqDe4PVwCEBZIp8rlcs3E2GYFOzpoiXKbFJoomeuzW6J9iz59JvkvTehlETEGliWcJZ kJbw== 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=N6CecO2lvPdlsxW8p85cc0Y0yTrSfjc+Nk1lo4vVrQdFX4JMHuEZ0U2swdgcUdy16y 87CU76lsOolV2BHg4eeK4ZO5yNtqJxM8/DrrQfw3bWViYfsOS181DCJRhraDMg7dff/J dKpiW7B3nft90PuTflMYQRCPrDFWRId/RvLgod+ikyHrCAsb+Uc9ZUt3bpi2YWIqsFzF S9EqfFhNCeijbvbg1p182vqy1GqsqUYHWEDoLCUGY6IIK3M7dG/jjBgjNg7AwKOqQ5oV aCqAOIZMWto0IC+D2Knr8jzIYqSr7zihOx9vSdoRW7jgrlny7egrC08/63715u1M8Nxw anQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QHKVEumf; 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 y15si6598535edu.593.2021.08.10.10.34.58; Tue, 10 Aug 2021 10:35:23 -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=QHKVEumf; 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 S232246AbhHJRdL (ORCPT + 99 others); Tue, 10 Aug 2021 13:33:11 -0400 Received: from mail.kernel.org ([198.145.29.99]:33646 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231851AbhHJRcv (ORCPT ); Tue, 10 Aug 2021 13:32:51 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 54FA260F56; Tue, 10 Aug 2021 17:32:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1628616748; bh=KbIjo1CgdEAh2k2GBQcCXB+34k/yUSrGN+77x1fi/0o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QHKVEumfC9nuf6aIPzZ0B9AHLkTDlaWShFTIIQNRSf1ckobpyOB9tTF8mOQztfS4k JjovqpN8Y/zshb23HexIYEviyCLXZL4MyZerIA3o21HYsm/y+Q60r7/ZR0i8Pwtnmm yJr2DdEGjVYmxa0acUao2KpZgljfM49mMo51Huns= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Maxim Devaev , Phil Elwell Subject: [PATCH 4.19 29/54] usb: gadget: f_hid: fixed NULL pointer dereference Date: Tue, 10 Aug 2021 19:30:23 +0200 Message-Id: <20210810172945.137030436@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210810172944.179901509@linuxfoundation.org> References: <20210810172944.179901509@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: