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