ioport.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/sched.h>
  7. #include <linux/sched/task_stack.h>
  8. #include <linux/kernel.h>
  9. #include <linux/capability.h>
  10. #include <linux/errno.h>
  11. #include <linux/types.h>
  12. #include <linux/ioport.h>
  13. #include <linux/smp.h>
  14. #include <linux/stddef.h>
  15. #include <linux/slab.h>
  16. #include <linux/thread_info.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/bitmap.h>
  19. #include <asm/syscalls.h>
  20. #include <asm/desc.h>
  21. /*
  22. * this changes the io permissions bitmap in the current task.
  23. */
  24. long ksys_ioperm(unsigned long from, unsigned long num, int turn_on)
  25. {
  26. struct thread_struct *t = &current->thread;
  27. struct tss_struct *tss;
  28. unsigned int i, max_long, bytes, bytes_updated;
  29. if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
  30. return -EINVAL;
  31. if (turn_on && !capable(CAP_SYS_RAWIO))
  32. return -EPERM;
  33. /*
  34. * If it's the first ioperm() call in this thread's lifetime, set the
  35. * IO bitmap up. ioperm() is much less timing critical than clone(),
  36. * this is why we delay this operation until now:
  37. */
  38. if (!t->io_bitmap_ptr) {
  39. unsigned long *bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
  40. if (!bitmap)
  41. return -ENOMEM;
  42. memset(bitmap, 0xff, IO_BITMAP_BYTES);
  43. t->io_bitmap_ptr = bitmap;
  44. set_thread_flag(TIF_IO_BITMAP);
  45. /*
  46. * Now that we have an IO bitmap, we need our TSS limit to be
  47. * correct. It's fine if we are preempted after doing this:
  48. * with TIF_IO_BITMAP set, context switches will keep our TSS
  49. * limit correct.
  50. */
  51. preempt_disable();
  52. refresh_tss_limit();
  53. preempt_enable();
  54. }
  55. /*
  56. * do it in the per-thread copy and in the TSS ...
  57. *
  58. * Disable preemption via get_cpu() - we must not switch away
  59. * because the ->io_bitmap_max value must match the bitmap
  60. * contents:
  61. */
  62. tss = &per_cpu(cpu_tss_rw, get_cpu());
  63. if (turn_on)
  64. bitmap_clear(t->io_bitmap_ptr, from, num);
  65. else
  66. bitmap_set(t->io_bitmap_ptr, from, num);
  67. /*
  68. * Search for a (possibly new) maximum. This is simple and stupid,
  69. * to keep it obviously correct:
  70. */
  71. max_long = 0;
  72. for (i = 0; i < IO_BITMAP_LONGS; i++)
  73. if (t->io_bitmap_ptr[i] != ~0UL)
  74. max_long = i;
  75. bytes = (max_long + 1) * sizeof(unsigned long);
  76. bytes_updated = max(bytes, t->io_bitmap_max);
  77. t->io_bitmap_max = bytes;
  78. /* Update the TSS: */
  79. memcpy(tss->io_bitmap, t->io_bitmap_ptr, bytes_updated);
  80. put_cpu();
  81. return 0;
  82. }
  83. SYSCALL_DEFINE3(ioperm, unsigned long, from, unsigned long, num, int, turn_on)
  84. {
  85. return ksys_ioperm(from, num, turn_on);
  86. }
  87. /*
  88. * sys_iopl has to be used when you want to access the IO ports
  89. * beyond the 0x3ff range: to get the full 65536 ports bitmapped
  90. * you'd need 8kB of bitmaps/process, which is a bit excessive.
  91. *
  92. * Here we just change the flags value on the stack: we allow
  93. * only the super-user to do it. This depends on the stack-layout
  94. * on system-call entry - see also fork() and the signal handling
  95. * code.
  96. */
  97. SYSCALL_DEFINE1(iopl, unsigned int, level)
  98. {
  99. struct pt_regs *regs = current_pt_regs();
  100. struct thread_struct *t = &current->thread;
  101. /*
  102. * Careful: the IOPL bits in regs->flags are undefined under Xen PV
  103. * and changing them has no effect.
  104. */
  105. unsigned int old = t->iopl >> X86_EFLAGS_IOPL_BIT;
  106. if (level > 3)
  107. return -EINVAL;
  108. /* Trying to gain more privileges? */
  109. if (level > old) {
  110. if (!capable(CAP_SYS_RAWIO))
  111. return -EPERM;
  112. }
  113. regs->flags = (regs->flags & ~X86_EFLAGS_IOPL) |
  114. (level << X86_EFLAGS_IOPL_BIT);
  115. t->iopl = level << X86_EFLAGS_IOPL_BIT;
  116. set_iopl_mask(t->iopl);
  117. return 0;
  118. }