1
2
3
4
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
14#include <asm/io_bitmap.h>
15#include <asm/desc.h>
16#include <asm/syscalls.h>
17
18#ifdef CONFIG_X86_IOPL_IOPERM
19
20static atomic64_t io_bitmap_sequence;
21
22void io_bitmap_share(struct task_struct *tsk)
23{
24
25 if (current->thread.io_bitmap) {
26
27
28
29
30 refcount_inc(¤t->thread.io_bitmap->refcnt);
31 tsk->thread.io_bitmap = current->thread.io_bitmap;
32 }
33 set_tsk_thread_flag(tsk, TIF_IO_BITMAP);
34}
35
36static void task_update_io_bitmap(struct task_struct *tsk)
37{
38 struct thread_struct *t = &tsk->thread;
39
40 if (t->iopl_emul == 3 || t->io_bitmap) {
41
42 set_tsk_thread_flag(tsk, TIF_IO_BITMAP);
43 } else {
44 clear_tsk_thread_flag(tsk, TIF_IO_BITMAP);
45
46 preempt_disable();
47 tss_update_io_bitmap();
48 preempt_enable();
49 }
50}
51
52void io_bitmap_exit(struct task_struct *tsk)
53{
54 struct io_bitmap *iobm = tsk->thread.io_bitmap;
55
56 tsk->thread.io_bitmap = NULL;
57 task_update_io_bitmap(tsk);
58 if (iobm && refcount_dec_and_test(&iobm->refcnt))
59 kfree(iobm);
60}
61
62
63
64
65long ksys_ioperm(unsigned long from, unsigned long num, int turn_on)
66{
67 struct thread_struct *t = ¤t->thread;
68 unsigned int i, max_long;
69 struct io_bitmap *iobm;
70
71 if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
72 return -EINVAL;
73 if (turn_on && (!capable(CAP_SYS_RAWIO) ||
74 security_locked_down(LOCKDOWN_IOPORT)))
75 return -EPERM;
76
77
78
79
80
81
82 iobm = t->io_bitmap;
83 if (!iobm) {
84
85 if (!turn_on)
86 return 0;
87 iobm = kmalloc(sizeof(*iobm), GFP_KERNEL);
88 if (!iobm)
89 return -ENOMEM;
90
91 memset(iobm->bitmap, 0xff, sizeof(iobm->bitmap));
92 refcount_set(&iobm->refcnt, 1);
93 }
94
95
96
97
98
99
100 if (refcount_read(&iobm->refcnt) > 1) {
101 iobm = kmemdup(iobm, sizeof(*iobm), GFP_KERNEL);
102 if (!iobm)
103 return -ENOMEM;
104 refcount_set(&iobm->refcnt, 1);
105 io_bitmap_exit(current);
106 }
107
108
109
110
111
112
113 t->io_bitmap = iobm;
114
115 set_thread_flag(TIF_IO_BITMAP);
116
117
118
119
120
121 if (turn_on)
122 bitmap_clear(iobm->bitmap, from, num);
123 else
124 bitmap_set(iobm->bitmap, from, num);
125
126
127
128
129
130 max_long = UINT_MAX;
131 for (i = 0; i < IO_BITMAP_LONGS; i++) {
132 if (iobm->bitmap[i] != ~0UL)
133 max_long = i;
134 }
135
136 if (max_long == UINT_MAX) {
137 io_bitmap_exit(current);
138 return 0;
139 }
140
141 iobm->max = (max_long + 1) * sizeof(unsigned long);
142
143
144
145
146
147 iobm->sequence = atomic64_add_return(1, &io_bitmap_sequence);
148
149 return 0;
150}
151
152SYSCALL_DEFINE3(ioperm, unsigned long, from, unsigned long, num, int, turn_on)
153{
154 return ksys_ioperm(from, num, turn_on);
155}
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173SYSCALL_DEFINE1(iopl, unsigned int, level)
174{
175 struct thread_struct *t = ¤t->thread;
176 unsigned int old;
177
178 if (level > 3)
179 return -EINVAL;
180
181 old = t->iopl_emul;
182
183
184 if (level == old)
185 return 0;
186
187
188 if (level > old) {
189 if (!capable(CAP_SYS_RAWIO) ||
190 security_locked_down(LOCKDOWN_IOPORT))
191 return -EPERM;
192 }
193
194 t->iopl_emul = level;
195 task_update_io_bitmap(current);
196
197 return 0;
198}
199
200#else
201
202long ksys_ioperm(unsigned long from, unsigned long num, int turn_on)
203{
204 return -ENOSYS;
205}
206SYSCALL_DEFINE3(ioperm, unsigned long, from, unsigned long, num, int, turn_on)
207{
208 return -ENOSYS;
209}
210
211SYSCALL_DEFINE1(iopl, unsigned int, level)
212{
213 return -ENOSYS;
214}
215#endif
216