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