Skip to content

Commit 3319556

Browse files
rpedgecohansendc
authored andcommitted
x86/shstk: Handle vfork clone failure correctly
Shadow stacks are allocated automatically and freed on exit, depending on the clone flags. The two cases where new shadow stacks are not allocated are !CLONE_VM (fork()) and CLONE_VFORK (vfork()). For !CLONE_VM, although a new stack is not allocated, it can be freed normally because it will happen in the child's copy of the VM. However, for CLONE_VFORK the parent and the child are actually using the same shadow stack. So the kernel doesn't need to allocate *or* free a shadow stack for a CLONE_VFORK child. CLONE_VFORK children already need special tracking to avoid returning to userspace until the child exits or execs. Shadow stack uses this same tracking to avoid freeing CLONE_VFORK shadow stacks. However, the tracking is not setup until the clone has succeeded (internally). Which means, if a CLONE_VFORK fails, the existing logic will not know it is a CLONE_VFORK and proceed to unmap the parents shadow stack. This error handling cleanup logic runs via exit_thread() in the bad_fork_cleanup_thread label in copy_process(). The issue was seen in the glibc test "posix/tst-spawn3-pidfd" while running with shadow stack using currently out-of-tree glibc patches. Fix it by not unmapping the vfork shadow stack in the error case as well. Since clone is implemented in core code, it is not ideal to pass the clone flags along the error path in order to have shadow stack code have symmetric logic in the freeing half of the thread shadow stack handling. Instead use the existing state for thread shadow stacks to track whether the thread is managing its own shadow stack. For CLONE_VFORK, simply set shstk->base and shstk->size to 0, and have it mean the thread is not managing a shadow stack and so should skip cleanup work. Implement this by breaking up the CLONE_VFORK and !CLONE_VM cases in shstk_alloc_thread_stack() to separate conditionals since, the logic is now different between them. In the case of CLONE_VFORK && !CLONE_VM, the existing behavior is to not clean up the shadow stack in the child (which should go away quickly with either be exit or exec), so maintain that behavior by handling the CLONE_VFORK case first in the allocation path. This new logioc cleanly handles the case of normal, successful CLONE_VFORK's skipping cleaning up their shadow stack's on exit as well. So remove the existing, vfork shadow stack freeing logic. This is in deactivate_mm() where vfork_done is used to tell if it is a vfork child that can skip cleaning up the thread shadow stack. Fixes: b2926a3 ("x86/shstk: Handle thread shadow stack") Reported-by: H.J. Lu <hjl.tools@gmail.com> Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Tested-by: H.J. Lu <hjl.tools@gmail.com> Link: https://lore.kernel.org/all/20230908203655.543765-2-rick.p.edgecombe%40intel.com
1 parent 01b057b commit 3319556

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

arch/x86/include/asm/mmu_context.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ do { \
186186
#else
187187
#define deactivate_mm(tsk, mm) \
188188
do { \
189-
if (!tsk->vfork_done) \
190-
shstk_free(tsk); \
189+
shstk_free(tsk); \
191190
load_gs_index(0); \
192191
loadsegment(fs, 0); \
193192
} while (0)

arch/x86/kernel/shstk.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,21 @@ unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, unsigned long cl
205205
return 0;
206206

207207
/*
208-
* For CLONE_VM, except vfork, the child needs a separate shadow
208+
* For CLONE_VFORK the child will share the parents shadow stack.
209+
* Make sure to clear the internal tracking of the thread shadow
210+
* stack so the freeing logic run for child knows to leave it alone.
211+
*/
212+
if (clone_flags & CLONE_VFORK) {
213+
shstk->base = 0;
214+
shstk->size = 0;
215+
return 0;
216+
}
217+
218+
/*
219+
* For !CLONE_VM the child will use a copy of the parents shadow
209220
* stack.
210221
*/
211-
if ((clone_flags & (CLONE_VFORK | CLONE_VM)) != CLONE_VM)
222+
if (!(clone_flags & CLONE_VM))
212223
return 0;
213224

214225
size = adjust_shstk_size(stack_size);
@@ -408,6 +419,13 @@ void shstk_free(struct task_struct *tsk)
408419
if (!tsk->mm || tsk->mm != current->mm)
409420
return;
410421

422+
/*
423+
* If shstk->base is NULL, then this task is not managing its
424+
* own shadow stack (CLONE_VFORK). So skip freeing it.
425+
*/
426+
if (!shstk->base)
427+
return;
428+
411429
unmap_shadow_stack(shstk->base, shstk->size);
412430
}
413431

0 commit comments

Comments
 (0)