Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2928464rwb; Mon, 15 Aug 2022 14:14:05 -0700 (PDT) X-Google-Smtp-Source: AA6agR6oaIsGJTKd5FouReESJe3/Yf2UaC4HxmTS21aBmQD5lWaKZZ//cGX4u8ZOOkkdXOpvgVoW X-Received: by 2002:a17:902:d48a:b0:16f:c31:7005 with SMTP id c10-20020a170902d48a00b0016f0c317005mr18789819plg.173.1660597941883; Mon, 15 Aug 2022 14:12:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660597941; cv=none; d=google.com; s=arc-20160816; b=PHBOEZhmW7c+vb+rQfhyNXf7qqsnegnj+ng6TiQ1qlTEE1BM2vOhcUiMSwjvdXKgrF TXGtddGmTJunx0hNaU3gpKDrmOeWmSxmirQzcNdlqubL3HOzSLcTjWLPtBjXllErGhuy 66TkGXlvlo6nqVWR4BxYXo3+25IHk0KS1rNKnZHiWZvlG/TCzMIKKu5u3rq7iQH9x3/2 uTFtwdGX5n1mQaHPABU4nOAvXABH84lqkdLhGPgwo5dvbGQzlqR44tPBoGPbO/2D80xr qHMLPmLOiXjGhJ5x0tc720XfNhEK/g1y5QpRc177OQo+lpJpMw1aqeS0kmTeHeOU15uJ TiQQ== 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=rqBOVmT5bWyyvjNyz6C7V4c1pAuv7I+q7B01M4BKVQw=; b=cxV1l6LCB3NfrlxQHh95GLM7Ire9O5fvHVpdJ40Y8HeW75JAr2roKU1D7A+H605ggc TVZv0C+wOb3d0LyzHTkPi/cVPE0kI8oxtfzUXs4ZC36yWRjDAYu5mNJcDT3yzVd8+mMg h8MdhiNoKLb2lAbyr1csVlhLX0Ni6YT+bxc1z+ETcu7nGlwHHkd7mb8Nu2MISxP3wANQ OejaGodXVibriyqVky+cGTAupg7fyRnYKqq5pnkv3zQ1gHPvk/p6f4bxjXVeT0NWC/XI 2fZNAXnwooz5od1+fd9lasrAm1/WEoaRc+2flStwGz4xjP2YQcUBv7QS6vk7Whovfrww W7rA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="KwFVr/XB"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l13-20020a170903244d00b0016efa52d3ffsi12301754pls.241.2022.08.15.14.12.09; Mon, 15 Aug 2022 14:12:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="KwFVr/XB"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S1343847AbiHOTft (ORCPT + 99 others); Mon, 15 Aug 2022 15:35:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241621AbiHOT2N (ORCPT ); Mon, 15 Aug 2022 15:28:13 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B7905D0C4; Mon, 15 Aug 2022 11:43:49 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 2C3BB61120; Mon, 15 Aug 2022 18:43:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 19EE8C433B5; Mon, 15 Aug 2022 18:43:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660589027; bh=y05tG2XekrDOE2v1i1ySAuL9G93xiQJcKq6uz3F+EMM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KwFVr/XB7Kgqt4wbEQat2Zzq9oqbdYDGCw9cfrKzEdgn7PnmG+g7XIVJ/K4+ANmZB YCF3msLFShIiFubIdOF4ZoSUlIwKfAtZ7OsUCFiDQZlZb2H5ygqg8TKFKaBhmZm9sr apr2pHbVtLzrZw6xyO16tNPA62LF1NONmJcscVjI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daniel Starke , Sasha Levin Subject: [PATCH 5.15 589/779] tty: n_gsm: fix race condition in gsmld_write() Date: Mon, 15 Aug 2022 20:03:53 +0200 Message-Id: <20220815180402.512558272@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180337.130757997@linuxfoundation.org> References: <20220815180337.130757997@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Daniel Starke [ Upstream commit 32dd59f96924f45e33bc79854f7a00679c0fa28e ] The function may be used by the user directly and also by the n_gsm internal functions. They can lead into a race condition which results in interleaved frames if both are writing at the same time. The receiving side is not able to decode those interleaved frames correctly. Add a lock around the low side tty write to avoid race conditions and frame interleaving between user originated writes and n_gsm writes. Fixes: e1eaea46bb40 ("tty: n_gsm line discipline") Signed-off-by: Daniel Starke Link: https://lore.kernel.org/r/20220701061652.39604-9-daniel.starke@siemens.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/tty/n_gsm.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c index 3f65990fc959..23fcb34240ac 100644 --- a/drivers/tty/n_gsm.c +++ b/drivers/tty/n_gsm.c @@ -2911,11 +2911,24 @@ static ssize_t gsmld_read(struct tty_struct *tty, struct file *file, static ssize_t gsmld_write(struct tty_struct *tty, struct file *file, const unsigned char *buf, size_t nr) { - int space = tty_write_room(tty); + struct gsm_mux *gsm = tty->disc_data; + unsigned long flags; + int space; + int ret; + + if (!gsm) + return -ENODEV; + + ret = -ENOBUFS; + spin_lock_irqsave(&gsm->tx_lock, flags); + space = tty_write_room(tty); if (space >= nr) - return tty->ops->write(tty, buf, nr); - set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); - return -ENOBUFS; + ret = tty->ops->write(tty, buf, nr); + else + set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); + spin_unlock_irqrestore(&gsm->tx_lock, flags); + + return ret; } /** -- 2.35.1