Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp2494754ybk; Tue, 12 May 2020 00:24:06 -0700 (PDT) X-Google-Smtp-Source: APiQypKeqB8vsLtxdMLEYL/+gRUmI+Gfl20csx41EaIt9b3V/b0OLDOPVy4cGxFEl1Ef7bLT/fB3 X-Received: by 2002:a17:906:86d6:: with SMTP id j22mr9401282ejy.1.1589268246699; Tue, 12 May 2020 00:24:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589268246; cv=none; d=google.com; s=arc-20160816; b=0yx24yiKtMZcQAIFZHOUoGIR/79yx9qqTJN0AdNMbze4o+zmVZSvW0UrIJSF7i0cUW S3z75iHdCWbXqhNSet6W0Zjj1eY6YANbOMtpzOJKv+gJelyejeSVbUFNvlVCRtbQAklO X+TY+hBgAH+TDRuTKcWASWNCGlu1ENg25kVsBnR8ckmD44QyyylizuZN7bI9eE9GM/mZ 5E+vuuEUJEKZ/I3r0Ueo7b8iv1PlY441QfGhBCUeIHi8vSaegAw7G82AMyuqgVAQKyIR 8dIFTyY+rxQQfg3rP9cEKBPz+tHaqT5wAjk/5wQzu0cZVirW6hQxeTpBRioZHu2n1x9w yZQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:autocrypt:from:references:cc:to:subject; bh=Ckhr97itu7ecwTtHKMg3mpPJYyi0Q3AKX98QEWTxYHs=; b=po7dQH4xskftQ8XnZLL+o+9cjgGdL5m5rmkkGAnJxEH2ji3p581TiZGxevB52py7/b 0IAoAkO/5KsdYPPokYrJQPIePEmjD8nc39+aMt1F9l5w+m4MsyxDGvyXt5glXQ40bGkB uOTQUKbFPwATOvXGlQhN3XKt5y4rKQudzlk3OAa2yNn+NtZGpG1L/qzSd1KlcMr6oSNi 431uJiIOSukvkNU4UVD6G+lAdeDTMy9Zph7dxb7F0updw0jyoNbe1YJ0CbXP57k6iRr5 xBSvRjjW5dS7ei6WOAQ6c7tYiYXeBRMjUyBlrscuCDm7JByVtNoaNK4tYMRPAUOyj/Jm 5zXQ== 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 g8si2811002ejb.85.2020.05.12.00.23.43; Tue, 12 May 2020 00:24:06 -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 S1726187AbgELHWU (ORCPT + 99 others); Tue, 12 May 2020 03:22:20 -0400 Received: from mx2.suse.de ([195.135.220.15]:50344 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725813AbgELHWU (ORCPT ); Tue, 12 May 2020 03:22:20 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id D4916B1A2; Tue, 12 May 2020 07:22:19 +0000 (UTC) Subject: Re: [PATCH] tty: hvc: Fix data abort due to race in hvc_open To: Greg KH , rananta@codeaurora.org Cc: andrew@daynix.com, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org References: <20200428032601.22127-1-rananta@codeaurora.org> <20200506094851.GA2787548@kroah.com> <98bbe7afabf48d8e8fe839fdc9e836a5@codeaurora.org> <20200510064819.GB3400311@kroah.com> <77d889be4e0cb0e6e30f96199e2d843d@codeaurora.org> <20200511073913.GA1347819@kroah.com> From: Jiri Slaby Autocrypt: addr=jsla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k3nhec4P9M7UWTSmYnIBJsclDQRx5wSh0Mc9Y/psx9 B42WbV4xrtiiydfBtO6tH6c9mT5Ng+d1sN/VTSPyfQARAQABiQIfBBgBAgAJBQJOkueGAhsM AAoJEL0lsQQGtHBJN7UQAIDvgxaW8iGuEZZ36XFtewH56WYvVUefs6+Pep9ox/9ZXcETv0vk DUgPKnQAajG/ViOATWqADYHINAEuNvTKtLWmlipAI5JBgE+5g9UOT4i69OmP/is3a/dHlFZ3 qjNk1EEGyvioeycJhla0RjakKw5PoETbypxsBTXk5EyrSdD/I2Hez9YGW/RcI/WC8Y4Z/7FS ITZhASwaCOzy/vX2yC6iTx4AMFt+a6Z6uH/xGE8pG5NbGtd02r+m7SfuEDoG3Hs1iMGecPyV XxCVvSV6dwRQFc0UOZ1a6ywwCWfGOYqFnJvfSbUiCMV8bfRSWhnNQYLIuSv/nckyi8CzCYIg c21cfBvnwiSfWLZTTj1oWyj5a0PPgGOdgGoIvVjYXul3yXYeYOqbYjiC5t99JpEeIFupxIGV ciMk6t3pDrq7n7Vi/faqT+c4vnjazJi0UMfYnnAzYBa9+NkfW0w5W9Uy7kW/v7SffH/2yFiK 9HKkJqkN9xYEYaxtfl5pelF8idoxMZpTvCZY7jhnl2IemZCBMs6s338wS12Qro5WEAxV6cjD VSdmcD5l9plhKGLmgVNCTe8DPv81oDn9s0cIRLg9wNnDtj8aIiH8lBHwfUkpn32iv0uMV6Ae sLxhDWfOR4N+wu1gzXWgLel4drkCJcuYK5IL1qaZDcuGR8RPo3jbFO7Y Message-ID: <0f7791f5-0a53-59f6-7277-247a789f30c2@suse.cz> Date: Tue, 12 May 2020 09:22:15 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 In-Reply-To: <20200511073913.GA1347819@kroah.com> Content-Type: text/plain; charset=iso-8859-2 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11. 05. 20, 9:39, Greg KH wrote: > On Mon, May 11, 2020 at 12:23:58AM -0700, rananta@codeaurora.org wrote: >> On 2020-05-09 23:48, Greg KH wrote: >>> On Sat, May 09, 2020 at 06:30:56PM -0700, rananta@codeaurora.org wrote: >>>> On 2020-05-06 02:48, Greg KH wrote: >>>>> On Mon, Apr 27, 2020 at 08:26:01PM -0700, Raghavendra Rao Ananta wrote: >>>>>> Potentially, hvc_open() can be called in parallel when two tasks calls >>>>>> open() on /dev/hvcX. In such a scenario, if the >>>>>> hp->ops->notifier_add() >>>>>> callback in the function fails, where it sets the tty->driver_data to >>>>>> NULL, the parallel hvc_open() can see this NULL and cause a memory >>>>>> abort. >>>>>> Hence, serialize hvc_open and check if tty->private_data is NULL >>>>>> before >>>>>> proceeding ahead. >>>>>> >>>>>> The issue can be easily reproduced by launching two tasks >>>>>> simultaneously >>>>>> that does nothing but open() and close() on /dev/hvcX. >>>>>> For example: >>>>>> $ ./simple_open_close /dev/hvc0 & ./simple_open_close /dev/hvc0 & >>>>>> >>>>>> Signed-off-by: Raghavendra Rao Ananta >>>>>> --- >>>>>> drivers/tty/hvc/hvc_console.c | 16 ++++++++++++++-- >>>>>> 1 file changed, 14 insertions(+), 2 deletions(-) >>>>>> >>>>>> diff --git a/drivers/tty/hvc/hvc_console.c >>>>>> b/drivers/tty/hvc/hvc_console.c >>>>>> index 436cc51c92c3..ebe26fe5ac09 100644 >>>>>> --- a/drivers/tty/hvc/hvc_console.c >>>>>> +++ b/drivers/tty/hvc/hvc_console.c >>>>>> @@ -75,6 +75,8 @@ static LIST_HEAD(hvc_structs); >>>>>> */ >>>>>> static DEFINE_MUTEX(hvc_structs_mutex); >>>>>> >>>>>> +/* Mutex to serialize hvc_open */ >>>>>> +static DEFINE_MUTEX(hvc_open_mutex); >>>>>> /* >>>>>> * This value is used to assign a tty->index value to a hvc_struct >>>>>> based >>>>>> * upon order of exposure via hvc_probe(), when we can not match it >>>>>> to >>>>>> @@ -346,16 +348,24 @@ static int hvc_install(struct tty_driver >>>>>> *driver, struct tty_struct *tty) >>>>>> */ >>>>>> static int hvc_open(struct tty_struct *tty, struct file * filp) >>>>>> { >>>>>> - struct hvc_struct *hp = tty->driver_data; >>>>>> + struct hvc_struct *hp; >>>>>> unsigned long flags; >>>>>> int rc = 0; >>>>>> >>>>>> + mutex_lock(&hvc_open_mutex); >>>>>> + >>>>>> + hp = tty->driver_data; >>>>>> + if (!hp) { >>>>>> + rc = -EIO; >>>>>> + goto out; >>>>>> + } >>>>>> + >>>>>> spin_lock_irqsave(&hp->port.lock, flags); >>>>>> /* Check and then increment for fast path open. */ >>>>>> if (hp->port.count++ > 0) { >>>>>> spin_unlock_irqrestore(&hp->port.lock, flags); >>>>>> hvc_kick(); >>>>>> - return 0; >>>>>> + goto out; >>>>>> } /* else count == 0 */ >>>>>> spin_unlock_irqrestore(&hp->port.lock, flags); >>>>> >>>>> Wait, why isn't this driver just calling tty_port_open() instead of >>>>> trying to open-code all of this? >>>>> >>>>> Keeping a single mutext for open will not protect it from close, it will >>>>> just slow things down a bit. There should already be a tty lock held by >>>>> the tty core for open() to keep it from racing things, right? >>>> The tty lock should have been held, but not likely across >>>> ->install() and >>>> ->open() callbacks, thus resulting in a race between hvc_install() and >>>> hvc_open(), >>> >>> How? The tty lock is held in install, and should not conflict with >>> open(), otherwise, we would be seeing this happen in all tty drivers, >>> right? >>> >> Well, I was expecting the same, but IIRC, I see that the open() was being >> called in parallel for the same device node. > > So open and install are happening at the same time? And the tty_lock() > does not protect the needed fields from being protected properly? If > not, what fields are being touched without the lock? > >> Is it expected that the tty core would allow only one thread to >> access the dev-node, while blocking the other, or is it the client >> driver's responsibility to handle the exclusiveness? > > The tty core should handle this correctly, for things that can mess > stuff up (like install and open at the same time). A driver should not > have to worry about that. > >>>> where hvc_install() sets a data and the hvc_open() clears it. >>>> hvc_open() >>>> doesn't >>>> check if the data was set to NULL and proceeds. >>> >>> What data is being set that hvc_open is checking? >> hvc_install sets tty->private_data to hp, while hvc_open sets it to NULL (in >> one of the paths). > > I see no use of private_data in drivers/tty/hvc/ so what exactly are you > referring to? He likely means tty->driver_data. And there exactly lays the issue. commit bdb498c20040616e94b05c31a0ceb3e134b7e829 Author: Jiri Slaby Date: Tue Aug 7 21:48:04 2012 +0200 TTY: hvc_console, add tty install added hvc_install but did not move 'tty->driver_data = NULL;' from hvc_open's fail path to hvc_cleanup. IOW hvc_open now NULLs tty->driver_data even for another task which opened the tty earlier. The same holds for "tty_port_tty_set(&hp->port, NULL);" there. And actually "tty_port_put(&hp->port);" is also incorrect for the 2nd task opening the tty. So, a mutex with tty->driver_data check in open is not definitely the way to go. This mess needs to be sorted out properly. Sure, a good start would be a conversion to tty_port_open. Right after dropping "tty: hvc: Fix data abort due to race in hvc_open" from tty/tty-next :). What I *don't* understand is why hp->ops->notifier_add fails, given the open does not allow multiple opens anyway? thanks, -- js suse labs