Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp3029766pxb; Mon, 18 Oct 2021 06:56:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwGJmUfrWL6sARptnDbUMxmffprMGe8wEKbzMAM4ndRIiW8qFX8OYPrq0J+3LXVwTE+UWR3 X-Received: by 2002:a63:1a0e:: with SMTP id a14mr24283594pga.195.1634565416981; Mon, 18 Oct 2021 06:56:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634565416; cv=none; d=google.com; s=arc-20160816; b=ZwRZT3wH7VLrwgBv0YkHoAKxL6jf3Pk0WmzgSxgQ/cxXGL781RQno6BssKaWvdhpH4 dO2/E90g5Q8dBaKQgIefkfgSzxxTs8spOZp6Fy93VuyTETfVO1ucvjkLhM+CH/lNt3Pz eWrk50AbAMff6RCNwzFD4BbTqlwo2F2xA49OgsKgK/+d81FTNyUvJnt2Ln704lnc+BLL Xuxl7tbPOREaz85etdARq+OIbqSG5PFtzBHjCBhMrkjidPgFm4Km4LgU6K8fYg5eTc0f ae7zI/XSFZ1k+RZFXTNPTDRnv43tuZW3yyJr5XvkoYVr6BAP9G3snAUUqD0DcVeqm5Ag 6fRg== 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=D55LT6HbtCbeP3eZIufx0JTkDRf6/zOR5SOCjYCfU5Q=; b=rFMKTA5oK5HG03GGu2SGWl0FmO2NpyDv5CdGMDfrZXvL8MMB5jKo9H4WmK/DhChJYB FRjDQfE3pIWP1M8Y9LhmcvaHiw3CmGw5K4xGrsDFaCZrXqeJf7AAI4WFfXM9zImOvx3Z OAYIc0ONKl2Mh1ip5CDb5ZQMaBM72MTY/JqOtu+V11t2j5w2kzoUBjr9hq8jCn7KFcvo talM07WTJpLT45CXf0NLsii4iWTb8cyTUE4c7pKL26b9B0ifZl9F6Jfg/rLdMBEbuJBA GR8n8ToCiLeNgRfhIMR9vNHa2T1S1yHUtD4KEvug+33UK6x//EQXOy5iO3T5iIorxQGP JSIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=SEN3cqTC; 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 z6si26445139pjq.23.2021.10.18.06.56.43; Mon, 18 Oct 2021 06:56:56 -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=SEN3cqTC; 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 S234047AbhJRN5V (ORCPT + 99 others); Mon, 18 Oct 2021 09:57:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:57670 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234220AbhJRNzP (ORCPT ); Mon, 18 Oct 2021 09:55:15 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2425F6142A; Mon, 18 Oct 2021 13:39:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1634564396; bh=yfN7EGh3Al7/4fTwI+49e2k8sdmx5o55f6mHhmRtwjw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SEN3cqTCJEYkUFgIc/IBJ+DfbFFxo7TqhxRZtKntV8/r77KJFNpb52Wh27Z+KhCo8 vzpsh2hUZ4YdsjKpx6YWYfKhjhkbeXMJMmGF8Up8Dn1cg401cP7IdkIJp1pjJiJmzZ 0HURZLheaobdL6ibeqTqZbOrStlLR8xE2/Yc3tFc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lu Baolu , Johan Hovold , Mathias Nyman Subject: [PATCH 5.14 040/151] USB: xhci: dbc: fix tty registration race Date: Mon, 18 Oct 2021 15:23:39 +0200 Message-Id: <20211018132341.994837286@linuxfoundation.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211018132340.682786018@linuxfoundation.org> References: <20211018132340.682786018@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: Johan Hovold commit 880de403777376e50bdf60def359fa50a722006f upstream. Make sure to allocate resources before registering the tty device to avoid having a racing open() and write() fail to enable rx or dereference a NULL pointer when accessing the uninitialised fifo. Fixes: dfba2174dc42 ("usb: xhci: Add DbC support in xHCI driver") Cc: stable@vger.kernel.org # 4.16 Cc: Lu Baolu Signed-off-by: Johan Hovold Signed-off-by: Mathias Nyman Link: https://lore.kernel.org/r/20211008092547.3996295-4-mathias.nyman@linux.intel.com Signed-off-by: Greg Kroah-Hartman --- drivers/usb/host/xhci-dbgtty.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) --- a/drivers/usb/host/xhci-dbgtty.c +++ b/drivers/usb/host/xhci-dbgtty.c @@ -408,40 +408,38 @@ static int xhci_dbc_tty_register_device( return -EBUSY; xhci_dbc_tty_init_port(dbc, port); - tty_dev = tty_port_register_device(&port->port, - dbc_tty_driver, 0, NULL); - if (IS_ERR(tty_dev)) { - ret = PTR_ERR(tty_dev); - goto register_fail; - } ret = kfifo_alloc(&port->write_fifo, DBC_WRITE_BUF_SIZE, GFP_KERNEL); if (ret) - goto buf_alloc_fail; + goto err_exit_port; ret = xhci_dbc_alloc_requests(dbc, BULK_IN, &port->read_pool, dbc_read_complete); if (ret) - goto request_fail; + goto err_free_fifo; ret = xhci_dbc_alloc_requests(dbc, BULK_OUT, &port->write_pool, dbc_write_complete); if (ret) - goto request_fail; + goto err_free_requests; + + tty_dev = tty_port_register_device(&port->port, + dbc_tty_driver, 0, NULL); + if (IS_ERR(tty_dev)) { + ret = PTR_ERR(tty_dev); + goto err_free_requests; + } port->registered = true; return 0; -request_fail: +err_free_requests: xhci_dbc_free_requests(&port->read_pool); xhci_dbc_free_requests(&port->write_pool); +err_free_fifo: kfifo_free(&port->write_fifo); - -buf_alloc_fail: - tty_unregister_device(dbc_tty_driver, 0); - -register_fail: +err_exit_port: xhci_dbc_tty_exit_port(port); dev_err(dbc->dev, "can't register tty port, err %d\n", ret);