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