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