Skip to content

Commit fb0baba

Browse files
dcpleungnashif
authored andcommitted
x86: initialize privileged stack during thread init
This adds the bits to initialize the privileged stack for each thread during thread initialization. This prevents information leaking if the thread stack is reused, and also aids in calculating stack space usage during system calls. Signed-off-by: Daniel Leung <[email protected]>
1 parent c25fa96 commit fb0baba

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

arch/x86/core/userspace.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,28 @@ void *z_x86_userspace_prepare_thread(struct k_thread *thread)
9393

9494
if ((thread->base.user_options & K_USER) != 0U) {
9595
initial_entry = arch_user_mode_enter;
96+
97+
#ifdef CONFIG_INIT_STACKS
98+
/* setup_thread_stack() does not initialize the architecture specific
99+
* privileged stack. So we need to do it manually here as this function
100+
* is called by arch_new_thread() via z_setup_new_thread() after
101+
* setup_thread_stack() but before thread starts running.
102+
*
103+
* Note that only user threads have privileged stacks and kernel
104+
* only threads do not.
105+
*
106+
* Also note that this needs to be done before calling
107+
* z_x86_userspace_enter() where it clears the user stack.
108+
* That function requires using the privileged stack for
109+
* code execution so we cannot clear that at the same time.
110+
*/
111+
struct z_x86_thread_stack_header *hdr_stack_obj =
112+
(struct z_x86_thread_stack_header *)thread->stack_obj;
113+
114+
(void)memset(&hdr_stack_obj->privilege_stack[0], 0xaa,
115+
sizeof(hdr_stack_obj->privilege_stack));
116+
#endif
117+
96118
} else {
97119
initial_entry = z_thread_entry;
98120
}

0 commit comments

Comments
 (0)