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>
15 static int is_ignored(int sig)
17 return (sigismember(¤t->blocked, sig) ||
18 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
22 * tty_check_change - check for POSIX terminal changes
24 * @sig: signal to send
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
89 * Only callable by the session leader and only if it does not already have
90 * a controlling terminal.
92 * Caller must hold: tty_lock()
93 * a readlock on tasklist_lock
96 static void __proc_set_tty(struct tty_struct *tty)
100 spin_lock_irqsave(&tty->ctrl_lock, flags);
102 * The session and fg pgrp references will be non-NULL if
103 * tiocsctty() is stealing the controlling tty
105 put_pid(tty->session);
107 tty->pgrp = get_pid(task_pgrp(current));
108 tty->session = get_pid(task_session(current));
109 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
110 if (current->signal->tty) {
111 tty_debug(tty, "current tty %s not NULL!!\n",
112 current->signal->tty->name);
113 tty_kref_put(current->signal->tty);
115 put_pid(current->signal->tty_old_pgrp);
116 current->signal->tty = tty_kref_get(tty);
117 current->signal->tty_old_pgrp = NULL;
120 static void proc_set_tty(struct tty_struct *tty)
122 spin_lock_irq(¤t->sighand->siglock);
124 spin_unlock_irq(¤t->sighand->siglock);
128 * Called by tty_open() to set the controlling tty if applicable.
130 void tty_open_proc_set_tty(struct file *filp, struct tty_struct *tty)
132 read_lock(&tasklist_lock);
133 spin_lock_irq(¤t->sighand->siglock);
134 if (current->signal->leader &&
135 !current->signal->tty &&
136 tty->session == NULL) {
138 * Don't let a process that only has write access to the tty
139 * obtain the privileges associated with having a tty as
140 * controlling terminal (being able to reopen it with full
141 * access through /dev/tty, being able to perform pushback).
142 * Many distributions set the group of all ttys to "tty" and
143 * grant write-only access to all terminals for setgid tty
144 * binaries, which should not imply full privileges on all ttys.
146 * This could theoretically break old code that performs open()
147 * on a write-only file descriptor. In that case, it might be
148 * necessary to also permit this if
149 * inode_permission(inode, MAY_READ) == 0.
151 if (filp->f_mode & FMODE_READ)
154 spin_unlock_irq(¤t->sighand->siglock);
155 read_unlock(&tasklist_lock);
158 struct tty_struct *get_current_tty(void)
160 struct tty_struct *tty;
163 spin_lock_irqsave(¤t->sighand->siglock, flags);
164 tty = tty_kref_get(current->signal->tty);
165 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
168 EXPORT_SYMBOL_GPL(get_current_tty);
171 * Called from tty_release().
173 void session_clear_tty(struct pid *session)
175 struct task_struct *p;
176 do_each_pid_task(session, PIDTYPE_SID, p) {
178 } while_each_pid_task(session, PIDTYPE_SID, p);
182 * tty_signal_session_leader - sends SIGHUP to session leader
183 * @tty: controlling tty
184 * @exit_session: if non-zero, signal all foreground group processes
186 * Send SIGHUP and SIGCONT to the session leader and its process group.
187 * Optionally, signal all processes in the foreground process group.
189 * Returns the number of processes in the session with this tty
190 * as their controlling terminal. This value is used to drop
191 * tty references for those processes.
193 int tty_signal_session_leader(struct tty_struct *tty, int exit_session)
195 struct task_struct *p;
197 struct pid *tty_pgrp = NULL;
199 read_lock(&tasklist_lock);
201 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
202 spin_lock_irq(&p->sighand->siglock);
203 if (p->signal->tty == tty) {
204 p->signal->tty = NULL;
205 /* We defer the dereferences outside fo
209 if (!p->signal->leader) {
210 spin_unlock_irq(&p->sighand->siglock);
213 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
214 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
215 put_pid(p->signal->tty_old_pgrp); /* A noop */
216 spin_lock(&tty->ctrl_lock);
217 tty_pgrp = get_pid(tty->pgrp);
219 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
220 spin_unlock(&tty->ctrl_lock);
221 spin_unlock_irq(&p->sighand->siglock);
222 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
224 read_unlock(&tasklist_lock);
228 kill_pgrp(tty_pgrp, SIGHUP, exit_session);
236 * disassociate_ctty - disconnect controlling tty
237 * @on_exit: true if exiting so need to "hang up" the session
239 * This function is typically called only by the session leader, when
240 * it wants to disassociate itself from its controlling tty.
242 * It performs the following functions:
243 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
244 * (2) Clears the tty from being controlling the session
245 * (3) Clears the controlling tty for all processes in the
248 * The argument on_exit is set to 1 if called when a process is
249 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
252 * BTM is taken for hysterical raisons, and held when
253 * called from no_tty().
254 * tty_mutex is taken to protect tty
255 * ->siglock is taken to protect ->signal/->sighand
256 * tasklist_lock is taken to walk process list for sessions
257 * ->siglock is taken to protect ->signal/->sighand
259 void disassociate_ctty(int on_exit)
261 struct tty_struct *tty;
263 if (!current->signal->leader)
266 tty = get_current_tty();
268 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) {
269 tty_vhangup_session(tty);
271 struct pid *tty_pgrp = tty_get_pgrp(tty);
273 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
275 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
281 } else if (on_exit) {
282 struct pid *old_pgrp;
283 spin_lock_irq(¤t->sighand->siglock);
284 old_pgrp = current->signal->tty_old_pgrp;
285 current->signal->tty_old_pgrp = NULL;
286 spin_unlock_irq(¤t->sighand->siglock);
288 kill_pgrp(old_pgrp, SIGHUP, on_exit);
289 kill_pgrp(old_pgrp, SIGCONT, on_exit);
295 spin_lock_irq(¤t->sighand->siglock);
296 put_pid(current->signal->tty_old_pgrp);
297 current->signal->tty_old_pgrp = NULL;
298 tty = tty_kref_get(current->signal->tty);
299 spin_unlock_irq(¤t->sighand->siglock);
305 spin_lock_irqsave(&tty->ctrl_lock, flags);
306 put_pid(tty->session);
310 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
315 /* Now clear signal->tty under the lock */
316 read_lock(&tasklist_lock);
317 session_clear_tty(task_session(current));
318 read_unlock(&tasklist_lock);
323 * no_tty - Ensure the current process does not have a controlling tty
327 /* FIXME: Review locking here. The tty_lock never covered any race
328 between a new association and proc_clear_tty but possible we need
329 to protect against this anyway */
330 struct task_struct *tsk = current;
331 disassociate_ctty(0);
336 * tiocsctty - set controlling tty
337 * @tty: tty structure
338 * @file: file structure used to check permissions
339 * @arg: user argument
341 * This ioctl is used to manage job control. It permits a session
342 * leader to set this tty as the controlling tty for the session.
345 * Takes tty_lock() to serialize proc_set_tty() for this tty
346 * Takes tasklist_lock internally to walk sessions
347 * Takes ->siglock() when updating signal->tty
349 static int tiocsctty(struct tty_struct *tty, struct file *file, int arg)
354 read_lock(&tasklist_lock);
356 if (current->signal->leader && (task_session(current) == tty->session))
360 * The process must be a session leader and
361 * not have a controlling tty already.
363 if (!current->signal->leader || current->signal->tty) {
370 * This tty is already the controlling
371 * tty for another session group!
373 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
377 session_clear_tty(tty->session);
384 /* See the comment in tty_open_proc_set_tty(). */
385 if ((file->f_mode & FMODE_READ) == 0 && !capable(CAP_SYS_ADMIN)) {
392 read_unlock(&tasklist_lock);
398 * tty_get_pgrp - return a ref counted pgrp pid
401 * Returns a refcounted instance of the pid struct for the process
402 * group controlling the tty.
404 struct pid *tty_get_pgrp(struct tty_struct *tty)
409 spin_lock_irqsave(&tty->ctrl_lock, flags);
410 pgrp = get_pid(tty->pgrp);
411 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
415 EXPORT_SYMBOL_GPL(tty_get_pgrp);
418 * This checks not only the pgrp, but falls back on the pid if no
419 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
422 * The caller must hold rcu lock or the tasklist lock.
424 static struct pid *session_of_pgrp(struct pid *pgrp)
426 struct task_struct *p;
427 struct pid *sid = NULL;
429 p = pid_task(pgrp, PIDTYPE_PGID);
431 p = pid_task(pgrp, PIDTYPE_PID);
433 sid = task_session(p);
439 * tiocgpgrp - get process group
440 * @tty: tty passed by user
441 * @real_tty: tty side of the tty passed by the user if a pty else the tty
444 * Obtain the process group of the tty. If there is no process group
447 * Locking: none. Reference to current->signal->tty is safe.
449 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
454 * (tty == real_tty) is a cheap way of
455 * testing if the tty is NOT a master pty.
457 if (tty == real_tty && current->signal->tty != real_tty)
459 pid = tty_get_pgrp(real_tty);
460 ret = put_user(pid_vnr(pid), p);
466 * tiocspgrp - attempt to set process group
467 * @tty: tty passed by user
468 * @real_tty: tty side device matching tty passed by user
471 * Set the process group of the tty to the session passed. Only
472 * permitted where the tty session is our session.
474 * Locking: RCU, ctrl lock
476 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
480 int retval = tty_check_change(real_tty);
487 if (get_user(pgrp_nr, p))
492 spin_lock_irq(&real_tty->ctrl_lock);
493 if (!current->signal->tty ||
494 (current->signal->tty != real_tty) ||
495 (real_tty->session != task_session(current))) {
497 goto out_unlock_ctrl;
500 pgrp = find_vpid(pgrp_nr);
505 if (session_of_pgrp(pgrp) != task_session(current))
508 put_pid(real_tty->pgrp);
509 real_tty->pgrp = get_pid(pgrp);
513 spin_unlock_irq(&real_tty->ctrl_lock);
518 * tiocgsid - get session id
519 * @tty: tty passed by user
520 * @real_tty: tty side of the tty passed by the user if a pty else the tty
521 * @p: pointer to returned session id
523 * Obtain the session id of the tty. If there is no session
526 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
532 * (tty == real_tty) is a cheap way of
533 * testing if the tty is NOT a master pty.
535 if (tty == real_tty && current->signal->tty != real_tty)
538 spin_lock_irqsave(&real_tty->ctrl_lock, flags);
539 if (!real_tty->session)
541 sid = pid_vnr(real_tty->session);
542 spin_unlock_irqrestore(&real_tty->ctrl_lock, flags);
544 return put_user(sid, p);
547 spin_unlock_irqrestore(&real_tty->ctrl_lock, flags);
552 * Called from tty_ioctl(). If tty is a pty then real_tty is the slave side,
553 * if not then tty == real_tty.
555 long tty_jobctrl_ioctl(struct tty_struct *tty, struct tty_struct *real_tty,
556 struct file *file, unsigned int cmd, unsigned long arg)
558 void __user *p = (void __user *)arg;
562 if (current->signal->tty != tty)
567 return tiocsctty(real_tty, file, arg);
569 return tiocgpgrp(tty, real_tty, p);
571 return tiocspgrp(tty, real_tty, p);
573 return tiocgsid(tty, real_tty, p);