xref: /linux/drivers/tty/tty_jobctrl.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a1235b3eSNicolas Pitre /*
3a1235b3eSNicolas Pitre  *  Copyright (C) 1991, 1992  Linus Torvalds
4a1235b3eSNicolas Pitre  */
5a1235b3eSNicolas Pitre 
6a1235b3eSNicolas Pitre #include <linux/types.h>
7a1235b3eSNicolas Pitre #include <linux/errno.h>
8a1235b3eSNicolas Pitre #include <linux/signal.h>
9a1235b3eSNicolas Pitre #include <linux/sched/signal.h>
10a1235b3eSNicolas Pitre #include <linux/sched/task.h>
11a1235b3eSNicolas Pitre #include <linux/tty.h>
12a1235b3eSNicolas Pitre #include <linux/fcntl.h>
13a1235b3eSNicolas Pitre #include <linux/uaccess.h>
1498602c01SGreg Kroah-Hartman #include "tty.h"
15a1235b3eSNicolas Pitre 
is_ignored(int sig)16a1235b3eSNicolas Pitre static int is_ignored(int sig)
17a1235b3eSNicolas Pitre {
18a1235b3eSNicolas Pitre 	return (sigismember(&current->blocked, sig) ||
19a1235b3eSNicolas Pitre 		current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
20a1235b3eSNicolas Pitre }
21a1235b3eSNicolas Pitre 
22a1235b3eSNicolas Pitre /**
236ef6785dSLee Jones  *	__tty_check_change	-	check for POSIX terminal changes
24a1235b3eSNicolas Pitre  *	@tty: tty to check
25bc38fe24SLee Jones  *	@sig: signal to send
26a1235b3eSNicolas Pitre  *
27a1235b3eSNicolas Pitre  *	If we try to write to, or set the state of, a terminal and we're
28a1235b3eSNicolas Pitre  *	not in the foreground, send a SIGTTOU.  If the signal is blocked or
29a1235b3eSNicolas Pitre  *	ignored, go ahead and perform the operation.  (POSIX 7.2)
30a1235b3eSNicolas Pitre  *
3164d608dbSJiri Slaby  *	Locking: ctrl.lock
32a1235b3eSNicolas Pitre  */
__tty_check_change(struct tty_struct * tty,int sig)33a1235b3eSNicolas Pitre int __tty_check_change(struct tty_struct *tty, int sig)
34a1235b3eSNicolas Pitre {
35a1235b3eSNicolas Pitre 	unsigned long flags;
36a1235b3eSNicolas Pitre 	struct pid *pgrp, *tty_pgrp;
37a1235b3eSNicolas Pitre 	int ret = 0;
38a1235b3eSNicolas Pitre 
39a1235b3eSNicolas Pitre 	if (current->signal->tty != tty)
40a1235b3eSNicolas Pitre 		return 0;
41a1235b3eSNicolas Pitre 
42a1235b3eSNicolas Pitre 	rcu_read_lock();
43a1235b3eSNicolas Pitre 	pgrp = task_pgrp(current);
44a1235b3eSNicolas Pitre 
4564d608dbSJiri Slaby 	spin_lock_irqsave(&tty->ctrl.lock, flags);
4664d608dbSJiri Slaby 	tty_pgrp = tty->ctrl.pgrp;
4764d608dbSJiri Slaby 	spin_unlock_irqrestore(&tty->ctrl.lock, flags);
48a1235b3eSNicolas Pitre 
49cf90c06fSDavid Emett 	if (tty_pgrp && pgrp != tty_pgrp) {
50a1235b3eSNicolas Pitre 		if (is_ignored(sig)) {
51a1235b3eSNicolas Pitre 			if (sig == SIGTTIN)
52a1235b3eSNicolas Pitre 				ret = -EIO;
53a1235b3eSNicolas Pitre 		} else if (is_current_pgrp_orphaned())
54a1235b3eSNicolas Pitre 			ret = -EIO;
55a1235b3eSNicolas Pitre 		else {
56a1235b3eSNicolas Pitre 			kill_pgrp(pgrp, sig, 1);
57a1235b3eSNicolas Pitre 			set_thread_flag(TIF_SIGPENDING);
58a1235b3eSNicolas Pitre 			ret = -ERESTARTSYS;
59a1235b3eSNicolas Pitre 		}
60a1235b3eSNicolas Pitre 	}
61a1235b3eSNicolas Pitre 	rcu_read_unlock();
62a1235b3eSNicolas Pitre 
63a1235b3eSNicolas Pitre 	if (!tty_pgrp)
64a1235b3eSNicolas Pitre 		tty_warn(tty, "sig=%d, tty->pgrp == NULL!\n", sig);
65a1235b3eSNicolas Pitre 
66a1235b3eSNicolas Pitre 	return ret;
67a1235b3eSNicolas Pitre }
68a1235b3eSNicolas Pitre 
tty_check_change(struct tty_struct * tty)69a1235b3eSNicolas Pitre int tty_check_change(struct tty_struct *tty)
70a1235b3eSNicolas Pitre {
71a1235b3eSNicolas Pitre 	return __tty_check_change(tty, SIGTTOU);
72a1235b3eSNicolas Pitre }
73a1235b3eSNicolas Pitre EXPORT_SYMBOL(tty_check_change);
74a1235b3eSNicolas Pitre 
proc_clear_tty(struct task_struct * p)75a1235b3eSNicolas Pitre void proc_clear_tty(struct task_struct *p)
76a1235b3eSNicolas Pitre {
77a1235b3eSNicolas Pitre 	unsigned long flags;
78a1235b3eSNicolas Pitre 	struct tty_struct *tty;
79d4e1d903SXiaofei Tan 
80a1235b3eSNicolas Pitre 	spin_lock_irqsave(&p->sighand->siglock, flags);
81a1235b3eSNicolas Pitre 	tty = p->signal->tty;
82a1235b3eSNicolas Pitre 	p->signal->tty = NULL;
83a1235b3eSNicolas Pitre 	spin_unlock_irqrestore(&p->sighand->siglock, flags);
84a1235b3eSNicolas Pitre 	tty_kref_put(tty);
85a1235b3eSNicolas Pitre }
86a1235b3eSNicolas Pitre 
87a1235b3eSNicolas Pitre /**
886ef6785dSLee Jones  * __proc_set_tty -  set the controlling terminal
89bc38fe24SLee Jones  *	@tty: tty structure
90a1235b3eSNicolas Pitre  *
91a1235b3eSNicolas Pitre  * Only callable by the session leader and only if it does not already have
92a1235b3eSNicolas Pitre  * a controlling terminal.
93a1235b3eSNicolas Pitre  *
94a1235b3eSNicolas Pitre  * Caller must hold:  tty_lock()
95a1235b3eSNicolas Pitre  *		      a readlock on tasklist_lock
96a1235b3eSNicolas Pitre  *		      sighand lock
97a1235b3eSNicolas Pitre  */
__proc_set_tty(struct tty_struct * tty)98a1235b3eSNicolas Pitre static void __proc_set_tty(struct tty_struct *tty)
99a1235b3eSNicolas Pitre {
100a1235b3eSNicolas Pitre 	unsigned long flags;
101a1235b3eSNicolas Pitre 
10264d608dbSJiri Slaby 	spin_lock_irqsave(&tty->ctrl.lock, flags);
103a1235b3eSNicolas Pitre 	/*
104a1235b3eSNicolas Pitre 	 * The session and fg pgrp references will be non-NULL if
105a1235b3eSNicolas Pitre 	 * tiocsctty() is stealing the controlling tty
106a1235b3eSNicolas Pitre 	 */
10764d608dbSJiri Slaby 	put_pid(tty->ctrl.session);
10864d608dbSJiri Slaby 	put_pid(tty->ctrl.pgrp);
10964d608dbSJiri Slaby 	tty->ctrl.pgrp = get_pid(task_pgrp(current));
11064d608dbSJiri Slaby 	tty->ctrl.session = get_pid(task_session(current));
11164d608dbSJiri Slaby 	spin_unlock_irqrestore(&tty->ctrl.lock, flags);
112a1235b3eSNicolas Pitre 	if (current->signal->tty) {
113a1235b3eSNicolas Pitre 		tty_debug(tty, "current tty %s not NULL!!\n",
114a1235b3eSNicolas Pitre 			  current->signal->tty->name);
115a1235b3eSNicolas Pitre 		tty_kref_put(current->signal->tty);
116a1235b3eSNicolas Pitre 	}
117a1235b3eSNicolas Pitre 	put_pid(current->signal->tty_old_pgrp);
118a1235b3eSNicolas Pitre 	current->signal->tty = tty_kref_get(tty);
119a1235b3eSNicolas Pitre 	current->signal->tty_old_pgrp = NULL;
120a1235b3eSNicolas Pitre }
121a1235b3eSNicolas Pitre 
proc_set_tty(struct tty_struct * tty)122a1235b3eSNicolas Pitre static void proc_set_tty(struct tty_struct *tty)
123a1235b3eSNicolas Pitre {
124a1235b3eSNicolas Pitre 	spin_lock_irq(&current->sighand->siglock);
125a1235b3eSNicolas Pitre 	__proc_set_tty(tty);
126a1235b3eSNicolas Pitre 	spin_unlock_irq(&current->sighand->siglock);
127a1235b3eSNicolas Pitre }
128a1235b3eSNicolas Pitre 
129a1235b3eSNicolas Pitre /*
130a1235b3eSNicolas Pitre  * Called by tty_open() to set the controlling tty if applicable.
131a1235b3eSNicolas Pitre  */
tty_open_proc_set_tty(struct file * filp,struct tty_struct * tty)132a1235b3eSNicolas Pitre void tty_open_proc_set_tty(struct file *filp, struct tty_struct *tty)
133a1235b3eSNicolas Pitre {
134a1235b3eSNicolas Pitre 	read_lock(&tasklist_lock);
135a1235b3eSNicolas Pitre 	spin_lock_irq(&current->sighand->siglock);
136a1235b3eSNicolas Pitre 	if (current->signal->leader &&
137a1235b3eSNicolas Pitre 	    !current->signal->tty &&
13864d608dbSJiri Slaby 	    tty->ctrl.session == NULL) {
139a1235b3eSNicolas Pitre 		/*
140a1235b3eSNicolas Pitre 		 * Don't let a process that only has write access to the tty
141a1235b3eSNicolas Pitre 		 * obtain the privileges associated with having a tty as
142a1235b3eSNicolas Pitre 		 * controlling terminal (being able to reopen it with full
143a1235b3eSNicolas Pitre 		 * access through /dev/tty, being able to perform pushback).
144a1235b3eSNicolas Pitre 		 * Many distributions set the group of all ttys to "tty" and
145a1235b3eSNicolas Pitre 		 * grant write-only access to all terminals for setgid tty
146a1235b3eSNicolas Pitre 		 * binaries, which should not imply full privileges on all ttys.
147a1235b3eSNicolas Pitre 		 *
148a1235b3eSNicolas Pitre 		 * This could theoretically break old code that performs open()
149a1235b3eSNicolas Pitre 		 * on a write-only file descriptor. In that case, it might be
150a1235b3eSNicolas Pitre 		 * necessary to also permit this if
151a1235b3eSNicolas Pitre 		 * inode_permission(inode, MAY_READ) == 0.
152a1235b3eSNicolas Pitre 		 */
153a1235b3eSNicolas Pitre 		if (filp->f_mode & FMODE_READ)
154a1235b3eSNicolas Pitre 			__proc_set_tty(tty);
155a1235b3eSNicolas Pitre 	}
156a1235b3eSNicolas Pitre 	spin_unlock_irq(&current->sighand->siglock);
157a1235b3eSNicolas Pitre 	read_unlock(&tasklist_lock);
158a1235b3eSNicolas Pitre }
159a1235b3eSNicolas Pitre 
get_current_tty(void)160a1235b3eSNicolas Pitre struct tty_struct *get_current_tty(void)
161a1235b3eSNicolas Pitre {
162a1235b3eSNicolas Pitre 	struct tty_struct *tty;
163a1235b3eSNicolas Pitre 	unsigned long flags;
164a1235b3eSNicolas Pitre 
165a1235b3eSNicolas Pitre 	spin_lock_irqsave(&current->sighand->siglock, flags);
166a1235b3eSNicolas Pitre 	tty = tty_kref_get(current->signal->tty);
167a1235b3eSNicolas Pitre 	spin_unlock_irqrestore(&current->sighand->siglock, flags);
168a1235b3eSNicolas Pitre 	return tty;
169a1235b3eSNicolas Pitre }
170a1235b3eSNicolas Pitre EXPORT_SYMBOL_GPL(get_current_tty);
171a1235b3eSNicolas Pitre 
172a1235b3eSNicolas Pitre /*
173a1235b3eSNicolas Pitre  * Called from tty_release().
174a1235b3eSNicolas Pitre  */
session_clear_tty(struct pid * session)175a1235b3eSNicolas Pitre void session_clear_tty(struct pid *session)
176a1235b3eSNicolas Pitre {
177a1235b3eSNicolas Pitre 	struct task_struct *p;
178d4e1d903SXiaofei Tan 
179a1235b3eSNicolas Pitre 	do_each_pid_task(session, PIDTYPE_SID, p) {
180a1235b3eSNicolas Pitre 		proc_clear_tty(p);
181a1235b3eSNicolas Pitre 	} while_each_pid_task(session, PIDTYPE_SID, p);
182a1235b3eSNicolas Pitre }
183a1235b3eSNicolas Pitre 
184a1235b3eSNicolas Pitre /**
185a1235b3eSNicolas Pitre  *	tty_signal_session_leader	- sends SIGHUP to session leader
186fa441954SJiri Slaby  *	@tty: controlling tty
187fa441954SJiri Slaby  *	@exit_session: if non-zero, signal all foreground group processes
188a1235b3eSNicolas Pitre  *
189a1235b3eSNicolas Pitre  *	Send SIGHUP and SIGCONT to the session leader and its process group.
190a1235b3eSNicolas Pitre  *	Optionally, signal all processes in the foreground process group.
191a1235b3eSNicolas Pitre  *
192a1235b3eSNicolas Pitre  *	Returns the number of processes in the session with this tty
193a1235b3eSNicolas Pitre  *	as their controlling terminal. This value is used to drop
194a1235b3eSNicolas Pitre  *	tty references for those processes.
195a1235b3eSNicolas Pitre  */
tty_signal_session_leader(struct tty_struct * tty,int exit_session)196a1235b3eSNicolas Pitre int tty_signal_session_leader(struct tty_struct *tty, int exit_session)
197a1235b3eSNicolas Pitre {
198a1235b3eSNicolas Pitre 	struct task_struct *p;
199a1235b3eSNicolas Pitre 	int refs = 0;
200a1235b3eSNicolas Pitre 	struct pid *tty_pgrp = NULL;
201a1235b3eSNicolas Pitre 
202a1235b3eSNicolas Pitre 	read_lock(&tasklist_lock);
20364d608dbSJiri Slaby 	if (tty->ctrl.session) {
20464d608dbSJiri Slaby 		do_each_pid_task(tty->ctrl.session, PIDTYPE_SID, p) {
205a1235b3eSNicolas Pitre 			spin_lock_irq(&p->sighand->siglock);
206a1235b3eSNicolas Pitre 			if (p->signal->tty == tty) {
207a1235b3eSNicolas Pitre 				p->signal->tty = NULL;
20863eeafd4SXiaofei Tan 				/*
20963eeafd4SXiaofei Tan 				 * We defer the dereferences outside of
21063eeafd4SXiaofei Tan 				 * the tasklist lock.
21163eeafd4SXiaofei Tan 				 */
212a1235b3eSNicolas Pitre 				refs++;
213a1235b3eSNicolas Pitre 			}
214a1235b3eSNicolas Pitre 			if (!p->signal->leader) {
215a1235b3eSNicolas Pitre 				spin_unlock_irq(&p->sighand->siglock);
216a1235b3eSNicolas Pitre 				continue;
217a1235b3eSNicolas Pitre 			}
218e71ba124SEric W. Biederman 			send_signal_locked(SIGHUP, SEND_SIG_PRIV, p, PIDTYPE_TGID);
219e71ba124SEric W. Biederman 			send_signal_locked(SIGCONT, SEND_SIG_PRIV, p, PIDTYPE_TGID);
220a1235b3eSNicolas Pitre 			put_pid(p->signal->tty_old_pgrp);  /* A noop */
22164d608dbSJiri Slaby 			spin_lock(&tty->ctrl.lock);
22264d608dbSJiri Slaby 			tty_pgrp = get_pid(tty->ctrl.pgrp);
22364d608dbSJiri Slaby 			if (tty->ctrl.pgrp)
22464d608dbSJiri Slaby 				p->signal->tty_old_pgrp =
22564d608dbSJiri Slaby 					get_pid(tty->ctrl.pgrp);
22664d608dbSJiri Slaby 			spin_unlock(&tty->ctrl.lock);
227a1235b3eSNicolas Pitre 			spin_unlock_irq(&p->sighand->siglock);
22864d608dbSJiri Slaby 		} while_each_pid_task(tty->ctrl.session, PIDTYPE_SID, p);
229a1235b3eSNicolas Pitre 	}
230a1235b3eSNicolas Pitre 	read_unlock(&tasklist_lock);
231a1235b3eSNicolas Pitre 
232a1235b3eSNicolas Pitre 	if (tty_pgrp) {
233a1235b3eSNicolas Pitre 		if (exit_session)
234a1235b3eSNicolas Pitre 			kill_pgrp(tty_pgrp, SIGHUP, exit_session);
235a1235b3eSNicolas Pitre 		put_pid(tty_pgrp);
236a1235b3eSNicolas Pitre 	}
237a1235b3eSNicolas Pitre 
238a1235b3eSNicolas Pitre 	return refs;
239a1235b3eSNicolas Pitre }
240a1235b3eSNicolas Pitre 
241a1235b3eSNicolas Pitre /**
242a1235b3eSNicolas Pitre  *	disassociate_ctty	-	disconnect controlling tty
243a1235b3eSNicolas Pitre  *	@on_exit: true if exiting so need to "hang up" the session
244a1235b3eSNicolas Pitre  *
245a1235b3eSNicolas Pitre  *	This function is typically called only by the session leader, when
246a1235b3eSNicolas Pitre  *	it wants to disassociate itself from its controlling tty.
247a1235b3eSNicolas Pitre  *
248a1235b3eSNicolas Pitre  *	It performs the following functions:
249a1235b3eSNicolas Pitre  *	(1)  Sends a SIGHUP and SIGCONT to the foreground process group
250a1235b3eSNicolas Pitre  *	(2)  Clears the tty from being controlling the session
251a1235b3eSNicolas Pitre  *	(3)  Clears the controlling tty for all processes in the
252a1235b3eSNicolas Pitre  *		session group.
253a1235b3eSNicolas Pitre  *
254a1235b3eSNicolas Pitre  *	The argument on_exit is set to 1 if called when a process is
255a1235b3eSNicolas Pitre  *	exiting; it is 0 if called by the ioctl TIOCNOTTY.
256a1235b3eSNicolas Pitre  *
257a1235b3eSNicolas Pitre  *	Locking:
258a1235b3eSNicolas Pitre  *		BTM is taken for hysterical raisons, and held when
259a1235b3eSNicolas Pitre  *		  called from no_tty().
260a1235b3eSNicolas Pitre  *		  tty_mutex is taken to protect tty
261a1235b3eSNicolas Pitre  *		  ->siglock is taken to protect ->signal/->sighand
262a1235b3eSNicolas Pitre  *		  tasklist_lock is taken to walk process list for sessions
263a1235b3eSNicolas Pitre  *		    ->siglock is taken to protect ->signal/->sighand
264a1235b3eSNicolas Pitre  */
disassociate_ctty(int on_exit)265a1235b3eSNicolas Pitre void disassociate_ctty(int on_exit)
266a1235b3eSNicolas Pitre {
267a1235b3eSNicolas Pitre 	struct tty_struct *tty;
268a1235b3eSNicolas Pitre 
269a1235b3eSNicolas Pitre 	if (!current->signal->leader)
270a1235b3eSNicolas Pitre 		return;
271a1235b3eSNicolas Pitre 
272a1235b3eSNicolas Pitre 	tty = get_current_tty();
273a1235b3eSNicolas Pitre 	if (tty) {
274a1235b3eSNicolas Pitre 		if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) {
275a1235b3eSNicolas Pitre 			tty_vhangup_session(tty);
276a1235b3eSNicolas Pitre 		} else {
277a1235b3eSNicolas Pitre 			struct pid *tty_pgrp = tty_get_pgrp(tty);
278d4e1d903SXiaofei Tan 
279a1235b3eSNicolas Pitre 			if (tty_pgrp) {
280a1235b3eSNicolas Pitre 				kill_pgrp(tty_pgrp, SIGHUP, on_exit);
281a1235b3eSNicolas Pitre 				if (!on_exit)
282a1235b3eSNicolas Pitre 					kill_pgrp(tty_pgrp, SIGCONT, on_exit);
283a1235b3eSNicolas Pitre 				put_pid(tty_pgrp);
284a1235b3eSNicolas Pitre 			}
285a1235b3eSNicolas Pitre 		}
286a1235b3eSNicolas Pitre 		tty_kref_put(tty);
287a1235b3eSNicolas Pitre 
288a1235b3eSNicolas Pitre 	} else if (on_exit) {
289a1235b3eSNicolas Pitre 		struct pid *old_pgrp;
290d4e1d903SXiaofei Tan 
291a1235b3eSNicolas Pitre 		spin_lock_irq(&current->sighand->siglock);
292a1235b3eSNicolas Pitre 		old_pgrp = current->signal->tty_old_pgrp;
293a1235b3eSNicolas Pitre 		current->signal->tty_old_pgrp = NULL;
294a1235b3eSNicolas Pitre 		spin_unlock_irq(&current->sighand->siglock);
295a1235b3eSNicolas Pitre 		if (old_pgrp) {
296a1235b3eSNicolas Pitre 			kill_pgrp(old_pgrp, SIGHUP, on_exit);
297a1235b3eSNicolas Pitre 			kill_pgrp(old_pgrp, SIGCONT, on_exit);
298a1235b3eSNicolas Pitre 			put_pid(old_pgrp);
299a1235b3eSNicolas Pitre 		}
300a1235b3eSNicolas Pitre 		return;
301a1235b3eSNicolas Pitre 	}
302a1235b3eSNicolas Pitre 
303*11e7f27bSYi Yang 	tty = get_current_tty();
304a1235b3eSNicolas Pitre 	if (tty) {
305a1235b3eSNicolas Pitre 		unsigned long flags;
306c8bcd9c5SJann Horn 
307c8bcd9c5SJann Horn 		tty_lock(tty);
30864d608dbSJiri Slaby 		spin_lock_irqsave(&tty->ctrl.lock, flags);
30964d608dbSJiri Slaby 		put_pid(tty->ctrl.session);
31064d608dbSJiri Slaby 		put_pid(tty->ctrl.pgrp);
31164d608dbSJiri Slaby 		tty->ctrl.session = NULL;
31264d608dbSJiri Slaby 		tty->ctrl.pgrp = NULL;
31364d608dbSJiri Slaby 		spin_unlock_irqrestore(&tty->ctrl.lock, flags);
314c8bcd9c5SJann Horn 		tty_unlock(tty);
315a1235b3eSNicolas Pitre 		tty_kref_put(tty);
316a1235b3eSNicolas Pitre 	}
317a1235b3eSNicolas Pitre 
318*11e7f27bSYi Yang 	/* If tty->ctrl.pgrp is not NULL, it may be assigned to
319*11e7f27bSYi Yang 	 * current->signal->tty_old_pgrp in a race condition, and
320*11e7f27bSYi Yang 	 * cause pid memleak. Release current->signal->tty_old_pgrp
321*11e7f27bSYi Yang 	 * after tty->ctrl.pgrp set to NULL.
322*11e7f27bSYi Yang 	 */
323*11e7f27bSYi Yang 	spin_lock_irq(&current->sighand->siglock);
324*11e7f27bSYi Yang 	put_pid(current->signal->tty_old_pgrp);
325*11e7f27bSYi Yang 	current->signal->tty_old_pgrp = NULL;
326*11e7f27bSYi Yang 	spin_unlock_irq(&current->sighand->siglock);
327*11e7f27bSYi Yang 
328a1235b3eSNicolas Pitre 	/* Now clear signal->tty under the lock */
329a1235b3eSNicolas Pitre 	read_lock(&tasklist_lock);
330a1235b3eSNicolas Pitre 	session_clear_tty(task_session(current));
331a1235b3eSNicolas Pitre 	read_unlock(&tasklist_lock);
332a1235b3eSNicolas Pitre }
333a1235b3eSNicolas Pitre 
3343adf2aa8SValdis Klētnieks /*
335a1235b3eSNicolas Pitre  *
336a1235b3eSNicolas Pitre  *	no_tty	- Ensure the current process does not have a controlling tty
337a1235b3eSNicolas Pitre  */
no_tty(void)338a1235b3eSNicolas Pitre void no_tty(void)
339a1235b3eSNicolas Pitre {
34063eeafd4SXiaofei Tan 	/*
34163eeafd4SXiaofei Tan 	 * FIXME: Review locking here. The tty_lock never covered any race
34263eeafd4SXiaofei Tan 	 * between a new association and proc_clear_tty but possibly we need
34363eeafd4SXiaofei Tan 	 * to protect against this anyway.
34463eeafd4SXiaofei Tan 	 */
345a1235b3eSNicolas Pitre 	struct task_struct *tsk = current;
346d4e1d903SXiaofei Tan 
347a1235b3eSNicolas Pitre 	disassociate_ctty(0);
348a1235b3eSNicolas Pitre 	proc_clear_tty(tsk);
349a1235b3eSNicolas Pitre }
350a1235b3eSNicolas Pitre 
351a1235b3eSNicolas Pitre /**
352a1235b3eSNicolas Pitre  *	tiocsctty	-	set controlling tty
353a1235b3eSNicolas Pitre  *	@tty: tty structure
354bc38fe24SLee Jones  *	@file: file structure used to check permissions
355a1235b3eSNicolas Pitre  *	@arg: user argument
356a1235b3eSNicolas Pitre  *
357a1235b3eSNicolas Pitre  *	This ioctl is used to manage job control. It permits a session
358a1235b3eSNicolas Pitre  *	leader to set this tty as the controlling tty for the session.
359a1235b3eSNicolas Pitre  *
360a1235b3eSNicolas Pitre  *	Locking:
361a1235b3eSNicolas Pitre  *		Takes tty_lock() to serialize proc_set_tty() for this tty
362a1235b3eSNicolas Pitre  *		Takes tasklist_lock internally to walk sessions
363a1235b3eSNicolas Pitre  *		Takes ->siglock() when updating signal->tty
364a1235b3eSNicolas Pitre  */
tiocsctty(struct tty_struct * tty,struct file * file,int arg)365a1235b3eSNicolas Pitre static int tiocsctty(struct tty_struct *tty, struct file *file, int arg)
366a1235b3eSNicolas Pitre {
367a1235b3eSNicolas Pitre 	int ret = 0;
368a1235b3eSNicolas Pitre 
369a1235b3eSNicolas Pitre 	tty_lock(tty);
370a1235b3eSNicolas Pitre 	read_lock(&tasklist_lock);
371a1235b3eSNicolas Pitre 
37264d608dbSJiri Slaby 	if (current->signal->leader &&
37364d608dbSJiri Slaby 			task_session(current) == tty->ctrl.session)
374a1235b3eSNicolas Pitre 		goto unlock;
375a1235b3eSNicolas Pitre 
376a1235b3eSNicolas Pitre 	/*
377a1235b3eSNicolas Pitre 	 * The process must be a session leader and
378a1235b3eSNicolas Pitre 	 * not have a controlling tty already.
379a1235b3eSNicolas Pitre 	 */
380a1235b3eSNicolas Pitre 	if (!current->signal->leader || current->signal->tty) {
381a1235b3eSNicolas Pitre 		ret = -EPERM;
382a1235b3eSNicolas Pitre 		goto unlock;
383a1235b3eSNicolas Pitre 	}
384a1235b3eSNicolas Pitre 
38564d608dbSJiri Slaby 	if (tty->ctrl.session) {
386a1235b3eSNicolas Pitre 		/*
387a1235b3eSNicolas Pitre 		 * This tty is already the controlling
388a1235b3eSNicolas Pitre 		 * tty for another session group!
389a1235b3eSNicolas Pitre 		 */
390a1235b3eSNicolas Pitre 		if (arg == 1 && capable(CAP_SYS_ADMIN)) {
391a1235b3eSNicolas Pitre 			/*
392a1235b3eSNicolas Pitre 			 * Steal it away
393a1235b3eSNicolas Pitre 			 */
39464d608dbSJiri Slaby 			session_clear_tty(tty->ctrl.session);
395a1235b3eSNicolas Pitre 		} else {
396a1235b3eSNicolas Pitre 			ret = -EPERM;
397a1235b3eSNicolas Pitre 			goto unlock;
398a1235b3eSNicolas Pitre 		}
399a1235b3eSNicolas Pitre 	}
400a1235b3eSNicolas Pitre 
401a1235b3eSNicolas Pitre 	/* See the comment in tty_open_proc_set_tty(). */
402a1235b3eSNicolas Pitre 	if ((file->f_mode & FMODE_READ) == 0 && !capable(CAP_SYS_ADMIN)) {
403a1235b3eSNicolas Pitre 		ret = -EPERM;
404a1235b3eSNicolas Pitre 		goto unlock;
405a1235b3eSNicolas Pitre 	}
406a1235b3eSNicolas Pitre 
407a1235b3eSNicolas Pitre 	proc_set_tty(tty);
408a1235b3eSNicolas Pitre unlock:
409a1235b3eSNicolas Pitre 	read_unlock(&tasklist_lock);
410a1235b3eSNicolas Pitre 	tty_unlock(tty);
411a1235b3eSNicolas Pitre 	return ret;
412a1235b3eSNicolas Pitre }
413a1235b3eSNicolas Pitre 
414a1235b3eSNicolas Pitre /**
415a1235b3eSNicolas Pitre  *	tty_get_pgrp	-	return a ref counted pgrp pid
416a1235b3eSNicolas Pitre  *	@tty: tty to read
417a1235b3eSNicolas Pitre  *
418a1235b3eSNicolas Pitre  *	Returns a refcounted instance of the pid struct for the process
419a1235b3eSNicolas Pitre  *	group controlling the tty.
420a1235b3eSNicolas Pitre  */
tty_get_pgrp(struct tty_struct * tty)421a1235b3eSNicolas Pitre struct pid *tty_get_pgrp(struct tty_struct *tty)
422a1235b3eSNicolas Pitre {
423a1235b3eSNicolas Pitre 	unsigned long flags;
424a1235b3eSNicolas Pitre 	struct pid *pgrp;
425a1235b3eSNicolas Pitre 
42664d608dbSJiri Slaby 	spin_lock_irqsave(&tty->ctrl.lock, flags);
42764d608dbSJiri Slaby 	pgrp = get_pid(tty->ctrl.pgrp);
42864d608dbSJiri Slaby 	spin_unlock_irqrestore(&tty->ctrl.lock, flags);
429a1235b3eSNicolas Pitre 
430a1235b3eSNicolas Pitre 	return pgrp;
431a1235b3eSNicolas Pitre }
432a1235b3eSNicolas Pitre EXPORT_SYMBOL_GPL(tty_get_pgrp);
433a1235b3eSNicolas Pitre 
434a1235b3eSNicolas Pitre /*
435a1235b3eSNicolas Pitre  * This checks not only the pgrp, but falls back on the pid if no
436a1235b3eSNicolas Pitre  * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
437a1235b3eSNicolas Pitre  * without this...
438a1235b3eSNicolas Pitre  *
439a1235b3eSNicolas Pitre  * The caller must hold rcu lock or the tasklist lock.
440a1235b3eSNicolas Pitre  */
session_of_pgrp(struct pid * pgrp)441a1235b3eSNicolas Pitre static struct pid *session_of_pgrp(struct pid *pgrp)
442a1235b3eSNicolas Pitre {
443a1235b3eSNicolas Pitre 	struct task_struct *p;
444a1235b3eSNicolas Pitre 	struct pid *sid = NULL;
445a1235b3eSNicolas Pitre 
446a1235b3eSNicolas Pitre 	p = pid_task(pgrp, PIDTYPE_PGID);
447a1235b3eSNicolas Pitre 	if (p == NULL)
448a1235b3eSNicolas Pitre 		p = pid_task(pgrp, PIDTYPE_PID);
449a1235b3eSNicolas Pitre 	if (p != NULL)
450a1235b3eSNicolas Pitre 		sid = task_session(p);
451a1235b3eSNicolas Pitre 
452a1235b3eSNicolas Pitre 	return sid;
453a1235b3eSNicolas Pitre }
454a1235b3eSNicolas Pitre 
455a1235b3eSNicolas Pitre /**
456a1235b3eSNicolas Pitre  *	tiocgpgrp		-	get process group
457a1235b3eSNicolas Pitre  *	@tty: tty passed by user
458a1235b3eSNicolas Pitre  *	@real_tty: tty side of the tty passed by the user if a pty else the tty
459a1235b3eSNicolas Pitre  *	@p: returned pid
460a1235b3eSNicolas Pitre  *
461a1235b3eSNicolas Pitre  *	Obtain the process group of the tty. If there is no process group
462a1235b3eSNicolas Pitre  *	return an error.
463a1235b3eSNicolas Pitre  *
464a1235b3eSNicolas Pitre  *	Locking: none. Reference to current->signal->tty is safe.
465a1235b3eSNicolas Pitre  */
tiocgpgrp(struct tty_struct * tty,struct tty_struct * real_tty,pid_t __user * p)466a1235b3eSNicolas Pitre static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
467a1235b3eSNicolas Pitre {
468a1235b3eSNicolas Pitre 	struct pid *pid;
469a1235b3eSNicolas Pitre 	int ret;
470a1235b3eSNicolas Pitre 	/*
471a1235b3eSNicolas Pitre 	 * (tty == real_tty) is a cheap way of
472a1235b3eSNicolas Pitre 	 * testing if the tty is NOT a master pty.
473a1235b3eSNicolas Pitre 	 */
474a1235b3eSNicolas Pitre 	if (tty == real_tty && current->signal->tty != real_tty)
475a1235b3eSNicolas Pitre 		return -ENOTTY;
476a1235b3eSNicolas Pitre 	pid = tty_get_pgrp(real_tty);
477a1235b3eSNicolas Pitre 	ret =  put_user(pid_vnr(pid), p);
478a1235b3eSNicolas Pitre 	put_pid(pid);
479a1235b3eSNicolas Pitre 	return ret;
480a1235b3eSNicolas Pitre }
481a1235b3eSNicolas Pitre 
482a1235b3eSNicolas Pitre /**
483a1235b3eSNicolas Pitre  *	tiocspgrp		-	attempt to set process group
484a1235b3eSNicolas Pitre  *	@tty: tty passed by user
485a1235b3eSNicolas Pitre  *	@real_tty: tty side device matching tty passed by user
486a1235b3eSNicolas Pitre  *	@p: pid pointer
487a1235b3eSNicolas Pitre  *
488a1235b3eSNicolas Pitre  *	Set the process group of the tty to the session passed. Only
489a1235b3eSNicolas Pitre  *	permitted where the tty session is our session.
490a1235b3eSNicolas Pitre  *
491a1235b3eSNicolas Pitre  *	Locking: RCU, ctrl lock
492a1235b3eSNicolas Pitre  */
tiocspgrp(struct tty_struct * tty,struct tty_struct * real_tty,pid_t __user * p)493a1235b3eSNicolas Pitre static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
494a1235b3eSNicolas Pitre {
495a1235b3eSNicolas Pitre 	struct pid *pgrp;
496a1235b3eSNicolas Pitre 	pid_t pgrp_nr;
497a1235b3eSNicolas Pitre 	int retval = tty_check_change(real_tty);
498a1235b3eSNicolas Pitre 
499a1235b3eSNicolas Pitre 	if (retval == -EIO)
500a1235b3eSNicolas Pitre 		return -ENOTTY;
501a1235b3eSNicolas Pitre 	if (retval)
502a1235b3eSNicolas Pitre 		return retval;
503c8bcd9c5SJann Horn 
504a1235b3eSNicolas Pitre 	if (get_user(pgrp_nr, p))
505a1235b3eSNicolas Pitre 		return -EFAULT;
506a1235b3eSNicolas Pitre 	if (pgrp_nr < 0)
507a1235b3eSNicolas Pitre 		return -EINVAL;
508c8bcd9c5SJann Horn 
50964d608dbSJiri Slaby 	spin_lock_irq(&real_tty->ctrl.lock);
510c8bcd9c5SJann Horn 	if (!current->signal->tty ||
511c8bcd9c5SJann Horn 	    (current->signal->tty != real_tty) ||
51264d608dbSJiri Slaby 	    (real_tty->ctrl.session != task_session(current))) {
513c8bcd9c5SJann Horn 		retval = -ENOTTY;
514c8bcd9c5SJann Horn 		goto out_unlock_ctrl;
515c8bcd9c5SJann Horn 	}
516a1235b3eSNicolas Pitre 	rcu_read_lock();
517a1235b3eSNicolas Pitre 	pgrp = find_vpid(pgrp_nr);
518a1235b3eSNicolas Pitre 	retval = -ESRCH;
519a1235b3eSNicolas Pitre 	if (!pgrp)
520a1235b3eSNicolas Pitre 		goto out_unlock;
521a1235b3eSNicolas Pitre 	retval = -EPERM;
522a1235b3eSNicolas Pitre 	if (session_of_pgrp(pgrp) != task_session(current))
523a1235b3eSNicolas Pitre 		goto out_unlock;
524a1235b3eSNicolas Pitre 	retval = 0;
52564d608dbSJiri Slaby 	put_pid(real_tty->ctrl.pgrp);
52664d608dbSJiri Slaby 	real_tty->ctrl.pgrp = get_pid(pgrp);
527a1235b3eSNicolas Pitre out_unlock:
528a1235b3eSNicolas Pitre 	rcu_read_unlock();
529c8bcd9c5SJann Horn out_unlock_ctrl:
53064d608dbSJiri Slaby 	spin_unlock_irq(&real_tty->ctrl.lock);
531a1235b3eSNicolas Pitre 	return retval;
532a1235b3eSNicolas Pitre }
533a1235b3eSNicolas Pitre 
534a1235b3eSNicolas Pitre /**
535a1235b3eSNicolas Pitre  *	tiocgsid		-	get session id
536a1235b3eSNicolas Pitre  *	@tty: tty passed by user
537a1235b3eSNicolas Pitre  *	@real_tty: tty side of the tty passed by the user if a pty else the tty
538a1235b3eSNicolas Pitre  *	@p: pointer to returned session id
539a1235b3eSNicolas Pitre  *
540a1235b3eSNicolas Pitre  *	Obtain the session id of the tty. If there is no session
541a1235b3eSNicolas Pitre  *	return an error.
542a1235b3eSNicolas Pitre  */
tiocgsid(struct tty_struct * tty,struct tty_struct * real_tty,pid_t __user * p)543a1235b3eSNicolas Pitre static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
544a1235b3eSNicolas Pitre {
545c8bcd9c5SJann Horn 	unsigned long flags;
546c8bcd9c5SJann Horn 	pid_t sid;
547c8bcd9c5SJann Horn 
548a1235b3eSNicolas Pitre 	/*
549a1235b3eSNicolas Pitre 	 * (tty == real_tty) is a cheap way of
550a1235b3eSNicolas Pitre 	 * testing if the tty is NOT a master pty.
551a1235b3eSNicolas Pitre 	 */
552a1235b3eSNicolas Pitre 	if (tty == real_tty && current->signal->tty != real_tty)
553a1235b3eSNicolas Pitre 		return -ENOTTY;
554c8bcd9c5SJann Horn 
55564d608dbSJiri Slaby 	spin_lock_irqsave(&real_tty->ctrl.lock, flags);
55664d608dbSJiri Slaby 	if (!real_tty->ctrl.session)
557c8bcd9c5SJann Horn 		goto err;
55864d608dbSJiri Slaby 	sid = pid_vnr(real_tty->ctrl.session);
55964d608dbSJiri Slaby 	spin_unlock_irqrestore(&real_tty->ctrl.lock, flags);
560c8bcd9c5SJann Horn 
561c8bcd9c5SJann Horn 	return put_user(sid, p);
562c8bcd9c5SJann Horn 
563c8bcd9c5SJann Horn err:
56464d608dbSJiri Slaby 	spin_unlock_irqrestore(&real_tty->ctrl.lock, flags);
565a1235b3eSNicolas Pitre 	return -ENOTTY;
566a1235b3eSNicolas Pitre }
567a1235b3eSNicolas Pitre 
568a1235b3eSNicolas Pitre /*
569a1235b3eSNicolas Pitre  * Called from tty_ioctl(). If tty is a pty then real_tty is the slave side,
570a1235b3eSNicolas Pitre  * if not then tty == real_tty.
571a1235b3eSNicolas Pitre  */
tty_jobctrl_ioctl(struct tty_struct * tty,struct tty_struct * real_tty,struct file * file,unsigned int cmd,unsigned long arg)572a1235b3eSNicolas Pitre long tty_jobctrl_ioctl(struct tty_struct *tty, struct tty_struct *real_tty,
573a1235b3eSNicolas Pitre 		       struct file *file, unsigned int cmd, unsigned long arg)
574a1235b3eSNicolas Pitre {
575a1235b3eSNicolas Pitre 	void __user *p = (void __user *)arg;
576a1235b3eSNicolas Pitre 
577a1235b3eSNicolas Pitre 	switch (cmd) {
578a1235b3eSNicolas Pitre 	case TIOCNOTTY:
579a1235b3eSNicolas Pitre 		if (current->signal->tty != tty)
580a1235b3eSNicolas Pitre 			return -ENOTTY;
581a1235b3eSNicolas Pitre 		no_tty();
582a1235b3eSNicolas Pitre 		return 0;
583a1235b3eSNicolas Pitre 	case TIOCSCTTY:
584a1235b3eSNicolas Pitre 		return tiocsctty(real_tty, file, arg);
585a1235b3eSNicolas Pitre 	case TIOCGPGRP:
586a1235b3eSNicolas Pitre 		return tiocgpgrp(tty, real_tty, p);
587a1235b3eSNicolas Pitre 	case TIOCSPGRP:
588a1235b3eSNicolas Pitre 		return tiocspgrp(tty, real_tty, p);
589a1235b3eSNicolas Pitre 	case TIOCGSID:
590a1235b3eSNicolas Pitre 		return tiocgsid(tty, real_tty, p);
591a1235b3eSNicolas Pitre 	}
592a1235b3eSNicolas Pitre 	return -ENOIOCTLCMD;
593a1235b3eSNicolas Pitre }
594