Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

riscv/shstk: If needed allocate a new shadow stack on clone

Userspace specifies CLONE_VM to share address space and spawn new
thread. 'clone' allows userspace to specify a new stack for a new
thread. However there is no way to specify a new shadow stack base
address without changing the API. This patch allocates a new shadow
stack whenever CLONE_VM is given.

In case of CLONE_VFORK, the parent is suspended until the child
finishes; thus the child can use the parent's shadow stack. In case of
!CLONE_VM, COW kicks in because entire address space is copied from
parent to child.

'clone3' is extensible and can provide mechanisms for specifying the
shadow stack as an input parameter. This is not settled yet and is
being extensively discussed on the mailing list. Once that's settled,
this code should be adapted.

Reviewed-by: Zong Li <zong.li@sifive.com>
Signed-off-by: Deepak Gupta <debug@rivosinc.com>
Tested-by: Andreas Korb <andreas.korb@aisec.fraunhofer.de> # QEMU, custom CVA6
Tested-by: Valentin Haudiquet <valentin.haudiquet@canonical.com>
Link: https://patch.msgid.link/20251112-v5_user_cfi_series-v23-11-b55691eacf4f@rivosinc.com
[pjw@kernel.org: cleaned up patch description]
Signed-off-by: Paul Walmsley <pjw@kernel.org>

authored by

Deepak Gupta and committed by
Paul Walmsley
fd44a4a8 c70772af

+162
+7
arch/riscv/include/asm/mmu_context.h
··· 48 48 } 49 49 #endif 50 50 51 + #define deactivate_mm deactivate_mm 52 + static inline void deactivate_mm(struct task_struct *tsk, 53 + struct mm_struct *mm) 54 + { 55 + shstk_release(tsk); 56 + } 57 + 51 58 #include <asm-generic/mmu_context.h> 52 59 53 60 #endif /* _ASM_RISCV_MMU_CONTEXT_H */
+25
arch/riscv/include/asm/usercfi.h
··· 8 8 #ifndef __ASSEMBLER__ 9 9 #include <linux/types.h> 10 10 11 + struct task_struct; 12 + struct kernel_clone_args; 13 + 11 14 #ifdef CONFIG_RISCV_USER_CFI 12 15 struct cfi_state { 13 16 unsigned long ubcfi_en : 1; /* Enable for backward cfi. */ ··· 18 15 unsigned long shdw_stk_base; /* Base address of shadow stack */ 19 16 unsigned long shdw_stk_size; /* size of shadow stack */ 20 17 }; 18 + 19 + unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, 20 + const struct kernel_clone_args *args); 21 + void shstk_release(struct task_struct *tsk); 22 + void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, unsigned long size); 23 + unsigned long get_shstk_base(struct task_struct *task, unsigned long *size); 24 + void set_active_shstk(struct task_struct *task, unsigned long shstk_addr); 25 + bool is_shstk_enabled(struct task_struct *task); 26 + 27 + #else 28 + 29 + #define shstk_alloc_thread_stack(tsk, args) 0 30 + 31 + #define shstk_release(tsk) 32 + 33 + #define get_shstk_base(task, size) 0UL 34 + 35 + #define set_shstk_base(task, shstk_addr, size) do {} while (0) 36 + 37 + #define set_active_shstk(task, shstk_addr) do {} while (0) 38 + 39 + #define is_shstk_enabled(task) false 21 40 22 41 #endif /* CONFIG_RISCV_USER_CFI */ 23 42
+10
arch/riscv/kernel/process.c
··· 31 31 #include <asm/vector.h> 32 32 #include <asm/cpufeature.h> 33 33 #include <asm/exec.h> 34 + #include <asm/usercfi.h> 34 35 35 36 #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK) 36 37 #include <linux/stackprotector.h> ··· 227 226 u64 clone_flags = args->flags; 228 227 unsigned long usp = args->stack; 229 228 unsigned long tls = args->tls; 229 + unsigned long ssp = 0; 230 230 struct pt_regs *childregs = task_pt_regs(p); 231 231 232 232 /* Ensure all threads in this mm have the same pointer masking mode. */ ··· 247 245 p->thread.s[1] = (unsigned long)args->fn_arg; 248 246 p->thread.ra = (unsigned long)ret_from_fork_kernel_asm; 249 247 } else { 248 + /* allocate new shadow stack if needed. In case of CLONE_VM we have to */ 249 + ssp = shstk_alloc_thread_stack(p, args); 250 + if (IS_ERR_VALUE(ssp)) 251 + return PTR_ERR((void *)ssp); 252 + 250 253 *childregs = *(current_pt_regs()); 251 254 /* Turn off status.VS */ 252 255 riscv_v_vstate_off(childregs); 253 256 if (usp) /* User fork */ 254 257 childregs->sp = usp; 258 + /* if needed, set new ssp */ 259 + if (ssp) 260 + set_active_shstk(p, ssp); 255 261 if (clone_flags & CLONE_SETTLS) 256 262 childregs->tp = tls; 257 263 childregs->a0 = 0; /* Return value of fork() */
+120
arch/riscv/kernel/usercfi.c
··· 19 19 20 20 #define SHSTK_ENTRY_SIZE sizeof(void *) 21 21 22 + bool is_shstk_enabled(struct task_struct *task) 23 + { 24 + return task->thread_info.user_cfi_state.ubcfi_en; 25 + } 26 + 27 + void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, unsigned long size) 28 + { 29 + task->thread_info.user_cfi_state.shdw_stk_base = shstk_addr; 30 + task->thread_info.user_cfi_state.shdw_stk_size = size; 31 + } 32 + 33 + unsigned long get_shstk_base(struct task_struct *task, unsigned long *size) 34 + { 35 + if (size) 36 + *size = task->thread_info.user_cfi_state.shdw_stk_size; 37 + return task->thread_info.user_cfi_state.shdw_stk_base; 38 + } 39 + 40 + void set_active_shstk(struct task_struct *task, unsigned long shstk_addr) 41 + { 42 + task->thread_info.user_cfi_state.user_shdw_stk = shstk_addr; 43 + } 44 + 45 + /* 46 + * If size is 0, then to be compatible with regular stack we want it to be as big as 47 + * regular stack. Else PAGE_ALIGN it and return back 48 + */ 49 + static unsigned long calc_shstk_size(unsigned long size) 50 + { 51 + if (size) 52 + return PAGE_ALIGN(size); 53 + 54 + return PAGE_ALIGN(min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G)); 55 + } 56 + 22 57 /* 23 58 * Writes on shadow stack can either be `sspush` or `ssamoswap`. `sspush` can happen 24 59 * implicitly on current shadow stack pointed to by CSR_SSP. `ssamoswap` takes pointer to ··· 172 137 return -EOVERFLOW; 173 138 174 139 return allocate_shadow_stack(addr, aligned_size, size, set_tok); 140 + } 141 + 142 + /* 143 + * This gets called during clone/clone3/fork. And is needed to allocate a shadow stack for 144 + * cases where CLONE_VM is specified and thus a different stack is specified by user. We 145 + * thus need a separate shadow stack too. How a separate shadow stack is specified by 146 + * user is still being debated. Once that's settled, remove this part of the comment. 147 + * This function simply returns 0 if shadow stacks are not supported or if separate shadow 148 + * stack allocation is not needed (like in case of !CLONE_VM) 149 + */ 150 + unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, 151 + const struct kernel_clone_args *args) 152 + { 153 + unsigned long addr, size; 154 + 155 + /* If shadow stack is not supported, return 0 */ 156 + if (!cpu_supports_shadow_stack()) 157 + return 0; 158 + 159 + /* 160 + * If shadow stack is not enabled on the new thread, skip any 161 + * switch to a new shadow stack. 162 + */ 163 + if (!is_shstk_enabled(tsk)) 164 + return 0; 165 + 166 + /* 167 + * For CLONE_VFORK the child will share the parents shadow stack. 168 + * Set base = 0 and size = 0, this is special means to track this state 169 + * so the freeing logic run for child knows to leave it alone. 170 + */ 171 + if (args->flags & CLONE_VFORK) { 172 + set_shstk_base(tsk, 0, 0); 173 + return 0; 174 + } 175 + 176 + /* 177 + * For !CLONE_VM the child will use a copy of the parents shadow 178 + * stack. 179 + */ 180 + if (!(args->flags & CLONE_VM)) 181 + return 0; 182 + 183 + /* 184 + * reaching here means, CLONE_VM was specified and thus a separate shadow 185 + * stack is needed for new cloned thread. Note: below allocation is happening 186 + * using current mm. 187 + */ 188 + size = calc_shstk_size(args->stack_size); 189 + addr = allocate_shadow_stack(0, size, 0, false); 190 + if (IS_ERR_VALUE(addr)) 191 + return addr; 192 + 193 + set_shstk_base(tsk, addr, size); 194 + 195 + return addr + size; 196 + } 197 + 198 + void shstk_release(struct task_struct *tsk) 199 + { 200 + unsigned long base = 0, size = 0; 201 + /* If shadow stack is not supported or not enabled, nothing to release */ 202 + if (!cpu_supports_shadow_stack() || !is_shstk_enabled(tsk)) 203 + return; 204 + 205 + /* 206 + * When fork() with CLONE_VM fails, the child (tsk) already has a 207 + * shadow stack allocated, and exit_thread() calls this function to 208 + * free it. In this case the parent (current) and the child share 209 + * the same mm struct. Move forward only when they're same. 210 + */ 211 + if (!tsk->mm || tsk->mm != current->mm) 212 + return; 213 + 214 + /* 215 + * We know shadow stack is enabled but if base is NULL, then 216 + * this task is not managing its own shadow stack (CLONE_VFORK). So 217 + * skip freeing it. 218 + */ 219 + base = get_shstk_base(tsk, &size); 220 + if (!base) 221 + return; 222 + 223 + vm_munmap(base, size); 224 + set_shstk_base(tsk, 0, 0); 175 225 }