[RAMEN9610-22009]tty: Fix ->pgrp locking in tiocspgrp()
authorJann Horn <jannh@google.com>
Thu, 3 Dec 2020 01:25:04 +0000 (02:25 +0100)
committerlingsen2 <lingsen2@lenovo.com>
Wed, 21 Apr 2021 03:42:03 +0000 (11:42 +0800)
commitffa580eb04df28a6061e2d67fe60c00497dfc39d
tree935d72f09a2f4738b5e24483b8c0e417204ed62b
parent3d19862ce96b5bfab87af7989977f1c596a7a00a
[RAMEN9610-22009]tty: Fix ->pgrp locking in tiocspgrp()

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.

Change-Id: I000d97f52d398cdf171fb8fb53d9b91456e9dc4c
Fixes: 47f86834bbd4 ("redo locking of tty->pgrp")
CC: stable@kernel.org
Signed-off-by: Jann Horn <jannh@google.com>
Reviewed-by: Jiri Slaby <jirislaby@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/tty/tty_jobctrl.c