tty_jobctrl.c 14 KB

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