Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp5012961pxu; Thu, 10 Dec 2020 10:41:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJyuPj5WV6+CaKNI3RHGk/DgIUUxdsUVstcNosFxUPOc4SLTrwyu56MR40Yi5PDWU9jDfH7o X-Received: by 2002:a17:906:5f97:: with SMTP id a23mr7898042eju.128.1607625700774; Thu, 10 Dec 2020 10:41:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607625700; cv=none; d=google.com; s=arc-20160816; b=akB0b2CD0I0uy67xhzgex0XRHFTMlh8ORDWbNl/pf6bQtgdrXg/+zCgl2BdQPjiSkm kDoDMFXDeSE4YT58qGkQ8BBeqOqNeUOcRYZvT/qSUD0eZacx1EhBhAGl38O7Wjo+USR7 BPc9YicEMdQtL4irfF7bpUVBfdzCpiV1bPcsr7gG2x/al8t8F6Wy6s6r4o8zh9ndtmNY m3oBbkMUTRTYI1Qk+eCZjQBy/MmLQ0PRAjmopUqtMM8wbqI5RrHx/qZvn6o66lFpHeLt DQl9X3W9qhp59l1W9VvJzkXhbhKFavtMxXq+XaE6m5KX+9qXG1PJBeJVvtOQZn+zC50/ TDcQ== 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=4BEyFy1Fvn8+nWuL6shFqV/WlJSXSXEjmwbGgc7Ylfk=; b=Z9T/D+9iAu1LeiNo0wMYIXxhk5vA6h4Z1oMk60SZZYBl5mY9k25dY6nJCgeJUm2Av2 u1NbPIhaYGur7M4NbilNUdokh1fgiXnV5lpDDXxnVh0uC38ECpKw9NILUtcdLbJST/vZ 70odUXPsuhPI7qgAxonE8Ho7ikyY//LgRM310nLfr2kXrwPnwhXaR/kbaw2sTOcWr42v aRdX4opIVoKf3McLILWca5EJ4A8GCXnPaIXgZyXHAY+4urgM9EVBcQXP7GJtyXxye9/K LoZZbZiuLDIbou2ZQ2C4GNKR0aQ4W3w6Tk4R9MSn8zDWno9ueBJYV2q1Kcf2z5VxT2Y5 8Jaw== 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 z17si3173146eda.610.2020.12.10.10.41.17; Thu, 10 Dec 2020 10:41:40 -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 S2390767AbgLJSha (ORCPT + 99 others); Thu, 10 Dec 2020 13:37:30 -0500 Received: from mail.kernel.org ([198.145.29.99]:41404 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390547AbgLJOcz (ORCPT ); Thu, 10 Dec 2020 09:32:55 -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.19 10/39] tty: Fix ->pgrp locking in tiocspgrp() Date: Thu, 10 Dec 2020 15:26:49 +0100 Message-Id: <20201210142602.801013772@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201210142602.272595094@linuxfoundation.org> References: <20201210142602.272595094@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 @@ -494,10 +494,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;