ioport.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This contains the io-permission bitmap code - written by obz, with changes
  4. * by Linus. 32/64 bits code unification by Miguel Botón.
  5. */
  6. #include <linux/capability.h>
  7. #include <linux/security.h>
  8. #include <linux/syscalls.h>
  9. #include <linux/bitmap.h>
  10. #include <linux/ioport.h>
  11. #include <linux/sched.h>
  12. #include <linux/slab.h>
  13. #include <asm/io_bitmap.h>
  14. #include <asm/desc.h>
  15. #include <asm/syscalls.h>
  16. #ifdef CONFIG_X86_IOPL_IOPERM
  17. static atomic64_t io_bitmap_sequence;
  18. void io_bitmap_share(struct task_struct *tsk)
  19. {
  20. /* Can be NULL when current->thread.iopl_emul == 3 */
  21. if (current->thread.io_bitmap) {
  22. /*
  23. * Take a refcount on current's bitmap. It can be used by
  24. * both tasks as long as none of them changes the bitmap.
  25. */
  26. refcount_inc(&current->thread.io_bitmap->refcnt);
  27. tsk->thread.io_bitmap = current->thread.io_bitmap;
  28. }
  29. set_tsk_thread_flag(tsk, TIF_IO_BITMAP);
  30. }
  31. static void task_update_io_bitmap(struct task_struct *tsk)
  32. {
  33. struct thread_struct *t = &tsk->thread;
  34. if (t->iopl_emul == 3 || t->io_bitmap) {
  35. /* TSS update is handled on exit to user space */
  36. set_tsk_thread_flag(tsk, TIF_IO_BITMAP);
  37. } else {
  38. clear_tsk_thread_flag(tsk, TIF_IO_BITMAP);
  39. /* Invalidate TSS */
  40. preempt_disable();
  41. tss_update_io_bitmap();
  42. preempt_enable();
  43. }
  44. }
  45. void io_bitmap_exit(struct task_struct *tsk)
  46. {
  47. struct io_bitmap *iobm = tsk->thread.io_bitmap;
  48. tsk->thread.io_bitmap = NULL;
  49. task_update_io_bitmap(tsk);
  50. if (iobm && refcount_dec_and_test(&iobm->refcnt))
  51. kfree(iobm);
  52. }
  53. /*
  54. * This changes the io permissions bitmap in the current task.
  55. */
  56. long ksys_ioperm(unsigned long from, unsigned long num, int turn_on)
  57. {
  58. struct thread_struct *t = &current->thread;
  59. unsigned int i, max_long;
  60. struct io_bitmap *iobm;
  61. if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
  62. return -EINVAL;
  63. if (turn_on && (!capable(CAP_SYS_RAWIO) ||
  64. security_locked_down(LOCKDOWN_IOPORT)))
  65. return -EPERM;
  66. /*
  67. * If it's the first ioperm() call in this thread's lifetime, set the
  68. * IO bitmap up. ioperm() is much less timing critical than clone(),
  69. * this is why we delay this operation until now:
  70. */
  71. iobm = t->io_bitmap;
  72. if (!iobm) {
  73. /* No point to allocate a bitmap just to clear permissions */
  74. if (!turn_on)
  75. return 0;
  76. iobm = kmalloc(sizeof(*iobm), GFP_KERNEL);
  77. if (!iobm)
  78. return -ENOMEM;
  79. memset(iobm->bitmap, 0xff, sizeof(iobm->bitmap));
  80. refcount_set(&iobm->refcnt, 1);
  81. }
  82. /*
  83. * If the bitmap is not shared, then nothing can take a refcount as
  84. * current can obviously not fork at the same time. If it's shared
  85. * duplicate it and drop the refcount on the original one.
  86. */
  87. if (refcount_read(&iobm->refcnt) > 1) {
  88. iobm = kmemdup(iobm, sizeof(*iobm), GFP_KERNEL);
  89. if (!iobm)
  90. return -ENOMEM;
  91. refcount_set(&iobm->refcnt, 1);
  92. io_bitmap_exit(current);
  93. }
  94. /*
  95. * Store the bitmap pointer (might be the same if the task already
  96. * head one). Must be done here so freeing the bitmap when all
  97. * permissions are dropped has the pointer set up.
  98. */
  99. t->io_bitmap = iobm;
  100. /* Mark it active for context switching and exit to user mode */
  101. set_thread_flag(TIF_IO_BITMAP);
  102. /*
  103. * Update the tasks bitmap. The update of the TSS bitmap happens on
  104. * exit to user mode. So this needs no protection.
  105. */
  106. if (turn_on)
  107. bitmap_clear(iobm->bitmap, from, num);
  108. else
  109. bitmap_set(iobm->bitmap, from, num);
  110. /*
  111. * Search for a (possibly new) maximum. This is simple and stupid,
  112. * to keep it obviously correct:
  113. */
  114. max_long = UINT_MAX;
  115. for (i = 0; i < IO_BITMAP_LONGS; i++) {
  116. if (iobm->bitmap[i] != ~0UL)
  117. max_long = i;
  118. }
  119. /* All permissions dropped? */
  120. if (max_long == UINT_MAX) {
  121. io_bitmap_exit(current);
  122. return 0;
  123. }
  124. iobm->max = (max_long + 1) * sizeof(unsigned long);
  125. /*
  126. * Update the sequence number to force a TSS update on return to
  127. * user mode.
  128. */
  129. iobm->sequence = atomic64_add_return(1, &io_bitmap_sequence);
  130. return 0;
  131. }
  132. SYSCALL_DEFINE3(ioperm, unsigned long, from, unsigned long, num, int, turn_on)
  133. {
  134. return ksys_ioperm(from, num, turn_on);
  135. }
  136. /*
  137. * The sys_iopl functionality depends on the level argument, which if
  138. * granted for the task is used to enable access to all 65536 I/O ports.
  139. *
  140. * This does not use the IOPL mechanism provided by the CPU as that would
  141. * also allow the user space task to use the CLI/STI instructions.
  142. *
  143. * Disabling interrupts in a user space task is dangerous as it might lock
  144. * up the machine and the semantics vs. syscalls and exceptions is
  145. * undefined.
  146. *
  147. * Setting IOPL to level 0-2 is disabling I/O permissions. Level 3
  148. * 3 enables them.
  149. *
  150. * IOPL is strictly per thread and inherited on fork.
  151. */
  152. SYSCALL_DEFINE1(iopl, unsigned int, level)
  153. {
  154. struct thread_struct *t = &current->thread;
  155. unsigned int old;
  156. if (level > 3)
  157. return -EINVAL;
  158. old = t->iopl_emul;
  159. /* No point in going further if nothing changes */
  160. if (level == old)
  161. return 0;
  162. /* Trying to gain more privileges? */
  163. if (level > old) {
  164. if (!capable(CAP_SYS_RAWIO) ||
  165. security_locked_down(LOCKDOWN_IOPORT))
  166. return -EPERM;
  167. }
  168. t->iopl_emul = level;
  169. task_update_io_bitmap(current);
  170. return 0;
  171. }
  172. #else /* CONFIG_X86_IOPL_IOPERM */
  173. long ksys_ioperm(unsigned long from, unsigned long num, int turn_on)
  174. {
  175. return -ENOSYS;
  176. }
  177. SYSCALL_DEFINE3(ioperm, unsigned long, from, unsigned long, num, int, turn_on)
  178. {
  179. return -ENOSYS;
  180. }
  181. SYSCALL_DEFINE1(iopl, unsigned int, level)
  182. {
  183. return -ENOSYS;
  184. }
  185. #endif