Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1263151iob; Thu, 19 May 2022 02:58:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzjPjZ6dKMevDfwffTeADbKTDiKoISyJgEhkvXBM4JbGreTp5tbf0hP6/IB41i32/zM6uie X-Received: by 2002:a17:902:bf06:b0:14d:8c72:96c6 with SMTP id bi6-20020a170902bf0600b0014d8c7296c6mr4042565plb.156.1652954296256; Thu, 19 May 2022 02:58:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652954296; cv=none; d=google.com; s=arc-20160816; b=lvxi68l6MKhsv+Y4FMDAvTsb2DHOhgBp7PHFKUB3x6+qOOc07vWqC1WX6gG12kGATW okACWMzr05nB0JKXSrO2HyFNw7FI6WW4jelCKTHPBa8jN3bT7Uw+1R81+T6j+Idl7Eme ZSj6HXwSCsQd5ETlxe/qTedWKTSbmnlboTjUtVk2CPjjDUsIfNvGZmLDfGHUYVqB6ILH /05l6e6UrgNk0xjCo1JmPMYeOJeZW+a8srwKqwLzC5a1Klaq2hkOXc2UXlm6T7gbVzTd Iau8MxyiTM5TTogLV4A5xE/z6pV4J/RPLgaw0eRAgpdIUIWxG9K5MHIAT12LiiPmnl5L MVYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:feedback-id:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=nqoNi8RvH8jFm/9BUGmPRREecUmmOw+Z6oka13L341g=; b=A3Rwjl5IYg5jTkQfW+An2Wrvev+uHt+XMpCzP85RcIM6OBbuuvcbTLPcoHbveMbGiw lM0Ft6sjE9/RkUadcPlIXVUwxvkq9iHHb7BLhtQHMz25SC+Xc56WLBK4l8L+PWSmcwxZ tSh1rO3mYFuu+2RgB0PwQrpqf1lY1QvnKzpjc687bAoT6qXuU9IVilMjl6Dhd4mBBXMX XmZeU82XTICtSZWVY9hFIuD0Q24pMLQnqoI5klQh+G2V7xWi24k3+AvEJp6XeVld+mJA O/bZqaEb5a85BI9owl+XMu3rStXo0z/WHoW0eaHp7KFlzKUTmrBSjbbqywZczWo+7P4n ORXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@siemens.com header.s=fm1 header.b=c2x674HT; 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=siemens.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b17-20020a170902e95100b0015d56a3c9d0si6734242pll.534.2022.05.19.02.58.00; Thu, 19 May 2022 02:58:16 -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=@siemens.com header.s=fm1 header.b=c2x674HT; 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=siemens.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234703AbiESHK2 (ORCPT + 99 others); Thu, 19 May 2022 03:10:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234540AbiESHJ0 (ORCPT ); Thu, 19 May 2022 03:09:26 -0400 Received: from mta-65-225.siemens.flowmailer.net (mta-65-225.siemens.flowmailer.net [185.136.65.225]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 987D1BA541 for ; Thu, 19 May 2022 00:09:24 -0700 (PDT) Received: by mta-65-225.siemens.flowmailer.net with ESMTPSA id 20220519070924e22288415dd2e655ba for ; Thu, 19 May 2022 09:09:24 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=fm1; d=siemens.com; i=daniel.starke@siemens.com; h=Date:From:Subject:To:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:Cc:References:In-Reply-To; bh=nqoNi8RvH8jFm/9BUGmPRREecUmmOw+Z6oka13L341g=; b=c2x674HTjv0Jcr6ksQjsNaHxn1qdXMlvDirbRsxrQfYaBomRKa01KkrPyuSiSjiwPzUHm/ hhhm+OabeG3YliIA6MANSsHYnIzaBFfbKN1rw2X/evccG6vxd7jDYyPw2yDGzQVDxvvnfGJm adIdZ7OicIkSabjSi8pJQmbRi0OeU=; From: "D. Starke" To: linux-serial@vger.kernel.org, gregkh@linuxfoundation.org, jirislaby@kernel.org Cc: linux-kernel@vger.kernel.org, Daniel Starke Subject: [PATCH v2 9/9] tty: n_gsm: fix race condition in gsmld_write() Date: Thu, 19 May 2022 09:07:57 +0200 Message-Id: <20220519070757.2096-9-daniel.starke@siemens.com> In-Reply-To: <20220519070757.2096-1-daniel.starke@siemens.com> References: <20220519070757.2096-1-daniel.starke@siemens.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Flowmailer-Platform: Siemens Feedback-ID: 519:519-314044:519-21489:flowmailer X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 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") Cc: stable@vger.kernel.org Signed-off-by: Daniel Starke --- drivers/tty/n_gsm.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) See patch 6 regarding changes since to v1. diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c index cc349f5a37fb..640c0f0aa7b1 100644 --- a/drivers/tty/n_gsm.c +++ b/drivers/tty/n_gsm.c @@ -2999,11 +2999,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.34.1