ioport.c 5.5 KB

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