blob: 1c34b767c84ca650386f1b550aa5e3f13d24d5ce [file] [log] [blame]
Qiaowei Ren57319d82014-11-14 07:18:27 -08001/*
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>
Dave Hansenfcc7ffd2014-11-14 07:18:28 -08009#include <linux/slab.h>
Ingo Molnar589ee622017-02-04 00:16:44 +010010#include <linux/mm_types.h>
Qiaowei Ren57319d82014-11-14 07:18:27 -080011#include <linux/syscalls.h>
12#include <linux/sched/sysctl.h>
13
Dave Hansenfe3d1972014-11-14 07:18:29 -080014#include <asm/insn.h>
Qiaowei Ren57319d82014-11-14 07:18:27 -080015#include <asm/mman.h>
Dave Hansen1de4fa12014-11-14 07:18:31 -080016#include <asm/mmu_context.h>
Qiaowei Ren57319d82014-11-14 07:18:27 -080017#include <asm/mpx.h>
Dave Hansenfe3d1972014-11-14 07:18:29 -080018#include <asm/processor.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020019#include <asm/fpu/internal.h>
Qiaowei Ren57319d82014-11-14 07:18:27 -080020
Dave Hansene7126cf2015-06-07 11:37:03 -070021#define CREATE_TRACE_POINTS
22#include <asm/trace/mpx.h>
23
Dave Hansen613fcb72015-06-07 11:37:05 -070024static inline unsigned long mpx_bd_size_bytes(struct mm_struct *mm)
25{
26 if (is_64bit_mm(mm))
27 return MPX_BD_SIZE_BYTES_64;
28 else
29 return MPX_BD_SIZE_BYTES_32;
30}
31
32static inline unsigned long mpx_bt_size_bytes(struct mm_struct *mm)
33{
34 if (is_64bit_mm(mm))
35 return MPX_BT_SIZE_BYTES_64;
36 else
37 return MPX_BT_SIZE_BYTES_32;
38}
39
Qiaowei Ren57319d82014-11-14 07:18:27 -080040/*
41 * This is really a simplified "vm_mmap". it only handles MPX
42 * bounds tables (the bounds directory is user-allocated).
Qiaowei Ren57319d82014-11-14 07:18:27 -080043 */
44static unsigned long mpx_mmap(unsigned long len)
45{
Qiaowei Ren57319d82014-11-14 07:18:27 -080046 struct mm_struct *mm = current->mm;
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -070047 unsigned long addr, populate;
Qiaowei Ren57319d82014-11-14 07:18:27 -080048
Dave Hanseneb099e52015-06-07 11:37:02 -070049 /* Only bounds table can be allocated here */
Dave Hansen613fcb72015-06-07 11:37:05 -070050 if (len != mpx_bt_size_bytes(mm))
Qiaowei Ren57319d82014-11-14 07:18:27 -080051 return -EINVAL;
52
53 down_write(&mm->mmap_sem);
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -070054 addr = do_mmap(NULL, 0, len, PROT_READ | PROT_WRITE,
Mike Rapoport897ab3e2017-02-24 14:58:22 -080055 MAP_ANONYMOUS | MAP_PRIVATE, VM_MPX, 0, &populate, NULL);
Qiaowei Ren57319d82014-11-14 07:18:27 -080056 up_write(&mm->mmap_sem);
Oleg Nesterov1fcfd8d2015-09-09 15:39:29 -070057 if (populate)
58 mm_populate(addr, populate);
59
60 return addr;
Qiaowei Ren57319d82014-11-14 07:18:27 -080061}
Dave Hansenfcc7ffd2014-11-14 07:18:28 -080062
63enum reg_type {
64 REG_TYPE_RM = 0,
65 REG_TYPE_INDEX,
66 REG_TYPE_BASE,
67};
68
Dave Hansen68c009c2014-11-18 10:23:43 -080069static int get_reg_offset(struct insn *insn, struct pt_regs *regs,
70 enum reg_type type)
Dave Hansenfcc7ffd2014-11-14 07:18:28 -080071{
72 int regno = 0;
73
74 static const int regoff[] = {
75 offsetof(struct pt_regs, ax),
76 offsetof(struct pt_regs, cx),
77 offsetof(struct pt_regs, dx),
78 offsetof(struct pt_regs, bx),
79 offsetof(struct pt_regs, sp),
80 offsetof(struct pt_regs, bp),
81 offsetof(struct pt_regs, si),
82 offsetof(struct pt_regs, di),
83#ifdef CONFIG_X86_64
84 offsetof(struct pt_regs, r8),
85 offsetof(struct pt_regs, r9),
86 offsetof(struct pt_regs, r10),
87 offsetof(struct pt_regs, r11),
88 offsetof(struct pt_regs, r12),
89 offsetof(struct pt_regs, r13),
90 offsetof(struct pt_regs, r14),
91 offsetof(struct pt_regs, r15),
92#endif
93 };
94 int nr_registers = ARRAY_SIZE(regoff);
95 /*
96 * Don't possibly decode a 32-bit instructions as
97 * reading a 64-bit-only register.
98 */
99 if (IS_ENABLED(CONFIG_X86_64) && !insn->x86_64)
100 nr_registers -= 8;
101
102 switch (type) {
103 case REG_TYPE_RM:
104 regno = X86_MODRM_RM(insn->modrm.value);
Dave Hansen8e8efe02015-11-30 16:31:13 -0800105 if (X86_REX_B(insn->rex_prefix.value))
Dave Hansenfcc7ffd2014-11-14 07:18:28 -0800106 regno += 8;
107 break;
108
109 case REG_TYPE_INDEX:
110 regno = X86_SIB_INDEX(insn->sib.value);
Dave Hansen8e8efe02015-11-30 16:31:13 -0800111 if (X86_REX_X(insn->rex_prefix.value))
Dave Hansenfcc7ffd2014-11-14 07:18:28 -0800112 regno += 8;
113 break;
114
115 case REG_TYPE_BASE:
116 regno = X86_SIB_BASE(insn->sib.value);
Dave Hansen8e8efe02015-11-30 16:31:13 -0800117 if (X86_REX_B(insn->rex_prefix.value))
Dave Hansenfcc7ffd2014-11-14 07:18:28 -0800118 regno += 8;
119 break;
120
121 default:
122 pr_err("invalid register type");
123 BUG();
124 break;
125 }
126
Colin Ian King9bf148cb2016-02-26 18:55:31 +0000127 if (regno >= nr_registers) {
Dave Hansenfcc7ffd2014-11-14 07:18:28 -0800128 WARN_ONCE(1, "decoded an instruction with an invalid register");
129 return -EINVAL;
130 }
131 return regoff[regno];
132}
133
134/*
135 * return the address being referenced be instruction
136 * for rm=3 returning the content of the rm reg
137 * for rm!=3 calculates the address using SIB and Disp
138 */
139static void __user *mpx_get_addr_ref(struct insn *insn, struct pt_regs *regs)
140{
Dave Hansen68c009c2014-11-18 10:23:43 -0800141 unsigned long addr, base, indx;
142 int addr_offset, base_offset, indx_offset;
Dave Hansenfcc7ffd2014-11-14 07:18:28 -0800143 insn_byte_t sib;
144
145 insn_get_modrm(insn);
146 insn_get_sib(insn);
147 sib = insn->sib.value;
148
149 if (X86_MODRM_MOD(insn->modrm.value) == 3) {
150 addr_offset = get_reg_offset(insn, regs, REG_TYPE_RM);
151 if (addr_offset < 0)
152 goto out_err;
153 addr = regs_get_register(regs, addr_offset);
154 } else {
155 if (insn->sib.nbytes) {
156 base_offset = get_reg_offset(insn, regs, REG_TYPE_BASE);
157 if (base_offset < 0)
158 goto out_err;
159
160 indx_offset = get_reg_offset(insn, regs, REG_TYPE_INDEX);
161 if (indx_offset < 0)
162 goto out_err;
163
164 base = regs_get_register(regs, base_offset);
165 indx = regs_get_register(regs, indx_offset);
166 addr = base + indx * (1 << X86_SIB_SCALE(sib));
167 } else {
168 addr_offset = get_reg_offset(insn, regs, REG_TYPE_RM);
169 if (addr_offset < 0)
170 goto out_err;
171 addr = regs_get_register(regs, addr_offset);
172 }
173 addr += insn->displacement.value;
174 }
175 return (void __user *)addr;
176out_err:
177 return (void __user *)-1;
178}
179
180static int mpx_insn_decode(struct insn *insn,
181 struct pt_regs *regs)
182{
183 unsigned char buf[MAX_INSN_SIZE];
184 int x86_64 = !test_thread_flag(TIF_IA32);
185 int not_copied;
186 int nr_copied;
187
188 not_copied = copy_from_user(buf, (void __user *)regs->ip, sizeof(buf));
189 nr_copied = sizeof(buf) - not_copied;
190 /*
191 * The decoder _should_ fail nicely if we pass it a short buffer.
192 * But, let's not depend on that implementation detail. If we
193 * did not get anything, just error out now.
194 */
195 if (!nr_copied)
196 return -EFAULT;
197 insn_init(insn, buf, nr_copied, x86_64);
198 insn_get_length(insn);
199 /*
200 * copy_from_user() tries to get as many bytes as we could see in
201 * the largest possible instruction. If the instruction we are
202 * after is shorter than that _and_ we attempt to copy from
203 * something unreadable, we might get a short read. This is OK
204 * as long as the read did not stop in the middle of the
205 * instruction. Check to see if we got a partial instruction.
206 */
207 if (nr_copied < insn->length)
208 return -EFAULT;
209
210 insn_get_opcode(insn);
211 /*
212 * We only _really_ need to decode bndcl/bndcn/bndcu
213 * Error out on anything else.
214 */
215 if (insn->opcode.bytes[0] != 0x0f)
216 goto bad_opcode;
217 if ((insn->opcode.bytes[1] != 0x1a) &&
218 (insn->opcode.bytes[1] != 0x1b))
219 goto bad_opcode;
220
221 return 0;
222bad_opcode:
223 return -EINVAL;
224}
225
226/*
227 * If a bounds overflow occurs then a #BR is generated. This
228 * function decodes MPX instructions to get violation address
229 * and set this address into extended struct siginfo.
230 *
231 * Note that this is not a super precise way of doing this.
232 * Userspace could have, by the time we get here, written
233 * anything it wants in to the instructions. We can not
234 * trust anything about it. They might not be valid
235 * instructions or might encode invalid registers, etc...
236 *
237 * The caller is expected to kfree() the returned siginfo_t.
238 */
Dave Hansen46a6e0c2015-06-07 11:37:02 -0700239siginfo_t *mpx_generate_siginfo(struct pt_regs *regs)
Dave Hansenfcc7ffd2014-11-14 07:18:28 -0800240{
Dave Hansen1126cb452015-09-02 16:31:29 -0700241 const struct mpx_bndreg_state *bndregs;
242 const struct mpx_bndreg *bndreg;
Dave Hansenfe3d1972014-11-14 07:18:29 -0800243 siginfo_t *info = NULL;
Dave Hansenfcc7ffd2014-11-14 07:18:28 -0800244 struct insn insn;
245 uint8_t bndregno;
246 int err;
Dave Hansenfcc7ffd2014-11-14 07:18:28 -0800247
248 err = mpx_insn_decode(&insn, regs);
249 if (err)
250 goto err_out;
251
252 /*
253 * We know at this point that we are only dealing with
254 * MPX instructions.
255 */
256 insn_get_modrm(&insn);
257 bndregno = X86_MODRM_REG(insn.modrm.value);
258 if (bndregno > 3) {
259 err = -EINVAL;
260 goto err_out;
261 }
Dave Hansena84eeaa2015-06-07 11:37:01 -0700262 /* get bndregs field from current task's xsave area */
Dave Hansend91cab72015-09-02 16:31:26 -0700263 bndregs = get_xsave_field_ptr(XFEATURE_MASK_BNDREGS);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800264 if (!bndregs) {
265 err = -EINVAL;
266 goto err_out;
267 }
268 /* now go select the individual register in the set of 4 */
Dave Hansen1126cb452015-09-02 16:31:29 -0700269 bndreg = &bndregs->bndreg[bndregno];
Dave Hansenfe3d1972014-11-14 07:18:29 -0800270
Dave Hansenfcc7ffd2014-11-14 07:18:28 -0800271 info = kzalloc(sizeof(*info), GFP_KERNEL);
272 if (!info) {
273 err = -ENOMEM;
274 goto err_out;
275 }
276 /*
277 * The registers are always 64-bit, but the upper 32
278 * bits are ignored in 32-bit mode. Also, note that the
279 * upper bounds are architecturally represented in 1's
280 * complement form.
281 *
282 * The 'unsigned long' cast is because the compiler
283 * complains when casting from integers to different-size
284 * pointers.
285 */
Dave Hansenfe3d1972014-11-14 07:18:29 -0800286 info->si_lower = (void __user *)(unsigned long)bndreg->lower_bound;
287 info->si_upper = (void __user *)(unsigned long)~bndreg->upper_bound;
Dave Hansenfcc7ffd2014-11-14 07:18:28 -0800288 info->si_addr_lsb = 0;
289 info->si_signo = SIGSEGV;
290 info->si_errno = 0;
291 info->si_code = SEGV_BNDERR;
292 info->si_addr = mpx_get_addr_ref(&insn, regs);
293 /*
294 * We were not able to extract an address from the instruction,
295 * probably because there was something invalid in it.
296 */
Tobias Klauser45382862017-01-12 16:53:11 +0100297 if (info->si_addr == (void __user *)-1) {
Dave Hansenfcc7ffd2014-11-14 07:18:28 -0800298 err = -EINVAL;
299 goto err_out;
300 }
Dave Hansen97efebf2015-06-07 11:37:03 -0700301 trace_mpx_bounds_register_exception(info->si_addr, bndreg);
Dave Hansenfcc7ffd2014-11-14 07:18:28 -0800302 return info;
303err_out:
Dave Hansenfe3d1972014-11-14 07:18:29 -0800304 /* info might be NULL, but kfree() handles that */
305 kfree(info);
Dave Hansenfcc7ffd2014-11-14 07:18:28 -0800306 return ERR_PTR(err);
307}
Dave Hansenfe3d1972014-11-14 07:18:29 -0800308
Dave Hansen46a6e0c2015-06-07 11:37:02 -0700309static __user void *mpx_get_bounds_dir(void)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800310{
Dave Hansen1126cb452015-09-02 16:31:29 -0700311 const struct mpx_bndcsr *bndcsr;
Dave Hansenfe3d1972014-11-14 07:18:29 -0800312
313 if (!cpu_feature_enabled(X86_FEATURE_MPX))
314 return MPX_INVALID_BOUNDS_DIR;
315
316 /*
317 * The bounds directory pointer is stored in a register
318 * only accessible if we first do an xsave.
319 */
Dave Hansend91cab72015-09-02 16:31:26 -0700320 bndcsr = get_xsave_field_ptr(XFEATURE_MASK_BNDCSR);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800321 if (!bndcsr)
322 return MPX_INVALID_BOUNDS_DIR;
323
324 /*
325 * Make sure the register looks valid by checking the
326 * enable bit.
327 */
328 if (!(bndcsr->bndcfgu & MPX_BNDCFG_ENABLE_FLAG))
329 return MPX_INVALID_BOUNDS_DIR;
330
331 /*
332 * Lastly, mask off the low bits used for configuration
333 * flags, and return the address of the bounds table.
334 */
335 return (void __user *)(unsigned long)
336 (bndcsr->bndcfgu & MPX_BNDCFG_ADDR_MASK);
337}
338
Dave Hansen46a6e0c2015-06-07 11:37:02 -0700339int mpx_enable_management(void)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800340{
341 void __user *bd_base = MPX_INVALID_BOUNDS_DIR;
Dave Hansen46a6e0c2015-06-07 11:37:02 -0700342 struct mm_struct *mm = current->mm;
Dave Hansenfe3d1972014-11-14 07:18:29 -0800343 int ret = 0;
344
345 /*
346 * runtime in the userspace will be responsible for allocation of
347 * the bounds directory. Then, it will save the base of the bounds
348 * directory into XSAVE/XRSTOR Save Area and enable MPX through
349 * XRSTOR instruction.
350 *
Dave Hansena84eeaa2015-06-07 11:37:01 -0700351 * The copy_xregs_to_kernel() beneath get_xsave_field_ptr() is
352 * expected to be relatively expensive. Storing the bounds
353 * directory here means that we do not have to do xsave in the
Mark Rutlandcb02de92016-12-16 12:40:55 +0000354 * unmap path; we can just use mm->context.bd_addr instead.
Dave Hansenfe3d1972014-11-14 07:18:29 -0800355 */
Dave Hansen46a6e0c2015-06-07 11:37:02 -0700356 bd_base = mpx_get_bounds_dir();
Dave Hansenfe3d1972014-11-14 07:18:29 -0800357 down_write(&mm->mmap_sem);
Mark Rutlandcb02de92016-12-16 12:40:55 +0000358 mm->context.bd_addr = bd_base;
359 if (mm->context.bd_addr == MPX_INVALID_BOUNDS_DIR)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800360 ret = -ENXIO;
361
362 up_write(&mm->mmap_sem);
363 return ret;
364}
365
Dave Hansen46a6e0c2015-06-07 11:37:02 -0700366int mpx_disable_management(void)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800367{
368 struct mm_struct *mm = current->mm;
369
370 if (!cpu_feature_enabled(X86_FEATURE_MPX))
371 return -ENXIO;
372
373 down_write(&mm->mmap_sem);
Mark Rutlandcb02de92016-12-16 12:40:55 +0000374 mm->context.bd_addr = MPX_INVALID_BOUNDS_DIR;
Dave Hansenfe3d1972014-11-14 07:18:29 -0800375 up_write(&mm->mmap_sem);
376 return 0;
377}
378
Dave Hansen6ac52bb2015-06-07 11:37:05 -0700379static int mpx_cmpxchg_bd_entry(struct mm_struct *mm,
380 unsigned long *curval,
381 unsigned long __user *addr,
382 unsigned long old_val, unsigned long new_val)
383{
384 int ret;
385 /*
386 * user_atomic_cmpxchg_inatomic() actually uses sizeof()
387 * the pointer that we pass to it to figure out how much
388 * data to cmpxchg. We have to be careful here not to
389 * pass a pointer to a 64-bit data type when we only want
390 * a 32-bit copy.
391 */
392 if (is_64bit_mm(mm)) {
393 ret = user_atomic_cmpxchg_inatomic(curval,
394 addr, old_val, new_val);
395 } else {
396 u32 uninitialized_var(curval_32);
397 u32 old_val_32 = old_val;
398 u32 new_val_32 = new_val;
399 u32 __user *addr_32 = (u32 __user *)addr;
400
401 ret = user_atomic_cmpxchg_inatomic(&curval_32,
402 addr_32, old_val_32, new_val_32);
403 *curval = curval_32;
404 }
405 return ret;
406}
407
Dave Hansenfe3d1972014-11-14 07:18:29 -0800408/*
Dave Hansen613fcb72015-06-07 11:37:05 -0700409 * With 32-bit mode, a bounds directory is 4MB, and the size of each
410 * bounds table is 16KB. With 64-bit mode, a bounds directory is 2GB,
Dave Hansenfe3d1972014-11-14 07:18:29 -0800411 * and the size of each bounds table is 4MB.
412 */
Dave Hansen613fcb72015-06-07 11:37:05 -0700413static int allocate_bt(struct mm_struct *mm, long __user *bd_entry)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800414{
415 unsigned long expected_old_val = 0;
416 unsigned long actual_old_val = 0;
417 unsigned long bt_addr;
Dave Hansena1149fc2015-06-07 11:37:04 -0700418 unsigned long bd_new_entry;
Dave Hansenfe3d1972014-11-14 07:18:29 -0800419 int ret = 0;
420
421 /*
422 * Carve the virtual space out of userspace for the new
423 * bounds table:
424 */
Dave Hansen613fcb72015-06-07 11:37:05 -0700425 bt_addr = mpx_mmap(mpx_bt_size_bytes(mm));
Dave Hansenfe3d1972014-11-14 07:18:29 -0800426 if (IS_ERR((void *)bt_addr))
427 return PTR_ERR((void *)bt_addr);
428 /*
429 * Set the valid flag (kinda like _PAGE_PRESENT in a pte)
430 */
Dave Hansena1149fc2015-06-07 11:37:04 -0700431 bd_new_entry = bt_addr | MPX_BD_ENTRY_VALID_FLAG;
Dave Hansenfe3d1972014-11-14 07:18:29 -0800432
433 /*
434 * Go poke the address of the new bounds table in to the
435 * bounds directory entry out in userspace memory. Note:
436 * we may race with another CPU instantiating the same table.
437 * In that case the cmpxchg will see an unexpected
438 * 'actual_old_val'.
439 *
440 * This can fault, but that's OK because we do not hold
441 * mmap_sem at this point, unlike some of the other part
442 * of the MPX code that have to pagefault_disable().
443 */
Dave Hansen6ac52bb2015-06-07 11:37:05 -0700444 ret = mpx_cmpxchg_bd_entry(mm, &actual_old_val, bd_entry,
445 expected_old_val, bd_new_entry);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800446 if (ret)
447 goto out_unmap;
448
449 /*
450 * The user_atomic_cmpxchg_inatomic() will only return nonzero
451 * for faults, *not* if the cmpxchg itself fails. Now we must
452 * verify that the cmpxchg itself completed successfully.
453 */
454 /*
455 * We expected an empty 'expected_old_val', but instead found
456 * an apparently valid entry. Assume we raced with another
457 * thread to instantiate this table and desclare succecss.
458 */
459 if (actual_old_val & MPX_BD_ENTRY_VALID_FLAG) {
460 ret = 0;
461 goto out_unmap;
462 }
463 /*
464 * We found a non-empty bd_entry but it did not have the
465 * VALID_FLAG set. Return an error which will result in
466 * a SEGV since this probably means that somebody scribbled
467 * some invalid data in to a bounds table.
468 */
469 if (expected_old_val != actual_old_val) {
470 ret = -EINVAL;
471 goto out_unmap;
472 }
Dave Hansencd4996d2015-06-07 11:37:04 -0700473 trace_mpx_new_bounds_table(bt_addr);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800474 return 0;
475out_unmap:
Dave Hansen613fcb72015-06-07 11:37:05 -0700476 vm_munmap(bt_addr, mpx_bt_size_bytes(mm));
Dave Hansenfe3d1972014-11-14 07:18:29 -0800477 return ret;
478}
479
480/*
481 * When a BNDSTX instruction attempts to save bounds to a bounds
482 * table, it will first attempt to look up the table in the
483 * first-level bounds directory. If it does not find a table in
484 * the directory, a #BR is generated and we get here in order to
485 * allocate a new table.
486 *
487 * With 32-bit mode, the size of BD is 4MB, and the size of each
488 * bound table is 16KB. With 64-bit mode, the size of BD is 2GB,
489 * and the size of each bound table is 4MB.
490 */
Dave Hansen46a6e0c2015-06-07 11:37:02 -0700491static int do_mpx_bt_fault(void)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800492{
493 unsigned long bd_entry, bd_base;
Dave Hansen1126cb452015-09-02 16:31:29 -0700494 const struct mpx_bndcsr *bndcsr;
Dave Hansen613fcb72015-06-07 11:37:05 -0700495 struct mm_struct *mm = current->mm;
Dave Hansenfe3d1972014-11-14 07:18:29 -0800496
Dave Hansend91cab72015-09-02 16:31:26 -0700497 bndcsr = get_xsave_field_ptr(XFEATURE_MASK_BNDCSR);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800498 if (!bndcsr)
499 return -EINVAL;
500 /*
501 * Mask off the preserve and enable bits
502 */
503 bd_base = bndcsr->bndcfgu & MPX_BNDCFG_ADDR_MASK;
504 /*
505 * The hardware provides the address of the missing or invalid
506 * entry via BNDSTATUS, so we don't have to go look it up.
507 */
508 bd_entry = bndcsr->bndstatus & MPX_BNDSTA_ADDR_MASK;
509 /*
510 * Make sure the directory entry is within where we think
511 * the directory is.
512 */
513 if ((bd_entry < bd_base) ||
Dave Hansen613fcb72015-06-07 11:37:05 -0700514 (bd_entry >= bd_base + mpx_bd_size_bytes(mm)))
Dave Hansenfe3d1972014-11-14 07:18:29 -0800515 return -EINVAL;
516
Dave Hansen613fcb72015-06-07 11:37:05 -0700517 return allocate_bt(mm, (long __user *)bd_entry);
Dave Hansenfe3d1972014-11-14 07:18:29 -0800518}
519
Dave Hansen46a6e0c2015-06-07 11:37:02 -0700520int mpx_handle_bd_fault(void)
Dave Hansenfe3d1972014-11-14 07:18:29 -0800521{
522 /*
523 * Userspace never asked us to manage the bounds tables,
524 * so refuse to help.
525 */
526 if (!kernel_managing_mpx_tables(current->mm))
527 return -EINVAL;
528
Joerg Roedel5ed386e2017-04-06 16:19:22 +0200529 return do_mpx_bt_fault();
Dave Hansenfe3d1972014-11-14 07:18:29 -0800530}
Dave Hansen1de4fa12014-11-14 07:18:31 -0800531
532/*
533 * A thin wrapper around get_user_pages(). Returns 0 if the
534 * fault was resolved or -errno if not.
535 */
536static int mpx_resolve_fault(long __user *addr, int write)
537{
538 long gup_ret;
539 int nr_pages = 1;
Dave Hansen1de4fa12014-11-14 07:18:31 -0800540
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100541 gup_ret = get_user_pages((unsigned long)addr, nr_pages,
542 write ? FOLL_WRITE : 0, NULL, NULL);
Dave Hansen1de4fa12014-11-14 07:18:31 -0800543 /*
544 * get_user_pages() returns number of pages gotten.
545 * 0 means we failed to fault in and get anything,
546 * probably because 'addr' is bad.
547 */
548 if (!gup_ret)
549 return -EFAULT;
550 /* Other error, return it */
551 if (gup_ret < 0)
552 return gup_ret;
553 /* must have gup'd a page and gup_ret>0, success */
554 return 0;
555}
556
Dave Hansen54587652015-06-07 11:37:04 -0700557static unsigned long mpx_bd_entry_to_bt_addr(struct mm_struct *mm,
558 unsigned long bd_entry)
559{
560 unsigned long bt_addr = bd_entry;
561 int align_to_bytes;
562 /*
563 * Bit 0 in a bt_entry is always the valid bit.
564 */
565 bt_addr &= ~MPX_BD_ENTRY_VALID_FLAG;
566 /*
567 * Tables are naturally aligned at 8-byte boundaries
568 * on 64-bit and 4-byte boundaries on 32-bit. The
569 * documentation makes it appear that the low bits
570 * are ignored by the hardware, so we do the same.
571 */
572 if (is_64bit_mm(mm))
573 align_to_bytes = 8;
574 else
575 align_to_bytes = 4;
576 bt_addr &= ~(align_to_bytes-1);
577 return bt_addr;
578}
579
Dave Hansen1de4fa12014-11-14 07:18:31 -0800580/*
Dave Hansen46561c32015-11-11 10:19:31 -0800581 * We only want to do a 4-byte get_user() on 32-bit. Otherwise,
582 * we might run off the end of the bounds table if we are on
583 * a 64-bit kernel and try to get 8 bytes.
584 */
Tobias Klauser6bce7252017-03-08 14:30:34 +0100585static int get_user_bd_entry(struct mm_struct *mm, unsigned long *bd_entry_ret,
Dave Hansen46561c32015-11-11 10:19:31 -0800586 long __user *bd_entry_ptr)
587{
588 u32 bd_entry_32;
589 int ret;
590
591 if (is_64bit_mm(mm))
592 return get_user(*bd_entry_ret, bd_entry_ptr);
593
594 /*
595 * Note that get_user() uses the type of the *pointer* to
596 * establish the size of the get, not the destination.
597 */
598 ret = get_user(bd_entry_32, (u32 __user *)bd_entry_ptr);
599 *bd_entry_ret = bd_entry_32;
600 return ret;
601}
602
603/*
Dave Hansen1de4fa12014-11-14 07:18:31 -0800604 * Get the base of bounds tables pointed by specific bounds
605 * directory entry.
606 */
607static int get_bt_addr(struct mm_struct *mm,
Dave Hansen54587652015-06-07 11:37:04 -0700608 long __user *bd_entry_ptr,
609 unsigned long *bt_addr_result)
Dave Hansen1de4fa12014-11-14 07:18:31 -0800610{
611 int ret;
612 int valid_bit;
Dave Hansen54587652015-06-07 11:37:04 -0700613 unsigned long bd_entry;
614 unsigned long bt_addr;
Dave Hansen1de4fa12014-11-14 07:18:31 -0800615
Dave Hansen54587652015-06-07 11:37:04 -0700616 if (!access_ok(VERIFY_READ, (bd_entry_ptr), sizeof(*bd_entry_ptr)))
Dave Hansen1de4fa12014-11-14 07:18:31 -0800617 return -EFAULT;
618
619 while (1) {
620 int need_write = 0;
621
622 pagefault_disable();
Dave Hansen46561c32015-11-11 10:19:31 -0800623 ret = get_user_bd_entry(mm, &bd_entry, bd_entry_ptr);
Dave Hansen1de4fa12014-11-14 07:18:31 -0800624 pagefault_enable();
625 if (!ret)
626 break;
627 if (ret == -EFAULT)
Dave Hansen54587652015-06-07 11:37:04 -0700628 ret = mpx_resolve_fault(bd_entry_ptr, need_write);
Dave Hansen1de4fa12014-11-14 07:18:31 -0800629 /*
630 * If we could not resolve the fault, consider it
631 * userspace's fault and error out.
632 */
633 if (ret)
634 return ret;
635 }
636
Dave Hansen54587652015-06-07 11:37:04 -0700637 valid_bit = bd_entry & MPX_BD_ENTRY_VALID_FLAG;
638 bt_addr = mpx_bd_entry_to_bt_addr(mm, bd_entry);
Dave Hansen1de4fa12014-11-14 07:18:31 -0800639
640 /*
641 * When the kernel is managing bounds tables, a bounds directory
642 * entry will either have a valid address (plus the valid bit)
643 * *OR* be completely empty. If we see a !valid entry *and* some
644 * data in the address field, we know something is wrong. This
645 * -EINVAL return will cause a SIGSEGV.
646 */
Dave Hansen54587652015-06-07 11:37:04 -0700647 if (!valid_bit && bt_addr)
Dave Hansen1de4fa12014-11-14 07:18:31 -0800648 return -EINVAL;
649 /*
650 * Do we have an completely zeroed bt entry? That is OK. It
651 * just means there was no bounds table for this memory. Make
652 * sure to distinguish this from -EINVAL, which will cause
653 * a SEGV.
654 */
655 if (!valid_bit)
656 return -ENOENT;
657
Dave Hansen54587652015-06-07 11:37:04 -0700658 *bt_addr_result = bt_addr;
Dave Hansen1de4fa12014-11-14 07:18:31 -0800659 return 0;
660}
661
Dave Hansen613fcb72015-06-07 11:37:05 -0700662static inline int bt_entry_size_bytes(struct mm_struct *mm)
663{
664 if (is_64bit_mm(mm))
665 return MPX_BT_ENTRY_BYTES_64;
666 else
667 return MPX_BT_ENTRY_BYTES_32;
668}
669
670/*
671 * Take a virtual address and turns it in to the offset in bytes
672 * inside of the bounds table where the bounds table entry
673 * controlling 'addr' can be found.
674 */
675static unsigned long mpx_get_bt_entry_offset_bytes(struct mm_struct *mm,
676 unsigned long addr)
677{
678 unsigned long bt_table_nr_entries;
679 unsigned long offset = addr;
680
681 if (is_64bit_mm(mm)) {
682 /* Bottom 3 bits are ignored on 64-bit */
683 offset >>= 3;
684 bt_table_nr_entries = MPX_BT_NR_ENTRIES_64;
685 } else {
686 /* Bottom 2 bits are ignored on 32-bit */
687 offset >>= 2;
688 bt_table_nr_entries = MPX_BT_NR_ENTRIES_32;
689 }
690 /*
691 * We know the size of the table in to which we are
692 * indexing, and we have eliminated all the low bits
693 * which are ignored for indexing.
694 *
695 * Mask out all the high bits which we do not need
696 * to index in to the table. Note that the tables
697 * are always powers of two so this gives us a proper
698 * mask.
699 */
700 offset &= (bt_table_nr_entries-1);
701 /*
702 * We now have an entry offset in terms of *entries* in
703 * the table. We need to scale it back up to bytes.
704 */
705 offset *= bt_entry_size_bytes(mm);
706 return offset;
707}
708
709/*
710 * How much virtual address space does a single bounds
711 * directory entry cover?
712 *
713 * Note, we need a long long because 4GB doesn't fit in
714 * to a long on 32-bit.
715 */
716static inline unsigned long bd_entry_virt_space(struct mm_struct *mm)
717{
Dave Hansenf3119b82015-11-11 10:19:34 -0800718 unsigned long long virt_space;
719 unsigned long long GB = (1ULL << 30);
720
721 /*
722 * This covers 32-bit emulation as well as 32-bit kernels
Adam Buchbinder6a6256f2016-02-23 15:34:30 -0800723 * running on 64-bit hardware.
Dave Hansenf3119b82015-11-11 10:19:34 -0800724 */
725 if (!is_64bit_mm(mm))
726 return (4ULL * GB) / MPX_BD_NR_ENTRIES_32;
727
728 /*
729 * 'x86_virt_bits' returns what the hardware is capable
Adam Buchbinder6a6256f2016-02-23 15:34:30 -0800730 * of, and returns the full >32-bit address space when
Dave Hansenf3119b82015-11-11 10:19:34 -0800731 * running 32-bit kernels on 64-bit hardware.
732 */
733 virt_space = (1ULL << boot_cpu_data.x86_virt_bits);
734 return virt_space / MPX_BD_NR_ENTRIES_64;
Dave Hansen613fcb72015-06-07 11:37:05 -0700735}
736
737/*
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700738 * Free the backing physical pages of bounds table 'bt_addr'.
739 * Assume start...end is within that bounds table.
Dave Hansen613fcb72015-06-07 11:37:05 -0700740 */
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700741static noinline int zap_bt_entries_mapping(struct mm_struct *mm,
742 unsigned long bt_addr,
743 unsigned long start_mapping, unsigned long end_mapping)
744{
745 struct vm_area_struct *vma;
746 unsigned long addr, len;
747 unsigned long start;
748 unsigned long end;
749
750 /*
751 * if we 'end' on a boundary, the offset will be 0 which
752 * is not what we want. Back it up a byte to get the
753 * last bt entry. Then once we have the entry itself,
754 * move 'end' back up by the table entry size.
755 */
756 start = bt_addr + mpx_get_bt_entry_offset_bytes(mm, start_mapping);
757 end = bt_addr + mpx_get_bt_entry_offset_bytes(mm, end_mapping - 1);
758 /*
759 * Move end back up by one entry. Among other things
760 * this ensures that it remains page-aligned and does
761 * not screw up zap_page_range()
762 */
763 end += bt_entry_size_bytes(mm);
764
765 /*
766 * Find the first overlapping vma. If vma->vm_start > start, there
767 * will be a hole in the bounds table. This -EINVAL return will
768 * cause a SIGSEGV.
769 */
770 vma = find_vma(mm, start);
771 if (!vma || vma->vm_start > start)
772 return -EINVAL;
773
774 /*
775 * A NUMA policy on a VM_MPX VMA could cause this bounds table to
776 * be split. So we need to look across the entire 'start -> end'
777 * range of this bounds table, find all of the VM_MPX VMAs, and
778 * zap only those.
779 */
780 addr = start;
781 while (vma && vma->vm_start < end) {
782 /*
783 * We followed a bounds directory entry down
784 * here. If we find a non-MPX VMA, that's bad,
785 * so stop immediately and return an error. This
786 * probably results in a SIGSEGV.
787 */
Kirill A. Shutemova89652762015-07-20 14:29:58 -0700788 if (!(vma->vm_flags & VM_MPX))
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700789 return -EINVAL;
790
791 len = min(vma->vm_end, end) - addr;
Kirill A. Shutemovecf13852017-02-22 15:46:37 -0800792 zap_page_range(vma, addr, len);
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700793 trace_mpx_unmap_zap(addr, addr+len);
794
795 vma = vma->vm_next;
796 addr = vma->vm_start;
797 }
798 return 0;
799}
800
Dave Hansen613fcb72015-06-07 11:37:05 -0700801static unsigned long mpx_get_bd_entry_offset(struct mm_struct *mm,
802 unsigned long addr)
803{
804 /*
805 * There are several ways to derive the bd offsets. We
806 * use the following approach here:
807 * 1. We know the size of the virtual address space
808 * 2. We know the number of entries in a bounds table
809 * 3. We know that each entry covers a fixed amount of
810 * virtual address space.
811 * So, we can just divide the virtual address by the
812 * virtual space used by one entry to determine which
813 * entry "controls" the given virtual address.
814 */
815 if (is_64bit_mm(mm)) {
816 int bd_entry_size = 8; /* 64-bit pointer */
817 /*
818 * Take the 64-bit addressing hole in to account.
819 */
820 addr &= ((1UL << boot_cpu_data.x86_virt_bits) - 1);
821 return (addr / bd_entry_virt_space(mm)) * bd_entry_size;
822 } else {
823 int bd_entry_size = 4; /* 32-bit pointer */
824 /*
825 * 32-bit has no hole so this case needs no mask
826 */
827 return (addr / bd_entry_virt_space(mm)) * bd_entry_size;
828 }
829 /*
830 * The two return calls above are exact copies. If we
831 * pull out a single copy and put it in here, gcc won't
832 * realize that we're doing a power-of-2 divide and use
833 * shifts. It uses a real divide. If we put them up
834 * there, it manages to figure it out (gcc 4.8.3).
835 */
Dave Hansen1de4fa12014-11-14 07:18:31 -0800836}
837
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700838static int unmap_entire_bt(struct mm_struct *mm,
839 long __user *bd_entry, unsigned long bt_addr)
Dave Hansen1de4fa12014-11-14 07:18:31 -0800840{
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700841 unsigned long expected_old_val = bt_addr | MPX_BD_ENTRY_VALID_FLAG;
842 unsigned long uninitialized_var(actual_old_val);
Dave Hansen1de4fa12014-11-14 07:18:31 -0800843 int ret;
844
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700845 while (1) {
846 int need_write = 1;
847 unsigned long cleared_bd_entry = 0;
848
849 pagefault_disable();
850 ret = mpx_cmpxchg_bd_entry(mm, &actual_old_val,
851 bd_entry, expected_old_val, cleared_bd_entry);
852 pagefault_enable();
853 if (!ret)
854 break;
855 if (ret == -EFAULT)
856 ret = mpx_resolve_fault(bd_entry, need_write);
857 /*
858 * If we could not resolve the fault, consider it
859 * userspace's fault and error out.
860 */
861 if (ret)
862 return ret;
863 }
Dave Hansen1de4fa12014-11-14 07:18:31 -0800864 /*
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700865 * The cmpxchg was performed, check the results.
Dave Hansen1de4fa12014-11-14 07:18:31 -0800866 */
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700867 if (actual_old_val != expected_old_val) {
868 /*
869 * Someone else raced with us to unmap the table.
870 * That is OK, since we were both trying to do
871 * the same thing. Declare success.
872 */
873 if (!actual_old_val)
874 return 0;
875 /*
876 * Something messed with the bounds directory
877 * entry. We hold mmap_sem for read or write
878 * here, so it could not be a _new_ bounds table
879 * that someone just allocated. Something is
880 * wrong, so pass up the error and SIGSEGV.
881 */
882 return -EINVAL;
883 }
884 /*
885 * Note, we are likely being called under do_munmap() already. To
886 * avoid recursion, do_munmap() will check whether it comes
887 * from one bounds table through VM_MPX flag.
888 */
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800889 return do_munmap(mm, bt_addr, mpx_bt_size_bytes(mm), NULL);
Dave Hansen1de4fa12014-11-14 07:18:31 -0800890}
891
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700892static int try_unmap_single_bt(struct mm_struct *mm,
893 unsigned long start, unsigned long end)
Dave Hansen1de4fa12014-11-14 07:18:31 -0800894{
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700895 struct vm_area_struct *next;
896 struct vm_area_struct *prev;
Dave Hansen1de4fa12014-11-14 07:18:31 -0800897 /*
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700898 * "bta" == Bounds Table Area: the area controlled by the
899 * bounds table that we are unmapping.
900 */
901 unsigned long bta_start_vaddr = start & ~(bd_entry_virt_space(mm)-1);
902 unsigned long bta_end_vaddr = bta_start_vaddr + bd_entry_virt_space(mm);
903 unsigned long uninitialized_var(bt_addr);
904 void __user *bde_vaddr;
905 int ret;
906 /*
Dave Hansenbea03c52015-06-07 11:37:06 -0700907 * We already unlinked the VMAs from the mm's rbtree so 'start'
908 * is guaranteed to be in a hole. This gets us the first VMA
909 * before the hole in to 'prev' and the next VMA after the hole
910 * in to 'next'.
911 */
912 next = find_vma_prev(mm, start, &prev);
913 /*
914 * Do not count other MPX bounds table VMAs as neighbors.
915 * Although theoretically possible, we do not allow bounds
916 * tables for bounds tables so our heads do not explode.
917 * If we count them as neighbors here, we may end up with
918 * lots of tables even though we have no actual table
919 * entries in use.
920 */
Kirill A. Shutemova89652762015-07-20 14:29:58 -0700921 while (next && (next->vm_flags & VM_MPX))
Dave Hansenbea03c52015-06-07 11:37:06 -0700922 next = next->vm_next;
Kirill A. Shutemova89652762015-07-20 14:29:58 -0700923 while (prev && (prev->vm_flags & VM_MPX))
Dave Hansenbea03c52015-06-07 11:37:06 -0700924 prev = prev->vm_prev;
925 /*
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700926 * We know 'start' and 'end' lie within an area controlled
927 * by a single bounds table. See if there are any other
928 * VMAs controlled by that bounds table. If there are not
929 * then we can "expand" the are we are unmapping to possibly
930 * cover the entire table.
Dave Hansen1de4fa12014-11-14 07:18:31 -0800931 */
932 next = find_vma_prev(mm, start, &prev);
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700933 if ((!prev || prev->vm_end <= bta_start_vaddr) &&
934 (!next || next->vm_start >= bta_end_vaddr)) {
935 /*
936 * No neighbor VMAs controlled by same bounds
937 * table. Try to unmap the whole thing
938 */
939 start = bta_start_vaddr;
940 end = bta_end_vaddr;
Dave Hansen1de4fa12014-11-14 07:18:31 -0800941 }
942
Mark Rutlandcb02de92016-12-16 12:40:55 +0000943 bde_vaddr = mm->context.bd_addr + mpx_get_bd_entry_offset(mm, start);
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700944 ret = get_bt_addr(mm, bde_vaddr, &bt_addr);
Dave Hansen1de4fa12014-11-14 07:18:31 -0800945 /*
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700946 * No bounds table there, so nothing to unmap.
Dave Hansen1de4fa12014-11-14 07:18:31 -0800947 */
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700948 if (ret == -ENOENT) {
949 ret = 0;
950 return 0;
951 }
Dave Hansen1de4fa12014-11-14 07:18:31 -0800952 if (ret)
953 return ret;
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700954 /*
955 * We are unmapping an entire table. Either because the
956 * unmap that started this whole process was large enough
957 * to cover an entire table, or that the unmap was small
958 * but was the area covered by a bounds table.
959 */
960 if ((start == bta_start_vaddr) &&
961 (end == bta_end_vaddr))
962 return unmap_entire_bt(mm, bde_vaddr, bt_addr);
963 return zap_bt_entries_mapping(mm, bt_addr, start, end);
Dave Hansen1de4fa12014-11-14 07:18:31 -0800964}
965
966static int mpx_unmap_tables(struct mm_struct *mm,
967 unsigned long start, unsigned long end)
968{
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700969 unsigned long one_unmap_start;
Dave Hansen2a1dcb12015-06-07 11:37:03 -0700970 trace_mpx_unmap_search(start, end);
Dave Hansen1de4fa12014-11-14 07:18:31 -0800971
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700972 one_unmap_start = start;
973 while (one_unmap_start < end) {
974 int ret;
975 unsigned long next_unmap_start = ALIGN(one_unmap_start+1,
976 bd_entry_virt_space(mm));
977 unsigned long one_unmap_end = end;
978 /*
979 * if the end is beyond the current bounds table,
980 * move it back so we only deal with a single one
981 * at a time
982 */
983 if (one_unmap_end > next_unmap_start)
984 one_unmap_end = next_unmap_start;
985 ret = try_unmap_single_bt(mm, one_unmap_start, one_unmap_end);
Dave Hansen1de4fa12014-11-14 07:18:31 -0800986 if (ret)
987 return ret;
Dave Hansen1de4fa12014-11-14 07:18:31 -0800988
Dave Hansen3ceaccd2015-06-07 11:37:06 -0700989 one_unmap_start = next_unmap_start;
990 }
Dave Hansen1de4fa12014-11-14 07:18:31 -0800991 return 0;
992}
993
994/*
995 * Free unused bounds tables covered in a virtual address region being
996 * munmap()ed. Assume end > start.
997 *
998 * This function will be called by do_munmap(), and the VMAs covering
999 * the virtual address region start...end have already been split if
1000 * necessary, and the 'vma' is the first vma in this range (start -> end).
1001 */
1002void mpx_notify_unmap(struct mm_struct *mm, struct vm_area_struct *vma,
1003 unsigned long start, unsigned long end)
1004{
1005 int ret;
1006
1007 /*
1008 * Refuse to do anything unless userspace has asked
1009 * the kernel to help manage the bounds tables,
1010 */
1011 if (!kernel_managing_mpx_tables(current->mm))
1012 return;
1013 /*
1014 * This will look across the entire 'start -> end' range,
1015 * and find all of the non-VM_MPX VMAs.
1016 *
1017 * To avoid recursion, if a VM_MPX vma is found in the range
1018 * (start->end), we will not continue follow-up work. This
1019 * recursion represents having bounds tables for bounds tables,
1020 * which should not occur normally. Being strict about it here
1021 * helps ensure that we do not have an exploitable stack overflow.
1022 */
1023 do {
1024 if (vma->vm_flags & VM_MPX)
1025 return;
1026 vma = vma->vm_next;
1027 } while (vma && vma->vm_start < end);
1028
1029 ret = mpx_unmap_tables(mm, start, end);
1030 if (ret)
1031 force_sig(SIGSEGV, current);
1032}