|
8 | 8 |
|
9 | 9 | #include <linux/sched.h>
|
10 | 10 | #include <linux/bitops.h>
|
| 11 | +#include <linux/types.h> |
| 12 | +#include <linux/mm.h> |
| 13 | +#include <linux/mman.h> |
| 14 | +#include <linux/slab.h> |
| 15 | +#include <linux/uaccess.h> |
| 16 | +#include <linux/sched/signal.h> |
| 17 | +#include <linux/compat.h> |
| 18 | +#include <linux/sizes.h> |
| 19 | +#include <linux/user.h> |
| 20 | +#include <asm/msr.h> |
| 21 | +#include <asm/fpu/xstate.h> |
| 22 | +#include <asm/fpu/types.h> |
| 23 | +#include <asm/shstk.h> |
| 24 | +#include <asm/special_insns.h> |
| 25 | +#include <asm/fpu/api.h> |
11 | 26 | #include <asm/prctl.h>
|
12 | 27 |
|
| 28 | +static bool features_enabled(unsigned long features) |
| 29 | +{ |
| 30 | + return current->thread.features & features; |
| 31 | +} |
| 32 | + |
| 33 | +static void features_set(unsigned long features) |
| 34 | +{ |
| 35 | + current->thread.features |= features; |
| 36 | +} |
| 37 | + |
| 38 | +static void features_clr(unsigned long features) |
| 39 | +{ |
| 40 | + current->thread.features &= ~features; |
| 41 | +} |
| 42 | + |
| 43 | +static unsigned long alloc_shstk(unsigned long size) |
| 44 | +{ |
| 45 | + int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_ABOVE4G; |
| 46 | + struct mm_struct *mm = current->mm; |
| 47 | + unsigned long addr, unused; |
| 48 | + |
| 49 | + mmap_write_lock(mm); |
| 50 | + addr = do_mmap(NULL, addr, size, PROT_READ, flags, |
| 51 | + VM_SHADOW_STACK | VM_WRITE, 0, &unused, NULL); |
| 52 | + |
| 53 | + mmap_write_unlock(mm); |
| 54 | + |
| 55 | + return addr; |
| 56 | +} |
| 57 | + |
| 58 | +static unsigned long adjust_shstk_size(unsigned long size) |
| 59 | +{ |
| 60 | + if (size) |
| 61 | + return PAGE_ALIGN(size); |
| 62 | + |
| 63 | + return PAGE_ALIGN(min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G)); |
| 64 | +} |
| 65 | + |
| 66 | +static void unmap_shadow_stack(u64 base, u64 size) |
| 67 | +{ |
| 68 | + while (1) { |
| 69 | + int r; |
| 70 | + |
| 71 | + r = vm_munmap(base, size); |
| 72 | + |
| 73 | + /* |
| 74 | + * vm_munmap() returns -EINTR when mmap_lock is held by |
| 75 | + * something else, and that lock should not be held for a |
| 76 | + * long time. Retry it for the case. |
| 77 | + */ |
| 78 | + if (r == -EINTR) { |
| 79 | + cond_resched(); |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + /* |
| 84 | + * For all other types of vm_munmap() failure, either the |
| 85 | + * system is out of memory or there is bug. |
| 86 | + */ |
| 87 | + WARN_ON_ONCE(r); |
| 88 | + break; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +static int shstk_setup(void) |
| 93 | +{ |
| 94 | + struct thread_shstk *shstk = ¤t->thread.shstk; |
| 95 | + unsigned long addr, size; |
| 96 | + |
| 97 | + /* Already enabled */ |
| 98 | + if (features_enabled(ARCH_SHSTK_SHSTK)) |
| 99 | + return 0; |
| 100 | + |
| 101 | + /* Also not supported for 32 bit and x32 */ |
| 102 | + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK) || in_32bit_syscall()) |
| 103 | + return -EOPNOTSUPP; |
| 104 | + |
| 105 | + size = adjust_shstk_size(0); |
| 106 | + addr = alloc_shstk(size); |
| 107 | + if (IS_ERR_VALUE(addr)) |
| 108 | + return PTR_ERR((void *)addr); |
| 109 | + |
| 110 | + fpregs_lock_and_load(); |
| 111 | + wrmsrl(MSR_IA32_PL3_SSP, addr + size); |
| 112 | + wrmsrl(MSR_IA32_U_CET, CET_SHSTK_EN); |
| 113 | + fpregs_unlock(); |
| 114 | + |
| 115 | + shstk->base = addr; |
| 116 | + shstk->size = size; |
| 117 | + features_set(ARCH_SHSTK_SHSTK); |
| 118 | + |
| 119 | + return 0; |
| 120 | +} |
| 121 | + |
13 | 122 | void reset_thread_features(void)
|
14 | 123 | {
|
| 124 | + memset(¤t->thread.shstk, 0, sizeof(struct thread_shstk)); |
15 | 125 | current->thread.features = 0;
|
16 | 126 | current->thread.features_locked = 0;
|
17 | 127 | }
|
18 | 128 |
|
| 129 | +void shstk_free(struct task_struct *tsk) |
| 130 | +{ |
| 131 | + struct thread_shstk *shstk = &tsk->thread.shstk; |
| 132 | + |
| 133 | + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK) || |
| 134 | + !features_enabled(ARCH_SHSTK_SHSTK)) |
| 135 | + return; |
| 136 | + |
| 137 | + if (!tsk->mm) |
| 138 | + return; |
| 139 | + |
| 140 | + unmap_shadow_stack(shstk->base, shstk->size); |
| 141 | +} |
| 142 | + |
| 143 | +static int shstk_disable(void) |
| 144 | +{ |
| 145 | + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK)) |
| 146 | + return -EOPNOTSUPP; |
| 147 | + |
| 148 | + /* Already disabled? */ |
| 149 | + if (!features_enabled(ARCH_SHSTK_SHSTK)) |
| 150 | + return 0; |
| 151 | + |
| 152 | + fpregs_lock_and_load(); |
| 153 | + /* Disable WRSS too when disabling shadow stack */ |
| 154 | + wrmsrl(MSR_IA32_U_CET, 0); |
| 155 | + wrmsrl(MSR_IA32_PL3_SSP, 0); |
| 156 | + fpregs_unlock(); |
| 157 | + |
| 158 | + shstk_free(current); |
| 159 | + features_clr(ARCH_SHSTK_SHSTK); |
| 160 | + |
| 161 | + return 0; |
| 162 | +} |
| 163 | + |
19 | 164 | long shstk_prctl(struct task_struct *task, int option, unsigned long features)
|
20 | 165 | {
|
21 | 166 | if (option == ARCH_SHSTK_LOCK) {
|
|
0 commit comments