Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp25466pxb; Mon, 13 Sep 2021 11:59:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzduerpBHthEtqCBdCGITyTAWAhISPyJcFBNQG6JA+C+CxhuY2zaJ7zaJtjJHfYNbsN3oOX X-Received: by 2002:a02:9566:: with SMTP id y93mr11105827jah.55.1631559565664; Mon, 13 Sep 2021 11:59:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631559565; cv=none; d=google.com; s=arc-20160816; b=OrTp90QSKe8+BpM7SllPqsdlZ6lobknefc/0TypkWW6QzJWszhAaB3V2gzeT3WNFKp O9LhFkswC3XUADHrI6QvV6fe+PxUzrEBG/tLdyYcLPwAl6IldwMR1KlwcwOtD69g1zM+ juFQGJHwQRzZsN5nd0y7IISk7LdMNp70HWbPoX855Dgd3cjiXRIWp/U0bBTbCAwTOP23 2gDkMcjr9GuFhkcCTJ2k+XJ5qEwQCPODvKQMypD8BOFk+eid3SHVRJgg5FP5raRa1SuX HuxfX4JbTSoKH7BTnLQ2l39EzgP7JB/sVWMd1LmDshOSn5QCJoF/Y7fztmftDUNqy1er oXow== 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=jz+cWnK0SXZDy3gYC+rrLyBG0sS72hsy6j8ns2wq1ng=; b=X9XedXIo+ZS7+TyPg6urorcwOYqY60wJK4WJfhU3ZLCWfW53xxJUNxVmv+UR28kkBk RmATxa4D6xiSLoDLyYgz/+3j+zx8api3MQB/WV/By2nevxnT/sA6RiYjtJsOm87lySZO IMO+8MdsDCHjAkSTH+PoqsyUKpnCuO0BOWaBXbH8JuXhGkmS3ZOSNqraTCvvLTd5lpne BGCvVTojIHZx+WPekl7H1h29SNc/Ah4qa19kAJHR/bs9vDs7pdeFAX03ks9ZnOu8c7v5 OWyybqLgS1h4ml/ecijCUzbwpB4Xj9BOe3aQ7SJflsNMI59DhLpSJa+12fgFhnS6xWAg MT9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tvATMF4z; 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 u3si6966198jae.21.2021.09.13.11.59.13; Mon, 13 Sep 2021 11:59:25 -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=tvATMF4z; 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 S241874AbhIMN26 (ORCPT + 99 others); Mon, 13 Sep 2021 09:28:58 -0400 Received: from mail.kernel.org ([198.145.29.99]:49590 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242095AbhIMN04 (ORCPT ); Mon, 13 Sep 2021 09:26:56 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 6B034610CC; Mon, 13 Sep 2021 13:23:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631539383; bh=SfBues2A5Xcjg1xXDUXbodvUKEhsfSGLQY6Kb55kDF4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tvATMF4z7oJrMk7MOb1yYwTRBiZJgsgKKQ098wgcWRlHQL72Phj7rvB1wCY1knqst NUCaBrITqnstOREiEr5mUglw77csT3wmpKDGh5QkCCaJrHeMZmWYa++GLgXcBwfIh/ WY6osUqw1hXshB2QtfBJPD4EfbBA6BcfBk1igBzc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+97388eb9d31b997fe1d0@syzkaller.appspotmail.com, Jiri Slaby , Nguyen Dinh Phi Subject: [PATCH 5.4 132/144] tty: Fix data race between tiocsti() and flush_to_ldisc() Date: Mon, 13 Sep 2021 15:15:13 +0200 Message-Id: <20210913131052.342465555@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210913131047.974309396@linuxfoundation.org> References: <20210913131047.974309396@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Nguyen Dinh Phi commit bb2853a6a421a052268eee00fd5d3f6b3504b2b1 upstream. 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 waiting for the ops->receive_buf() to finish its work, 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 we will lost the data. Hence, change tioctsi() function to use the exclusive lock interface from tty_buffer to avoid the data race. Reported-by: syzbot+97388eb9d31b997fe1d0@syzkaller.appspotmail.com Reviewed-by: Jiri Slaby Signed-off-by: Nguyen Dinh Phi Link: https://lore.kernel.org/r/20210823000641.2082292-1-phind.uet@gmail.com Cc: stable Signed-off-by: Greg Kroah-Hartman --- drivers/tty/tty_io.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- a/drivers/tty/tty_io.c +++ b/drivers/tty/tty_io.c @@ -2176,8 +2176,6 @@ static int tty_fasync(int fd, struct fil * Locking: * Called functions take tty_ldiscs_lock * current->signal->tty check is safe without locks - * - * FIXME: may race normal receive processing */ static int tiocsti(struct tty_struct *tty, char __user *p) @@ -2193,8 +2191,10 @@ static int tiocsti(struct tty_struct *tt 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); tty_ldisc_deref(ld); return 0; }