Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp4210526ioo; Mon, 30 May 2022 22:04:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzS4vIClQ0wN+vuCI2zkzok4dkRxCPSxfS6fx3qlWUGoA2EVU2Trt7zuJBCm5Ne89AYIcp8 X-Received: by 2002:a17:906:3c9:b0:6fe:f893:ce1c with SMTP id c9-20020a17090603c900b006fef893ce1cmr33558616eja.137.1653973498114; Mon, 30 May 2022 22:04:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653973498; cv=none; d=google.com; s=arc-20160816; b=VKQyOH575z0l5SzHHoR6Ejj/Ku0DLDd457pARZSOvTQVXKHQihqGsxq4O1yJ2kp4rn ve6vDgwHV26z2OSV+ghaSrbZBW8F4ltdGIFXwKh/w2BoNP9ucxhNxPqK8/ae3Fpixftm eDllg3Akd5qTnQm4OA8w23IkzeCY92zwMw1LrKwLyrZ563XbcQF9wuGLRquSqSNSP0Yk MyzEo74vU4LIaCLwxyhou+Skum4xFt0Mo2MWpSAkWkwCJvUxtKSPiqjVrFpx4aDZMB/v /ZMGPSH0ebi+yvH8z3GOzz+MAICBXoqmZVRubEhhQi+wrK5JPBcJj/VXq4sEv2HPJfv4 oVOg== 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=m8BcC5RpZgB+i6fbhgtW1oR23hnpSx1Czk1VI7kZ69k=; b=KjI+CS0TpKiMUEnJ9iqRVA9dt4JQJRBzTehF7WJYPrzarXyccR/fb3cfJbvZC+7YUB XwdrWPpFRLSY9uSssEyP7F+CgxvtpGhbQKfdDlyw3/uRzEuLIqXZuJJnEMRL3BYE4PZd KfeEMCxPiPVkvbI0trsrpIb3UhOggv56hSz6xuCKYGChwE03VWtxck39Pbx2KtzEgiMi pNTDqY7zs5n2ksEAAtN5vnu3nTsBIVJLjzSlIqLSnb6Xi4STWcEYItDhi+I2odkfWoQi WJyUMlJYjToIqykrMRaPLo5Gn2ddDmw0XwVlDZxRq4ah/kJXPVzdoIjrgkndcN7Pr55e JLhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@siemens.com header.s=fm1 header.b=TvVGDyga; 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 gt9-20020a170906f20900b006e7f5a2a4b1si12231182ejb.853.2022.05.30.22.04.29; Mon, 30 May 2022 22:04:58 -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=TvVGDyga; 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 S238530AbiE3PjM (ORCPT + 99 others); Mon, 30 May 2022 11:39:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238030AbiE3Pi4 (ORCPT ); Mon, 30 May 2022 11:38:56 -0400 Received: from mta-64-227.siemens.flowmailer.net (mta-64-227.siemens.flowmailer.net [185.136.64.227]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A79C7BFC for ; Mon, 30 May 2022 07:46:33 -0700 (PDT) Received: by mta-64-227.siemens.flowmailer.net with ESMTPSA id 2022053014463192e5094154a767525e for ; Mon, 30 May 2022 16:46:31 +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=m8BcC5RpZgB+i6fbhgtW1oR23hnpSx1Czk1VI7kZ69k=; b=TvVGDygaccrl3cFGGey2An+3M+m61eMwk5ASnmE8dIJPm4EwtPumGi/6AxPhU3e7N8rsX9 ZcGNp0B34GTo+mvphg6ocpPIe12o7eZ7gBCrUec6prRnHo5g9Dph2nQw5zfIaBMpqYBFOjCp 1UXAQ+u7b2fwtGyWzfpSiBWopYh/Q=; 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 v3 9/9] tty: n_gsm: fix race condition in gsmld_write() Date: Mon, 30 May 2022 16:45:12 +0200 Message-Id: <20220530144512.2731-9-daniel.starke@siemens.com> In-Reply-To: <20220530144512.2731-1-daniel.starke@siemens.com> References: <20220530144512.2731-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=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 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(-) There have been no comments on v2, hence, no change was done. Link: https://lore.kernel.org/all/20220519070757.2096-9-daniel.starke@siemens.com/ diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c index b0b093e8e9d9..afd179a8481d 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