selection.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This module exports the functions:
  4. *
  5. * 'int set_selection_user(struct tiocl_selection __user *,
  6. * struct tty_struct *)'
  7. * 'int set_selection_kernel(struct tiocl_selection *, struct tty_struct *)'
  8. * 'void clear_selection(void)'
  9. * 'int paste_selection(struct tty_struct *)'
  10. * 'int sel_loadlut(u32 __user *)'
  11. *
  12. * Now that /dev/vcs exists, most of this can disappear again.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/tty.h>
  16. #include <linux/sched.h>
  17. #include <linux/mm.h>
  18. #include <linux/mutex.h>
  19. #include <linux/slab.h>
  20. #include <linux/types.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/kbd_kern.h>
  23. #include <linux/vt_kern.h>
  24. #include <linux/consolemap.h>
  25. #include <linux/selection.h>
  26. #include <linux/tiocl.h>
  27. #include <linux/console.h>
  28. #include <linux/tty_flip.h>
  29. #include <linux/sched/signal.h>
  30. /* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
  31. #define is_space_on_vt(c) ((c) == ' ')
  32. /* FIXME: all this needs locking */
  33. static struct vc_selection {
  34. struct mutex lock;
  35. struct vc_data *cons; /* must not be deallocated */
  36. char *buffer;
  37. unsigned int buf_len;
  38. volatile int start; /* cleared by clear_selection */
  39. int end;
  40. } vc_sel = {
  41. .lock = __MUTEX_INITIALIZER(vc_sel.lock),
  42. .start = -1,
  43. };
  44. /* clear_selection, highlight and highlight_pointer can be called
  45. from interrupt (via scrollback/front) */
  46. /* set reverse video on characters s-e of console with selection. */
  47. static inline void highlight(const int s, const int e)
  48. {
  49. invert_screen(vc_sel.cons, s, e-s+2, true);
  50. }
  51. /* use complementary color to show the pointer */
  52. static inline void highlight_pointer(const int where)
  53. {
  54. complement_pos(vc_sel.cons, where);
  55. }
  56. static u32
  57. sel_pos(int n, bool unicode)
  58. {
  59. if (unicode)
  60. return screen_glyph_unicode(vc_sel.cons, n / 2);
  61. return inverse_translate(vc_sel.cons, screen_glyph(vc_sel.cons, n),
  62. false);
  63. }
  64. /**
  65. * clear_selection - remove current selection
  66. *
  67. * Remove the current selection highlight, if any from the console holding the
  68. * selection.
  69. *
  70. * Locking: The caller must hold the console lock.
  71. */
  72. void clear_selection(void)
  73. {
  74. highlight_pointer(-1); /* hide the pointer */
  75. if (vc_sel.start != -1) {
  76. highlight(vc_sel.start, vc_sel.end);
  77. vc_sel.start = -1;
  78. }
  79. }
  80. EXPORT_SYMBOL_GPL(clear_selection);
  81. bool vc_is_sel(const struct vc_data *vc)
  82. {
  83. return vc == vc_sel.cons;
  84. }
  85. /*
  86. * User settable table: what characters are to be considered alphabetic?
  87. * 128 bits. Locked by the console lock.
  88. */
  89. static u32 inwordLut[]={
  90. 0x00000000, /* control chars */
  91. 0x03FFE000, /* digits and "-./" */
  92. 0x87FFFFFE, /* uppercase and '_' */
  93. 0x07FFFFFE, /* lowercase */
  94. };
  95. static inline int inword(const u32 c)
  96. {
  97. return c > 0x7f || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
  98. }
  99. /**
  100. * sel_loadlut() - load the LUT table
  101. * @lut: user table
  102. *
  103. * Load the LUT table from user space. Make a temporary copy so a partial
  104. * update doesn't make a mess.
  105. *
  106. * Locking: The console lock is acquired.
  107. */
  108. int sel_loadlut(u32 __user *lut)
  109. {
  110. u32 tmplut[ARRAY_SIZE(inwordLut)];
  111. if (copy_from_user(tmplut, lut, sizeof(inwordLut)))
  112. return -EFAULT;
  113. guard(console_lock)();
  114. memcpy(inwordLut, tmplut, sizeof(inwordLut));
  115. return 0;
  116. }
  117. /* does screen address p correspond to character at LH/RH edge of screen? */
  118. static inline int atedge(const int p, int size_row)
  119. {
  120. return (!(p % size_row) || !((p + 2) % size_row));
  121. }
  122. /* stores the char in UTF8 and returns the number of bytes used (1-4) */
  123. static int store_utf8(u32 c, char *p)
  124. {
  125. if (c < 0x80) {
  126. /* 0******* */
  127. p[0] = c;
  128. return 1;
  129. } else if (c < 0x800) {
  130. /* 110***** 10****** */
  131. p[0] = 0xc0 | (c >> 6);
  132. p[1] = 0x80 | (c & 0x3f);
  133. return 2;
  134. } else if (c < 0x10000) {
  135. /* 1110**** 10****** 10****** */
  136. p[0] = 0xe0 | (c >> 12);
  137. p[1] = 0x80 | ((c >> 6) & 0x3f);
  138. p[2] = 0x80 | (c & 0x3f);
  139. return 3;
  140. } else if (c < 0x110000) {
  141. /* 11110*** 10****** 10****** 10****** */
  142. p[0] = 0xf0 | (c >> 18);
  143. p[1] = 0x80 | ((c >> 12) & 0x3f);
  144. p[2] = 0x80 | ((c >> 6) & 0x3f);
  145. p[3] = 0x80 | (c & 0x3f);
  146. return 4;
  147. } else {
  148. /* outside Unicode, replace with U+FFFD */
  149. p[0] = 0xef;
  150. p[1] = 0xbf;
  151. p[2] = 0xbd;
  152. return 3;
  153. }
  154. }
  155. /**
  156. * set_selection_user - set the current selection.
  157. * @sel: user selection info
  158. * @tty: the console tty
  159. *
  160. * Invoked by the ioctl handle for the vt layer.
  161. *
  162. * Locking: The entire selection process is managed under the console_lock.
  163. * It's a lot under the lock but its hardly a performance path.
  164. */
  165. int set_selection_user(const struct tiocl_selection __user *sel,
  166. struct tty_struct *tty)
  167. {
  168. struct tiocl_selection v;
  169. if (copy_from_user(&v, sel, sizeof(*sel)))
  170. return -EFAULT;
  171. /*
  172. * TIOCL_SELCLEAR and TIOCL_SELPOINTER are OK to use without
  173. * CAP_SYS_ADMIN as they do not modify the selection.
  174. */
  175. switch (v.sel_mode) {
  176. case TIOCL_SELCLEAR:
  177. case TIOCL_SELPOINTER:
  178. break;
  179. default:
  180. if (!capable(CAP_SYS_ADMIN))
  181. return -EPERM;
  182. }
  183. return set_selection_kernel(&v, tty);
  184. }
  185. static int vc_selection_store_chars(struct vc_data *vc, bool unicode)
  186. {
  187. char *bp, *obp;
  188. unsigned int i;
  189. /* Allocate a new buffer before freeing the old one ... */
  190. /* chars can take up to 4 bytes with unicode */
  191. bp = kmalloc_array((vc_sel.end - vc_sel.start) / 2 + 1, unicode ? 4 : 1,
  192. GFP_KERNEL | __GFP_NOWARN);
  193. if (!bp) {
  194. printk(KERN_WARNING "selection: kmalloc() failed\n");
  195. clear_selection();
  196. return -ENOMEM;
  197. }
  198. kfree(vc_sel.buffer);
  199. vc_sel.buffer = bp;
  200. obp = bp;
  201. for (i = vc_sel.start; i <= vc_sel.end; i += 2) {
  202. u32 c = sel_pos(i, unicode);
  203. if (unicode)
  204. bp += store_utf8(c, bp);
  205. else
  206. *bp++ = c;
  207. if (!is_space_on_vt(c))
  208. obp = bp;
  209. if (!((i + 2) % vc->vc_size_row)) {
  210. /* strip trailing blanks from line and add newline,
  211. unless non-space at end of line. */
  212. if (obp != bp) {
  213. bp = obp;
  214. *bp++ = '\r';
  215. }
  216. obp = bp;
  217. }
  218. }
  219. vc_sel.buf_len = bp - vc_sel.buffer;
  220. return 0;
  221. }
  222. static int vc_do_selection(struct vc_data *vc, unsigned short mode, int ps,
  223. int pe)
  224. {
  225. int new_sel_start, new_sel_end, spc;
  226. bool unicode = vt_do_kdgkbmode(fg_console) == K_UNICODE;
  227. switch (mode) {
  228. case TIOCL_SELCHAR: /* character-by-character selection */
  229. new_sel_start = ps;
  230. new_sel_end = pe;
  231. break;
  232. case TIOCL_SELWORD: /* word-by-word selection */
  233. spc = is_space_on_vt(sel_pos(ps, unicode));
  234. for (new_sel_start = ps; ; ps -= 2) {
  235. if ((spc && !is_space_on_vt(sel_pos(ps, unicode))) ||
  236. (!spc && !inword(sel_pos(ps, unicode))))
  237. break;
  238. new_sel_start = ps;
  239. if (!(ps % vc->vc_size_row))
  240. break;
  241. }
  242. spc = is_space_on_vt(sel_pos(pe, unicode));
  243. for (new_sel_end = pe; ; pe += 2) {
  244. if ((spc && !is_space_on_vt(sel_pos(pe, unicode))) ||
  245. (!spc && !inword(sel_pos(pe, unicode))))
  246. break;
  247. new_sel_end = pe;
  248. if (!((pe + 2) % vc->vc_size_row))
  249. break;
  250. }
  251. break;
  252. case TIOCL_SELLINE: /* line-by-line selection */
  253. new_sel_start = rounddown(ps, vc->vc_size_row);
  254. new_sel_end = rounddown(pe, vc->vc_size_row) +
  255. vc->vc_size_row - 2;
  256. break;
  257. case TIOCL_SELPOINTER:
  258. highlight_pointer(pe);
  259. return 0;
  260. default:
  261. return -EINVAL;
  262. }
  263. /* remove the pointer */
  264. highlight_pointer(-1);
  265. /* select to end of line if on trailing space */
  266. if (new_sel_end > new_sel_start &&
  267. !atedge(new_sel_end, vc->vc_size_row) &&
  268. is_space_on_vt(sel_pos(new_sel_end, unicode))) {
  269. for (pe = new_sel_end + 2; ; pe += 2)
  270. if (!is_space_on_vt(sel_pos(pe, unicode)) ||
  271. atedge(pe, vc->vc_size_row))
  272. break;
  273. if (is_space_on_vt(sel_pos(pe, unicode)))
  274. new_sel_end = pe;
  275. }
  276. if (vc_sel.start == -1) /* no current selection */
  277. highlight(new_sel_start, new_sel_end);
  278. else if (new_sel_start == vc_sel.start)
  279. {
  280. if (new_sel_end == vc_sel.end) /* no action required */
  281. return 0;
  282. else if (new_sel_end > vc_sel.end) /* extend to right */
  283. highlight(vc_sel.end + 2, new_sel_end);
  284. else /* contract from right */
  285. highlight(new_sel_end + 2, vc_sel.end);
  286. }
  287. else if (new_sel_end == vc_sel.end)
  288. {
  289. if (new_sel_start < vc_sel.start) /* extend to left */
  290. highlight(new_sel_start, vc_sel.start - 2);
  291. else /* contract from left */
  292. highlight(vc_sel.start, new_sel_start - 2);
  293. }
  294. else /* some other case; start selection from scratch */
  295. {
  296. clear_selection();
  297. highlight(new_sel_start, new_sel_end);
  298. }
  299. vc_sel.start = new_sel_start;
  300. vc_sel.end = new_sel_end;
  301. return vc_selection_store_chars(vc, unicode);
  302. }
  303. static int vc_selection(struct vc_data *vc, struct tiocl_selection *v,
  304. struct tty_struct *tty)
  305. {
  306. int ps, pe;
  307. poke_blanked_console();
  308. if (v->sel_mode == TIOCL_SELCLEAR) {
  309. /* useful for screendump without selection highlights */
  310. clear_selection();
  311. return 0;
  312. }
  313. /* Historically 0 => max value */
  314. v->xs = umin(v->xs - 1, vc->vc_cols - 1);
  315. v->ys = umin(v->ys - 1, vc->vc_rows - 1);
  316. v->xe = umin(v->xe - 1, vc->vc_cols - 1);
  317. v->ye = umin(v->ye - 1, vc->vc_rows - 1);
  318. if (mouse_reporting() && (v->sel_mode & TIOCL_SELMOUSEREPORT)) {
  319. mouse_report(tty, v->sel_mode & TIOCL_SELBUTTONMASK, v->xs,
  320. v->ys);
  321. return 0;
  322. }
  323. ps = v->ys * vc->vc_size_row + (v->xs << 1);
  324. pe = v->ye * vc->vc_size_row + (v->xe << 1);
  325. if (ps > pe) /* make vc_sel.start <= vc_sel.end */
  326. swap(ps, pe);
  327. if (vc_sel.cons != vc) {
  328. clear_selection();
  329. vc_sel.cons = vc;
  330. }
  331. return vc_do_selection(vc, v->sel_mode, ps, pe);
  332. }
  333. int set_selection_kernel(struct tiocl_selection *v, struct tty_struct *tty)
  334. {
  335. guard(mutex)(&vc_sel.lock);
  336. guard(console_lock)();
  337. return vc_selection(vc_cons[fg_console].d, v, tty);
  338. }
  339. EXPORT_SYMBOL_GPL(set_selection_kernel);
  340. /* Insert the contents of the selection buffer into the
  341. * queue of the tty associated with the current console.
  342. * Invoked by ioctl().
  343. *
  344. * Locking: called without locks. Calls the ldisc wrongly with
  345. * unsafe methods,
  346. */
  347. int paste_selection(struct tty_struct *tty)
  348. {
  349. struct vc_data *vc = tty->driver_data;
  350. int pasted = 0;
  351. size_t count;
  352. struct tty_ldisc *ld;
  353. DECLARE_WAITQUEUE(wait, current);
  354. int ret = 0;
  355. bool bp = vc->vc_bracketed_paste;
  356. static const char bracketed_paste_start[] = "\033[200~";
  357. static const char bracketed_paste_end[] = "\033[201~";
  358. const char *bps = bp ? bracketed_paste_start : NULL;
  359. const char *bpe = bp ? bracketed_paste_end : NULL;
  360. scoped_guard(console_lock)
  361. poke_blanked_console();
  362. ld = tty_ldisc_ref_wait(tty);
  363. if (!ld)
  364. return -EIO; /* ldisc was hung up */
  365. tty_buffer_lock_exclusive(&vc->port);
  366. add_wait_queue(&vc->paste_wait, &wait);
  367. mutex_lock(&vc_sel.lock);
  368. while (vc_sel.buffer && (vc_sel.buf_len > pasted || bpe)) {
  369. set_current_state(TASK_INTERRUPTIBLE);
  370. if (signal_pending(current)) {
  371. ret = -EINTR;
  372. break;
  373. }
  374. if (tty_throttled(tty)) {
  375. mutex_unlock(&vc_sel.lock);
  376. schedule();
  377. mutex_lock(&vc_sel.lock);
  378. continue;
  379. }
  380. __set_current_state(TASK_RUNNING);
  381. if (bps) {
  382. bps += tty_ldisc_receive_buf(ld, bps, NULL, strlen(bps));
  383. if (*bps != '\0')
  384. continue;
  385. bps = NULL;
  386. }
  387. count = vc_sel.buf_len - pasted;
  388. if (count) {
  389. pasted += tty_ldisc_receive_buf(ld, vc_sel.buffer + pasted,
  390. NULL, count);
  391. if (vc_sel.buf_len > pasted)
  392. continue;
  393. }
  394. if (bpe) {
  395. bpe += tty_ldisc_receive_buf(ld, bpe, NULL, strlen(bpe));
  396. if (*bpe == '\0')
  397. bpe = NULL;
  398. }
  399. }
  400. mutex_unlock(&vc_sel.lock);
  401. remove_wait_queue(&vc->paste_wait, &wait);
  402. __set_current_state(TASK_RUNNING);
  403. tty_buffer_unlock_exclusive(&vc->port);
  404. tty_ldisc_deref(ld);
  405. return ret;
  406. }
  407. EXPORT_SYMBOL_GPL(paste_selection);