Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp36383pxu; Thu, 10 Dec 2020 17:29:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJw0U4I100yxc02KZceNdrmEOiRNuZ4+EEQ0JA9EqhtSqRHFMRPw1GSawNxhb+EbW67F+HIf X-Received: by 2002:a05:6402:4d2:: with SMTP id n18mr9454256edw.169.1607650185566; Thu, 10 Dec 2020 17:29:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607650185; cv=none; d=google.com; s=arc-20160816; b=uYZYSVkRuoSQxiXIzlPlI3DTzc8eHRqjar1EtjV89/mLF99r+0Vg5nmav0eGJz8sSl P1QfYcq6N49O2OVidG3GFqc+T6Q1b3dbMf1FlwmD0VWTsfokIGoOOtjHwXjqXSmZq+Od uYc8L0aCanQAO8ZQnW28pSpC33Lcz6duBSLqoTWLpXYh+ucJ+fkWe6HzNoQk3chCcqw0 UPHqHuBV6CMFsmHNMhYE/AVB/708cFHSQlYMkfb2344zGJYrGdjvd7AJ+cndl/hc/1KF HRKHCtDuvjTfLxentvbal8JndJtMdQpmC0iexsYCNFIyi3AswTX9lVBtOdAwQpq7umky hAIQ== 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; bh=/aCPuCZLEtDPBsvISUvx6LjPwAaDulNmeBsLGGxbOHM=; b=W71s0EoQLzU2+1b2nOrLH/QFzV4AdplHlXKAD/YXq8kyvymlLMxQ5Db81AXgbq9Xsq uq+JCaO/+do+ezTh0aWJWNyoV+9yhaWd/wm3BskDBkxD8qMql2Kx0YaM0EDPkyXn26GG WNkG8O663WrgWDfCVQn7A/KU1/cwXJbwFgOjEUR8igbLXU4YLieliF5u1ADBVRKbBKKl gxgIyuohRvc8RSEICtzcNhJPQWGzkbHpZV2W+VLUdcIojdUxeOzk8idUOJ0XTBjX4yC3 H3DD5SQJesUdWYLHwQlHTcoYUzF9NCXJW6SsT9c0ScMsBtaw87NGioCa6CY3ANJZ6CKg H3qw== 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; dmarc=fail (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 bt17si4093019edb.469.2020.12.10.17.29.23; Thu, 10 Dec 2020 17:29:45 -0800 (PST) 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390774AbgLJOdi (ORCPT + 99 others); Thu, 10 Dec 2020 09:33:38 -0500 Received: from mail.kernel.org ([198.145.29.99]:40014 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389280AbgLJOcd (ORCPT ); Thu, 10 Dec 2020 09:32:33 -0500 From: Greg Kroah-Hartman Authentication-Results: mail.kernel.org; dkim=permerror (bad message/signature format) To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, stable@kernel.org, Jann Horn , Jiri Slaby Subject: [PATCH 4.14 12/31] tty: Fix ->pgrp locking in tiocspgrp() Date: Thu, 10 Dec 2020 15:26:49 +0100 Message-Id: <20201210142602.704815107@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201210142602.099683598@linuxfoundation.org> References: <20201210142602.099683598@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: Jann Horn commit 54ffccbf053b5b6ca4f6e45094b942fab92a25fc upstream. tiocspgrp() takes two tty_struct pointers: One to the tty that userspace passed to ioctl() (`tty`) and one to the TTY being changed (`real_tty`). These pointers are different when ioctl() is called with a master fd. To properly lock real_tty->pgrp, we must take real_tty->ctrl_lock. This bug makes it possible for racing ioctl(TIOCSPGRP, ...) calls on both sides of a PTY pair to corrupt the refcount of `struct pid`, leading to use-after-free errors. Fixes: 47f86834bbd4 ("redo locking of tty->pgrp") CC: stable@kernel.org Signed-off-by: Jann Horn Reviewed-by: Jiri Slaby Signed-off-by: Greg Kroah-Hartman --- drivers/tty/tty_jobctrl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- a/drivers/tty/tty_jobctrl.c +++ b/drivers/tty/tty_jobctrl.c @@ -493,10 +493,10 @@ static int tiocspgrp(struct tty_struct * if (session_of_pgrp(pgrp) != task_session(current)) goto out_unlock; retval = 0; - spin_lock_irq(&tty->ctrl_lock); + spin_lock_irq(&real_tty->ctrl_lock); put_pid(real_tty->pgrp); real_tty->pgrp = get_pid(pgrp); - spin_unlock_irq(&tty->ctrl_lock); + spin_unlock_irq(&real_tty->ctrl_lock); out_unlock: rcu_read_unlock(); return retval;