Skip to content

Commit 506496b

Browse files
brooniectmarinas
authored andcommitted
arm64/gcs: Ensure that new threads have a GCS
When a new thread is created by a thread with GCS enabled the GCS needs to be specified along with the regular stack. Unfortunately plain clone() is not extensible and existing clone3() users will not specify a stack so all existing code would be broken if we mandated specifying the stack explicitly. For compatibility with these cases and also x86 (which did not initially implement clone3() support for shadow stacks) if no GCS is specified we will allocate one so when a thread is created which has GCS enabled allocate one for it. We follow the extensively discussed x86 implementation and allocate min(RLIMIT_STACK/2, 2G). Since the GCS only stores the call stack and not any variables this should be more than sufficient for most applications. GCSs allocated via this mechanism will be freed when the thread exits. Reviewed-by: Thiago Jung Bauermann <[email protected]> Acked-by: Yury Khrustalev <[email protected]> Signed-off-by: Mark Brown <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent fc84bc5 commit 506496b

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

arch/arm64/include/asm/gcs.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <asm/types.h>
99
#include <asm/uaccess.h>
1010

11+
struct kernel_clone_args;
12+
1113
static inline void gcsb_dsync(void)
1214
{
1315
asm volatile(".inst 0xd503227f" : : : "memory");
@@ -58,6 +60,8 @@ static inline bool task_gcs_el0_enabled(struct task_struct *task)
5860
void gcs_set_el0_mode(struct task_struct *task);
5961
void gcs_free(struct task_struct *task);
6062
void gcs_preserve_current_state(void);
63+
unsigned long gcs_alloc_thread_stack(struct task_struct *tsk,
64+
const struct kernel_clone_args *args);
6165

6266
#else
6367

@@ -69,6 +73,11 @@ static inline bool task_gcs_el0_enabled(struct task_struct *task)
6973
static inline void gcs_set_el0_mode(struct task_struct *task) { }
7074
static inline void gcs_free(struct task_struct *task) { }
7175
static inline void gcs_preserve_current_state(void) { }
76+
static inline unsigned long gcs_alloc_thread_stack(struct task_struct *tsk,
77+
const struct kernel_clone_args *args)
78+
{
79+
return -ENOTSUPP;
80+
}
7281

7382
#endif
7483

arch/arm64/include/asm/mmu_context.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <asm/cacheflush.h>
2121
#include <asm/cpufeature.h>
2222
#include <asm/daifflags.h>
23+
#include <asm/gcs.h>
2324
#include <asm/proc-fns.h>
2425
#include <asm/cputype.h>
2526
#include <asm/sysreg.h>
@@ -311,6 +312,14 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
311312
return por_el0_allows_pkey(vma_pkey(vma), write, execute);
312313
}
313314

315+
#define deactivate_mm deactivate_mm
316+
static inline void deactivate_mm(struct task_struct *tsk,
317+
struct mm_struct *mm)
318+
{
319+
gcs_free(tsk);
320+
}
321+
322+
314323
#include <asm-generic/mmu_context.h>
315324

316325
#endif /* !__ASSEMBLY__ */

arch/arm64/kernel/process.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,35 @@ static void flush_gcs(void)
294294
write_sysreg_s(0, SYS_GCSPR_EL0);
295295
}
296296

297+
static int copy_thread_gcs(struct task_struct *p,
298+
const struct kernel_clone_args *args)
299+
{
300+
unsigned long gcs;
301+
302+
if (!system_supports_gcs())
303+
return 0;
304+
305+
p->thread.gcs_base = 0;
306+
p->thread.gcs_size = 0;
307+
308+
gcs = gcs_alloc_thread_stack(p, args);
309+
if (IS_ERR_VALUE(gcs))
310+
return PTR_ERR((void *)gcs);
311+
312+
p->thread.gcs_el0_mode = current->thread.gcs_el0_mode;
313+
p->thread.gcs_el0_locked = current->thread.gcs_el0_locked;
314+
315+
return 0;
316+
}
317+
297318
#else
298319

299320
static void flush_gcs(void) { }
321+
static int copy_thread_gcs(struct task_struct *p,
322+
const struct kernel_clone_args *args)
323+
{
324+
return 0;
325+
}
300326

301327
#endif
302328

@@ -313,6 +339,7 @@ void flush_thread(void)
313339
void arch_release_task_struct(struct task_struct *tsk)
314340
{
315341
fpsimd_release_task(tsk);
342+
gcs_free(tsk);
316343
}
317344

318345
int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
@@ -376,6 +403,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
376403
unsigned long stack_start = args->stack;
377404
unsigned long tls = args->tls;
378405
struct pt_regs *childregs = task_pt_regs(p);
406+
int ret;
379407

380408
memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context));
381409

@@ -420,6 +448,10 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
420448
p->thread.uw.tp_value = tls;
421449
p->thread.tpidr2_el0 = 0;
422450
}
451+
452+
ret = copy_thread_gcs(p, args);
453+
if (ret != 0)
454+
return ret;
423455
} else {
424456
/*
425457
* A kthread has no context to ERET to, so ensure any buggy

arch/arm64/mm/gcs.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,69 @@
55
#include <linux/syscalls.h>
66
#include <linux/types.h>
77

8+
#include <asm/cmpxchg.h>
89
#include <asm/cpufeature.h>
10+
#include <asm/gcs.h>
911
#include <asm/page.h>
1012

13+
static unsigned long alloc_gcs(unsigned long addr, unsigned long size)
14+
{
15+
int flags = MAP_ANONYMOUS | MAP_PRIVATE;
16+
struct mm_struct *mm = current->mm;
17+
unsigned long mapped_addr, unused;
18+
19+
if (addr)
20+
flags |= MAP_FIXED_NOREPLACE;
21+
22+
mmap_write_lock(mm);
23+
mapped_addr = do_mmap(NULL, addr, size, PROT_READ, flags,
24+
VM_SHADOW_STACK | VM_WRITE, 0, &unused, NULL);
25+
mmap_write_unlock(mm);
26+
27+
return mapped_addr;
28+
}
29+
30+
static unsigned long gcs_size(unsigned long size)
31+
{
32+
if (size)
33+
return PAGE_ALIGN(size);
34+
35+
/* Allocate RLIMIT_STACK/2 with limits of PAGE_SIZE..2G */
36+
size = PAGE_ALIGN(min_t(unsigned long long,
37+
rlimit(RLIMIT_STACK) / 2, SZ_2G));
38+
return max(PAGE_SIZE, size);
39+
}
40+
41+
unsigned long gcs_alloc_thread_stack(struct task_struct *tsk,
42+
const struct kernel_clone_args *args)
43+
{
44+
unsigned long addr, size;
45+
46+
if (!system_supports_gcs())
47+
return 0;
48+
49+
if (!task_gcs_el0_enabled(tsk))
50+
return 0;
51+
52+
if ((args->flags & (CLONE_VFORK | CLONE_VM)) != CLONE_VM) {
53+
tsk->thread.gcspr_el0 = read_sysreg_s(SYS_GCSPR_EL0);
54+
return 0;
55+
}
56+
57+
size = args->stack_size / 2;
58+
59+
size = gcs_size(size);
60+
addr = alloc_gcs(0, size);
61+
if (IS_ERR_VALUE(addr))
62+
return addr;
63+
64+
tsk->thread.gcs_base = addr;
65+
tsk->thread.gcs_size = size;
66+
tsk->thread.gcspr_el0 = addr + size - sizeof(u64);
67+
68+
return addr;
69+
}
70+
1171
/*
1272
* Apply the GCS mode configured for the specified task to the
1373
* hardware.
@@ -33,6 +93,15 @@ void gcs_free(struct task_struct *task)
3393
if (!system_supports_gcs())
3494
return;
3595

96+
/*
97+
* When fork() with CLONE_VM fails, the child (tsk) already
98+
* has a GCS allocated, and exit_thread() calls this function
99+
* to free it. In this case the parent (current) and the
100+
* child share the same mm struct.
101+
*/
102+
if (!task->mm || task->mm != current->mm)
103+
return;
104+
36105
if (task->thread.gcs_base)
37106
vm_munmap(task->thread.gcs_base, task->thread.gcs_size);
38107

0 commit comments

Comments
 (0)