]>
Commit | Line | Data |
---|---|---|
57319d80 QR |
1 | /* |
2 | * mpx.c - Memory Protection eXtensions | |
3 | * | |
4 | * Copyright (c) 2014, Intel Corporation. | |
5 | * Qiaowei Ren <qiaowei.ren@intel.com> | |
6 | * Dave Hansen <dave.hansen@intel.com> | |
7 | */ | |
8 | #include <linux/kernel.h> | |
fcc7ffd6 | 9 | #include <linux/slab.h> |
57319d80 QR |
10 | #include <linux/syscalls.h> |
11 | #include <linux/sched/sysctl.h> | |
12 | ||
fe3d197f | 13 | #include <asm/insn.h> |
57319d80 | 14 | #include <asm/mman.h> |
1de4fa14 | 15 | #include <asm/mmu_context.h> |
57319d80 | 16 | #include <asm/mpx.h> |
fe3d197f | 17 | #include <asm/processor.h> |
78f7f1e5 | 18 | #include <asm/fpu/internal.h> |
57319d80 | 19 | |
e7126cf5 DH |
20 | #define CREATE_TRACE_POINTS |
21 | #include <asm/trace/mpx.h> | |
22 | ||
613fcb7d DH |
23 | static inline unsigned long mpx_bd_size_bytes(struct mm_struct *mm) |
24 | { | |
25 | if (is_64bit_mm(mm)) | |
26 | return MPX_BD_SIZE_BYTES_64; | |
27 | else | |
28 | return MPX_BD_SIZE_BYTES_32; | |
29 | } | |
30 | ||
31 | static inline unsigned long mpx_bt_size_bytes(struct mm_struct *mm) | |
32 | { | |
33 | if (is_64bit_mm(mm)) | |
34 | return MPX_BT_SIZE_BYTES_64; | |
35 | else | |
36 | return MPX_BT_SIZE_BYTES_32; | |
37 | } | |
38 | ||
57319d80 QR |
39 | /* |
40 | * This is really a simplified "vm_mmap". it only handles MPX | |
41 | * bounds tables (the bounds directory is user-allocated). | |
57319d80 QR |
42 | */ |
43 | static unsigned long mpx_mmap(unsigned long len) | |
44 | { | |
57319d80 | 45 | struct mm_struct *mm = current->mm; |
1fcfd8db | 46 | unsigned long addr, populate; |
57319d80 | 47 | |
eb099e5b | 48 | /* Only bounds table can be allocated here */ |
613fcb7d | 49 | if (len != mpx_bt_size_bytes(mm)) |
57319d80 QR |
50 | return -EINVAL; |
51 | ||
52 | down_write(&mm->mmap_sem); | |
1fcfd8db ON |
53 | addr = do_mmap(NULL, 0, len, PROT_READ | PROT_WRITE, |
54 | MAP_ANONYMOUS | MAP_PRIVATE, VM_MPX, 0, &populate); | |
57319d80 | 55 | up_write(&mm->mmap_sem); |
1fcfd8db ON |
56 | if (populate) |
57 | mm_populate(addr, populate); | |
58 | ||
59 | return addr; | |
57319d80 | 60 | } |
fcc7ffd6 DH |
61 | |
62 | enum reg_type { | |
63 | REG_TYPE_RM = 0, | |
64 | REG_TYPE_INDEX, | |
65 | REG_TYPE_BASE, | |
66 | }; | |
67 | ||
68c009c4 DH |
68 | static int get_reg_offset(struct insn *insn, struct pt_regs *regs, |
69 | enum reg_type type) | |
fcc7ffd6 DH |
70 | { |
71 | int regno = 0; | |
72 | ||
73 | static const int regoff[] = { | |
74 | offsetof(struct pt_regs, ax), | |
75 | offsetof(struct pt_regs, cx), | |
76 | offsetof(struct pt_regs, dx), | |
77 | offsetof(struct pt_regs, bx), | |
78 | offsetof(struct pt_regs, sp), | |
79 | offsetof(struct pt_regs, bp), | |
80 | offsetof(struct pt_regs, si), | |
81 | offsetof(struct pt_regs, di), | |
82 | #ifdef CONFIG_X86_64 | |
83 | offsetof(struct pt_regs, r8), | |
84 | offsetof(struct pt_regs, r9), | |
85 | offsetof(struct pt_regs, r10), | |
86 | offsetof(struct pt_regs, r11), | |
87 | offsetof(struct pt_regs, r12), | |
88 | offsetof(struct pt_regs, r13), | |
89 | offsetof(struct pt_regs, r14), | |
90 | offsetof(struct pt_regs, r15), | |
91 | #endif | |
92 | }; | |
93 | int nr_registers = ARRAY_SIZE(regoff); | |
94 | /* | |
95 | * Don't possibly decode a 32-bit instructions as | |
96 | * reading a 64-bit-only register. | |
97 | */ | |
98 | if (IS_ENABLED(CONFIG_X86_64) && !insn->x86_64) | |
99 | nr_registers -= 8; | |
100 | ||
101 | switch (type) { | |
102 | case REG_TYPE_RM: | |
103 | regno = X86_MODRM_RM(insn->modrm.value); | |
104 | if (X86_REX_B(insn->rex_prefix.value) == 1) | |
105 | regno += 8; | |
106 | break; | |
107 | ||
108 | case REG_TYPE_INDEX: | |
109 | regno = X86_SIB_INDEX(insn->sib.value); | |
110 | if (X86_REX_X(insn->rex_prefix.value) == 1) | |
111 | regno += 8; | |
112 | break; | |
113 | ||
114 | case REG_TYPE_BASE: | |
115 | regno = X86_SIB_BASE(insn->sib.value); | |
116 | if (X86_REX_B(insn->rex_prefix.value) == 1) | |
117 | regno += 8; | |
118 | break; | |
119 | ||
120 | default: | |
121 | pr_err("invalid register type"); | |
122 | BUG(); | |
123 | break; | |
124 | } | |
125 | ||
126 | if (regno > nr_registers) { | |
127 | WARN_ONCE(1, "decoded an instruction with an invalid register"); | |
128 | return -EINVAL; | |
129 | } | |
130 | return regoff[regno]; | |
131 | } | |
132 | ||
133 | /* | |
134 | * return the address being referenced be instruction | |
135 | * for rm=3 returning the content of the rm reg | |
136 | * for rm!=3 calculates the address using SIB and Disp | |
137 | */ | |
138 | static void __user *mpx_get_addr_ref(struct insn *insn, struct pt_regs *regs) | |
139 | { | |
68c009c4 DH |
140 | unsigned long addr, base, indx; |
141 | int addr_offset, base_offset, indx_offset; | |
fcc7ffd6 DH |
142 | insn_byte_t sib; |
143 | ||
144 | insn_get_modrm(insn); | |
145 | insn_get_sib(insn); | |
146 | sib = insn->sib.value; | |
147 | ||
148 | if (X86_MODRM_MOD(insn->modrm.value) == 3) { | |
149 | addr_offset = get_reg_offset(insn, regs, REG_TYPE_RM); | |
150 | if (addr_offset < 0) | |
151 | goto out_err; | |
152 | addr = regs_get_register(regs, addr_offset); | |
153 | } else { | |
154 | if (insn->sib.nbytes) { | |
155 | base_offset = get_reg_offset(insn, regs, REG_TYPE_BASE); | |
156 | if (base_offset < 0) | |
157 | goto out_err; | |
158 | ||
159 | indx_offset = get_reg_offset(insn, regs, REG_TYPE_INDEX); | |
160 | if (indx_offset < 0) | |
161 | goto out_err; | |
162 | ||
163 | base = regs_get_register(regs, base_offset); | |
164 | indx = regs_get_register(regs, indx_offset); | |
165 | addr = base + indx * (1 << X86_SIB_SCALE(sib)); | |
166 | } else { | |
167 | addr_offset = get_reg_offset(insn, regs, REG_TYPE_RM); | |
168 | if (addr_offset < 0) | |
169 | goto out_err; | |
170 | addr = regs_get_register(regs, addr_offset); | |
171 | } | |
172 | addr += insn->displacement.value; | |
173 | } | |
174 | return (void __user *)addr; | |
175 | out_err: | |
176 | return (void __user *)-1; | |
177 | } | |
178 | ||
179 | static int mpx_insn_decode(struct insn *insn, | |
180 | struct pt_regs *regs) | |
181 | { | |
182 | unsigned char buf[MAX_INSN_SIZE]; | |
183 | int x86_64 = !test_thread_flag(TIF_IA32); | |
184 | int not_copied; | |
185 | int nr_copied; | |
186 | ||
187 | not_copied = copy_from_user(buf, (void __user *)regs->ip, sizeof(buf)); | |
188 | nr_copied = sizeof(buf) - not_copied; | |
189 | /* | |
190 | * The decoder _should_ fail nicely if we pass it a short buffer. | |
191 | * But, let's not depend on that implementation detail. If we | |
192 | * did not get anything, just error out now. | |
193 | */ | |
194 | if (!nr_copied) | |
195 | return -EFAULT; | |
196 | insn_init(insn, buf, nr_copied, x86_64); | |
197 | insn_get_length(insn); | |
198 | /* | |
199 | * copy_from_user() tries to get as many bytes as we could see in | |
200 | * the largest possible instruction. If the instruction we are | |
201 | * after is shorter than that _and_ we attempt to copy from | |
202 | * something unreadable, we might get a short read. This is OK | |
203 | * as long as the read did not stop in the middle of the | |
204 | * instruction. Check to see if we got a partial instruction. | |
205 | */ | |
206 | if (nr_copied < insn->length) | |
207 | return -EFAULT; | |
208 | ||
209 | insn_get_opcode(insn); | |
210 | /* | |
211 | * We only _really_ need to decode bndcl/bndcn/bndcu | |
212 | * Error out on anything else. | |
213 | */ | |
214 | if (insn->opcode.bytes[0] != 0x0f) | |
215 | goto bad_opcode; | |
216 | if ((insn->opcode.bytes[1] != 0x1a) && | |
217 | (insn->opcode.bytes[1] != 0x1b)) | |
218 | goto bad_opcode; | |
219 | ||
220 | return 0; | |
221 | bad_opcode: | |
222 | return -EINVAL; | |
223 | } | |
224 | ||
225 | /* | |
226 | * If a bounds overflow occurs then a #BR is generated. This | |
227 | * function decodes MPX instructions to get violation address | |
228 | * and set this address into extended struct siginfo. | |
229 | * | |
230 | * Note that this is not a super precise way of doing this. | |
231 | * Userspace could have, by the time we get here, written | |
232 | * anything it wants in to the instructions. We can not | |
233 | * trust anything about it. They might not be valid | |
234 | * instructions or might encode invalid registers, etc... | |
235 | * | |
236 | * The caller is expected to kfree() the returned siginfo_t. | |
237 | */ | |
46a6e0cf | 238 | siginfo_t *mpx_generate_siginfo(struct pt_regs *regs) |
fcc7ffd6 | 239 | { |
a84eeaa9 | 240 | const struct bndreg *bndregs, *bndreg; |
fe3d197f | 241 | siginfo_t *info = NULL; |
fcc7ffd6 DH |
242 | struct insn insn; |
243 | uint8_t bndregno; | |
244 | int err; | |
fcc7ffd6 DH |
245 | |
246 | err = mpx_insn_decode(&insn, regs); | |
247 | if (err) | |
248 | goto err_out; | |
249 | ||
250 | /* | |
251 | * We know at this point that we are only dealing with | |
252 | * MPX instructions. | |
253 | */ | |
254 | insn_get_modrm(&insn); | |
255 | bndregno = X86_MODRM_REG(insn.modrm.value); | |
256 | if (bndregno > 3) { | |
257 | err = -EINVAL; | |
258 | goto err_out; | |
259 | } | |
a84eeaa9 DH |
260 | /* get bndregs field from current task's xsave area */ |
261 | bndregs = get_xsave_field_ptr(XSTATE_BNDREGS); | |
fe3d197f DH |
262 | if (!bndregs) { |
263 | err = -EINVAL; | |
264 | goto err_out; | |
265 | } | |
266 | /* now go select the individual register in the set of 4 */ | |
267 | bndreg = &bndregs[bndregno]; | |
268 | ||
fcc7ffd6 DH |
269 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
270 | if (!info) { | |
271 | err = -ENOMEM; | |
272 | goto err_out; | |
273 | } | |
274 | /* | |
275 | * The registers are always 64-bit, but the upper 32 | |
276 | * bits are ignored in 32-bit mode. Also, note that the | |
277 | * upper bounds are architecturally represented in 1's | |
278 | * complement form. | |
279 | * | |
280 | * The 'unsigned long' cast is because the compiler | |
281 | * complains when casting from integers to different-size | |
282 | * pointers. | |
283 | */ | |
fe3d197f DH |
284 | info->si_lower = (void __user *)(unsigned long)bndreg->lower_bound; |
285 | info->si_upper = (void __user *)(unsigned long)~bndreg->upper_bound; | |
fcc7ffd6 DH |
286 | info->si_addr_lsb = 0; |
287 | info->si_signo = SIGSEGV; | |
288 | info->si_errno = 0; | |
289 | info->si_code = SEGV_BNDERR; | |
290 | info->si_addr = mpx_get_addr_ref(&insn, regs); | |
291 | /* | |
292 | * We were not able to extract an address from the instruction, | |
293 | * probably because there was something invalid in it. | |
294 | */ | |
295 | if (info->si_addr == (void *)-1) { | |
296 | err = -EINVAL; | |
297 | goto err_out; | |
298 | } | |
97efebf1 | 299 | trace_mpx_bounds_register_exception(info->si_addr, bndreg); |
fcc7ffd6 DH |
300 | return info; |
301 | err_out: | |
fe3d197f DH |
302 | /* info might be NULL, but kfree() handles that */ |
303 | kfree(info); | |
fcc7ffd6 DH |
304 | return ERR_PTR(err); |
305 | } | |
fe3d197f | 306 | |
46a6e0cf | 307 | static __user void *mpx_get_bounds_dir(void) |
fe3d197f | 308 | { |
a84eeaa9 | 309 | const struct bndcsr *bndcsr; |
fe3d197f DH |
310 | |
311 | if (!cpu_feature_enabled(X86_FEATURE_MPX)) | |
312 | return MPX_INVALID_BOUNDS_DIR; | |
313 | ||
314 | /* | |
315 | * The bounds directory pointer is stored in a register | |
316 | * only accessible if we first do an xsave. | |
317 | */ | |
a84eeaa9 | 318 | bndcsr = get_xsave_field_ptr(XSTATE_BNDCSR); |
fe3d197f DH |
319 | if (!bndcsr) |
320 | return MPX_INVALID_BOUNDS_DIR; | |
321 | ||
322 | /* | |
323 | * Make sure the register looks valid by checking the | |
324 | * enable bit. | |
325 | */ | |
326 | if (!(bndcsr->bndcfgu & MPX_BNDCFG_ENABLE_FLAG)) | |
327 | return MPX_INVALID_BOUNDS_DIR; | |
328 | ||
329 | /* | |
330 | * Lastly, mask off the low bits used for configuration | |
331 | * flags, and return the address of the bounds table. | |
332 | */ | |
333 | return (void __user *)(unsigned long) | |
334 | (bndcsr->bndcfgu & MPX_BNDCFG_ADDR_MASK); | |
335 | } | |
336 | ||
46a6e0cf | 337 | int mpx_enable_management(void) |
fe3d197f DH |
338 | { |
339 | void __user *bd_base = MPX_INVALID_BOUNDS_DIR; | |
46a6e0cf | 340 | struct mm_struct *mm = current->mm; |
fe3d197f DH |
341 | int ret = 0; |
342 | ||
343 | /* | |
344 | * runtime in the userspace will be responsible for allocation of | |
345 | * the bounds directory. Then, it will save the base of the bounds | |
346 | * directory into XSAVE/XRSTOR Save Area and enable MPX through | |
347 | * XRSTOR instruction. | |
348 | * | |
a84eeaa9 DH |
349 | * The copy_xregs_to_kernel() beneath get_xsave_field_ptr() is |
350 | * expected to be relatively expensive. Storing the bounds | |
351 | * directory here means that we do not have to do xsave in the | |
352 | * unmap path; we can just use mm->bd_addr instead. | |
fe3d197f | 353 | */ |
46a6e0cf | 354 | bd_base = mpx_get_bounds_dir(); |
fe3d197f DH |
355 | down_write(&mm->mmap_sem); |
356 | mm->bd_addr = bd_base; | |
357 | if (mm->bd_addr == MPX_INVALID_BOUNDS_DIR) | |
358 | ret = -ENXIO; | |
359 | ||
360 | up_write(&mm->mmap_sem); | |
361 | return ret; | |
362 | } | |
363 | ||
46a6e0cf | 364 | int mpx_disable_management(void) |
fe3d197f DH |
365 | { |
366 | struct mm_struct *mm = current->mm; | |
367 | ||
368 | if (!cpu_feature_enabled(X86_FEATURE_MPX)) | |
369 | return -ENXIO; | |
370 | ||
371 | down_write(&mm->mmap_sem); | |
372 | mm->bd_addr = MPX_INVALID_BOUNDS_DIR; | |
373 | up_write(&mm->mmap_sem); | |
374 | return 0; | |
375 | } | |
376 | ||
6ac52bb4 DH |
377 | static int mpx_cmpxchg_bd_entry(struct mm_struct *mm, |
378 | unsigned long *curval, | |
379 | unsigned long __user *addr, | |
380 | unsigned long old_val, unsigned long new_val) | |
381 | { | |
382 | int ret; | |
383 | /* | |
384 | * user_atomic_cmpxchg_inatomic() actually uses sizeof() | |
385 | * the pointer that we pass to it to figure out how much | |
386 | * data to cmpxchg. We have to be careful here not to | |
387 | * pass a pointer to a 64-bit data type when we only want | |
388 | * a 32-bit copy. | |
389 | */ | |
390 | if (is_64bit_mm(mm)) { | |
391 | ret = user_atomic_cmpxchg_inatomic(curval, | |
392 | addr, old_val, new_val); | |
393 | } else { | |
394 | u32 uninitialized_var(curval_32); | |
395 | u32 old_val_32 = old_val; | |
396 | u32 new_val_32 = new_val; | |
397 | u32 __user *addr_32 = (u32 __user *)addr; | |
398 | ||
399 | ret = user_atomic_cmpxchg_inatomic(&curval_32, | |
400 | addr_32, old_val_32, new_val_32); | |
401 | *curval = curval_32; | |
402 | } | |
403 | return ret; | |
404 | } | |
405 | ||
fe3d197f | 406 | /* |
613fcb7d DH |
407 | * With 32-bit mode, a bounds directory is 4MB, and the size of each |
408 | * bounds table is 16KB. With 64-bit mode, a bounds directory is 2GB, | |
fe3d197f DH |
409 | * and the size of each bounds table is 4MB. |
410 | */ | |
613fcb7d | 411 | static int allocate_bt(struct mm_struct *mm, long __user *bd_entry) |
fe3d197f DH |
412 | { |
413 | unsigned long expected_old_val = 0; | |
414 | unsigned long actual_old_val = 0; | |
415 | unsigned long bt_addr; | |
a1149fc8 | 416 | unsigned long bd_new_entry; |
fe3d197f DH |
417 | int ret = 0; |
418 | ||
419 | /* | |
420 | * Carve the virtual space out of userspace for the new | |
421 | * bounds table: | |
422 | */ | |
613fcb7d | 423 | bt_addr = mpx_mmap(mpx_bt_size_bytes(mm)); |
fe3d197f DH |
424 | if (IS_ERR((void *)bt_addr)) |
425 | return PTR_ERR((void *)bt_addr); | |
426 | /* | |
427 | * Set the valid flag (kinda like _PAGE_PRESENT in a pte) | |
428 | */ | |
a1149fc8 | 429 | bd_new_entry = bt_addr | MPX_BD_ENTRY_VALID_FLAG; |
fe3d197f DH |
430 | |
431 | /* | |
432 | * Go poke the address of the new bounds table in to the | |
433 | * bounds directory entry out in userspace memory. Note: | |
434 | * we may race with another CPU instantiating the same table. | |
435 | * In that case the cmpxchg will see an unexpected | |
436 | * 'actual_old_val'. | |
437 | * | |
438 | * This can fault, but that's OK because we do not hold | |
439 | * mmap_sem at this point, unlike some of the other part | |
440 | * of the MPX code that have to pagefault_disable(). | |
441 | */ | |
6ac52bb4 DH |
442 | ret = mpx_cmpxchg_bd_entry(mm, &actual_old_val, bd_entry, |
443 | expected_old_val, bd_new_entry); | |
fe3d197f DH |
444 | if (ret) |
445 | goto out_unmap; | |
446 | ||
447 | /* | |
448 | * The user_atomic_cmpxchg_inatomic() will only return nonzero | |
449 | * for faults, *not* if the cmpxchg itself fails. Now we must | |
450 | * verify that the cmpxchg itself completed successfully. | |
451 | */ | |
452 | /* | |
453 | * We expected an empty 'expected_old_val', but instead found | |
454 | * an apparently valid entry. Assume we raced with another | |
455 | * thread to instantiate this table and desclare succecss. | |
456 | */ | |
457 | if (actual_old_val & MPX_BD_ENTRY_VALID_FLAG) { | |
458 | ret = 0; | |
459 | goto out_unmap; | |
460 | } | |
461 | /* | |
462 | * We found a non-empty bd_entry but it did not have the | |
463 | * VALID_FLAG set. Return an error which will result in | |
464 | * a SEGV since this probably means that somebody scribbled | |
465 | * some invalid data in to a bounds table. | |
466 | */ | |
467 | if (expected_old_val != actual_old_val) { | |
468 | ret = -EINVAL; | |
469 | goto out_unmap; | |
470 | } | |
cd4996dc | 471 | trace_mpx_new_bounds_table(bt_addr); |
fe3d197f DH |
472 | return 0; |
473 | out_unmap: | |
613fcb7d | 474 | vm_munmap(bt_addr, mpx_bt_size_bytes(mm)); |
fe3d197f DH |
475 | return ret; |
476 | } | |
477 | ||
478 | /* | |
479 | * When a BNDSTX instruction attempts to save bounds to a bounds | |
480 | * table, it will first attempt to look up the table in the | |
481 | * first-level bounds directory. If it does not find a table in | |
482 | * the directory, a #BR is generated and we get here in order to | |
483 | * allocate a new table. | |
484 | * | |
485 | * With 32-bit mode, the size of BD is 4MB, and the size of each | |
486 | * bound table is 16KB. With 64-bit mode, the size of BD is 2GB, | |
487 | * and the size of each bound table is 4MB. | |
488 | */ | |
46a6e0cf | 489 | static int do_mpx_bt_fault(void) |
fe3d197f DH |
490 | { |
491 | unsigned long bd_entry, bd_base; | |
a84eeaa9 | 492 | const struct bndcsr *bndcsr; |
613fcb7d | 493 | struct mm_struct *mm = current->mm; |
fe3d197f | 494 | |
a84eeaa9 | 495 | bndcsr = get_xsave_field_ptr(XSTATE_BNDCSR); |
fe3d197f DH |
496 | if (!bndcsr) |
497 | return -EINVAL; | |
498 | /* | |
499 | * Mask off the preserve and enable bits | |
500 | */ | |
501 | bd_base = bndcsr->bndcfgu & MPX_BNDCFG_ADDR_MASK; | |
502 | /* | |
503 | * The hardware provides the address of the missing or invalid | |
504 | * entry via BNDSTATUS, so we don't have to go look it up. | |
505 | */ | |
506 | bd_entry = bndcsr->bndstatus & MPX_BNDSTA_ADDR_MASK; | |
507 | /* | |
508 | * Make sure the directory entry is within where we think | |
509 | * the directory is. | |
510 | */ | |
511 | if ((bd_entry < bd_base) || | |
613fcb7d | 512 | (bd_entry >= bd_base + mpx_bd_size_bytes(mm))) |
fe3d197f DH |
513 | return -EINVAL; |
514 | ||
613fcb7d | 515 | return allocate_bt(mm, (long __user *)bd_entry); |
fe3d197f DH |
516 | } |
517 | ||
46a6e0cf | 518 | int mpx_handle_bd_fault(void) |
fe3d197f DH |
519 | { |
520 | /* | |
521 | * Userspace never asked us to manage the bounds tables, | |
522 | * so refuse to help. | |
523 | */ | |
524 | if (!kernel_managing_mpx_tables(current->mm)) | |
525 | return -EINVAL; | |
526 | ||
46a6e0cf | 527 | if (do_mpx_bt_fault()) { |
fe3d197f DH |
528 | force_sig(SIGSEGV, current); |
529 | /* | |
530 | * The force_sig() is essentially "handling" this | |
531 | * exception, so we do not pass up the error | |
532 | * from do_mpx_bt_fault(). | |
533 | */ | |
534 | } | |
535 | return 0; | |
536 | } | |
1de4fa14 DH |
537 | |
538 | /* | |
539 | * A thin wrapper around get_user_pages(). Returns 0 if the | |
540 | * fault was resolved or -errno if not. | |
541 | */ | |
542 | static int mpx_resolve_fault(long __user *addr, int write) | |
543 | { | |
544 | long gup_ret; | |
545 | int nr_pages = 1; | |
546 | int force = 0; | |
547 | ||
548 | gup_ret = get_user_pages(current, current->mm, (unsigned long)addr, | |
549 | nr_pages, write, force, NULL, NULL); | |
550 | /* | |
551 | * get_user_pages() returns number of pages gotten. | |
552 | * 0 means we failed to fault in and get anything, | |
553 | * probably because 'addr' is bad. | |
554 | */ | |
555 | if (!gup_ret) | |
556 | return -EFAULT; | |
557 | /* Other error, return it */ | |
558 | if (gup_ret < 0) | |
559 | return gup_ret; | |
560 | /* must have gup'd a page and gup_ret>0, success */ | |
561 | return 0; | |
562 | } | |
563 | ||
54587653 DH |
564 | static unsigned long mpx_bd_entry_to_bt_addr(struct mm_struct *mm, |
565 | unsigned long bd_entry) | |
566 | { | |
567 | unsigned long bt_addr = bd_entry; | |
568 | int align_to_bytes; | |
569 | /* | |
570 | * Bit 0 in a bt_entry is always the valid bit. | |
571 | */ | |
572 | bt_addr &= ~MPX_BD_ENTRY_VALID_FLAG; | |
573 | /* | |
574 | * Tables are naturally aligned at 8-byte boundaries | |
575 | * on 64-bit and 4-byte boundaries on 32-bit. The | |
576 | * documentation makes it appear that the low bits | |
577 | * are ignored by the hardware, so we do the same. | |
578 | */ | |
579 | if (is_64bit_mm(mm)) | |
580 | align_to_bytes = 8; | |
581 | else | |
582 | align_to_bytes = 4; | |
583 | bt_addr &= ~(align_to_bytes-1); | |
584 | return bt_addr; | |
585 | } | |
586 | ||
1de4fa14 DH |
587 | /* |
588 | * Get the base of bounds tables pointed by specific bounds | |
589 | * directory entry. | |
590 | */ | |
591 | static int get_bt_addr(struct mm_struct *mm, | |
54587653 DH |
592 | long __user *bd_entry_ptr, |
593 | unsigned long *bt_addr_result) | |
1de4fa14 DH |
594 | { |
595 | int ret; | |
596 | int valid_bit; | |
54587653 DH |
597 | unsigned long bd_entry; |
598 | unsigned long bt_addr; | |
1de4fa14 | 599 | |
54587653 | 600 | if (!access_ok(VERIFY_READ, (bd_entry_ptr), sizeof(*bd_entry_ptr))) |
1de4fa14 DH |
601 | return -EFAULT; |
602 | ||
603 | while (1) { | |
604 | int need_write = 0; | |
605 | ||
606 | pagefault_disable(); | |
54587653 | 607 | ret = get_user(bd_entry, bd_entry_ptr); |
1de4fa14 DH |
608 | pagefault_enable(); |
609 | if (!ret) | |
610 | break; | |
611 | if (ret == -EFAULT) | |
54587653 | 612 | ret = mpx_resolve_fault(bd_entry_ptr, need_write); |
1de4fa14 DH |
613 | /* |
614 | * If we could not resolve the fault, consider it | |
615 | * userspace's fault and error out. | |
616 | */ | |
617 | if (ret) | |
618 | return ret; | |
619 | } | |
620 | ||
54587653 DH |
621 | valid_bit = bd_entry & MPX_BD_ENTRY_VALID_FLAG; |
622 | bt_addr = mpx_bd_entry_to_bt_addr(mm, bd_entry); | |
1de4fa14 DH |
623 | |
624 | /* | |
625 | * When the kernel is managing bounds tables, a bounds directory | |
626 | * entry will either have a valid address (plus the valid bit) | |
627 | * *OR* be completely empty. If we see a !valid entry *and* some | |
628 | * data in the address field, we know something is wrong. This | |
629 | * -EINVAL return will cause a SIGSEGV. | |
630 | */ | |
54587653 | 631 | if (!valid_bit && bt_addr) |
1de4fa14 DH |
632 | return -EINVAL; |
633 | /* | |
634 | * Do we have an completely zeroed bt entry? That is OK. It | |
635 | * just means there was no bounds table for this memory. Make | |
636 | * sure to distinguish this from -EINVAL, which will cause | |
637 | * a SEGV. | |
638 | */ | |
639 | if (!valid_bit) | |
640 | return -ENOENT; | |
641 | ||
54587653 | 642 | *bt_addr_result = bt_addr; |
1de4fa14 DH |
643 | return 0; |
644 | } | |
645 | ||
613fcb7d DH |
646 | static inline int bt_entry_size_bytes(struct mm_struct *mm) |
647 | { | |
648 | if (is_64bit_mm(mm)) | |
649 | return MPX_BT_ENTRY_BYTES_64; | |
650 | else | |
651 | return MPX_BT_ENTRY_BYTES_32; | |
652 | } | |
653 | ||
654 | /* | |
655 | * Take a virtual address and turns it in to the offset in bytes | |
656 | * inside of the bounds table where the bounds table entry | |
657 | * controlling 'addr' can be found. | |
658 | */ | |
659 | static unsigned long mpx_get_bt_entry_offset_bytes(struct mm_struct *mm, | |
660 | unsigned long addr) | |
661 | { | |
662 | unsigned long bt_table_nr_entries; | |
663 | unsigned long offset = addr; | |
664 | ||
665 | if (is_64bit_mm(mm)) { | |
666 | /* Bottom 3 bits are ignored on 64-bit */ | |
667 | offset >>= 3; | |
668 | bt_table_nr_entries = MPX_BT_NR_ENTRIES_64; | |
669 | } else { | |
670 | /* Bottom 2 bits are ignored on 32-bit */ | |
671 | offset >>= 2; | |
672 | bt_table_nr_entries = MPX_BT_NR_ENTRIES_32; | |
673 | } | |
674 | /* | |
675 | * We know the size of the table in to which we are | |
676 | * indexing, and we have eliminated all the low bits | |
677 | * which are ignored for indexing. | |
678 | * | |
679 | * Mask out all the high bits which we do not need | |
680 | * to index in to the table. Note that the tables | |
681 | * are always powers of two so this gives us a proper | |
682 | * mask. | |
683 | */ | |
684 | offset &= (bt_table_nr_entries-1); | |
685 | /* | |
686 | * We now have an entry offset in terms of *entries* in | |
687 | * the table. We need to scale it back up to bytes. | |
688 | */ | |
689 | offset *= bt_entry_size_bytes(mm); | |
690 | return offset; | |
691 | } | |
692 | ||
693 | /* | |
694 | * How much virtual address space does a single bounds | |
695 | * directory entry cover? | |
696 | * | |
697 | * Note, we need a long long because 4GB doesn't fit in | |
698 | * to a long on 32-bit. | |
699 | */ | |
700 | static inline unsigned long bd_entry_virt_space(struct mm_struct *mm) | |
701 | { | |
702 | unsigned long long virt_space = (1ULL << boot_cpu_data.x86_virt_bits); | |
703 | if (is_64bit_mm(mm)) | |
704 | return virt_space / MPX_BD_NR_ENTRIES_64; | |
705 | else | |
706 | return virt_space / MPX_BD_NR_ENTRIES_32; | |
707 | } | |
708 | ||
709 | /* | |
3ceaccdf DH |
710 | * Free the backing physical pages of bounds table 'bt_addr'. |
711 | * Assume start...end is within that bounds table. | |
613fcb7d | 712 | */ |
3ceaccdf DH |
713 | static noinline int zap_bt_entries_mapping(struct mm_struct *mm, |
714 | unsigned long bt_addr, | |
715 | unsigned long start_mapping, unsigned long end_mapping) | |
716 | { | |
717 | struct vm_area_struct *vma; | |
718 | unsigned long addr, len; | |
719 | unsigned long start; | |
720 | unsigned long end; | |
721 | ||
722 | /* | |
723 | * if we 'end' on a boundary, the offset will be 0 which | |
724 | * is not what we want. Back it up a byte to get the | |
725 | * last bt entry. Then once we have the entry itself, | |
726 | * move 'end' back up by the table entry size. | |
727 | */ | |
728 | start = bt_addr + mpx_get_bt_entry_offset_bytes(mm, start_mapping); | |
729 | end = bt_addr + mpx_get_bt_entry_offset_bytes(mm, end_mapping - 1); | |
730 | /* | |
731 | * Move end back up by one entry. Among other things | |
732 | * this ensures that it remains page-aligned and does | |
733 | * not screw up zap_page_range() | |
734 | */ | |
735 | end += bt_entry_size_bytes(mm); | |
736 | ||
737 | /* | |
738 | * Find the first overlapping vma. If vma->vm_start > start, there | |
739 | * will be a hole in the bounds table. This -EINVAL return will | |
740 | * cause a SIGSEGV. | |
741 | */ | |
742 | vma = find_vma(mm, start); | |
743 | if (!vma || vma->vm_start > start) | |
744 | return -EINVAL; | |
745 | ||
746 | /* | |
747 | * A NUMA policy on a VM_MPX VMA could cause this bounds table to | |
748 | * be split. So we need to look across the entire 'start -> end' | |
749 | * range of this bounds table, find all of the VM_MPX VMAs, and | |
750 | * zap only those. | |
751 | */ | |
752 | addr = start; | |
753 | while (vma && vma->vm_start < end) { | |
754 | /* | |
755 | * We followed a bounds directory entry down | |
756 | * here. If we find a non-MPX VMA, that's bad, | |
757 | * so stop immediately and return an error. This | |
758 | * probably results in a SIGSEGV. | |
759 | */ | |
a8965276 | 760 | if (!(vma->vm_flags & VM_MPX)) |
3ceaccdf DH |
761 | return -EINVAL; |
762 | ||
763 | len = min(vma->vm_end, end) - addr; | |
764 | zap_page_range(vma, addr, len, NULL); | |
765 | trace_mpx_unmap_zap(addr, addr+len); | |
766 | ||
767 | vma = vma->vm_next; | |
768 | addr = vma->vm_start; | |
769 | } | |
770 | return 0; | |
771 | } | |
772 | ||
613fcb7d DH |
773 | static unsigned long mpx_get_bd_entry_offset(struct mm_struct *mm, |
774 | unsigned long addr) | |
775 | { | |
776 | /* | |
777 | * There are several ways to derive the bd offsets. We | |
778 | * use the following approach here: | |
779 | * 1. We know the size of the virtual address space | |
780 | * 2. We know the number of entries in a bounds table | |
781 | * 3. We know that each entry covers a fixed amount of | |
782 | * virtual address space. | |
783 | * So, we can just divide the virtual address by the | |
784 | * virtual space used by one entry to determine which | |
785 | * entry "controls" the given virtual address. | |
786 | */ | |
787 | if (is_64bit_mm(mm)) { | |
788 | int bd_entry_size = 8; /* 64-bit pointer */ | |
789 | /* | |
790 | * Take the 64-bit addressing hole in to account. | |
791 | */ | |
792 | addr &= ((1UL << boot_cpu_data.x86_virt_bits) - 1); | |
793 | return (addr / bd_entry_virt_space(mm)) * bd_entry_size; | |
794 | } else { | |
795 | int bd_entry_size = 4; /* 32-bit pointer */ | |
796 | /* | |
797 | * 32-bit has no hole so this case needs no mask | |
798 | */ | |
799 | return (addr / bd_entry_virt_space(mm)) * bd_entry_size; | |
800 | } | |
801 | /* | |
802 | * The two return calls above are exact copies. If we | |
803 | * pull out a single copy and put it in here, gcc won't | |
804 | * realize that we're doing a power-of-2 divide and use | |
805 | * shifts. It uses a real divide. If we put them up | |
806 | * there, it manages to figure it out (gcc 4.8.3). | |
807 | */ | |
1de4fa14 DH |
808 | } |
809 | ||
3ceaccdf DH |
810 | static int unmap_entire_bt(struct mm_struct *mm, |
811 | long __user *bd_entry, unsigned long bt_addr) | |
1de4fa14 | 812 | { |
3ceaccdf DH |
813 | unsigned long expected_old_val = bt_addr | MPX_BD_ENTRY_VALID_FLAG; |
814 | unsigned long uninitialized_var(actual_old_val); | |
1de4fa14 DH |
815 | int ret; |
816 | ||
3ceaccdf DH |
817 | while (1) { |
818 | int need_write = 1; | |
819 | unsigned long cleared_bd_entry = 0; | |
820 | ||
821 | pagefault_disable(); | |
822 | ret = mpx_cmpxchg_bd_entry(mm, &actual_old_val, | |
823 | bd_entry, expected_old_val, cleared_bd_entry); | |
824 | pagefault_enable(); | |
825 | if (!ret) | |
826 | break; | |
827 | if (ret == -EFAULT) | |
828 | ret = mpx_resolve_fault(bd_entry, need_write); | |
829 | /* | |
830 | * If we could not resolve the fault, consider it | |
831 | * userspace's fault and error out. | |
832 | */ | |
833 | if (ret) | |
834 | return ret; | |
835 | } | |
1de4fa14 | 836 | /* |
3ceaccdf | 837 | * The cmpxchg was performed, check the results. |
1de4fa14 | 838 | */ |
3ceaccdf DH |
839 | if (actual_old_val != expected_old_val) { |
840 | /* | |
841 | * Someone else raced with us to unmap the table. | |
842 | * That is OK, since we were both trying to do | |
843 | * the same thing. Declare success. | |
844 | */ | |
845 | if (!actual_old_val) | |
846 | return 0; | |
847 | /* | |
848 | * Something messed with the bounds directory | |
849 | * entry. We hold mmap_sem for read or write | |
850 | * here, so it could not be a _new_ bounds table | |
851 | * that someone just allocated. Something is | |
852 | * wrong, so pass up the error and SIGSEGV. | |
853 | */ | |
854 | return -EINVAL; | |
855 | } | |
856 | /* | |
857 | * Note, we are likely being called under do_munmap() already. To | |
858 | * avoid recursion, do_munmap() will check whether it comes | |
859 | * from one bounds table through VM_MPX flag. | |
860 | */ | |
861 | return do_munmap(mm, bt_addr, mpx_bt_size_bytes(mm)); | |
1de4fa14 DH |
862 | } |
863 | ||
3ceaccdf DH |
864 | static int try_unmap_single_bt(struct mm_struct *mm, |
865 | unsigned long start, unsigned long end) | |
1de4fa14 | 866 | { |
3ceaccdf DH |
867 | struct vm_area_struct *next; |
868 | struct vm_area_struct *prev; | |
869 | /* | |
870 | * "bta" == Bounds Table Area: the area controlled by the | |
871 | * bounds table that we are unmapping. | |
872 | */ | |
873 | unsigned long bta_start_vaddr = start & ~(bd_entry_virt_space(mm)-1); | |
874 | unsigned long bta_end_vaddr = bta_start_vaddr + bd_entry_virt_space(mm); | |
875 | unsigned long uninitialized_var(bt_addr); | |
876 | void __user *bde_vaddr; | |
1de4fa14 | 877 | int ret; |
bea03c50 DH |
878 | /* |
879 | * We already unlinked the VMAs from the mm's rbtree so 'start' | |
880 | * is guaranteed to be in a hole. This gets us the first VMA | |
881 | * before the hole in to 'prev' and the next VMA after the hole | |
882 | * in to 'next'. | |
883 | */ | |
884 | next = find_vma_prev(mm, start, &prev); | |
885 | /* | |
886 | * Do not count other MPX bounds table VMAs as neighbors. | |
887 | * Although theoretically possible, we do not allow bounds | |
888 | * tables for bounds tables so our heads do not explode. | |
889 | * If we count them as neighbors here, we may end up with | |
890 | * lots of tables even though we have no actual table | |
891 | * entries in use. | |
892 | */ | |
a8965276 | 893 | while (next && (next->vm_flags & VM_MPX)) |
bea03c50 | 894 | next = next->vm_next; |
a8965276 | 895 | while (prev && (prev->vm_flags & VM_MPX)) |
bea03c50 | 896 | prev = prev->vm_prev; |
1de4fa14 | 897 | /* |
3ceaccdf DH |
898 | * We know 'start' and 'end' lie within an area controlled |
899 | * by a single bounds table. See if there are any other | |
900 | * VMAs controlled by that bounds table. If there are not | |
901 | * then we can "expand" the are we are unmapping to possibly | |
902 | * cover the entire table. | |
1de4fa14 DH |
903 | */ |
904 | next = find_vma_prev(mm, start, &prev); | |
3ceaccdf DH |
905 | if ((!prev || prev->vm_end <= bta_start_vaddr) && |
906 | (!next || next->vm_start >= bta_end_vaddr)) { | |
907 | /* | |
908 | * No neighbor VMAs controlled by same bounds | |
909 | * table. Try to unmap the whole thing | |
910 | */ | |
911 | start = bta_start_vaddr; | |
912 | end = bta_end_vaddr; | |
1de4fa14 DH |
913 | } |
914 | ||
3ceaccdf DH |
915 | bde_vaddr = mm->bd_addr + mpx_get_bd_entry_offset(mm, start); |
916 | ret = get_bt_addr(mm, bde_vaddr, &bt_addr); | |
1de4fa14 | 917 | /* |
3ceaccdf | 918 | * No bounds table there, so nothing to unmap. |
1de4fa14 | 919 | */ |
3ceaccdf DH |
920 | if (ret == -ENOENT) { |
921 | ret = 0; | |
922 | return 0; | |
923 | } | |
1de4fa14 DH |
924 | if (ret) |
925 | return ret; | |
3ceaccdf DH |
926 | /* |
927 | * We are unmapping an entire table. Either because the | |
928 | * unmap that started this whole process was large enough | |
929 | * to cover an entire table, or that the unmap was small | |
930 | * but was the area covered by a bounds table. | |
931 | */ | |
932 | if ((start == bta_start_vaddr) && | |
933 | (end == bta_end_vaddr)) | |
934 | return unmap_entire_bt(mm, bde_vaddr, bt_addr); | |
935 | return zap_bt_entries_mapping(mm, bt_addr, start, end); | |
1de4fa14 DH |
936 | } |
937 | ||
938 | static int mpx_unmap_tables(struct mm_struct *mm, | |
939 | unsigned long start, unsigned long end) | |
940 | { | |
3ceaccdf | 941 | unsigned long one_unmap_start; |
2a1dcb1f | 942 | trace_mpx_unmap_search(start, end); |
1de4fa14 | 943 | |
3ceaccdf DH |
944 | one_unmap_start = start; |
945 | while (one_unmap_start < end) { | |
946 | int ret; | |
947 | unsigned long next_unmap_start = ALIGN(one_unmap_start+1, | |
948 | bd_entry_virt_space(mm)); | |
949 | unsigned long one_unmap_end = end; | |
950 | /* | |
951 | * if the end is beyond the current bounds table, | |
952 | * move it back so we only deal with a single one | |
953 | * at a time | |
954 | */ | |
955 | if (one_unmap_end > next_unmap_start) | |
956 | one_unmap_end = next_unmap_start; | |
957 | ret = try_unmap_single_bt(mm, one_unmap_start, one_unmap_end); | |
1de4fa14 DH |
958 | if (ret) |
959 | return ret; | |
1de4fa14 | 960 | |
3ceaccdf DH |
961 | one_unmap_start = next_unmap_start; |
962 | } | |
1de4fa14 DH |
963 | return 0; |
964 | } | |
965 | ||
966 | /* | |
967 | * Free unused bounds tables covered in a virtual address region being | |
968 | * munmap()ed. Assume end > start. | |
969 | * | |
970 | * This function will be called by do_munmap(), and the VMAs covering | |
971 | * the virtual address region start...end have already been split if | |
972 | * necessary, and the 'vma' is the first vma in this range (start -> end). | |
973 | */ | |
974 | void mpx_notify_unmap(struct mm_struct *mm, struct vm_area_struct *vma, | |
975 | unsigned long start, unsigned long end) | |
976 | { | |
977 | int ret; | |
978 | ||
979 | /* | |
980 | * Refuse to do anything unless userspace has asked | |
981 | * the kernel to help manage the bounds tables, | |
982 | */ | |
983 | if (!kernel_managing_mpx_tables(current->mm)) | |
984 | return; | |
985 | /* | |
986 | * This will look across the entire 'start -> end' range, | |
987 | * and find all of the non-VM_MPX VMAs. | |
988 | * | |
989 | * To avoid recursion, if a VM_MPX vma is found in the range | |
990 | * (start->end), we will not continue follow-up work. This | |
991 | * recursion represents having bounds tables for bounds tables, | |
992 | * which should not occur normally. Being strict about it here | |
993 | * helps ensure that we do not have an exploitable stack overflow. | |
994 | */ | |
995 | do { | |
996 | if (vma->vm_flags & VM_MPX) | |
997 | return; | |
998 | vma = vma->vm_next; | |
999 | } while (vma && vma->vm_start < end); | |
1000 | ||
1001 | ret = mpx_unmap_tables(mm, start, end); | |
1002 | if (ret) | |
1003 | force_sig(SIGSEGV, current); | |
1004 | } |