tty_jobctrl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. tty->session = get_pid(task_session(current));
  94. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  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. spin_unlock_irq(&current->sighand->siglock);
  271. if (tty) {
  272. unsigned long flags;
  273. tty_lock(tty);
  274. spin_lock_irqsave(&tty->ctrl_lock, flags);
  275. put_pid(tty->session);
  276. put_pid(tty->pgrp);
  277. tty->session = NULL;
  278. tty->pgrp = NULL;
  279. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  280. tty_unlock(tty);
  281. tty_kref_put(tty);
  282. }
  283. /* Now clear signal->tty under the lock */
  284. read_lock(&tasklist_lock);
  285. session_clear_tty(task_session(current));
  286. read_unlock(&tasklist_lock);
  287. }
  288. /*
  289. *
  290. * no_tty - Ensure the current process does not have a controlling tty
  291. */
  292. void no_tty(void)
  293. {
  294. /* FIXME: Review locking here. The tty_lock never covered any race
  295. between a new association and proc_clear_tty but possible we need
  296. to protect against this anyway */
  297. struct task_struct *tsk = current;
  298. disassociate_ctty(0);
  299. proc_clear_tty(tsk);
  300. }
  301. /**
  302. * tiocsctty - set controlling tty
  303. * @tty: tty structure
  304. * @arg: user argument
  305. *
  306. * This ioctl is used to manage job control. It permits a session
  307. * leader to set this tty as the controlling tty for the session.
  308. *
  309. * Locking:
  310. * Takes tty_lock() to serialize proc_set_tty() for this tty
  311. * Takes tasklist_lock internally to walk sessions
  312. * Takes ->siglock() when updating signal->tty
  313. */
  314. static int tiocsctty(struct tty_struct *tty, struct file *file, int arg)
  315. {
  316. int ret = 0;
  317. tty_lock(tty);
  318. read_lock(&tasklist_lock);
  319. if (current->signal->leader && (task_session(current) == tty->session))
  320. goto unlock;
  321. /*
  322. * The process must be a session leader and
  323. * not have a controlling tty already.
  324. */
  325. if (!current->signal->leader || current->signal->tty) {
  326. ret = -EPERM;
  327. goto unlock;
  328. }
  329. if (tty->session) {
  330. /*
  331. * This tty is already the controlling
  332. * tty for another session group!
  333. */
  334. if (arg == 1 && capable(CAP_SYS_ADMIN)) {
  335. /*
  336. * Steal it away
  337. */
  338. session_clear_tty(tty->session);
  339. } else {
  340. ret = -EPERM;
  341. goto unlock;
  342. }
  343. }
  344. /* See the comment in tty_open_proc_set_tty(). */
  345. if ((file->f_mode & FMODE_READ) == 0 && !capable(CAP_SYS_ADMIN)) {
  346. ret = -EPERM;
  347. goto unlock;
  348. }
  349. proc_set_tty(tty);
  350. unlock:
  351. read_unlock(&tasklist_lock);
  352. tty_unlock(tty);
  353. return ret;
  354. }
  355. /**
  356. * tty_get_pgrp - return a ref counted pgrp pid
  357. * @tty: tty to read
  358. *
  359. * Returns a refcounted instance of the pid struct for the process
  360. * group controlling the tty.
  361. */
  362. struct pid *tty_get_pgrp(struct tty_struct *tty)
  363. {
  364. unsigned long flags;
  365. struct pid *pgrp;
  366. spin_lock_irqsave(&tty->ctrl_lock, flags);
  367. pgrp = get_pid(tty->pgrp);
  368. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  369. return pgrp;
  370. }
  371. EXPORT_SYMBOL_GPL(tty_get_pgrp);
  372. /*
  373. * This checks not only the pgrp, but falls back on the pid if no
  374. * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
  375. * without this...
  376. *
  377. * The caller must hold rcu lock or the tasklist lock.
  378. */
  379. static struct pid *session_of_pgrp(struct pid *pgrp)
  380. {
  381. struct task_struct *p;
  382. struct pid *sid = NULL;
  383. p = pid_task(pgrp, PIDTYPE_PGID);
  384. if (p == NULL)
  385. p = pid_task(pgrp, PIDTYPE_PID);
  386. if (p != NULL)
  387. sid = task_session(p);
  388. return sid;
  389. }
  390. /**
  391. * tiocgpgrp - get process group
  392. * @tty: tty passed by user
  393. * @real_tty: tty side of the tty passed by the user if a pty else the tty
  394. * @p: returned pid
  395. *
  396. * Obtain the process group of the tty. If there is no process group
  397. * return an error.
  398. *
  399. * Locking: none. Reference to current->signal->tty is safe.
  400. */
  401. static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  402. {
  403. struct pid *pid;
  404. int ret;
  405. /*
  406. * (tty == real_tty) is a cheap way of
  407. * testing if the tty is NOT a master pty.
  408. */
  409. if (tty == real_tty && current->signal->tty != real_tty)
  410. return -ENOTTY;
  411. pid = tty_get_pgrp(real_tty);
  412. ret = put_user(pid_vnr(pid), p);
  413. put_pid(pid);
  414. return ret;
  415. }
  416. /**
  417. * tiocspgrp - attempt to set process group
  418. * @tty: tty passed by user
  419. * @real_tty: tty side device matching tty passed by user
  420. * @p: pid pointer
  421. *
  422. * Set the process group of the tty to the session passed. Only
  423. * permitted where the tty session is our session.
  424. *
  425. * Locking: RCU, ctrl lock
  426. */
  427. static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  428. {
  429. struct pid *pgrp;
  430. pid_t pgrp_nr;
  431. int retval = tty_check_change(real_tty);
  432. if (retval == -EIO)
  433. return -ENOTTY;
  434. if (retval)
  435. return retval;
  436. if (get_user(pgrp_nr, p))
  437. return -EFAULT;
  438. if (pgrp_nr < 0)
  439. return -EINVAL;
  440. spin_lock_irq(&real_tty->ctrl_lock);
  441. if (!current->signal->tty ||
  442. (current->signal->tty != real_tty) ||
  443. (real_tty->session != task_session(current))) {
  444. retval = -ENOTTY;
  445. goto out_unlock_ctrl;
  446. }
  447. rcu_read_lock();
  448. pgrp = find_vpid(pgrp_nr);
  449. retval = -ESRCH;
  450. if (!pgrp)
  451. goto out_unlock;
  452. retval = -EPERM;
  453. if (session_of_pgrp(pgrp) != task_session(current))
  454. goto out_unlock;
  455. retval = 0;
  456. put_pid(real_tty->pgrp);
  457. real_tty->pgrp = get_pid(pgrp);
  458. out_unlock:
  459. rcu_read_unlock();
  460. out_unlock_ctrl:
  461. spin_unlock_irq(&real_tty->ctrl_lock);
  462. return retval;
  463. }
  464. /**
  465. * tiocgsid - get session id
  466. * @tty: tty passed by user
  467. * @real_tty: tty side of the tty passed by the user if a pty else the tty
  468. * @p: pointer to returned session id
  469. *
  470. * Obtain the session id of the tty. If there is no session
  471. * return an error.
  472. */
  473. static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
  474. {
  475. unsigned long flags;
  476. pid_t sid;
  477. /*
  478. * (tty == real_tty) is a cheap way of
  479. * testing if the tty is NOT a master pty.
  480. */
  481. if (tty == real_tty && current->signal->tty != real_tty)
  482. return -ENOTTY;
  483. spin_lock_irqsave(&real_tty->ctrl_lock, flags);
  484. if (!real_tty->session)
  485. goto err;
  486. sid = pid_vnr(real_tty->session);
  487. spin_unlock_irqrestore(&real_tty->ctrl_lock, flags);
  488. return put_user(sid, p);
  489. err:
  490. spin_unlock_irqrestore(&real_tty->ctrl_lock, flags);
  491. return -ENOTTY;
  492. }
  493. /*
  494. * Called from tty_ioctl(). If tty is a pty then real_tty is the slave side,
  495. * if not then tty == real_tty.
  496. */
  497. long tty_jobctrl_ioctl(struct tty_struct *tty, struct tty_struct *real_tty,
  498. struct file *file, unsigned int cmd, unsigned long arg)
  499. {
  500. void __user *p = (void __user *)arg;
  501. switch (cmd) {
  502. case TIOCNOTTY:
  503. if (current->signal->tty != tty)
  504. return -ENOTTY;
  505. no_tty();
  506. return 0;
  507. case TIOCSCTTY:
  508. return tiocsctty(real_tty, file, arg);
  509. case TIOCGPGRP:
  510. return tiocgpgrp(tty, real_tty, p);
  511. case TIOCSPGRP:
  512. return tiocspgrp(tty, real_tty, p);
  513. case TIOCGSID:
  514. return tiocgsid(tty, real_tty, p);
  515. }
  516. return -ENOIOCTLCMD;
  517. }