1
2
3
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#include "tty.h"
15
16static int is_ignored(int sig)
17{
18 return (sigismember(¤t->blocked, sig) ||
19 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
20}
21
22
23
24
25
26
27
28
29
30
31
32
33int __tty_check_change(struct tty_struct *tty, int sig)
34{
35 unsigned long flags;
36 struct pid *pgrp, *tty_pgrp;
37 int ret = 0;
38
39 if (current->signal->tty != tty)
40 return 0;
41
42 rcu_read_lock();
43 pgrp = task_pgrp(current);
44
45 spin_lock_irqsave(&tty->ctrl.lock, flags);
46 tty_pgrp = tty->ctrl.pgrp;
47 spin_unlock_irqrestore(&tty->ctrl.lock, flags);
48
49 if (tty_pgrp && pgrp != tty_pgrp) {
50 if (is_ignored(sig)) {
51 if (sig == SIGTTIN)
52 ret = -EIO;
53 } else if (is_current_pgrp_orphaned())
54 ret = -EIO;
55 else {
56 kill_pgrp(pgrp, sig, 1);
57 set_thread_flag(TIF_SIGPENDING);
58 ret = -ERESTARTSYS;
59 }
60 }
61 rcu_read_unlock();
62
63 if (!tty_pgrp)
64 tty_warn(tty, "sig=%d, tty->pgrp == NULL!\n", sig);
65
66 return ret;
67}
68
69int tty_check_change(struct tty_struct *tty)
70{
71 return __tty_check_change(tty, SIGTTOU);
72}
73EXPORT_SYMBOL(tty_check_change);
74
75void proc_clear_tty(struct task_struct *p)
76{
77 unsigned long flags;
78 struct tty_struct *tty;
79
80 spin_lock_irqsave(&p->sighand->siglock, flags);
81 tty = p->signal->tty;
82 p->signal->tty = NULL;
83 spin_unlock_irqrestore(&p->sighand->siglock, flags);
84 tty_kref_put(tty);
85}
86
87
88
89
90
91
92
93
94
95
96
97
98static void __proc_set_tty(struct tty_struct *tty)
99{
100 unsigned long flags;
101
102 spin_lock_irqsave(&tty->ctrl.lock, flags);
103
104
105
106
107 put_pid(tty->ctrl.session);
108 put_pid(tty->ctrl.pgrp);
109 tty->ctrl.pgrp = get_pid(task_pgrp(current));
110 tty->ctrl.session = get_pid(task_session(current));
111 spin_unlock_irqrestore(&tty->ctrl.lock, flags);
112 if (current->signal->tty) {
113 tty_debug(tty, "current tty %s not NULL!!\n",
114 current->signal->tty->name);
115 tty_kref_put(current->signal->tty);
116 }
117 put_pid(current->signal->tty_old_pgrp);
118 current->signal->tty = tty_kref_get(tty);
119 current->signal->tty_old_pgrp = NULL;
120}
121
122static void proc_set_tty(struct tty_struct *tty)
123{
124 spin_lock_irq(¤t->sighand->siglock);
125 __proc_set_tty(tty);
126 spin_unlock_irq(¤t->sighand->siglock);
127}
128
129
130
131
132void tty_open_proc_set_tty(struct file *filp, struct tty_struct *tty)
133{
134 read_lock(&tasklist_lock);
135 spin_lock_irq(¤t->sighand->siglock);
136 if (current->signal->leader &&
137 !current->signal->tty &&
138 tty->ctrl.session == NULL) {
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 if (filp->f_mode & FMODE_READ)
154 __proc_set_tty(tty);
155 }
156 spin_unlock_irq(¤t->sighand->siglock);
157 read_unlock(&tasklist_lock);
158}
159
160struct tty_struct *get_current_tty(void)
161{
162 struct tty_struct *tty;
163 unsigned long flags;
164
165 spin_lock_irqsave(¤t->sighand->siglock, flags);
166 tty = tty_kref_get(current->signal->tty);
167 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
168 return tty;
169}
170EXPORT_SYMBOL_GPL(get_current_tty);
171
172
173
174
175void session_clear_tty(struct pid *session)
176{
177 struct task_struct *p;
178
179 do_each_pid_task(session, PIDTYPE_SID, p) {
180 proc_clear_tty(p);
181 } while_each_pid_task(session, PIDTYPE_SID, p);
182}
183
184
185
186
187
188
189
190
191
192
193
194
195
196int tty_signal_session_leader(struct tty_struct *tty, int exit_session)
197{
198 struct task_struct *p;
199 int refs = 0;
200 struct pid *tty_pgrp = NULL;
201
202 read_lock(&tasklist_lock);
203 if (tty->ctrl.session) {
204 do_each_pid_task(tty->ctrl.session, PIDTYPE_SID, p) {
205 spin_lock_irq(&p->sighand->siglock);
206 if (p->signal->tty == tty) {
207 p->signal->tty = NULL;
208
209
210
211
212 refs++;
213 }
214 if (!p->signal->leader) {
215 spin_unlock_irq(&p->sighand->siglock);
216 continue;
217 }
218 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
219 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
220 put_pid(p->signal->tty_old_pgrp);
221 spin_lock(&tty->ctrl.lock);
222 tty_pgrp = get_pid(tty->ctrl.pgrp);
223 if (tty->ctrl.pgrp)
224 p->signal->tty_old_pgrp =
225 get_pid(tty->ctrl.pgrp);
226 spin_unlock(&tty->ctrl.lock);
227 spin_unlock_irq(&p->sighand->siglock);
228 } while_each_pid_task(tty->ctrl.session, PIDTYPE_SID, p);
229 }
230 read_unlock(&tasklist_lock);
231
232 if (tty_pgrp) {
233 if (exit_session)
234 kill_pgrp(tty_pgrp, SIGHUP, exit_session);
235 put_pid(tty_pgrp);
236 }
237
238 return refs;
239}
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265void disassociate_ctty(int on_exit)
266{
267 struct tty_struct *tty;
268
269 if (!current->signal->leader)
270 return;
271
272 tty = get_current_tty();
273 if (tty) {
274 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) {
275 tty_vhangup_session(tty);
276 } else {
277 struct pid *tty_pgrp = tty_get_pgrp(tty);
278
279 if (tty_pgrp) {
280 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
281 if (!on_exit)
282 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
283 put_pid(tty_pgrp);
284 }
285 }
286 tty_kref_put(tty);
287
288 } else if (on_exit) {
289 struct pid *old_pgrp;
290
291 spin_lock_irq(¤t->sighand->siglock);
292 old_pgrp = current->signal->tty_old_pgrp;
293 current->signal->tty_old_pgrp = NULL;
294 spin_unlock_irq(¤t->sighand->siglock);
295 if (old_pgrp) {
296 kill_pgrp(old_pgrp, SIGHUP, on_exit);
297 kill_pgrp(old_pgrp, SIGCONT, on_exit);
298 put_pid(old_pgrp);
299 }
300 return;
301 }
302
303 spin_lock_irq(¤t->sighand->siglock);
304 put_pid(current->signal->tty_old_pgrp);
305 current->signal->tty_old_pgrp = NULL;
306 tty = tty_kref_get(current->signal->tty);
307 spin_unlock_irq(¤t->sighand->siglock);
308
309 if (tty) {
310 unsigned long flags;
311
312 tty_lock(tty);
313 spin_lock_irqsave(&tty->ctrl.lock, flags);
314 put_pid(tty->ctrl.session);
315 put_pid(tty->ctrl.pgrp);
316 tty->ctrl.session = NULL;
317 tty->ctrl.pgrp = NULL;
318 spin_unlock_irqrestore(&tty->ctrl.lock, flags);
319 tty_unlock(tty);
320 tty_kref_put(tty);
321 }
322
323
324 read_lock(&tasklist_lock);
325 session_clear_tty(task_session(current));
326 read_unlock(&tasklist_lock);
327}
328
329
330
331
332
333void no_tty(void)
334{
335
336
337
338
339
340 struct task_struct *tsk = current;
341
342 disassociate_ctty(0);
343 proc_clear_tty(tsk);
344}
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360static int tiocsctty(struct tty_struct *tty, struct file *file, int arg)
361{
362 int ret = 0;
363
364 tty_lock(tty);
365 read_lock(&tasklist_lock);
366
367 if (current->signal->leader &&
368 task_session(current) == tty->ctrl.session)
369 goto unlock;
370
371
372
373
374
375 if (!current->signal->leader || current->signal->tty) {
376 ret = -EPERM;
377 goto unlock;
378 }
379
380 if (tty->ctrl.session) {
381
382
383
384
385 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
386
387
388
389 session_clear_tty(tty->ctrl.session);
390 } else {
391 ret = -EPERM;
392 goto unlock;
393 }
394 }
395
396
397 if ((file->f_mode & FMODE_READ) == 0 && !capable(CAP_SYS_ADMIN)) {
398 ret = -EPERM;
399 goto unlock;
400 }
401
402 proc_set_tty(tty);
403unlock:
404 read_unlock(&tasklist_lock);
405 tty_unlock(tty);
406 return ret;
407}
408
409
410
411
412
413
414
415
416struct pid *tty_get_pgrp(struct tty_struct *tty)
417{
418 unsigned long flags;
419 struct pid *pgrp;
420
421 spin_lock_irqsave(&tty->ctrl.lock, flags);
422 pgrp = get_pid(tty->ctrl.pgrp);
423 spin_unlock_irqrestore(&tty->ctrl.lock, flags);
424
425 return pgrp;
426}
427EXPORT_SYMBOL_GPL(tty_get_pgrp);
428
429
430
431
432
433
434
435
436static struct pid *session_of_pgrp(struct pid *pgrp)
437{
438 struct task_struct *p;
439 struct pid *sid = NULL;
440
441 p = pid_task(pgrp, PIDTYPE_PGID);
442 if (p == NULL)
443 p = pid_task(pgrp, PIDTYPE_PID);
444 if (p != NULL)
445 sid = task_session(p);
446
447 return sid;
448}
449
450
451
452
453
454
455
456
457
458
459
460
461static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
462{
463 struct pid *pid;
464 int ret;
465
466
467
468
469 if (tty == real_tty && current->signal->tty != real_tty)
470 return -ENOTTY;
471 pid = tty_get_pgrp(real_tty);
472 ret = put_user(pid_vnr(pid), p);
473 put_pid(pid);
474 return ret;
475}
476
477
478
479
480
481
482
483
484
485
486
487
488static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
489{
490 struct pid *pgrp;
491 pid_t pgrp_nr;
492 int retval = tty_check_change(real_tty);
493
494 if (retval == -EIO)
495 return -ENOTTY;
496 if (retval)
497 return retval;
498
499 if (get_user(pgrp_nr, p))
500 return -EFAULT;
501 if (pgrp_nr < 0)
502 return -EINVAL;
503
504 spin_lock_irq(&real_tty->ctrl.lock);
505 if (!current->signal->tty ||
506 (current->signal->tty != real_tty) ||
507 (real_tty->ctrl.session != task_session(current))) {
508 retval = -ENOTTY;
509 goto out_unlock_ctrl;
510 }
511 rcu_read_lock();
512 pgrp = find_vpid(pgrp_nr);
513 retval = -ESRCH;
514 if (!pgrp)
515 goto out_unlock;
516 retval = -EPERM;
517 if (session_of_pgrp(pgrp) != task_session(current))
518 goto out_unlock;
519 retval = 0;
520 put_pid(real_tty->ctrl.pgrp);
521 real_tty->ctrl.pgrp = get_pid(pgrp);
522out_unlock:
523 rcu_read_unlock();
524out_unlock_ctrl:
525 spin_unlock_irq(&real_tty->ctrl.lock);
526 return retval;
527}
528
529
530
531
532
533
534
535
536
537
538static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
539{
540 unsigned long flags;
541 pid_t sid;
542
543
544
545
546
547 if (tty == real_tty && current->signal->tty != real_tty)
548 return -ENOTTY;
549
550 spin_lock_irqsave(&real_tty->ctrl.lock, flags);
551 if (!real_tty->ctrl.session)
552 goto err;
553 sid = pid_vnr(real_tty->ctrl.session);
554 spin_unlock_irqrestore(&real_tty->ctrl.lock, flags);
555
556 return put_user(sid, p);
557
558err:
559 spin_unlock_irqrestore(&real_tty->ctrl.lock, flags);
560 return -ENOTTY;
561}
562
563
564
565
566
567long tty_jobctrl_ioctl(struct tty_struct *tty, struct tty_struct *real_tty,
568 struct file *file, unsigned int cmd, unsigned long arg)
569{
570 void __user *p = (void __user *)arg;
571
572 switch (cmd) {
573 case TIOCNOTTY:
574 if (current->signal->tty != tty)
575 return -ENOTTY;
576 no_tty();
577 return 0;
578 case TIOCSCTTY:
579 return tiocsctty(real_tty, file, arg);
580 case TIOCGPGRP:
581 return tiocgpgrp(tty, real_tty, p);
582 case TIOCSPGRP:
583 return tiocspgrp(tty, real_tty, p);
584 case TIOCGSID:
585 return tiocgsid(tty, real_tty, p);
586 }
587 return -ENOIOCTLCMD;
588}
589