tty_jobctrl.c 14 KB

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