Received: by 2002:a05:6a10:8a4d:0:0:0:0 with SMTP id dn13csp556273pxb; Fri, 13 Aug 2021 00:44:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzQv/5Cc9DlOFQSk/VLjqJA0YKbHoPG5yAOZBXe7MR2zroMFK8+3+VcyRQXcKiN1P46pBuN X-Received: by 2002:a50:da0e:: with SMTP id z14mr1469465edj.73.1628840667048; Fri, 13 Aug 2021 00:44:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628840667; cv=none; d=google.com; s=arc-20160816; b=PYZnwAD8RAbmVY7vdIOQ8ASJIHhELu+KP0C72kcoALlD2lhKNyTPKMaIkJtDwalP5N nRD685aaTSlXy8ieHCKGBgEEhzrSRjMSy/QHZn38ZmmTbwapGmnKbnjLARVUsYzpl9i/ ypRlNl8ClpQK0SOr7qWYuJH1xmGR/sJFiMwDAWZDjkNcqcozjAPWYj7/DZs7gQtD+UDU upLKUAIX4EKuNKrPTLngnGywZZkUU25IhY/RoRdLHFmnIWdiJSAgF5w7C/g525bi2cMM yRWh0VweXeTCC1Xtzqh3HJyTMxv/6irFcmQGeI3mdEaz/OfdvOLHjE1Z9jLYZZjUP1H2 FbFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=Izd+QuwvMRc9c396ADYtcjfAVXv63HnOfL26zdV9alY=; b=xYHyHWUXdRbdp/Aa54+qdre6w8zUU1H6Y25DvOSW5rtpXgVib8ZACrRQwP9k18VMD3 DfLKGejArbHDINjZxKtrF07OOI0vZy+OEcc0s6BPoSBG0JyxhMLO/NSvDO4kCGkjzuaX 2TorjkMEVhsreXJyLLoF1WqJMAd60nJTAJnN8chs+VoDtYmaKjNvA5h/o+7H9PXb9PhX GH4MJi0ArP9iR9iHHfnqb4v5UH6cbpDB3JSSUK+lA3f0sV4+gsfXTz7Gek8vSHAmXXGX J2he/KLaze5MnLRTc2evr6haxUyrZGAmFem0YpX9vDzLkTiowaTYUBC1XEjSa/kRFadg dhGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UzTtsDLO; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p17si996306edq.609.2021.08.13.00.43.55; Fri, 13 Aug 2021 00:44: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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UzTtsDLO; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239449AbhHMHd7 (ORCPT + 99 others); Fri, 13 Aug 2021 03:33:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:45222 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230469AbhHMHd6 (ORCPT ); Fri, 13 Aug 2021 03:33:58 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 86E4960720; Fri, 13 Aug 2021 07:33:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1628840012; bh=/Ez7HIp0+x6gkQ+AXt7EEyRoY2iUgGHXpwEagk1RKCA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=UzTtsDLO6hrRf1Kjf1f+mnW0PKeqBbHHUQDaGx/+IcI7qjKN63ucgw3OZw+qeNTdr eAiF7XUSphEMaUqwI24TgogVna/wL0nWwDNYbH5ggprJAovLp1MH/4m4GtlDz7jv1S CA/wuFx+U6ggIhw5FH5h0WjviZknlKjFn2NeAlQc= Date: Fri, 13 Aug 2021 09:33:29 +0200 From: Greg KH To: Nguyen Dinh Phi Cc: jirislaby@kernel.org, linux-kernel-mentees@lists.linuxfoundation.org, linux-kernel@vger.kernel.org, syzbot+97388eb9d31b997fe1d0@syzkaller.appspotmail.com Subject: Re: [PATCH] tty: Fix data race between tiocsti() and flush_to_ldisc() Message-ID: References: <20210807190513.3810821-1-phind.uet@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210807190513.3810821-1-phind.uet@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Aug 08, 2021 at 03:05:13AM +0800, Nguyen Dinh Phi wrote: > The ops->receive_buf() may be accessed concurrently from these two > functions. If the driver flushes data to the line discipline > receive_buf() method while tiocsti() is using the ops->receive_buf(), > the data race will happen. > > For example: > tty_ioctl |tty_ldisc_receive_buf > ->tioctsi | ->tty_port_default_receive_buf > | ->tty_ldisc_receive_buf > ->hci_uart_tty_receive | ->hci_uart_tty_receive > ->h4_recv | ->h4_recv > > In this case, the h4 receive buffer will be overwritten by the > latecomer, and it cause a memory leak. That looks to be a bug in the h4 code, if the receive_buf() call can not be run at the same time, it should have a lock in it, right? > Hence, change tioctsi() function to use the exclusive lock interface > from tty_buffer to avoid the data race. Where is the lock being grabbed from the other receive_buf() call path to ensure that the lock is always needed here? > > Signed-off-by: Nguyen Dinh Phi > Reported-by: syzbot+97388eb9d31b997fe1d0@syzkaller.appspotmail.com > --- > drivers/tty/tty_io.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c > index e8532006e960..746fe13a2054 100644 > --- a/drivers/tty/tty_io.c > +++ b/drivers/tty/tty_io.c > @@ -2307,8 +2307,10 @@ static int tiocsti(struct tty_struct *tty, char __user *p) > ld = tty_ldisc_ref_wait(tty); > if (!ld) > return -EIO; > + tty_buffer_lock_exclusive(tty->port); > if (ld->ops->receive_buf) > ld->ops->receive_buf(tty, &ch, &mbz, 1); > + tty_buffer_unlock_exclusive(tty->port); Did this fix the syzbot reported issue? thanks, greg k-h