Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3023655rwb; Mon, 15 Aug 2022 16:21:14 -0700 (PDT) X-Google-Smtp-Source: AA6agR54nzf6I/UNNGmd9ierZTehRKUPjOMRV7274YLp8RGkpwgaeGaN+AdXuz6n6OIEzWyxlY/f X-Received: by 2002:a17:907:7fa1:b0:734:b26a:17bc with SMTP id qk33-20020a1709077fa100b00734b26a17bcmr11647231ejc.157.1660605674269; Mon, 15 Aug 2022 16:21:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660605674; cv=none; d=google.com; s=arc-20160816; b=EOkQxafUnmI/V0mi0f3xcf8Oqsfv74xy0YC3SklDf1xCP9+gkXyYL+FCKsj7NmVoUV RbJgQMFGQR+FQ666NfRSOqqXHcJjoRlziHd4GV9bzXcBudAGMunu2Tga27SdSOo/I/d6 3pg04hfLqOHaw4+d/37SEnIHQvTISjwGXvU++02ZDUEKpZ1xnAmsqBBjEC2nfR93Re4M 3lZDXkgEFJ1/081ij+TUQw4aQH2qH1cPh0Kcok6XXqpEYdyjHuA6gWmMcQTKUkdKB1S/ qPGDRTa5+ZY77sGuMzMEBU3gTZgmsF9eU2srap9szx9SR8WmG4ekPREy8VO+YzPL/+WK e6yQ== 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=w/n6MijV7oNGaAObP70Mq5KvTYslt7oOa1EjmvrzLgA=; b=vpOC3qFWa7SgJ0ln37bxu2yxQHkGddh/464qi5FnM27hHapXO6iecFjlK4U3T9OMds iK7bob0oJXkYKaXLQXXH1Pd3FX8xS2TeAkZ8Mlq6WB64lgv5ZzLTbWY6uTzFkIQ/VBUX n5p3HhvgRsZQpQ3+sGywuHZEAQAfj4+jmriQzXfAaFxulXOSBx3bhGDsZyDTGUYYVWOZ waIdj+lI3kDxj2jKKHy7eKKbEF3MJEViusxQYbcKyXdwu/w8DEC3C1qd3KLnxXj6+jc/ 7Dn7hH9Xr6c4mqR8RDaj8pJxjMWw60wiP024d/36z9vZuLwWyZIUZRffLhdRHpnLBed8 DsMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=YfeRKkid; 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 bo2-20020a0564020b2200b0043ec4510a05si8282448edb.587.2022.08.15.16.20.49; Mon, 15 Aug 2022 16:21:14 -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=YfeRKkid; 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 S1351300AbiHOWlv (ORCPT + 99 others); Mon, 15 Aug 2022 18:41:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350830AbiHOWhM (ORCPT ); Mon, 15 Aug 2022 18:37:12 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E877A193D1; Mon, 15 Aug 2022 12:50:34 -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 ams.source.kernel.org (Postfix) with ESMTPS id CE4C3B81150; Mon, 15 Aug 2022 19:50:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0DE35C433C1; Mon, 15 Aug 2022 19:49:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660593000; bh=GAem095nFytx1SxT0FqDPzb4tbJ/6JEBo/T1YvkRwYk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YfeRKkid/VIYsjh+S56NEHbb3Uh8IM68QCW2TAWrzV9neQCx7AYEzxkzK0P26qGgw kS4AzoLTY040zHLtidIp+nIoHNUOYK9mAX5Flw8kc73+YfM8JmH+YBeHFp7PIOFB7V PGXjnnNYIUi8iLwU/5m+aieJt7XIOCi/q91HywfA= 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.18 0860/1095] tty: n_gsm: fix race condition in gsmld_write() Date: Mon, 15 Aug 2022 20:04:19 +0200 Message-Id: <20220815180504.915112317@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@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 e95004217292..e02f761a3a31 100644 --- a/drivers/tty/n_gsm.c +++ b/drivers/tty/n_gsm.c @@ -2863,11 +2863,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