1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 1991, 1992 Linus Torvalds
6 #include <linux/types.h>
7 #include <linux/errno.h>
8 #include <linux/signal.h>
9 #include <linux/sched/signal.h>
10 #include <linux/sched/task.h>
11 #include <linux/tty.h>
12 #include <linux/fcntl.h>
13 #include <linux/uaccess.h>
16 static int is_ignored(int sig)
18 return (sigismember(¤t->blocked, sig) ||
19 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
23 * tty_check_change - check for POSIX terminal changes
26 * If we try to write to, or set the state of, a terminal and we're
27 * not in the foreground, send a SIGTTOU. If the signal is blocked or
28 * ignored, go ahead and perform the operation. (POSIX 7.2)
32 int __tty_check_change(struct tty_struct *tty, int sig)
35 struct pid *pgrp, *tty_pgrp;
38 if (current->signal->tty != tty)
42 pgrp = task_pgrp(current);
44 spin_lock_irqsave(&tty->ctrl_lock, flags);
46 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
48 if (tty_pgrp && pgrp != tty_pgrp) {
49 if (is_ignored(sig)) {
52 } else if (is_current_pgrp_orphaned())
55 kill_pgrp(pgrp, sig, 1);
56 set_thread_flag(TIF_SIGPENDING);
63 tty_warn(tty, "sig=%d, tty->pgrp == NULL!\n", sig);
68 int tty_check_change(struct tty_struct *tty)
70 return __tty_check_change(tty, SIGTTOU);
72 EXPORT_SYMBOL(tty_check_change);
74 void proc_clear_tty(struct task_struct *p)
77 struct tty_struct *tty;
78 spin_lock_irqsave(&p->sighand->siglock, flags);
80 p->signal->tty = NULL;
81 spin_unlock_irqrestore(&p->sighand->siglock, flags);
86 * proc_set_tty - set the controlling terminal
88 * Only callable by the session leader and only if it does not already have
89 * a controlling terminal.
91 * Caller must hold: tty_lock()
92 * a readlock on tasklist_lock
95 static void __proc_set_tty(struct tty_struct *tty)
99 spin_lock_irqsave(&tty->ctrl_lock, flags);
101 * The session and fg pgrp references will be non-NULL if
102 * tiocsctty() is stealing the controlling tty
104 put_pid(tty->session);
106 tty->pgrp = get_pid(task_pgrp(current));
107 tty->session = get_pid(task_session(current));
108 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
109 if (current->signal->tty) {
110 tty_debug(tty, "current tty %s not NULL!!\n",
111 current->signal->tty->name);
112 tty_kref_put(current->signal->tty);
114 put_pid(current->signal->tty_old_pgrp);
115 current->signal->tty = tty_kref_get(tty);
116 current->signal->tty_old_pgrp = NULL;
119 static void proc_set_tty(struct tty_struct *tty)
121 spin_lock_irq(¤t->sighand->siglock);
123 spin_unlock_irq(¤t->sighand->siglock);
127 * Called by tty_open() to set the controlling tty if applicable.
129 void tty_open_proc_set_tty(struct file *filp, struct tty_struct *tty)
131 read_lock(&tasklist_lock);
132 spin_lock_irq(¤t->sighand->siglock);
133 if (current->signal->leader &&
134 !current->signal->tty &&
135 tty->session == NULL) {
137 * Don't let a process that only has write access to the tty
138 * obtain the privileges associated with having a tty as
139 * controlling terminal (being able to reopen it with full
140 * access through /dev/tty, being able to perform pushback).
141 * Many distributions set the group of all ttys to "tty" and
142 * grant write-only access to all terminals for setgid tty
143 * binaries, which should not imply full privileges on all ttys.
145 * This could theoretically break old code that performs open()
146 * on a write-only file descriptor. In that case, it might be
147 * necessary to also permit this if
148 * inode_permission(inode, MAY_READ) == 0.
150 if (filp->f_mode & FMODE_READ)
153 spin_unlock_irq(¤t->sighand->siglock);
154 read_unlock(&tasklist_lock);
157 struct tty_struct *get_current_tty(void)
159 struct tty_struct *tty;
162 spin_lock_irqsave(¤t->sighand->siglock, flags);
163 tty = tty_kref_get(current->signal->tty);
164 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
167 EXPORT_SYMBOL_GPL(get_current_tty);
170 * Called from tty_release().
172 void session_clear_tty(struct pid *session)
174 struct task_struct *p;
175 do_each_pid_task(session, PIDTYPE_SID, p) {
177 } while_each_pid_task(session, PIDTYPE_SID, p);
181 * tty_signal_session_leader - sends SIGHUP to session leader
182 * @tty: controlling tty
183 * @exit_session: if non-zero, signal all foreground group processes
185 * Send SIGHUP and SIGCONT to the session leader and its process group.
186 * Optionally, signal all processes in the foreground process group.
188 * Returns the number of processes in the session with this tty
189 * as their controlling terminal. This value is used to drop
190 * tty references for those processes.
192 int tty_signal_session_leader(struct tty_struct *tty, int exit_session)
194 struct task_struct *p;
196 struct pid *tty_pgrp = NULL;
198 read_lock(&tasklist_lock);
200 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
201 spin_lock_irq(&p->sighand->siglock);
202 if (p->signal->tty == tty) {
203 p->signal->tty = NULL;
204 /* We defer the dereferences outside fo
208 if (!p->signal->leader) {
209 spin_unlock_irq(&p->sighand->siglock);
212 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
213 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
214 put_pid(p->signal->tty_old_pgrp); /* A noop */
215 spin_lock(&tty->ctrl_lock);
216 tty_pgrp = get_pid(tty->pgrp);
218 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
219 spin_unlock(&tty->ctrl_lock);
220 spin_unlock_irq(&p->sighand->siglock);
221 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
223 read_unlock(&tasklist_lock);
227 kill_pgrp(tty_pgrp, SIGHUP, exit_session);
235 * disassociate_ctty - disconnect controlling tty
236 * @on_exit: true if exiting so need to "hang up" the session
238 * This function is typically called only by the session leader, when
239 * it wants to disassociate itself from its controlling tty.
241 * It performs the following functions:
242 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
243 * (2) Clears the tty from being controlling the session
244 * (3) Clears the controlling tty for all processes in the
247 * The argument on_exit is set to 1 if called when a process is
248 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
251 * BTM is taken for hysterical raisons, and held when
252 * called from no_tty().
253 * tty_mutex is taken to protect tty
254 * ->siglock is taken to protect ->signal/->sighand
255 * tasklist_lock is taken to walk process list for sessions
256 * ->siglock is taken to protect ->signal/->sighand
258 void disassociate_ctty(int on_exit)
260 struct tty_struct *tty;
262 if (!current->signal->leader)
265 tty = get_current_tty();
267 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) {
268 tty_vhangup_session(tty);
270 struct pid *tty_pgrp = tty_get_pgrp(tty);
272 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
274 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
280 } else if (on_exit) {
281 struct pid *old_pgrp;
282 spin_lock_irq(¤t->sighand->siglock);
283 old_pgrp = current->signal->tty_old_pgrp;
284 current->signal->tty_old_pgrp = NULL;
285 spin_unlock_irq(¤t->sighand->siglock);
287 kill_pgrp(old_pgrp, SIGHUP, on_exit);
288 kill_pgrp(old_pgrp, SIGCONT, on_exit);
294 tty = get_current_tty();
299 spin_lock_irqsave(&tty->ctrl_lock, flags);
300 put_pid(tty->session);
304 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
309 /* If tty->ctrl.pgrp is not NULL, it may be assigned to
310 * current->signal->tty_old_pgrp in a race condition, and
311 * cause pid memleak. Release current->signal->tty_old_pgrp
312 * after tty->ctrl.pgrp set to NULL.
314 spin_lock_irq(¤t->sighand->siglock);
315 put_pid(current->signal->tty_old_pgrp);
316 current->signal->tty_old_pgrp = NULL;
317 spin_unlock_irq(¤t->sighand->siglock);
319 /* Now clear signal->tty under the lock */
320 read_lock(&tasklist_lock);
321 session_clear_tty(task_session(current));
322 read_unlock(&tasklist_lock);
327 * no_tty - Ensure the current process does not have a controlling tty
331 /* FIXME: Review locking here. The tty_lock never covered any race
332 between a new association and proc_clear_tty but possible we need
333 to protect against this anyway */
334 struct task_struct *tsk = current;
335 disassociate_ctty(0);
340 * tiocsctty - set controlling tty
341 * @tty: tty structure
342 * @arg: user argument
344 * This ioctl is used to manage job control. It permits a session
345 * leader to set this tty as the controlling tty for the session.
348 * Takes tty_lock() to serialize proc_set_tty() for this tty
349 * Takes tasklist_lock internally to walk sessions
350 * Takes ->siglock() when updating signal->tty
352 static int tiocsctty(struct tty_struct *tty, struct file *file, int arg)
357 read_lock(&tasklist_lock);
359 if (current->signal->leader && (task_session(current) == tty->session))
363 * The process must be a session leader and
364 * not have a controlling tty already.
366 if (!current->signal->leader || current->signal->tty) {
373 * This tty is already the controlling
374 * tty for another session group!
376 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
380 session_clear_tty(tty->session);
387 /* See the comment in tty_open_proc_set_tty(). */
388 if ((file->f_mode & FMODE_READ) == 0 && !capable(CAP_SYS_ADMIN)) {
395 read_unlock(&tasklist_lock);
401 * tty_get_pgrp - return a ref counted pgrp pid
404 * Returns a refcounted instance of the pid struct for the process
405 * group controlling the tty.
407 struct pid *tty_get_pgrp(struct tty_struct *tty)
412 spin_lock_irqsave(&tty->ctrl_lock, flags);
413 pgrp = get_pid(tty->pgrp);
414 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
418 EXPORT_SYMBOL_GPL(tty_get_pgrp);
421 * This checks not only the pgrp, but falls back on the pid if no
422 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
425 * The caller must hold rcu lock or the tasklist lock.
427 static struct pid *session_of_pgrp(struct pid *pgrp)
429 struct task_struct *p;
430 struct pid *sid = NULL;
432 p = pid_task(pgrp, PIDTYPE_PGID);
434 p = pid_task(pgrp, PIDTYPE_PID);
436 sid = task_session(p);
442 * tiocgpgrp - get process group
443 * @tty: tty passed by user
444 * @real_tty: tty side of the tty passed by the user if a pty else the tty
447 * Obtain the process group of the tty. If there is no process group
450 * Locking: none. Reference to current->signal->tty is safe.
452 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
457 * (tty == real_tty) is a cheap way of
458 * testing if the tty is NOT a master pty.
460 if (tty == real_tty && current->signal->tty != real_tty)
462 pid = tty_get_pgrp(real_tty);
463 ret = put_user(pid_vnr(pid), p);
469 * tiocspgrp - attempt to set process group
470 * @tty: tty passed by user
471 * @real_tty: tty side device matching tty passed by user
474 * Set the process group of the tty to the session passed. Only
475 * permitted where the tty session is our session.
477 * Locking: RCU, ctrl lock
479 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
483 int retval = tty_check_change(real_tty);
490 if (get_user(pgrp_nr, p))
495 spin_lock_irq(&real_tty->ctrl_lock);
496 if (!current->signal->tty ||
497 (current->signal->tty != real_tty) ||
498 (real_tty->session != task_session(current))) {
500 goto out_unlock_ctrl;
503 pgrp = find_vpid(pgrp_nr);
508 if (session_of_pgrp(pgrp) != task_session(current))
511 put_pid(real_tty->pgrp);
512 real_tty->pgrp = get_pid(pgrp);
516 spin_unlock_irq(&real_tty->ctrl_lock);
521 * tiocgsid - get session id
522 * @tty: tty passed by user
523 * @real_tty: tty side of the tty passed by the user if a pty else the tty
524 * @p: pointer to returned session id
526 * Obtain the session id of the tty. If there is no session
529 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
535 * (tty == real_tty) is a cheap way of
536 * testing if the tty is NOT a master pty.
538 if (tty == real_tty && current->signal->tty != real_tty)
541 spin_lock_irqsave(&real_tty->ctrl_lock, flags);
542 if (!real_tty->session)
544 sid = pid_vnr(real_tty->session);
545 spin_unlock_irqrestore(&real_tty->ctrl_lock, flags);
547 return put_user(sid, p);
550 spin_unlock_irqrestore(&real_tty->ctrl_lock, flags);
555 * Called from tty_ioctl(). If tty is a pty then real_tty is the slave side,
556 * if not then tty == real_tty.
558 long tty_jobctrl_ioctl(struct tty_struct *tty, struct tty_struct *real_tty,
559 struct file *file, unsigned int cmd, unsigned long arg)
561 void __user *p = (void __user *)arg;
565 if (current->signal->tty != tty)
570 return tiocsctty(real_tty, file, arg);
572 return tiocgpgrp(tty, real_tty, p);
574 return tiocspgrp(tty, real_tty, p);
576 return tiocgsid(tty, real_tty, p);