Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp21936pxb; Wed, 14 Apr 2021 08:32:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwO8ehPfRMXLI8/bSC5bpA6j1e8tN2rzkOTMdOpAO1f/mI8DBHIiJRU99sVmZtJxid2P5d3 X-Received: by 2002:a05:6402:8:: with SMTP id d8mr41192532edu.368.1618414347792; Wed, 14 Apr 2021 08:32:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618414347; cv=none; d=google.com; s=arc-20160816; b=sUon8ewHXBS9N9K/vVZJJi3bP2kqPx6dt6W7MuMvpm5MWTMXEFUNmgfiBJE7e2dy21 aKswpZqfdO9PA2uHINk2z3xRJaU7LekLHfl2cCuQjqkYb9cgv4e29JYNgezCxkin30ZY 8ZQNRB0DAzfYcWXZn/J2LKL43+FjeiR7IDayrbyh/23KdP1/I9Nz1qCf4j3+OEC8C6Zy AESwg0/6EmaUmlqw5bVVPT0xFkp5Y+Rw/7Q46tm1OWK/MnmVO2x1uKr44T51Rl6wKHTz gV7R9reVlG4v0p91UW91K0gJqWs8U/mlyVICpz5Fot1ooVhcG+Kwy/6xVZlk7D3AZ92Z P2gg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:references:cc :to:from:subject; bh=fpNL3+Ls3kC/qlTwDmL6f21joD0Wl67L0cmHNz3XZiY=; b=ENj4wFBIwiZ7tsy3cMaBRQn92jG0qHw+na24LL+FpojzqTSQFf6qXNMhePcQtZ77HQ 5rqjfy9uWA2HOPJNzFH6zkp2HR8Y41fwEGi/M7ST0bLqUtrxsO4qJ5I7YEESuf7KELQT TllVdl0f09M83FFcJKpP/8R/nHwO2qliZheuucjxSaK7bFkTqXlrKYyYpKXXCX9Qs58v N4yRXKtZDAFh9vFnOoKp3HzY4JqbEV+l040yCAGzmEjEM/IBjgAKEGQIOvv4IycejZpP jzIUwHa/pVMBLRTS50+724nYsBox2dE8ocNOLS0uGwQ91/JYXqAG4Uo4muhGA7iHqP0N PCeA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a10si10959035ejr.711.2021.04.14.08.31.55; Wed, 14 Apr 2021 08:32:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235622AbhDNLMN (ORCPT + 99 others); Wed, 14 Apr 2021 07:12:13 -0400 Received: from www262.sakura.ne.jp ([202.181.97.72]:51287 "EHLO www262.sakura.ne.jp" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230281AbhDNLMM (ORCPT ); Wed, 14 Apr 2021 07:12:12 -0400 Received: from fsav304.sakura.ne.jp (fsav304.sakura.ne.jp [153.120.85.135]) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTP id 13EBBnhj073324; Wed, 14 Apr 2021 20:11:50 +0900 (JST) (envelope-from penguin-kernel@i-love.sakura.ne.jp) Received: from www262.sakura.ne.jp (202.181.97.72) by fsav304.sakura.ne.jp (F-Secure/fsigk_smtp/550/fsav304.sakura.ne.jp); Wed, 14 Apr 2021 20:11:49 +0900 (JST) X-Virus-Status: clean(F-Secure/fsigk_smtp/550/fsav304.sakura.ne.jp) Received: from [192.168.1.9] (M106072142033.v4.enabler.ne.jp [106.72.142.33]) (authenticated bits=0) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTPSA id 13EBBhIP073266 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NO); Wed, 14 Apr 2021 20:11:49 +0900 (JST) (envelope-from penguin-kernel@i-love.sakura.ne.jp) Subject: Re: How to handle concurrent access to /dev/ttyprintk ? From: Tetsuo Handa To: Greg Kroah-Hartman , Samo Pogacnik , Jiri Slaby Cc: Petr Mladek , Sergey Senozhatsky , Steven Rostedt , John Ogness , linux-kernel@vger.kernel.org, syzkaller-bugs References: <20210403041444.4081-1-penguin-kernel@I-love.SAKURA.ne.jp> <3c15d32f-c568-7f6f-fa7e-af4deb9b49f9@i-love.sakura.ne.jp> <051b550c-1cdd-6503-d2b7-0877bf0578fc@i-love.sakura.ne.jp> <32e75be6-6e9f-b33f-d585-13db220519da@i-love.sakura.ne.jp> Message-ID: <095d5393-b212-c4d8-5d6d-666bd505cc3d@i-love.sakura.ne.jp> Date: Wed, 14 Apr 2021 20:11:40 +0900 User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021/04/14 9:45, Tetsuo Handa wrote: > On 2021/04/12 21:04, Greg Kroah-Hartman wrote: >>> Since syzkaller is a fuzzer, syzkaller happily opens /dev/ttyprintk from >>> multiple threads. Should we update syzkaller to use CONFIG_TTY_PRINTK=n ? >> >> Why? Can you not hit the same tty code paths from any other tty driver >> being open multiple times? Why is ttyprintk somehow "special" here? > > I found a simplified reproducer. If we call ioctl(TIOCVHANGUP) on /dev/ttyprintk , > "struct ttyprintk_port tpk_port".port.count cannot be decremented by > tty_port_close() from tpk_close() due to tty_hung_up_p() == true when > close() is called. As a result, tty->count and port count gets out of sync. > > Then, when /dev/ttyprintk is opened again and then closed without calling > ioctl(TIOCVHANGUP), this message is printed due to tty_hung_up_p() == false. > > If I replace /dev/ttyprintk with /dev/ttyS0 in the reproducer shown above, > this message is not printed. > The difference between /dev/ttyS0 and /dev/ttyprintk is that the former provides uart_hangup() as "struct tty_operations"->hangup while the latter does not provide "struct tty_operations"->hangup . It seems to me that below patch avoids this message, but I'm not familiar with tty code. Is this fix correct? Don't we need to enforce all drivers to provide "struct tty_operations"->hangup in order to reset port count ? diff --git a/drivers/char/ttyprintk.c b/drivers/char/ttyprintk.c index 6a0059e508e3..ff3b9a41b91b 100644 --- a/drivers/char/ttyprintk.c +++ b/drivers/char/ttyprintk.c @@ -158,12 +158,26 @@ static int tpk_ioctl(struct tty_struct *tty, return 0; } +/* + * TTY operations hangup function. + */ +static void tpk_hangup(struct tty_struct *tty) +{ + struct ttyprintk_port *tpkp = tty->driver_data; + unsigned long flags; + + spin_lock_irqsave(&tpkp->port.lock, flags); + tpkp->port.count = 0; + spin_unlock_irqrestore(&tpkp->port.lock, flags); +} + static const struct tty_operations ttyprintk_ops = { .open = tpk_open, .close = tpk_close, .write = tpk_write, .write_room = tpk_write_room, .ioctl = tpk_ioctl, + .hangup = tpk_hangup, }; static const struct tty_port_operations null_ops = { };