Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Intel Memory Protection Keys management |
| 3 | * Copyright (c) 2015, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | */ |
Dave Hansen | 76de993 | 2016-07-29 09:30:23 -0700 | [diff] [blame] | 14 | #include <linux/debugfs.h> /* debugfs_create_u32() */ |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 15 | #include <linux/mm_types.h> /* mm_struct, vma, etc... */ |
| 16 | #include <linux/pkeys.h> /* PKEY_* */ |
| 17 | #include <uapi/asm-generic/mman-common.h> |
| 18 | |
| 19 | #include <asm/cpufeature.h> /* boot_cpu_has, ... */ |
| 20 | #include <asm/mmu_context.h> /* vma_pkey() */ |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 21 | |
| 22 | int __execute_only_pkey(struct mm_struct *mm) |
| 23 | { |
Dave Hansen | e8c24d3 | 2016-07-29 09:30:15 -0700 | [diff] [blame] | 24 | bool need_to_set_mm_pkey = false; |
| 25 | int execute_only_pkey = mm->context.execute_only_pkey; |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 26 | int ret; |
| 27 | |
Dave Hansen | e8c24d3 | 2016-07-29 09:30:15 -0700 | [diff] [blame] | 28 | /* Do we need to assign a pkey for mm's execute-only maps? */ |
| 29 | if (execute_only_pkey == -1) { |
| 30 | /* Go allocate one to use, which might fail */ |
| 31 | execute_only_pkey = mm_pkey_alloc(mm); |
| 32 | if (execute_only_pkey < 0) |
| 33 | return -1; |
| 34 | need_to_set_mm_pkey = true; |
| 35 | } |
| 36 | |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 37 | /* |
| 38 | * We do not want to go through the relatively costly |
| 39 | * dance to set PKRU if we do not need to. Check it |
| 40 | * first and assume that if the execute-only pkey is |
| 41 | * write-disabled that we do not have to set it |
Sebastian Andrzej Siewior | 2722146 | 2019-04-03 18:41:36 +0200 | [diff] [blame^] | 42 | * ourselves. |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 43 | */ |
Dave Hansen | e8c24d3 | 2016-07-29 09:30:15 -0700 | [diff] [blame] | 44 | if (!need_to_set_mm_pkey && |
Dave Hansen | e8c24d3 | 2016-07-29 09:30:15 -0700 | [diff] [blame] | 45 | !__pkru_allows_read(read_pkru(), execute_only_pkey)) { |
Dave Hansen | e8c24d3 | 2016-07-29 09:30:15 -0700 | [diff] [blame] | 46 | return execute_only_pkey; |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 47 | } |
Dave Hansen | e8c24d3 | 2016-07-29 09:30:15 -0700 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Set up PKRU so that it denies access for everything |
| 51 | * other than execution. |
| 52 | */ |
| 53 | ret = arch_set_user_pkey_access(current, execute_only_pkey, |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 54 | PKEY_DISABLE_ACCESS); |
| 55 | /* |
| 56 | * If the PKRU-set operation failed somehow, just return |
| 57 | * 0 and effectively disable execute-only support. |
| 58 | */ |
Dave Hansen | e8c24d3 | 2016-07-29 09:30:15 -0700 | [diff] [blame] | 59 | if (ret) { |
| 60 | mm_set_pkey_free(mm, execute_only_pkey); |
| 61 | return -1; |
| 62 | } |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 63 | |
Dave Hansen | e8c24d3 | 2016-07-29 09:30:15 -0700 | [diff] [blame] | 64 | /* We got one, store it and use it from here on out */ |
| 65 | if (need_to_set_mm_pkey) |
| 66 | mm->context.execute_only_pkey = execute_only_pkey; |
| 67 | return execute_only_pkey; |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma) |
| 71 | { |
| 72 | /* Do this check first since the vm_flags should be hot */ |
| 73 | if ((vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) != VM_EXEC) |
| 74 | return false; |
Dave Hansen | e8c24d3 | 2016-07-29 09:30:15 -0700 | [diff] [blame] | 75 | if (vma_pkey(vma) != vma->vm_mm->context.execute_only_pkey) |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 76 | return false; |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * This is only called for *plain* mprotect calls. |
| 83 | */ |
| 84 | int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot, int pkey) |
| 85 | { |
| 86 | /* |
| 87 | * Is this an mprotect_pkey() call? If so, never |
| 88 | * override the value that came from the user. |
| 89 | */ |
| 90 | if (pkey != -1) |
| 91 | return pkey; |
Dave Hansen | 0a0b152 | 2018-05-09 10:13:51 -0700 | [diff] [blame] | 92 | |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 93 | /* |
| 94 | * The mapping is execute-only. Go try to get the |
| 95 | * execute-only protection key. If we fail to do that, |
| 96 | * fall through as if we do not have execute-only |
Dave Hansen | 0a0b152 | 2018-05-09 10:13:51 -0700 | [diff] [blame] | 97 | * support in this mm. |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 98 | */ |
| 99 | if (prot == PROT_EXEC) { |
| 100 | pkey = execute_only_pkey(vma->vm_mm); |
| 101 | if (pkey > 0) |
| 102 | return pkey; |
Dave Hansen | 0a0b152 | 2018-05-09 10:13:51 -0700 | [diff] [blame] | 103 | } else if (vma_is_pkey_exec_only(vma)) { |
| 104 | /* |
| 105 | * Protections are *not* PROT_EXEC, but the mapping |
| 106 | * is using the exec-only pkey. This mapping was |
| 107 | * PROT_EXEC and will no longer be. Move back to |
| 108 | * the default pkey. |
| 109 | */ |
| 110 | return ARCH_DEFAULT_PKEY; |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 111 | } |
Dave Hansen | 0a0b152 | 2018-05-09 10:13:51 -0700 | [diff] [blame] | 112 | |
Dave Hansen | 62b5f7d | 2016-02-12 13:02:40 -0800 | [diff] [blame] | 113 | /* |
| 114 | * This is a vanilla, non-pkey mprotect (or we failed to |
| 115 | * setup execute-only), inherit the pkey from the VMA we |
| 116 | * are working on. |
| 117 | */ |
| 118 | return vma_pkey(vma); |
| 119 | } |
Dave Hansen | acd547b | 2016-07-29 09:30:21 -0700 | [diff] [blame] | 120 | |
| 121 | #define PKRU_AD_KEY(pkey) (PKRU_AD_BIT << ((pkey) * PKRU_BITS_PER_PKEY)) |
| 122 | |
| 123 | /* |
| 124 | * Make the default PKRU value (at execve() time) as restrictive |
| 125 | * as possible. This ensures that any threads clone()'d early |
| 126 | * in the process's lifetime will not accidentally get access |
| 127 | * to data which is pkey-protected later on. |
| 128 | */ |
Sebastian Andrzej Siewior | 1a4226e | 2018-11-28 23:20:10 +0100 | [diff] [blame] | 129 | static |
Dave Hansen | acd547b | 2016-07-29 09:30:21 -0700 | [diff] [blame] | 130 | u32 init_pkru_value = PKRU_AD_KEY( 1) | PKRU_AD_KEY( 2) | PKRU_AD_KEY( 3) | |
| 131 | PKRU_AD_KEY( 4) | PKRU_AD_KEY( 5) | PKRU_AD_KEY( 6) | |
| 132 | PKRU_AD_KEY( 7) | PKRU_AD_KEY( 8) | PKRU_AD_KEY( 9) | |
| 133 | PKRU_AD_KEY(10) | PKRU_AD_KEY(11) | PKRU_AD_KEY(12) | |
| 134 | PKRU_AD_KEY(13) | PKRU_AD_KEY(14) | PKRU_AD_KEY(15); |
| 135 | |
| 136 | /* |
| 137 | * Called from the FPU code when creating a fresh set of FPU |
| 138 | * registers. This is called from a very specific context where |
| 139 | * we know the FPU regstiers are safe for use and we can use PKRU |
Andy Lutomirski | e636508 | 2016-10-17 14:40:11 -0700 | [diff] [blame] | 140 | * directly. |
Dave Hansen | acd547b | 2016-07-29 09:30:21 -0700 | [diff] [blame] | 141 | */ |
| 142 | void copy_init_pkru_to_fpregs(void) |
| 143 | { |
| 144 | u32 init_pkru_value_snapshot = READ_ONCE(init_pkru_value); |
| 145 | /* |
| 146 | * Any write to PKRU takes it out of the XSAVE 'init |
| 147 | * state' which increases context switch cost. Avoid |
| 148 | * writing 0 when PKRU was already 0. |
| 149 | */ |
| 150 | if (!init_pkru_value_snapshot && !read_pkru()) |
| 151 | return; |
| 152 | /* |
| 153 | * Override the PKRU state that came from 'init_fpstate' |
| 154 | * with the baseline from the process. |
| 155 | */ |
| 156 | write_pkru(init_pkru_value_snapshot); |
| 157 | } |
Dave Hansen | 76de993 | 2016-07-29 09:30:23 -0700 | [diff] [blame] | 158 | |
| 159 | static ssize_t init_pkru_read_file(struct file *file, char __user *user_buf, |
| 160 | size_t count, loff_t *ppos) |
| 161 | { |
| 162 | char buf[32]; |
| 163 | unsigned int len; |
| 164 | |
| 165 | len = sprintf(buf, "0x%x\n", init_pkru_value); |
| 166 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 167 | } |
| 168 | |
| 169 | static ssize_t init_pkru_write_file(struct file *file, |
| 170 | const char __user *user_buf, size_t count, loff_t *ppos) |
| 171 | { |
| 172 | char buf[32]; |
| 173 | ssize_t len; |
| 174 | u32 new_init_pkru; |
| 175 | |
| 176 | len = min(count, sizeof(buf) - 1); |
| 177 | if (copy_from_user(buf, user_buf, len)) |
| 178 | return -EFAULT; |
| 179 | |
| 180 | /* Make the buffer a valid string that we can not overrun */ |
| 181 | buf[len] = '\0'; |
| 182 | if (kstrtouint(buf, 0, &new_init_pkru)) |
| 183 | return -EINVAL; |
| 184 | |
| 185 | /* |
| 186 | * Don't allow insane settings that will blow the system |
| 187 | * up immediately if someone attempts to disable access |
| 188 | * or writes to pkey 0. |
| 189 | */ |
| 190 | if (new_init_pkru & (PKRU_AD_BIT|PKRU_WD_BIT)) |
| 191 | return -EINVAL; |
| 192 | |
| 193 | WRITE_ONCE(init_pkru_value, new_init_pkru); |
| 194 | return count; |
| 195 | } |
| 196 | |
| 197 | static const struct file_operations fops_init_pkru = { |
| 198 | .read = init_pkru_read_file, |
| 199 | .write = init_pkru_write_file, |
| 200 | .llseek = default_llseek, |
| 201 | }; |
| 202 | |
| 203 | static int __init create_init_pkru_value(void) |
| 204 | { |
| 205 | debugfs_create_file("init_pkru", S_IRUSR | S_IWUSR, |
| 206 | arch_debugfs_dir, NULL, &fops_init_pkru); |
| 207 | return 0; |
| 208 | } |
| 209 | late_initcall(create_init_pkru_value); |
| 210 | |
| 211 | static __init int setup_init_pkru(char *opt) |
| 212 | { |
| 213 | u32 new_init_pkru; |
| 214 | |
| 215 | if (kstrtouint(opt, 0, &new_init_pkru)) |
| 216 | return 1; |
| 217 | |
| 218 | WRITE_ONCE(init_pkru_value, new_init_pkru); |
| 219 | |
| 220 | return 1; |
| 221 | } |
| 222 | __setup("init_pkru=", setup_init_pkru); |