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