Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp3774481pxt; Tue, 10 Aug 2021 11:02:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJztBzKFhBOsc4m0krypSj++9vx8N2+DnWKiAiw8il/8hhn6QNqZtkFz8yDOAw1mWtpvAXwC X-Received: by 2002:a92:cccf:: with SMTP id u15mr65166ilq.144.1628618523151; Tue, 10 Aug 2021 11:02:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628618523; cv=none; d=google.com; s=arc-20160816; b=k3VC2mxv4RPeL9ZqN6QDpxlWDRuhcCyPd9OGio67fnaXdvV2mcHGJ4VviCuMO23NaB abHlKmVN5hOF2iWolrgfMEiJpKx4DWHU9JxuZ5GRk56IgB5cddah2F3vg0xMoRHThBnl yndaKIMQYF6hs5MWlaV72cgmyyhnSxe+5OAaONCMrH2Pxou/4Gze/U/FGZtOwh5bulgY vYaFRZnZrbB4l1hBfa7vvjLFVujg8dKqBkrSG2BCLsuKN5seMqUfebRYbe9uwPDTfgBf wiZ4nu+1dWQc5rht5AWym2LSRvflCQvh1LEG/I2ZQ3eaklshpqQmVfKQd4IlSH1juotp bh0A== 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=hcHJwyZRlStcwhxBpsjJMaf/J6Nof3XqIwZK0FjlfjdbfvVDmmZ/ylRPIulQyXswRY Pnnua9dyoQ2PqTCzXl3/GPOCdVaBnU7QEYfC7f87UwicmInjkPZai+d8QksRt1pumAiR gUvP2VW5+KV3Rlx5fgLI88wzXI8xuDUzwrEXuhXHMa8yyfe274XEtYjc6139iFShr2ST zjZoHIwMdn3lIzIbFcNrgAVwuOF2J6ZcRqWxTdZvKXWDtG3t81vVxx2EW7Yfj2axliOO dVuUg68T35Je1PqmiWdPzbbawKgM/5/p5B8YDZAXseysxJb4QjrTM2GQuA5wBxgjYcbv XiCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=JbDx9AyK; 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 g17si22751616jao.103.2021.08.10.11.01.49; Tue, 10 Aug 2021 11:02:03 -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=JbDx9AyK; 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 S237474AbhHJSAk (ORCPT + 99 others); Tue, 10 Aug 2021 14:00:40 -0400 Received: from mail.kernel.org ([198.145.29.99]:59580 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237308AbhHJR47 (ORCPT ); Tue, 10 Aug 2021 13:56:59 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8EF2561078; Tue, 10 Aug 2021 17:45:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1628617524; bh=aXMTZTT3kSUoVe5ZEbLDNMkbdOrpCxLP548lkwqAPrY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JbDx9AyK1tPI8qbRo48umzO3i3oKjBO6OM1RRzSiP0x5dcFUW0LwDGhz1BH7u7SGs 2dJZHgsyEXS5m03hAv5sO1315dDFoRNcgljvjsg9+P1bhDPCcu4s1BSiIMxiF1/2Ef pCVEFRcyzSaJfXT7ABk2ceunvQRdrXz8Iz62NgVU= 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.13 095/175] usb: gadget: f_hid: fixed NULL pointer dereference Date: Tue, 10 Aug 2021 19:30:03 +0200 Message-Id: <20210810173004.083111183@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210810173000.928681411@linuxfoundation.org> References: <20210810173000.928681411@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: