Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3413783rwa; Tue, 23 Aug 2022 04:30:01 -0700 (PDT) X-Google-Smtp-Source: AA6agR5DO5Znh8CWPeTMYD43zE2EHQh3RMyGALU1Zd3gTU6gEwG+kh9qILNT8dizn8NrAD1o5RAt X-Received: by 2002:a17:907:c0e:b0:731:614:e507 with SMTP id ga14-20020a1709070c0e00b007310614e507mr16474330ejc.529.1661254119404; Tue, 23 Aug 2022 04:28:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661254119; cv=none; d=google.com; s=arc-20160816; b=J+DHJdJuacFkfK985XfV3RDNTUwJEfXNTAhH5fuhppyY+lk0D0UCAERXErV4uX+gHI rDsPbK8N8+gzRTXr+QMDMxYhBKQ4Bvhs7Xkqd7yF7xKOqR8vIxQdpiU7nsXGlRIqSouK emFS5hVf6e0tepOFTS1B1t2efDcQAMOKWXOE1hS6xBejLQdQox12Rd/96BOOjiFf+7Ft nBUYF8zWmq+yXipZ22POIzhr89tNitD7aBgCw6wH2ocs/tkYMTqMsy2IUo92RJd1THSj y3BREp6EKG6f0MFiDLf2QrHu0/Zj84hzoG0WDQfSPIOvHlk70uEW4WfBkIm9N31VtSaZ KH0A== 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=22fMMhxHt++mzcgew1DrIGs9R8T8JhBVvXH/Dh33chg=; b=d2UU3sWFaXfNgbE4+fpzXPLlpZIIjJGB3scYcXjX8m+XI7+t7XxIYvjVOqB7CwSSzu 1czTD02jL1DzJYrPWcSYs1bpgjBPCqHCqzWssaRifOEZjwEw26GhYR+SD902TQ6Qg4pi Dr1lK7bUkr6Pl28sWNdDit40a2doDY/VYmNxEcsvpMqUn8fkZuw/sCu5wQdel+0P7orI Z4cS3O1QG9FEE/pwWI6+8LRXbNdFGZYDqCzn+/jlMPgDvIOJw7SPcFupJth0PVDgA5/K SMsoOC9JdL53udZXs4Jxzrcu6tR9pCkAniKQ9Ba3EqK7HV+sOBshgITRBl0solVQXQ41 kuqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WSwCRxs4; 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 q20-20020a170906541400b0073d6b88fdc7si6886870ejo.738.2022.08.23.04.28.13; Tue, 23 Aug 2022 04:28:39 -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=WSwCRxs4; 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 S1356069AbiHWKqZ (ORCPT + 99 others); Tue, 23 Aug 2022 06:46:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351186AbiHWKjE (ORCPT ); Tue, 23 Aug 2022 06:39:04 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58D3D85FFE; Tue, 23 Aug 2022 02:07:56 -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 D1D7F6159F; Tue, 23 Aug 2022 09:07:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D659DC433B5; Tue, 23 Aug 2022 09:07:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661245675; bh=ONb3tkVzYtyqK8FLb86zi7zw7WUwqX9OhPALvR8Hmcs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WSwCRxs4ccyyBsF/xDqKtTt60Gz5GB52X5JAQTD2Z7IAT0aW8RcIWVh5yJgbKCZKS tZARTMuHZbCaVcWXr2cE9Zp/hYDWm55Z4IKFbWSLNBIT2X0pqFGSkKa+r9VsKCdQcT 2PcOH/nVlxePXJJqywkcYskU9RgDcuKNBW4MuoS0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daniel Starke , Sasha Levin Subject: [PATCH 4.19 154/287] tty: n_gsm: fix race condition in gsmld_write() Date: Tue, 23 Aug 2022 10:25:23 +0200 Message-Id: <20220823080105.811628279@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080100.268827165@linuxfoundation.org> References: <20220823080100.268827165@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 2c34a024b75f..3d45999fad1b 100644 --- a/drivers/tty/n_gsm.c +++ b/drivers/tty/n_gsm.c @@ -2519,11 +2519,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