vt_ioctl.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1992 obz under the linux copyright
  4. *
  5. * Dynamic diacritical handling - aeb@cwi.nl - Dec 1993
  6. * Dynamic keymap and string allocation - aeb@cwi.nl - May 1994
  7. * Restrict VT switching via ioctl() - grif@cs.ucr.edu - Dec 1995
  8. * Some code moved for less code duplication - Andi Kleen - Mar 1997
  9. * Check put/get_user, cleanups - acme@conectiva.com.br - Jun 2001
  10. */
  11. #include <linux/types.h>
  12. #include <linux/errno.h>
  13. #include <linux/sched/signal.h>
  14. #include <linux/tty.h>
  15. #include <linux/timer.h>
  16. #include <linux/kernel.h>
  17. #include <linux/compat.h>
  18. #include <linux/module.h>
  19. #include <linux/kd.h>
  20. #include <linux/vt.h>
  21. #include <linux/string.h>
  22. #include <linux/slab.h>
  23. #include <linux/major.h>
  24. #include <linux/fs.h>
  25. #include <linux/console.h>
  26. #include <linux/consolemap.h>
  27. #include <linux/signal.h>
  28. #include <linux/suspend.h>
  29. #include <linux/timex.h>
  30. #include <asm/io.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/nospec.h>
  33. #include <linux/kbd_kern.h>
  34. #include <linux/vt_kern.h>
  35. #include <linux/kbd_diacr.h>
  36. #include <linux/selection.h>
  37. bool vt_dont_switch;
  38. static inline bool vt_in_use(unsigned int i)
  39. {
  40. const struct vc_data *vc = vc_cons[i].d;
  41. /*
  42. * console_lock must be held to prevent the vc from being deallocated
  43. * while we're checking whether it's in-use.
  44. */
  45. WARN_CONSOLE_UNLOCKED();
  46. return vc && kref_read(&vc->port.kref) > 1;
  47. }
  48. static inline bool vt_busy(int i)
  49. {
  50. if (vt_in_use(i))
  51. return true;
  52. if (i == fg_console)
  53. return true;
  54. if (vc_is_sel(vc_cons[i].d))
  55. return true;
  56. return false;
  57. }
  58. /*
  59. * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
  60. * experimentation and study of X386 SYSV handling.
  61. *
  62. * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
  63. * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
  64. * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
  65. * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
  66. * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
  67. * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
  68. * to the current console is done by the main ioctl code.
  69. */
  70. #ifdef CONFIG_X86
  71. #include <asm/syscalls.h>
  72. #endif
  73. static void complete_change_console(struct vc_data *vc);
  74. /*
  75. * User space VT_EVENT handlers
  76. */
  77. struct vt_event_wait {
  78. struct list_head list;
  79. struct vt_event event;
  80. int done;
  81. };
  82. static LIST_HEAD(vt_events);
  83. static DEFINE_SPINLOCK(vt_event_lock);
  84. static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);
  85. /**
  86. * vt_event_post
  87. * @event: the event that occurred
  88. * @old: old console
  89. * @new: new console
  90. *
  91. * Post an VT event to interested VT handlers
  92. */
  93. void vt_event_post(unsigned int event, unsigned int old, unsigned int new)
  94. {
  95. struct list_head *pos, *head;
  96. unsigned long flags;
  97. int wake = 0;
  98. spin_lock_irqsave(&vt_event_lock, flags);
  99. head = &vt_events;
  100. list_for_each(pos, head) {
  101. struct vt_event_wait *ve = list_entry(pos,
  102. struct vt_event_wait, list);
  103. if (!(ve->event.event & event))
  104. continue;
  105. ve->event.event = event;
  106. /* kernel view is consoles 0..n-1, user space view is
  107. console 1..n with 0 meaning current, so we must bias */
  108. ve->event.oldev = old + 1;
  109. ve->event.newev = new + 1;
  110. wake = 1;
  111. ve->done = 1;
  112. }
  113. spin_unlock_irqrestore(&vt_event_lock, flags);
  114. if (wake)
  115. wake_up_interruptible(&vt_event_waitqueue);
  116. }
  117. static void __vt_event_queue(struct vt_event_wait *vw)
  118. {
  119. unsigned long flags;
  120. /* Prepare the event */
  121. INIT_LIST_HEAD(&vw->list);
  122. vw->done = 0;
  123. /* Queue our event */
  124. spin_lock_irqsave(&vt_event_lock, flags);
  125. list_add(&vw->list, &vt_events);
  126. spin_unlock_irqrestore(&vt_event_lock, flags);
  127. }
  128. static void __vt_event_wait(struct vt_event_wait *vw)
  129. {
  130. /* Wait for it to pass */
  131. wait_event_interruptible(vt_event_waitqueue, vw->done);
  132. }
  133. static void __vt_event_dequeue(struct vt_event_wait *vw)
  134. {
  135. unsigned long flags;
  136. /* Dequeue it */
  137. spin_lock_irqsave(&vt_event_lock, flags);
  138. list_del(&vw->list);
  139. spin_unlock_irqrestore(&vt_event_lock, flags);
  140. }
  141. /**
  142. * vt_event_wait - wait for an event
  143. * @vw: our event
  144. *
  145. * Waits for an event to occur which completes our vt_event_wait
  146. * structure. On return the structure has wv->done set to 1 for success
  147. * or 0 if some event such as a signal ended the wait.
  148. */
  149. static void vt_event_wait(struct vt_event_wait *vw)
  150. {
  151. __vt_event_queue(vw);
  152. __vt_event_wait(vw);
  153. __vt_event_dequeue(vw);
  154. }
  155. /**
  156. * vt_event_wait_ioctl - event ioctl handler
  157. * @event: argument to ioctl (the event)
  158. *
  159. * Implement the VT_WAITEVENT ioctl using the VT event interface
  160. */
  161. static int vt_event_wait_ioctl(struct vt_event __user *event)
  162. {
  163. struct vt_event_wait vw;
  164. if (copy_from_user(&vw.event, event, sizeof(struct vt_event)))
  165. return -EFAULT;
  166. /* Highest supported event for now */
  167. if (vw.event.event & ~VT_MAX_EVENT)
  168. return -EINVAL;
  169. vt_event_wait(&vw);
  170. /* If it occurred report it */
  171. if (vw.done) {
  172. if (copy_to_user(event, &vw.event, sizeof(struct vt_event)))
  173. return -EFAULT;
  174. return 0;
  175. }
  176. return -EINTR;
  177. }
  178. /**
  179. * vt_waitactive - active console wait
  180. * @n: new console
  181. *
  182. * Helper for event waits. Used to implement the legacy
  183. * event waiting ioctls in terms of events
  184. */
  185. int vt_waitactive(int n)
  186. {
  187. struct vt_event_wait vw;
  188. do {
  189. vw.event.event = VT_EVENT_SWITCH;
  190. __vt_event_queue(&vw);
  191. if (n == fg_console + 1) {
  192. __vt_event_dequeue(&vw);
  193. break;
  194. }
  195. __vt_event_wait(&vw);
  196. __vt_event_dequeue(&vw);
  197. if (vw.done == 0)
  198. return -EINTR;
  199. } while (vw.event.newev != n);
  200. return 0;
  201. }
  202. /*
  203. * these are the valid i/o ports we're allowed to change. they map all the
  204. * video ports
  205. */
  206. #define GPFIRST 0x3b4
  207. #define GPLAST 0x3df
  208. #define GPNUM (GPLAST - GPFIRST + 1)
  209. /*
  210. * currently, setting the mode from KD_TEXT to KD_GRAPHICS doesn't do a whole
  211. * lot. i'm not sure if it should do any restoration of modes or what...
  212. *
  213. * XXX It should at least call into the driver, fbdev's definitely need to
  214. * restore their engine state. --BenH
  215. *
  216. * Called with the console lock held.
  217. */
  218. static int vt_kdsetmode(struct vc_data *vc, unsigned long mode)
  219. {
  220. switch (mode) {
  221. case KD_GRAPHICS:
  222. break;
  223. case KD_TEXT0:
  224. case KD_TEXT1:
  225. mode = KD_TEXT;
  226. fallthrough;
  227. case KD_TEXT:
  228. break;
  229. default:
  230. return -EINVAL;
  231. }
  232. if (vc->vc_mode == mode)
  233. return 0;
  234. vc->vc_mode = mode;
  235. if (vc->vc_num != fg_console)
  236. return 0;
  237. /* explicitly blank/unblank the screen if switching modes */
  238. if (mode == KD_TEXT)
  239. do_unblank_screen(1);
  240. else
  241. do_blank_screen(1);
  242. return 0;
  243. }
  244. static int vt_k_ioctl(struct tty_struct *tty, unsigned int cmd,
  245. unsigned long arg, bool perm)
  246. {
  247. struct vc_data *vc = tty->driver_data;
  248. void __user *up = (void __user *)arg;
  249. unsigned int console = vc->vc_num;
  250. int ret;
  251. switch (cmd) {
  252. case KIOCSOUND:
  253. if (!perm)
  254. return -EPERM;
  255. /*
  256. * The use of PIT_TICK_RATE is historic, it used to be
  257. * the platform-dependent CLOCK_TICK_RATE between 2.6.12
  258. * and 2.6.36, which was a minor but unfortunate ABI
  259. * change. kd_mksound is locked by the input layer.
  260. */
  261. if (arg)
  262. arg = PIT_TICK_RATE / arg;
  263. kd_mksound(arg, 0);
  264. break;
  265. case KDMKTONE:
  266. if (!perm)
  267. return -EPERM;
  268. {
  269. unsigned int ticks, count;
  270. /*
  271. * Generate the tone for the appropriate number of ticks.
  272. * If the time is zero, turn off sound ourselves.
  273. */
  274. ticks = msecs_to_jiffies((arg >> 16) & 0xffff);
  275. count = ticks ? (arg & 0xffff) : 0;
  276. if (count)
  277. count = PIT_TICK_RATE / count;
  278. kd_mksound(count, ticks);
  279. break;
  280. }
  281. case KDGKBTYPE:
  282. /*
  283. * this is naïve.
  284. */
  285. return put_user(KB_101, (char __user *)arg);
  286. /*
  287. * These cannot be implemented on any machine that implements
  288. * ioperm() in user level (such as Alpha PCs) or not at all.
  289. *
  290. * XXX: you should never use these, just call ioperm directly..
  291. */
  292. #ifdef CONFIG_X86
  293. case KDADDIO:
  294. case KDDELIO:
  295. /*
  296. * KDADDIO and KDDELIO may be able to add ports beyond what
  297. * we reject here, but to be safe...
  298. *
  299. * These are locked internally via sys_ioperm
  300. */
  301. if (arg < GPFIRST || arg > GPLAST)
  302. return -EINVAL;
  303. return ksys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
  304. case KDENABIO:
  305. case KDDISABIO:
  306. return ksys_ioperm(GPFIRST, GPNUM,
  307. (cmd == KDENABIO)) ? -ENXIO : 0;
  308. #endif
  309. /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
  310. case KDKBDREP:
  311. {
  312. struct kbd_repeat kbrep;
  313. if (!capable(CAP_SYS_TTY_CONFIG))
  314. return -EPERM;
  315. if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat)))
  316. return -EFAULT;
  317. ret = kbd_rate(&kbrep);
  318. if (ret)
  319. return ret;
  320. if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
  321. return -EFAULT;
  322. break;
  323. }
  324. case KDSETMODE:
  325. if (!perm)
  326. return -EPERM;
  327. console_lock();
  328. ret = vt_kdsetmode(vc, arg);
  329. console_unlock();
  330. return ret;
  331. case KDGETMODE:
  332. return put_user(vc->vc_mode, (int __user *)arg);
  333. case KDMAPDISP:
  334. case KDUNMAPDISP:
  335. /*
  336. * these work like a combination of mmap and KDENABIO.
  337. * this could be easily finished.
  338. */
  339. return -EINVAL;
  340. case KDSKBMODE:
  341. if (!perm)
  342. return -EPERM;
  343. ret = vt_do_kdskbmode(console, arg);
  344. if (ret)
  345. return ret;
  346. tty_ldisc_flush(tty);
  347. break;
  348. case KDGKBMODE:
  349. return put_user(vt_do_kdgkbmode(console), (int __user *)arg);
  350. /* this could be folded into KDSKBMODE, but for compatibility
  351. reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
  352. case KDSKBMETA:
  353. return vt_do_kdskbmeta(console, arg);
  354. case KDGKBMETA:
  355. /* FIXME: should review whether this is worth locking */
  356. return put_user(vt_do_kdgkbmeta(console), (int __user *)arg);
  357. case KDGETKEYCODE:
  358. case KDSETKEYCODE:
  359. if(!capable(CAP_SYS_TTY_CONFIG))
  360. perm = 0;
  361. return vt_do_kbkeycode_ioctl(cmd, up, perm);
  362. case KDGKBENT:
  363. case KDSKBENT:
  364. return vt_do_kdsk_ioctl(cmd, up, perm, console);
  365. case KDGKBSENT:
  366. case KDSKBSENT:
  367. return vt_do_kdgkb_ioctl(cmd, up, perm);
  368. /* Diacritical processing. Handled in keyboard.c as it has
  369. to operate on the keyboard locks and structures */
  370. case KDGKBDIACR:
  371. case KDGKBDIACRUC:
  372. case KDSKBDIACR:
  373. case KDSKBDIACRUC:
  374. return vt_do_diacrit(cmd, up, perm);
  375. /* the ioctls below read/set the flags usually shown in the leds */
  376. /* don't use them - they will go away without warning */
  377. case KDGKBLED:
  378. case KDSKBLED:
  379. case KDGETLED:
  380. case KDSETLED:
  381. return vt_do_kdskled(console, cmd, arg, perm);
  382. /*
  383. * A process can indicate its willingness to accept signals
  384. * generated by pressing an appropriate key combination.
  385. * Thus, one can have a daemon that e.g. spawns a new console
  386. * upon a keypress and then changes to it.
  387. * See also the kbrequest field of inittab(5).
  388. */
  389. case KDSIGACCEPT:
  390. if (!perm || !capable(CAP_KILL))
  391. return -EPERM;
  392. if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
  393. return -EINVAL;
  394. spin_lock_irq(&vt_spawn_con.lock);
  395. put_pid(vt_spawn_con.pid);
  396. vt_spawn_con.pid = get_pid(task_pid(current));
  397. vt_spawn_con.sig = arg;
  398. spin_unlock_irq(&vt_spawn_con.lock);
  399. break;
  400. case KDFONTOP: {
  401. struct console_font_op op;
  402. if (copy_from_user(&op, up, sizeof(op)))
  403. return -EFAULT;
  404. if (!perm && op.op != KD_FONT_OP_GET)
  405. return -EPERM;
  406. ret = con_font_op(vc, &op);
  407. if (ret)
  408. return ret;
  409. if (copy_to_user(up, &op, sizeof(op)))
  410. return -EFAULT;
  411. break;
  412. }
  413. default:
  414. return -ENOIOCTLCMD;
  415. }
  416. return 0;
  417. }
  418. static inline int do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud,
  419. bool perm, struct vc_data *vc)
  420. {
  421. struct unimapdesc tmp;
  422. if (copy_from_user(&tmp, user_ud, sizeof tmp))
  423. return -EFAULT;
  424. switch (cmd) {
  425. case PIO_UNIMAP:
  426. if (!perm)
  427. return -EPERM;
  428. return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
  429. case GIO_UNIMAP:
  430. if (!perm && fg_console != vc->vc_num)
  431. return -EPERM;
  432. return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct),
  433. tmp.entries);
  434. }
  435. return 0;
  436. }
  437. static int vt_io_ioctl(struct vc_data *vc, unsigned int cmd, void __user *up,
  438. bool perm)
  439. {
  440. switch (cmd) {
  441. case PIO_CMAP:
  442. if (!perm)
  443. return -EPERM;
  444. return con_set_cmap(up);
  445. case GIO_CMAP:
  446. return con_get_cmap(up);
  447. case PIO_SCRNMAP:
  448. if (!perm)
  449. return -EPERM;
  450. return con_set_trans_old(up);
  451. case GIO_SCRNMAP:
  452. return con_get_trans_old(up);
  453. case PIO_UNISCRNMAP:
  454. if (!perm)
  455. return -EPERM;
  456. return con_set_trans_new(up);
  457. case GIO_UNISCRNMAP:
  458. return con_get_trans_new(up);
  459. case PIO_UNIMAPCLR:
  460. if (!perm)
  461. return -EPERM;
  462. con_clear_unimap(vc);
  463. break;
  464. case PIO_UNIMAP:
  465. case GIO_UNIMAP:
  466. return do_unimap_ioctl(cmd, up, perm, vc);
  467. default:
  468. return -ENOIOCTLCMD;
  469. }
  470. return 0;
  471. }
  472. static int vt_reldisp(struct vc_data *vc, unsigned int swtch)
  473. {
  474. int newvt, ret;
  475. if (vc->vt_mode.mode != VT_PROCESS)
  476. return -EINVAL;
  477. /* Switched-to response */
  478. if (vc->vt_newvt < 0) {
  479. /* If it's just an ACK, ignore it */
  480. return swtch == VT_ACKACQ ? 0 : -EINVAL;
  481. }
  482. /* Switching-from response */
  483. if (swtch == 0) {
  484. /* Switch disallowed, so forget we were trying to do it. */
  485. vc->vt_newvt = -1;
  486. return 0;
  487. }
  488. /* The current vt has been released, so complete the switch. */
  489. newvt = vc->vt_newvt;
  490. vc->vt_newvt = -1;
  491. ret = vc_allocate(newvt);
  492. if (ret)
  493. return ret;
  494. /*
  495. * When we actually do the console switch, make sure we are atomic with
  496. * respect to other console switches..
  497. */
  498. complete_change_console(vc_cons[newvt].d);
  499. return 0;
  500. }
  501. static int vt_setactivate(struct vt_setactivate __user *sa)
  502. {
  503. struct vt_setactivate vsa;
  504. struct vc_data *nvc;
  505. int ret;
  506. if (copy_from_user(&vsa, sa, sizeof(vsa)))
  507. return -EFAULT;
  508. if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
  509. return -ENXIO;
  510. vsa.console--;
  511. vsa.console = array_index_nospec(vsa.console, MAX_NR_CONSOLES);
  512. console_lock();
  513. ret = vc_allocate(vsa.console);
  514. if (ret) {
  515. console_unlock();
  516. return ret;
  517. }
  518. /*
  519. * This is safe providing we don't drop the console sem between
  520. * vc_allocate and finishing referencing nvc.
  521. */
  522. nvc = vc_cons[vsa.console].d;
  523. nvc->vt_mode = vsa.mode;
  524. nvc->vt_mode.frsig = 0;
  525. put_pid(nvc->vt_pid);
  526. nvc->vt_pid = get_pid(task_pid(current));
  527. console_unlock();
  528. /* Commence switch and lock */
  529. /* Review set_console locks */
  530. set_console(vsa.console);
  531. return 0;
  532. }
  533. /* deallocate a single console, if possible (leave 0) */
  534. static int vt_disallocate(unsigned int vc_num)
  535. {
  536. struct vc_data *vc = NULL;
  537. int ret = 0;
  538. console_lock();
  539. if (vt_busy(vc_num))
  540. ret = -EBUSY;
  541. else if (vc_num)
  542. vc = vc_deallocate(vc_num);
  543. console_unlock();
  544. if (vc && vc_num >= MIN_NR_CONSOLES)
  545. tty_port_put(&vc->port);
  546. return ret;
  547. }
  548. /* deallocate all unused consoles, but leave 0 */
  549. static void vt_disallocate_all(void)
  550. {
  551. struct vc_data *vc[MAX_NR_CONSOLES];
  552. int i;
  553. console_lock();
  554. for (i = 1; i < MAX_NR_CONSOLES; i++)
  555. if (!vt_busy(i))
  556. vc[i] = vc_deallocate(i);
  557. else
  558. vc[i] = NULL;
  559. console_unlock();
  560. for (i = 1; i < MAX_NR_CONSOLES; i++) {
  561. if (vc[i] && i >= MIN_NR_CONSOLES)
  562. tty_port_put(&vc[i]->port);
  563. }
  564. }
  565. static int vt_resizex(struct vc_data *vc, struct vt_consize __user *cs)
  566. {
  567. struct vt_consize v;
  568. int i;
  569. if (copy_from_user(&v, cs, sizeof(struct vt_consize)))
  570. return -EFAULT;
  571. /* FIXME: Should check the copies properly */
  572. if (!v.v_vlin)
  573. v.v_vlin = vc->vc_scan_lines;
  574. if (v.v_clin) {
  575. int rows = v.v_vlin / v.v_clin;
  576. if (v.v_rows != rows) {
  577. if (v.v_rows) /* Parameters don't add up */
  578. return -EINVAL;
  579. v.v_rows = rows;
  580. }
  581. }
  582. if (v.v_vcol && v.v_ccol) {
  583. int cols = v.v_vcol / v.v_ccol;
  584. if (v.v_cols != cols) {
  585. if (v.v_cols)
  586. return -EINVAL;
  587. v.v_cols = cols;
  588. }
  589. }
  590. if (v.v_clin > 32)
  591. return -EINVAL;
  592. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  593. struct vc_data *vcp;
  594. if (!vc_cons[i].d)
  595. continue;
  596. console_lock();
  597. vcp = vc_cons[i].d;
  598. if (vcp) {
  599. int ret;
  600. int save_scan_lines = vcp->vc_scan_lines;
  601. int save_cell_height = vcp->vc_cell_height;
  602. if (v.v_vlin)
  603. vcp->vc_scan_lines = v.v_vlin;
  604. if (v.v_clin)
  605. vcp->vc_cell_height = v.v_clin;
  606. ret = __vc_resize(vcp, v.v_cols, v.v_rows, true);
  607. if (ret) {
  608. vcp->vc_scan_lines = save_scan_lines;
  609. vcp->vc_cell_height = save_cell_height;
  610. console_unlock();
  611. return ret;
  612. }
  613. }
  614. console_unlock();
  615. }
  616. return 0;
  617. }
  618. /*
  619. * We handle the console-specific ioctl's here. We allow the
  620. * capability to modify any console, not just the fg_console.
  621. */
  622. int vt_ioctl(struct tty_struct *tty,
  623. unsigned int cmd, unsigned long arg)
  624. {
  625. struct vc_data *vc = tty->driver_data;
  626. void __user *up = (void __user *)arg;
  627. int i, perm;
  628. int ret;
  629. /*
  630. * To have permissions to do most of the vt ioctls, we either have
  631. * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
  632. */
  633. perm = 0;
  634. if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
  635. perm = 1;
  636. ret = vt_k_ioctl(tty, cmd, arg, perm);
  637. if (ret != -ENOIOCTLCMD)
  638. return ret;
  639. ret = vt_io_ioctl(vc, cmd, up, perm);
  640. if (ret != -ENOIOCTLCMD)
  641. return ret;
  642. switch (cmd) {
  643. case TIOCLINUX:
  644. return tioclinux(tty, arg);
  645. case VT_SETMODE:
  646. {
  647. struct vt_mode tmp;
  648. if (!perm)
  649. return -EPERM;
  650. if (copy_from_user(&tmp, up, sizeof(struct vt_mode)))
  651. return -EFAULT;
  652. if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS)
  653. return -EINVAL;
  654. console_lock();
  655. vc->vt_mode = tmp;
  656. /* the frsig is ignored, so we set it to 0 */
  657. vc->vt_mode.frsig = 0;
  658. put_pid(vc->vt_pid);
  659. vc->vt_pid = get_pid(task_pid(current));
  660. /* no switch is required -- saw@shade.msu.ru */
  661. vc->vt_newvt = -1;
  662. console_unlock();
  663. break;
  664. }
  665. case VT_GETMODE:
  666. {
  667. struct vt_mode tmp;
  668. int rc;
  669. console_lock();
  670. memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
  671. console_unlock();
  672. rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
  673. if (rc)
  674. return -EFAULT;
  675. break;
  676. }
  677. /*
  678. * Returns global vt state. Note that VT 0 is always open, since
  679. * it's an alias for the current VT, and people can't use it here.
  680. * We cannot return state for more than 16 VTs, since v_state is short.
  681. */
  682. case VT_GETSTATE:
  683. {
  684. struct vt_stat __user *vtstat = up;
  685. unsigned short state, mask;
  686. if (put_user(fg_console + 1, &vtstat->v_active))
  687. return -EFAULT;
  688. state = 1; /* /dev/tty0 is always open */
  689. console_lock(); /* required by vt_in_use() */
  690. for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
  691. ++i, mask <<= 1)
  692. if (vt_in_use(i))
  693. state |= mask;
  694. console_unlock();
  695. return put_user(state, &vtstat->v_state);
  696. }
  697. /*
  698. * Returns the first available (non-opened) console.
  699. */
  700. case VT_OPENQRY:
  701. console_lock(); /* required by vt_in_use() */
  702. for (i = 0; i < MAX_NR_CONSOLES; ++i)
  703. if (!vt_in_use(i))
  704. break;
  705. console_unlock();
  706. i = i < MAX_NR_CONSOLES ? (i+1) : -1;
  707. return put_user(i, (int __user *)arg);
  708. /*
  709. * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
  710. * with num >= 1 (switches to vt 0, our console, are not allowed, just
  711. * to preserve sanity).
  712. */
  713. case VT_ACTIVATE:
  714. if (!perm)
  715. return -EPERM;
  716. if (arg == 0 || arg > MAX_NR_CONSOLES)
  717. return -ENXIO;
  718. arg--;
  719. arg = array_index_nospec(arg, MAX_NR_CONSOLES);
  720. console_lock();
  721. ret = vc_allocate(arg);
  722. console_unlock();
  723. if (ret)
  724. return ret;
  725. set_console(arg);
  726. break;
  727. case VT_SETACTIVATE:
  728. if (!perm)
  729. return -EPERM;
  730. return vt_setactivate(up);
  731. /*
  732. * wait until the specified VT has been activated
  733. */
  734. case VT_WAITACTIVE:
  735. if (!perm)
  736. return -EPERM;
  737. if (arg == 0 || arg > MAX_NR_CONSOLES)
  738. return -ENXIO;
  739. return vt_waitactive(arg);
  740. /*
  741. * If a vt is under process control, the kernel will not switch to it
  742. * immediately, but postpone the operation until the process calls this
  743. * ioctl, allowing the switch to complete.
  744. *
  745. * According to the X sources this is the behavior:
  746. * 0: pending switch-from not OK
  747. * 1: pending switch-from OK
  748. * 2: completed switch-to OK
  749. */
  750. case VT_RELDISP:
  751. if (!perm)
  752. return -EPERM;
  753. console_lock();
  754. ret = vt_reldisp(vc, arg);
  755. console_unlock();
  756. return ret;
  757. /*
  758. * Disallocate memory associated to VT (but leave VT1)
  759. */
  760. case VT_DISALLOCATE:
  761. if (arg > MAX_NR_CONSOLES)
  762. return -ENXIO;
  763. if (arg == 0) {
  764. vt_disallocate_all();
  765. break;
  766. }
  767. arg = array_index_nospec(arg - 1, MAX_NR_CONSOLES);
  768. return vt_disallocate(arg);
  769. case VT_RESIZE:
  770. {
  771. struct vt_sizes __user *vtsizes = up;
  772. struct vc_data *vc;
  773. ushort ll,cc;
  774. if (!perm)
  775. return -EPERM;
  776. if (get_user(ll, &vtsizes->v_rows) ||
  777. get_user(cc, &vtsizes->v_cols))
  778. return -EFAULT;
  779. console_lock();
  780. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  781. vc = vc_cons[i].d;
  782. if (vc) {
  783. /* FIXME: review v tty lock */
  784. __vc_resize(vc_cons[i].d, cc, ll, true);
  785. }
  786. }
  787. console_unlock();
  788. break;
  789. }
  790. case VT_RESIZEX:
  791. if (!perm)
  792. return -EPERM;
  793. return vt_resizex(vc, up);
  794. case VT_LOCKSWITCH:
  795. if (!capable(CAP_SYS_TTY_CONFIG))
  796. return -EPERM;
  797. vt_dont_switch = true;
  798. break;
  799. case VT_UNLOCKSWITCH:
  800. if (!capable(CAP_SYS_TTY_CONFIG))
  801. return -EPERM;
  802. vt_dont_switch = false;
  803. break;
  804. case VT_GETHIFONTMASK:
  805. return put_user(vc->vc_hi_font_mask,
  806. (unsigned short __user *)arg);
  807. case VT_WAITEVENT:
  808. return vt_event_wait_ioctl((struct vt_event __user *)arg);
  809. default:
  810. return -ENOIOCTLCMD;
  811. }
  812. return 0;
  813. }
  814. void reset_vc(struct vc_data *vc)
  815. {
  816. vc->vc_mode = KD_TEXT;
  817. vt_reset_unicode(vc->vc_num);
  818. vc->vt_mode.mode = VT_AUTO;
  819. vc->vt_mode.waitv = 0;
  820. vc->vt_mode.relsig = 0;
  821. vc->vt_mode.acqsig = 0;
  822. vc->vt_mode.frsig = 0;
  823. put_pid(vc->vt_pid);
  824. vc->vt_pid = NULL;
  825. vc->vt_newvt = -1;
  826. reset_palette(vc);
  827. }
  828. void vc_SAK(struct work_struct *work)
  829. {
  830. struct vc *vc_con =
  831. container_of(work, struct vc, SAK_work);
  832. struct vc_data *vc;
  833. struct tty_struct *tty;
  834. console_lock();
  835. vc = vc_con->d;
  836. if (vc) {
  837. /* FIXME: review tty ref counting */
  838. tty = vc->port.tty;
  839. /*
  840. * SAK should also work in all raw modes and reset
  841. * them properly.
  842. */
  843. if (tty)
  844. __do_SAK(tty);
  845. reset_vc(vc);
  846. }
  847. console_unlock();
  848. }
  849. #ifdef CONFIG_COMPAT
  850. struct compat_console_font_op {
  851. compat_uint_t op; /* operation code KD_FONT_OP_* */
  852. compat_uint_t flags; /* KD_FONT_FLAG_* */
  853. compat_uint_t width, height; /* font size */
  854. compat_uint_t charcount;
  855. compat_caddr_t data; /* font data with height fixed to 32 */
  856. };
  857. static inline int
  858. compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
  859. int perm, struct console_font_op *op, struct vc_data *vc)
  860. {
  861. int i;
  862. if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
  863. return -EFAULT;
  864. if (!perm && op->op != KD_FONT_OP_GET)
  865. return -EPERM;
  866. op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
  867. i = con_font_op(vc, op);
  868. if (i)
  869. return i;
  870. ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
  871. if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
  872. return -EFAULT;
  873. return 0;
  874. }
  875. struct compat_unimapdesc {
  876. unsigned short entry_ct;
  877. compat_caddr_t entries;
  878. };
  879. static inline int
  880. compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
  881. int perm, struct vc_data *vc)
  882. {
  883. struct compat_unimapdesc tmp;
  884. struct unipair __user *tmp_entries;
  885. if (copy_from_user(&tmp, user_ud, sizeof tmp))
  886. return -EFAULT;
  887. tmp_entries = compat_ptr(tmp.entries);
  888. switch (cmd) {
  889. case PIO_UNIMAP:
  890. if (!perm)
  891. return -EPERM;
  892. return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
  893. case GIO_UNIMAP:
  894. if (!perm && fg_console != vc->vc_num)
  895. return -EPERM;
  896. return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
  897. }
  898. return 0;
  899. }
  900. long vt_compat_ioctl(struct tty_struct *tty,
  901. unsigned int cmd, unsigned long arg)
  902. {
  903. struct vc_data *vc = tty->driver_data;
  904. struct console_font_op op; /* used in multiple places here */
  905. void __user *up = compat_ptr(arg);
  906. int perm;
  907. /*
  908. * To have permissions to do most of the vt ioctls, we either have
  909. * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
  910. */
  911. perm = 0;
  912. if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
  913. perm = 1;
  914. switch (cmd) {
  915. /*
  916. * these need special handlers for incompatible data structures
  917. */
  918. case KDFONTOP:
  919. return compat_kdfontop_ioctl(up, perm, &op, vc);
  920. case PIO_UNIMAP:
  921. case GIO_UNIMAP:
  922. return compat_unimap_ioctl(cmd, up, perm, vc);
  923. /*
  924. * all these treat 'arg' as an integer
  925. */
  926. case KIOCSOUND:
  927. case KDMKTONE:
  928. #ifdef CONFIG_X86
  929. case KDADDIO:
  930. case KDDELIO:
  931. #endif
  932. case KDSETMODE:
  933. case KDMAPDISP:
  934. case KDUNMAPDISP:
  935. case KDSKBMODE:
  936. case KDSKBMETA:
  937. case KDSKBLED:
  938. case KDSETLED:
  939. case KDSIGACCEPT:
  940. case VT_ACTIVATE:
  941. case VT_WAITACTIVE:
  942. case VT_RELDISP:
  943. case VT_DISALLOCATE:
  944. return vt_ioctl(tty, cmd, arg);
  945. /*
  946. * the rest has a compatible data structure behind arg,
  947. * but we have to convert it to a proper 64 bit pointer.
  948. */
  949. default:
  950. return vt_ioctl(tty, cmd, (unsigned long)up);
  951. }
  952. }
  953. #endif /* CONFIG_COMPAT */
  954. /*
  955. * Performs the back end of a vt switch. Called under the console
  956. * semaphore.
  957. */
  958. static void complete_change_console(struct vc_data *vc)
  959. {
  960. unsigned char old_vc_mode;
  961. int old = fg_console;
  962. last_console = fg_console;
  963. /*
  964. * If we're switching, we could be going from KD_GRAPHICS to
  965. * KD_TEXT mode or vice versa, which means we need to blank or
  966. * unblank the screen later.
  967. */
  968. old_vc_mode = vc_cons[fg_console].d->vc_mode;
  969. switch_screen(vc);
  970. /*
  971. * This can't appear below a successful kill_pid(). If it did,
  972. * then the *blank_screen operation could occur while X, having
  973. * received acqsig, is waking up on another processor. This
  974. * condition can lead to overlapping accesses to the VGA range
  975. * and the framebuffer (causing system lockups).
  976. *
  977. * To account for this we duplicate this code below only if the
  978. * controlling process is gone and we've called reset_vc.
  979. */
  980. if (old_vc_mode != vc->vc_mode) {
  981. if (vc->vc_mode == KD_TEXT)
  982. do_unblank_screen(1);
  983. else
  984. do_blank_screen(1);
  985. }
  986. /*
  987. * If this new console is under process control, send it a signal
  988. * telling it that it has acquired. Also check if it has died and
  989. * clean up (similar to logic employed in change_console())
  990. */
  991. if (vc->vt_mode.mode == VT_PROCESS) {
  992. /*
  993. * Send the signal as privileged - kill_pid() will
  994. * tell us if the process has gone or something else
  995. * is awry
  996. */
  997. if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
  998. /*
  999. * The controlling process has died, so we revert back to
  1000. * normal operation. In this case, we'll also change back
  1001. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1002. * but it saves the agony when the X server dies and the screen
  1003. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1004. * this outside of VT_PROCESS but there is no single process
  1005. * to account for and tracking tty count may be undesirable.
  1006. */
  1007. reset_vc(vc);
  1008. if (old_vc_mode != vc->vc_mode) {
  1009. if (vc->vc_mode == KD_TEXT)
  1010. do_unblank_screen(1);
  1011. else
  1012. do_blank_screen(1);
  1013. }
  1014. }
  1015. }
  1016. /*
  1017. * Wake anyone waiting for their VT to activate
  1018. */
  1019. vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
  1020. return;
  1021. }
  1022. /*
  1023. * Performs the front-end of a vt switch
  1024. */
  1025. void change_console(struct vc_data *new_vc)
  1026. {
  1027. struct vc_data *vc;
  1028. if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
  1029. return;
  1030. /*
  1031. * If this vt is in process mode, then we need to handshake with
  1032. * that process before switching. Essentially, we store where that
  1033. * vt wants to switch to and wait for it to tell us when it's done
  1034. * (via VT_RELDISP ioctl).
  1035. *
  1036. * We also check to see if the controlling process still exists.
  1037. * If it doesn't, we reset this vt to auto mode and continue.
  1038. * This is a cheap way to track process control. The worst thing
  1039. * that can happen is: we send a signal to a process, it dies, and
  1040. * the switch gets "lost" waiting for a response; hopefully, the
  1041. * user will try again, we'll detect the process is gone (unless
  1042. * the user waits just the right amount of time :-) and revert the
  1043. * vt to auto control.
  1044. */
  1045. vc = vc_cons[fg_console].d;
  1046. if (vc->vt_mode.mode == VT_PROCESS) {
  1047. /*
  1048. * Send the signal as privileged - kill_pid() will
  1049. * tell us if the process has gone or something else
  1050. * is awry.
  1051. *
  1052. * We need to set vt_newvt *before* sending the signal or we
  1053. * have a race.
  1054. */
  1055. vc->vt_newvt = new_vc->vc_num;
  1056. if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
  1057. /*
  1058. * It worked. Mark the vt to switch to and
  1059. * return. The process needs to send us a
  1060. * VT_RELDISP ioctl to complete the switch.
  1061. */
  1062. return;
  1063. }
  1064. /*
  1065. * The controlling process has died, so we revert back to
  1066. * normal operation. In this case, we'll also change back
  1067. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1068. * but it saves the agony when the X server dies and the screen
  1069. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1070. * this outside of VT_PROCESS but there is no single process
  1071. * to account for and tracking tty count may be undesirable.
  1072. */
  1073. reset_vc(vc);
  1074. /*
  1075. * Fall through to normal (VT_AUTO) handling of the switch...
  1076. */
  1077. }
  1078. /*
  1079. * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
  1080. */
  1081. if (vc->vc_mode == KD_GRAPHICS)
  1082. return;
  1083. complete_change_console(new_vc);
  1084. }
  1085. /* Perform a kernel triggered VT switch for suspend/resume */
  1086. static int disable_vt_switch;
  1087. int vt_move_to_console(unsigned int vt, int alloc)
  1088. {
  1089. int prev;
  1090. console_lock();
  1091. /* Graphics mode - up to X */
  1092. if (disable_vt_switch) {
  1093. console_unlock();
  1094. return 0;
  1095. }
  1096. prev = fg_console;
  1097. if (alloc && vc_allocate(vt)) {
  1098. /* we can't have a free VC for now. Too bad,
  1099. * we don't want to mess the screen for now. */
  1100. console_unlock();
  1101. return -ENOSPC;
  1102. }
  1103. if (set_console(vt)) {
  1104. /*
  1105. * We're unable to switch to the SUSPEND_CONSOLE.
  1106. * Let the calling function know so it can decide
  1107. * what to do.
  1108. */
  1109. console_unlock();
  1110. return -EIO;
  1111. }
  1112. console_unlock();
  1113. if (vt_waitactive(vt + 1)) {
  1114. pr_debug("Suspend: Can't switch VCs.");
  1115. return -EINTR;
  1116. }
  1117. return prev;
  1118. }
  1119. /*
  1120. * Normally during a suspend, we allocate a new console and switch to it.
  1121. * When we resume, we switch back to the original console. This switch
  1122. * can be slow, so on systems where the framebuffer can handle restoration
  1123. * of video registers anyways, there's little point in doing the console
  1124. * switch. This function allows you to disable it by passing it '0'.
  1125. */
  1126. void pm_set_vt_switch(int do_switch)
  1127. {
  1128. console_lock();
  1129. disable_vt_switch = !do_switch;
  1130. console_unlock();
  1131. }
  1132. EXPORT_SYMBOL(pm_set_vt_switch);