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