Skip to content

Commit 8f3e750

Browse files
brooniectmarinas
authored andcommitted
arm64/mm: Implement map_shadow_stack()
As discussed extensively in the changelog for the addition of this syscall on x86 ("x86/shstk: Introduce map_shadow_stack syscall") the existing mmap() and madvise() syscalls do not map entirely well onto the security requirements for guarded control stacks since they lead to windows where memory is allocated but not yet protected or stacks which are not properly and safely initialised. Instead a new syscall map_shadow_stack() has been defined which allocates and initialises a shadow stack page. Implement this for arm64. Two flags are provided, allowing applications to request that the stack be initialised with a valid cap token at the top of the stack and optionally also an end of stack marker above that. We support requesting an end of stack marker alone but since this is a NULL pointer it is indistinguishable from not initialising anything by itself. Reviewed-by: Thiago Jung Bauermann <[email protected]> Reviewed-by: Catalin Marinas <[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 b57180c commit 8f3e750

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

arch/arm64/mm/gcs.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,70 @@ unsigned long gcs_alloc_thread_stack(struct task_struct *tsk,
6868
return addr;
6969
}
7070

71+
SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsigned int, flags)
72+
{
73+
unsigned long alloc_size;
74+
unsigned long __user *cap_ptr;
75+
unsigned long cap_val;
76+
int ret = 0;
77+
int cap_offset;
78+
79+
if (!system_supports_gcs())
80+
return -EOPNOTSUPP;
81+
82+
if (flags & ~(SHADOW_STACK_SET_TOKEN | SHADOW_STACK_SET_MARKER))
83+
return -EINVAL;
84+
85+
if (!PAGE_ALIGNED(addr))
86+
return -EINVAL;
87+
88+
if (size == 8 || !IS_ALIGNED(size, 8))
89+
return -EINVAL;
90+
91+
/*
92+
* An overflow would result in attempting to write the restore token
93+
* to the wrong location. Not catastrophic, but just return the right
94+
* error code and block it.
95+
*/
96+
alloc_size = PAGE_ALIGN(size);
97+
if (alloc_size < size)
98+
return -EOVERFLOW;
99+
100+
addr = alloc_gcs(addr, alloc_size);
101+
if (IS_ERR_VALUE(addr))
102+
return addr;
103+
104+
/*
105+
* Put a cap token at the end of the allocated region so it
106+
* can be switched to.
107+
*/
108+
if (flags & SHADOW_STACK_SET_TOKEN) {
109+
/* Leave an extra empty frame as a top of stack marker? */
110+
if (flags & SHADOW_STACK_SET_MARKER)
111+
cap_offset = 2;
112+
else
113+
cap_offset = 1;
114+
115+
cap_ptr = (unsigned long __user *)(addr + size -
116+
(cap_offset * sizeof(unsigned long)));
117+
cap_val = GCS_CAP(cap_ptr);
118+
119+
put_user_gcs(cap_val, cap_ptr, &ret);
120+
if (ret != 0) {
121+
vm_munmap(addr, size);
122+
return -EFAULT;
123+
}
124+
125+
/*
126+
* Ensure the new cap is ordered before standard
127+
* memory accesses to the same location.
128+
*/
129+
gcsb_dsync();
130+
}
131+
132+
return addr;
133+
}
134+
71135
/*
72136
* Apply the GCS mode configured for the specified task to the
73137
* hardware.

0 commit comments

Comments
 (0)