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