From patchwork Tue Dec 12 11:17:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gavrilov Ilia X-Patchwork-Id: 177265 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7650990vqy; Tue, 12 Dec 2023 03:17:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IErooHqL70My88Z1LRHVkSEDh+cmihGz0M+p9wVtSIXzkGCzZVHYlrYJXZRnuMCyiYwY4GX X-Received: by 2002:a05:6a00:174a:b0:6ce:62d6:b59b with SMTP id j10-20020a056a00174a00b006ce62d6b59bmr3261935pfc.32.1702379867383; Tue, 12 Dec 2023 03:17:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702379867; cv=none; d=google.com; s=arc-20160816; b=yp3NxQD06R+dm+prqM/rStFAAJCbQIIYQzCe5b5pE5HBQwdGxxS+Hh05RVmu2ngJ/L dQy2qA35i1sgS41gUQLvOyUfpO2Vh9HM8Oj3NLGDoSBULb8Bvazy4kYg3d8gxHDUVg5A 33RNpz6AozSSejsREJTkZT9qGP7Cz5pbNdYIyiHPZBJ5hED3tuxiEYUHfyMcbiVAr+x/ lrbdD9LHZhuTdUXySaTJUFoSfyTu2EyJwWy9LBFWEktjtl6A7eks+4viHgSXU8P+xsAR 5P2Rse2ilIhNnTp+/8ph5Ln0xL2dvGlKojKf6bofBAlWfgMv3JGg0SlUgwkzKBpO9oqP F+jg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:cc:to:from:dkim-signature :dkim-filter; bh=zI2gWSJaikJQQTNYhl9y8i2BycI4SSfJAtCWPehw3t4=; fh=u9WMGtT6lxesL2nzz73lYcoMEOFVUAOsuQnbgfsY71g=; b=Kev/XzgYJCC6J0hKNpAot5WlWuturgiJGrhqrfjts+gTWxeFLPsHfk9D6fPO2n3QSa v9MowCCBYKmWpBjhFAtDKKqDDAExCxclMIOv2f4fB1IAT3qWkytdd/cHBON/LpITV7d7 JMKGwbSTCb0kTpn8lS6Z7krxCzCu66HbkQ7uWm2Z0PzDJPDY48MQgx0QhTvEsfLSBxyE ImeStfqflVIBncXfshZmnu/XU3uSormmXX7bTZ23sNc88TtmHTUUwOr7RGmczocbt1MM tZUq+EL80DNhbVlhO8mFmWpD3f36QSgHJnhpVxPdzsz7+3X80d1k6hthomBiwTAJMlII cY0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infotecs.ru header.s=mx header.b=sWtVXkhy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=infotecs.ru Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id ay20-20020a056a00301400b006cdd83f45c0si7719995pfb.229.2023.12.12.03.17.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 03:17:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@infotecs.ru header.s=mx header.b=sWtVXkhy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=infotecs.ru Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 4C35C807C75D; Tue, 12 Dec 2023 03:17:44 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346316AbjLLLRa (ORCPT + 99 others); Tue, 12 Dec 2023 06:17:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346323AbjLLLRX (ORCPT ); Tue, 12 Dec 2023 06:17:23 -0500 Received: from mx0.infotecs.ru (mx0.infotecs.ru [91.244.183.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F156BE8 for ; Tue, 12 Dec 2023 03:17:24 -0800 (PST) Received: from mx0.infotecs-nt (localhost [127.0.0.1]) by mx0.infotecs.ru (Postfix) with ESMTP id C40CF12012AD; Tue, 12 Dec 2023 14:17:21 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mx0.infotecs.ru C40CF12012AD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=infotecs.ru; s=mx; t=1702379842; bh=zI2gWSJaikJQQTNYhl9y8i2BycI4SSfJAtCWPehw3t4=; h=From:To:CC:Subject:Date:References:In-Reply-To:From; b=sWtVXkhytZVPVCz6YZf5pTlqRT5Dy82//QV2B4RwaTMQ9DIRHTl7rqRde8H/kTFiK Sm1aMsnQeZCeupp7HYXtxi815HRoahuzU7Z+ddeisMSGWmCLdQ+lOB547nq4Sd0FVG Wo973aXgo7QanfsQRYckA3ILnfmnuAJRy1WqtpFM= Received: from msk-exch-02.infotecs-nt (msk-exch-02.infotecs-nt [10.0.7.192]) by mx0.infotecs-nt (Postfix) with ESMTP id C205D316AAFE; Tue, 12 Dec 2023 14:17:21 +0300 (MSK) From: Gavrilov Ilia To: "stable@vger.kernel.org" , Greg Kroah-Hartman CC: Daniel Starke , Jiri Slaby , Russ Gorby , "linux-kernel@vger.kernel.org" , "lvc-project@linuxtesting.org" Subject: [PATCH 5.10 1/3] tty: n_gsm: fix tty registration before control channel open Thread-Topic: [PATCH 5.10 1/3] tty: n_gsm: fix tty registration before control channel open Thread-Index: AQHaLOzGya5kVhAh/kSEVoCyWeJ41A== Date: Tue, 12 Dec 2023 11:17:21 +0000 Message-ID: <20231212111431.4064760-2-Ilia.Gavrilov@infotecs.ru> References: <20231212111431.4064760-1-Ilia.Gavrilov@infotecs.ru> In-Reply-To: <20231212111431.4064760-1-Ilia.Gavrilov@infotecs.ru> Accept-Language: ru-RU, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.17.0.10] x-exclaimer-md-config: 208ac3cd-1ed4-4982-a353-bdefac89ac0a MIME-Version: 1.0 X-KLMS-Rule-ID: 5 X-KLMS-Message-Action: clean X-KLMS-AntiSpam-Status: not scanned, disabled by settings X-KLMS-AntiSpam-Interceptor-Info: not scanned X-KLMS-AntiPhishing: Clean, bases: 2023/12/12 08:32:00 X-KLMS-AntiVirus: Kaspersky Security for Linux Mail Server, version 8.0.3.30, bases: 2023/12/12 02:27:00 #22664189 X-KLMS-AntiVirus-Status: Clean, skipped X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, 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 lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 12 Dec 2023 03:17:44 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785074672037819987 X-GMAIL-MSGID: 1785074672037819987 From: Daniel Starke commit 01aecd917114577c423f07cec0d186ad007d76fc upstream. The current implementation registers/deregisters the user ttys at mux attach/detach. That means that the user devices are available before any control channel is open. However, user channel initialization requires an open control channel. Furthermore, the user is not informed if the mux restarts due to configuration changes. Put the registration/deregistration procedure into separate function to improve readability. Move registration to mux activation and deregistration to mux cleanup to keep the user devices only open as long as a control channel exists. The user will be informed via the device driver if the mux was reconfigured in a way that required a mux re-activation. This makes it necessary to add T2 initialization to gsmld_open() for the ldisc open code path (not the reconfiguration code path) to avoid deletion of an uninitialized T2 at mux cleanup. Fixes: d50f6dcaf22a ("tty: n_gsm: expose gsmtty device nodes at ldisc open time") Signed-off-by: Daniel Starke Link: https://lore.kernel.org/r/20220701061652.39604-2-daniel.starke@siemens.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Gavrilov Ilia --- drivers/tty/n_gsm.c | 117 ++++++++++++++++++++++++++++++-------------- 1 file changed, 79 insertions(+), 38 deletions(-) diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c index 3693ad9f4521..7a883a2c0c50 100644 --- a/drivers/tty/n_gsm.c +++ b/drivers/tty/n_gsm.c @@ -235,6 +235,7 @@ struct gsm_mux { struct gsm_dlci *dlci[NUM_DLCI]; int old_c_iflag; /* termios c_iflag value before attach */ bool constipated; /* Asked by remote to shut up */ + bool has_devices; /* Devices were registered */ spinlock_t tx_lock; unsigned int tx_bytes; /* TX data outstanding */ @@ -464,6 +465,68 @@ static u8 gsm_encode_modem(const struct gsm_dlci *dlci) return modembits; } +/** + * gsm_register_devices - register all tty devices for a given mux index + * + * @driver: the tty driver that describes the tty devices + * @index: the mux number is used to calculate the minor numbers of the + * ttys for this mux and may differ from the position in the + * mux array. + */ +static int gsm_register_devices(struct tty_driver *driver, unsigned int index) +{ + struct device *dev; + int i; + unsigned int base; + + if (!driver || index >= MAX_MUX) + return -EINVAL; + + base = index * NUM_DLCI; /* first minor for this index */ + for (i = 1; i < NUM_DLCI; i++) { + /* Don't register device 0 - this is the control channel + * and not a usable tty interface + */ + dev = tty_register_device(gsm_tty_driver, base + i, NULL); + if (IS_ERR(dev)) { + if (debug & 8) + pr_info("%s failed to register device minor %u", + __func__, base + i); + for (i--; i >= 1; i--) + tty_unregister_device(gsm_tty_driver, base + i); + return PTR_ERR(dev); + } + } + + return 0; +} + +/** + * gsm_unregister_devices - unregister all tty devices for a given mux index + * + * @driver: the tty driver that describes the tty devices + * @index: the mux number is used to calculate the minor numbers of the + * ttys for this mux and may differ from the position in the + * mux array. + */ +static void gsm_unregister_devices(struct tty_driver *driver, + unsigned int index) +{ + int i; + unsigned int base; + + if (!driver || index >= MAX_MUX) + return; + + base = index * NUM_DLCI; /* first minor for this index */ + for (i = 1; i < NUM_DLCI; i++) { + /* Don't unregister device 0 - this is the control + * channel and not a usable tty interface + */ + tty_unregister_device(gsm_tty_driver, base + i); + } +} + /** * gsm_print_packet - display a frame for debug * @hdr: header to print before decode @@ -2178,6 +2241,10 @@ static void gsm_cleanup_mux(struct gsm_mux *gsm, bool disc) del_timer_sync(&gsm->t2_timer); /* Free up any link layer users and finally the control channel */ + if (gsm->has_devices) { + gsm_unregister_devices(gsm_tty_driver, gsm->num); + gsm->has_devices = false; + } for (i = NUM_DLCI - 1; i >= 0; i--) if (gsm->dlci[i]) gsm_dlci_release(gsm->dlci[i]); @@ -2201,15 +2268,21 @@ static void gsm_cleanup_mux(struct gsm_mux *gsm, bool disc) static int gsm_activate_mux(struct gsm_mux *gsm) { struct gsm_dlci *dlci; + int ret; if (gsm->encoding == 0) gsm->receive = gsm0_receive; else gsm->receive = gsm1_receive; + ret = gsm_register_devices(gsm_tty_driver, gsm->num); + if (ret) + return ret; + dlci = gsm_dlci_alloc(gsm, 0); if (dlci == NULL) return -ENOMEM; + gsm->has_devices = true; gsm->dead = false; /* Tty opens are now permissible */ return 0; } @@ -2475,39 +2548,14 @@ static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len) * will need moving to an ioctl path. */ -static int gsmld_attach_gsm(struct tty_struct *tty, struct gsm_mux *gsm) +static void gsmld_attach_gsm(struct tty_struct *tty, struct gsm_mux *gsm) { - unsigned int base; - int ret, i; - gsm->tty = tty_kref_get(tty); /* Turn off tty XON/XOFF handling to handle it explicitly. */ gsm->old_c_iflag = tty->termios.c_iflag; tty->termios.c_iflag &= (IXON | IXOFF); - ret = gsm_activate_mux(gsm); - if (ret != 0) - tty_kref_put(gsm->tty); - else { - /* Don't register device 0 - this is the control channel and not - a usable tty interface */ - base = mux_num_to_base(gsm); /* Base for this MUX */ - for (i = 1; i < NUM_DLCI; i++) { - struct device *dev; - - dev = tty_register_device(gsm_tty_driver, - base + i, NULL); - if (IS_ERR(dev)) { - for (i--; i >= 1; i--) - tty_unregister_device(gsm_tty_driver, - base + i); - return PTR_ERR(dev); - } - } - } - return ret; } - /** * gsmld_detach_gsm - stop doing 0710 mux * @tty: tty attached to the mux @@ -2518,12 +2566,7 @@ static int gsmld_attach_gsm(struct tty_struct *tty, struct gsm_mux *gsm) static void gsmld_detach_gsm(struct tty_struct *tty, struct gsm_mux *gsm) { - unsigned int base = mux_num_to_base(gsm); /* Base for this MUX */ - int i; - WARN_ON(tty != gsm->tty); - for (i = 1; i < NUM_DLCI; i++) - tty_unregister_device(gsm_tty_driver, base + i); /* Restore tty XON/XOFF handling. */ gsm->tty->termios.c_iflag = gsm->old_c_iflag; tty_kref_put(gsm->tty); @@ -2619,7 +2662,6 @@ static void gsmld_close(struct tty_struct *tty) static int gsmld_open(struct tty_struct *tty) { struct gsm_mux *gsm; - int ret; if (tty->ops->write == NULL) return -EINVAL; @@ -2635,12 +2677,11 @@ static int gsmld_open(struct tty_struct *tty) /* Attach the initial passive connection */ gsm->encoding = 1; - ret = gsmld_attach_gsm(tty, gsm); - if (ret != 0) { - gsm_cleanup_mux(gsm, false); - mux_put(gsm); - } - return ret; + gsmld_attach_gsm(tty, gsm); + + timer_setup(&gsm->t2_timer, gsm_control_retransmit, 0); + + return 0; } /** From patchwork Tue Dec 12 11:17:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gavrilov Ilia X-Patchwork-Id: 177267 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7651117vqy; Tue, 12 Dec 2023 03:18:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IHtB19rTmVBHWR8cOXjIUxjjPyv7TQO6uN40Jh9RQK3Im9DIGHjy09IFtU1l0N49S88AGaZ X-Received: by 2002:a05:6a00:2387:b0:6cb:bc06:b058 with SMTP id f7-20020a056a00238700b006cbbc06b058mr3379678pfc.0.1702379889576; Tue, 12 Dec 2023 03:18:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702379889; cv=none; d=google.com; s=arc-20160816; b=kPllp2d6ab819anfqwUp8M/tZIavdwQs2cdR8v7wv0yoayWewZzYFN4vx97onBytyq MrjgAkKzP7GJYUqu5t8M1KCkBP9Y9rvaeP9rOmvm5DGx92/4jkGrax9wfNnLBnRWTBHe KhWV1LeBNO/C/NGzXbefXLfvVHXJqyLRLj0gzQYTuv0BnLjVFbQ3Kbu9hSGca3o6RSc/ Jo+rW4AyhXvf/o+8iYXCe2tkiKsVBdZ1ycsFqbEaRfd/Okts/V6GzrA06UIHCVGYN+lf lvWc/iXw4xykQOw7mGyz7xm9pNq7Jt/9NhkiwnLVMekkGyUjSfPfwHF1ZBot8+TQhb8V /iaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:cc:to:from:dkim-signature :dkim-filter; bh=NGgi0dcX52R2Hsj4L3+FbfcgEQzNcKvB9lQQ3MuTsMU=; fh=pXyOQNEb+817yi6tjB/1cXkb3oDI+NpZhxZJ/DMqhcM=; b=V+Rzt4YYw8hep9POSgCfhPgsp9t1dHTqoRliKmKZNNWmpXjApqYpwPhJ9/MF13jb+k +t/JEL/TFZPU6S6/DtAPWE5IxUiPyVdnbf0EDA3uFbz6q4EjU6wCl7WtMzDfliMkqXwr LV0SjIu48WJpZ7FM2uuDpMP69zvT5EMK6f/7fin2PfABXmCq3XoM0QUYYXtj6rHNp9hy pPn+KNEdLR80EpVNHE8yfL3YuEn+2AmgiaQINiXuZGsdJy5Zraro+g7CudIQTO+6YvB+ 2YRES9GLjuAZHSGEvdOsM5e8iKqf/iHgDsy0Yspxr6S7HKqX2XxXA9SOScMexiAyUPw1 lRig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infotecs.ru header.s=mx header.b=yf9uSjnf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=infotecs.ru Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id x20-20020a631714000000b0057e21f51ab3si7508800pgl.665.2023.12.12.03.18.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 03:18:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@infotecs.ru header.s=mx header.b=yf9uSjnf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=infotecs.ru Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 097168053C69; Tue, 12 Dec 2023 03:18:07 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346394AbjLLLRh (ORCPT + 99 others); Tue, 12 Dec 2023 06:17:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346325AbjLLLRX (ORCPT ); Tue, 12 Dec 2023 06:17:23 -0500 Received: from mx0.infotecs.ru (mx0.infotecs.ru [91.244.183.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19C63F3 for ; Tue, 12 Dec 2023 03:17:24 -0800 (PST) Received: from mx0.infotecs-nt (localhost [127.0.0.1]) by mx0.infotecs.ru (Postfix) with ESMTP id 01A9512012CC; Tue, 12 Dec 2023 14:17:22 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mx0.infotecs.ru 01A9512012CC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=infotecs.ru; s=mx; t=1702379842; bh=NGgi0dcX52R2Hsj4L3+FbfcgEQzNcKvB9lQQ3MuTsMU=; h=From:To:CC:Subject:Date:References:In-Reply-To:From; b=yf9uSjnfSWd9Z6nU5qzdpDu1E6K56w8q7WFPFG4TuxgPYtF2JMi4zUfPChAPYr/nA 8eb0GAF1eTAVW/7Mntf44BrvSbA0Hsrjh/nlAP3kR2IUHPPr9R7KscS7U8/9zzkS4Q atAMHnULdGJb873TXYD3d8cZGVwk/8yxi84neSNU= Received: from msk-exch-02.infotecs-nt (msk-exch-02.infotecs-nt [10.0.7.192]) by mx0.infotecs-nt (Postfix) with ESMTP id EE62D316EE01; Tue, 12 Dec 2023 14:17:21 +0300 (MSK) From: Gavrilov Ilia To: "stable@vger.kernel.org" , Greg Kroah-Hartman CC: Daniel Starke , Jiri Slaby , Russ Gorby , "linux-kernel@vger.kernel.org" , "lvc-project@linuxtesting.org" , Jiri Slaby Subject: [PATCH 5.10 2/3] tty: n_gsm, remove duplicates of parameters Thread-Topic: [PATCH 5.10 2/3] tty: n_gsm, remove duplicates of parameters Thread-Index: AQHaLOzGj9Cp/lEMc0uytGwtXjUNjQ== Date: Tue, 12 Dec 2023 11:17:21 +0000 Message-ID: <20231212111431.4064760-3-Ilia.Gavrilov@infotecs.ru> References: <20231212111431.4064760-1-Ilia.Gavrilov@infotecs.ru> In-Reply-To: <20231212111431.4064760-1-Ilia.Gavrilov@infotecs.ru> Accept-Language: ru-RU, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.17.0.10] x-exclaimer-md-config: 208ac3cd-1ed4-4982-a353-bdefac89ac0a MIME-Version: 1.0 X-KLMS-Rule-ID: 5 X-KLMS-Message-Action: clean X-KLMS-AntiSpam-Status: not scanned, disabled by settings X-KLMS-AntiSpam-Interceptor-Info: not scanned X-KLMS-AntiPhishing: Clean, bases: 2023/12/12 08:32:00 X-KLMS-AntiVirus: Kaspersky Security for Linux Mail Server, version 8.0.3.30, bases: 2023/12/12 02:27:00 #22664189 X-KLMS-AntiVirus-Status: Clean, skipped X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, 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 agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 12 Dec 2023 03:18:07 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785074694748898757 X-GMAIL-MSGID: 1785074694748898757 From: Jiri Slaby commit b93db97e1ca08e500305bc46b08c72e2232c4be1 upstream. dp, f, and i are only duplicates of gsmld_receive_buf's parameters. Use the parameters directly (cp, fp, and count) and delete these local variables. Signed-off-by: Jiri Slaby Link: https://lore.kernel.org/r/20210302062214.29627-41-jslaby@suse.cz Signed-off-by: Greg Kroah-Hartman Signed-off-by: Gavrilov Ilia --- drivers/tty/n_gsm.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c index 7a883a2c0c50..2455f952e0aa 100644 --- a/drivers/tty/n_gsm.c +++ b/drivers/tty/n_gsm.c @@ -2577,27 +2577,24 @@ static void gsmld_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count) { struct gsm_mux *gsm = tty->disc_data; - const unsigned char *dp; - char *f; - int i; char flags = TTY_NORMAL; if (debug & 4) print_hex_dump_bytes("gsmld_receive: ", DUMP_PREFIX_OFFSET, cp, count); - for (i = count, dp = cp, f = fp; i; i--, dp++) { - if (f) - flags = *f++; + for (; count; count--, cp++) { + if (fp) + flags = *fp++; switch (flags) { case TTY_NORMAL: - gsm->receive(gsm, *dp); + gsm->receive(gsm, *cp); break; case TTY_OVERRUN: case TTY_BREAK: case TTY_PARITY: case TTY_FRAME: - gsm_error(gsm, *dp, flags); + gsm_error(gsm, *cp, flags); break; default: WARN_ONCE(1, "%s: unknown flag %d\n", From patchwork Tue Dec 12 11:17:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gavrilov Ilia X-Patchwork-Id: 177266 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7651069vqy; Tue, 12 Dec 2023 03:18:01 -0800 (PST) X-Google-Smtp-Source: AGHT+IFgPhJJqs1ZPQj2cLijfGcwIDnNJu3ElgoIt5ttdQS2Xw0VCFp5c96H8lFqa+az9iAyRm5d X-Received: by 2002:a17:903:2988:b0:1d3:387c:f9b8 with SMTP id lm8-20020a170903298800b001d3387cf9b8mr166687plb.119.1702379880792; Tue, 12 Dec 2023 03:18:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702379880; cv=none; d=google.com; s=arc-20160816; b=dH6N/5ONjec5WSEmQzh2ZKPEyzzZpG6KJnanNxON41lTbbR5DKSTiX8/oThefv1+AM l2ZUpStPZ5V8ODwwsAVNiy/1KpOHZSKGtbaRbUhV0QRRhvEKtPstL2l4bSYiRtFLGSCC V/LGtdYWOhjE1hqAyaxDltz+ouNhgSFwEQ6cZyMDb3eftudDpjv06TjaQYyZxwzTzqD9 BDC/FXRQuuCobKGB6aZVdL66qaJMshDvPlHZRwQTlR9c7QgTr18gM59n/8Dj1WRi+MO6 /H5RisI4tj50u5PlkfMii7MVZ5uJzgr4bXJNpXBFsKJvOADnebTxjNuAELiXDBqUamn3 2Y4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:cc:to:from:dkim-signature :dkim-filter; bh=9bSZRvhCRx5ZJOxhq5dQQZcW3HOYwOVchaVahsGhmC4=; fh=u+G+CD1eYM6Vo3b8xzbHF1u8aVS/uLL7/k29XSHONQc=; b=xJJA1A6ZR4cPYKk7ArioySRfTQon9++I/DAd9H6QI6cOzpxQSnAgWZUJGxBco8Dx1R QaFtkPLoHzxHEcrILgK+4JKto71mGpXCuSTE6OF8rsNJbKiSus5ChfaMzZxCoi50Vl8x xMR06m2cebH00tAtkDcbAVeaspXTFR5fewan/H/KfOkTJmKfn6qKrUOn+oWeox8jGcvq IL0/1tvAbVsdKTO1TYLFO9wdlshJpBJSE/VV5VIeBa18XrCxfvi2PvMek1CNNpviZGpv 2LbdxdqE9xnK0PNsPmhYrheX5En2wzi9CH3RQKl0PTAEYb3t0i2NAy7b7Riyk3hXc74l vWnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infotecs.ru header.s=mx header.b=sUS+QwZS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=infotecs.ru Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id o12-20020a170902bccc00b001cfb4d36d44si7487879pls.64.2023.12.12.03.18.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 03:18:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@infotecs.ru header.s=mx header.b=sUS+QwZS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=infotecs.ru Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 35C9C80215FF; Tue, 12 Dec 2023 03:17:58 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346339AbjLLLRe (ORCPT + 99 others); Tue, 12 Dec 2023 06:17:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346326AbjLLLRX (ORCPT ); Tue, 12 Dec 2023 06:17:23 -0500 Received: from mx0.infotecs.ru (mx0.infotecs.ru [91.244.183.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C339EA for ; Tue, 12 Dec 2023 03:17:24 -0800 (PST) Received: from mx0.infotecs-nt (localhost [127.0.0.1]) by mx0.infotecs.ru (Postfix) with ESMTP id 29F5912012B4; Tue, 12 Dec 2023 14:17:22 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mx0.infotecs.ru 29F5912012B4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=infotecs.ru; s=mx; t=1702379842; bh=9bSZRvhCRx5ZJOxhq5dQQZcW3HOYwOVchaVahsGhmC4=; h=From:To:CC:Subject:Date:References:In-Reply-To:From; b=sUS+QwZS/BUI2i+pNguzOC3QXoOfVo8YH+i56eACSvkMkJzIts/9LltTnfytI91Qc C76Mxo6v1b4HQv07X//uQZVp4CaqDtpX3TOQ5ZLT3LQ9g+EGlFvagHhqb3jYYy6jV8 elrpT/F/ZgO3Cq2yYNtBtHUcmxMz7uCqGYjrv764= Received: from msk-exch-01.infotecs-nt (msk-exch-01.infotecs-nt [10.0.7.191]) by mx0.infotecs-nt (Postfix) with ESMTP id 25F4D316AAFE; Tue, 12 Dec 2023 14:17:22 +0300 (MSK) From: Gavrilov Ilia To: "stable@vger.kernel.org" , Greg Kroah-Hartman CC: Daniel Starke , Jiri Slaby , Russ Gorby , "linux-kernel@vger.kernel.org" , "lvc-project@linuxtesting.org" , "syzbot+e3563f0c94e188366dbb@syzkaller.appspotmail.com" , Mazin Al Haddad Subject: [PATCH 5.10 3/3] tty: n_gsm: add sanity check for gsm->receive in gsm_receive_buf() Thread-Topic: [PATCH 5.10 3/3] tty: n_gsm: add sanity check for gsm->receive in gsm_receive_buf() Thread-Index: AQHaLOzG6jNgFQFcyE2RxcW+B/dJcw== Date: Tue, 12 Dec 2023 11:17:21 +0000 Message-ID: <20231212111431.4064760-4-Ilia.Gavrilov@infotecs.ru> References: <20231212111431.4064760-1-Ilia.Gavrilov@infotecs.ru> In-Reply-To: <20231212111431.4064760-1-Ilia.Gavrilov@infotecs.ru> Accept-Language: ru-RU, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.17.0.10] x-exclaimer-md-config: 208ac3cd-1ed4-4982-a353-bdefac89ac0a MIME-Version: 1.0 X-KLMS-Rule-ID: 5 X-KLMS-Message-Action: clean X-KLMS-AntiSpam-Status: not scanned, disabled by settings X-KLMS-AntiSpam-Interceptor-Info: not scanned X-KLMS-AntiPhishing: Clean, bases: 2023/12/12 08:32:00 X-KLMS-AntiVirus: Kaspersky Security for Linux Mail Server, version 8.0.3.30, bases: 2023/12/12 02:27:00 #22664189 X-KLMS-AntiVirus-Status: Clean, skipped X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, 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 morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 12 Dec 2023 03:17:58 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785074686007922252 X-GMAIL-MSGID: 1785074686007922252 From: Mazin Al Haddad commit f16c6d2e58a4c2b972efcf9eb12390ee0ba3befb upstream A null pointer dereference can happen when attempting to access the "gsm->receive()" function in gsmld_receive_buf(). Currently, the code assumes that gsm->receive is only called after MUX activation. Since the gsmld_receive_buf() function can be accessed without the need to initialize the MUX, the gsm->receive() function will not be set and a NULL pointer dereference will occur. Fix this by avoiding the call to "gsm->receive()" in case the function is not initialized by adding a sanity check. Call Trace: gsmld_receive_buf+0x1c2/0x2f0 drivers/tty/n_gsm.c:2861 tiocsti drivers/tty/tty_io.c:2293 [inline] tty_ioctl+0xa75/0x15d0 drivers/tty/tty_io.c:2692 vfs_ioctl fs/ioctl.c:51 [inline] __do_sys_ioctl fs/ioctl.c:870 [inline] __se_sys_ioctl fs/ioctl.c:856 [inline] __x64_sys_ioctl+0x193/0x200 fs/ioctl.c:856 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x35/0xb0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcd Link: https://syzkaller.appspot.com/bug?id=bdf035c61447f8c6e0e6920315d577cb5cc35ac5 Fixes: 01aecd917114 ("tty: n_gsm: fix tty registration before control channel open") Reported-and-tested-by: syzbot+e3563f0c94e188366dbb@syzkaller.appspotmail.com Signed-off-by: Mazin Al Haddad Link: https://lore.kernel.org/r/20220814015211.84180-1-mazinalhaddad05@gmail.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Gavrilov Ilia --- drivers/tty/n_gsm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c index 2455f952e0aa..fa49529682ce 100644 --- a/drivers/tty/n_gsm.c +++ b/drivers/tty/n_gsm.c @@ -2588,7 +2588,8 @@ static void gsmld_receive_buf(struct tty_struct *tty, const unsigned char *cp, flags = *fp++; switch (flags) { case TTY_NORMAL: - gsm->receive(gsm, *cp); + if (gsm->receive) + gsm->receive(gsm, *cp); break; case TTY_OVERRUN: case TTY_BREAK: