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