Skip to content

Commit b57180c

Browse files
brooniectmarinas
authored andcommitted
arm64/gcs: Implement shadow stack prctl() interface
Implement the architecture neutral prctl() interface for setting the shadow stack status, this supports setting and reading the current GCS configuration for the current thread. Userspace can enable basic GCS functionality and additionally also support for GCS pushes and arbitrary GCS stores. It is expected that this prctl() will be called very early in application startup, for example by the dynamic linker, and not subsequently adjusted during normal operation. Users should carefully note that after enabling GCS for a thread GCS will become active with no call stack so it is not normally possible to return from the function that invoked the prctl(). State is stored per thread, enabling GCS for a thread causes a GCS to be allocated for that thread. Userspace may lock the current GCS configuration by specifying PR_SHADOW_STACK_ENABLE_LOCK, this prevents any further changes to the GCS configuration via any means. If GCS is not being enabled then all flags other than _LOCK are ignored, it is not possible to enable stores or pops without enabling GCS. When disabling the GCS we do not free the allocated stack, this allows for inspection of the GCS after disabling as part of fault reporting. Since it is not an expected use case and since it presents some complications in determining what to do with previously initialsed data on the GCS attempts to reenable GCS after this are rejected. This can be revisted if a use case arises. Reviewed-by: Thiago Jung Bauermann <[email protected]> Reviewed-by: Catalin Marinas <[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 506496b commit b57180c

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

arch/arm64/include/asm/gcs.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ static inline u64 gcsss2(void)
5050
return Xt;
5151
}
5252

53+
#define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK \
54+
(PR_SHADOW_STACK_ENABLE | PR_SHADOW_STACK_WRITE | PR_SHADOW_STACK_PUSH)
55+
5356
#ifdef CONFIG_ARM64_GCS
5457

5558
static inline bool task_gcs_el0_enabled(struct task_struct *task)
@@ -63,6 +66,20 @@ void gcs_preserve_current_state(void);
6366
unsigned long gcs_alloc_thread_stack(struct task_struct *tsk,
6467
const struct kernel_clone_args *args);
6568

69+
static inline int gcs_check_locked(struct task_struct *task,
70+
unsigned long new_val)
71+
{
72+
unsigned long cur_val = task->thread.gcs_el0_mode;
73+
74+
cur_val &= task->thread.gcs_el0_locked;
75+
new_val &= task->thread.gcs_el0_locked;
76+
77+
if (cur_val != new_val)
78+
return -EBUSY;
79+
80+
return 0;
81+
}
82+
6683
#else
6784

6885
static inline bool task_gcs_el0_enabled(struct task_struct *task)
@@ -78,6 +95,11 @@ static inline unsigned long gcs_alloc_thread_stack(struct task_struct *tsk,
7895
{
7996
return -ENOTSUPP;
8097
}
98+
static inline int gcs_check_locked(struct task_struct *task,
99+
unsigned long new_val)
100+
{
101+
return 0;
102+
}
81103

82104
#endif
83105

arch/arm64/include/asm/processor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ struct thread_struct {
187187
u64 por_el0;
188188
#ifdef CONFIG_ARM64_GCS
189189
unsigned int gcs_el0_mode;
190+
unsigned int gcs_el0_locked;
190191
u64 gcspr_el0;
191192
u64 gcs_base;
192193
u64 gcs_size;

arch/arm64/mm/gcs.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,82 @@ void gcs_free(struct task_struct *task)
109109
task->thread.gcs_base = 0;
110110
task->thread.gcs_size = 0;
111111
}
112+
113+
int arch_set_shadow_stack_status(struct task_struct *task, unsigned long arg)
114+
{
115+
unsigned long gcs, size;
116+
int ret;
117+
118+
if (!system_supports_gcs())
119+
return -EINVAL;
120+
121+
if (is_compat_thread(task_thread_info(task)))
122+
return -EINVAL;
123+
124+
/* Reject unknown flags */
125+
if (arg & ~PR_SHADOW_STACK_SUPPORTED_STATUS_MASK)
126+
return -EINVAL;
127+
128+
ret = gcs_check_locked(task, arg);
129+
if (ret != 0)
130+
return ret;
131+
132+
/* If we are enabling GCS then make sure we have a stack */
133+
if (arg & PR_SHADOW_STACK_ENABLE &&
134+
!task_gcs_el0_enabled(task)) {
135+
/* Do not allow GCS to be reenabled */
136+
if (task->thread.gcs_base || task->thread.gcspr_el0)
137+
return -EINVAL;
138+
139+
if (task != current)
140+
return -EBUSY;
141+
142+
size = gcs_size(0);
143+
gcs = alloc_gcs(0, size);
144+
if (!gcs)
145+
return -ENOMEM;
146+
147+
task->thread.gcspr_el0 = gcs + size - sizeof(u64);
148+
task->thread.gcs_base = gcs;
149+
task->thread.gcs_size = size;
150+
if (task == current)
151+
write_sysreg_s(task->thread.gcspr_el0,
152+
SYS_GCSPR_EL0);
153+
}
154+
155+
task->thread.gcs_el0_mode = arg;
156+
if (task == current)
157+
gcs_set_el0_mode(task);
158+
159+
return 0;
160+
}
161+
162+
int arch_get_shadow_stack_status(struct task_struct *task,
163+
unsigned long __user *arg)
164+
{
165+
if (!system_supports_gcs())
166+
return -EINVAL;
167+
168+
if (is_compat_thread(task_thread_info(task)))
169+
return -EINVAL;
170+
171+
return put_user(task->thread.gcs_el0_mode, arg);
172+
}
173+
174+
int arch_lock_shadow_stack_status(struct task_struct *task,
175+
unsigned long arg)
176+
{
177+
if (!system_supports_gcs())
178+
return -EINVAL;
179+
180+
if (is_compat_thread(task_thread_info(task)))
181+
return -EINVAL;
182+
183+
/*
184+
* We support locking unknown bits so applications can prevent
185+
* any changes in a future proof manner.
186+
*/
187+
task->thread.gcs_el0_locked |= arg;
188+
189+
return 0;
190+
}

0 commit comments

Comments
 (0)