tty_jobctrl.c 15 KB

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