Skip to content

Commit e2c2b04

Browse files
rpedgecogregkh
authored andcommitted
x86/shstk: Prevent deadlock during shstk sigreturn
[ Upstream commit 9874b29 ] During sigreturn the shadow stack signal frame is popped. The kernel does this by reading the shadow stack using normal read accesses. When it can't assume the memory is shadow stack, it takes extra steps to makes sure it is reading actual shadow stack memory and not other normal readable memory. It does this by holding the mmap read lock while doing the access and checking the flags of the VMA. Unfortunately that is not safe. If the read of the shadow stack sigframe hits a page fault, the fault handler will try to recursively grab another mmap read lock. This normally works ok, but if a writer on another CPU is also waiting, the second read lock could fail and cause a deadlock. Fix this by doing the read of the userspace memory via gup. Embed it in the get_shstk_data() helper. Currently there is a check that skips the lookup work when the SSP can be assumed to be on a shadow stack. While reorganizing the function, remove the optimization to make the tricky code flows more common, such that issues like this cannot escape detection for so long. [Due to missing per-vma MM sequence counter, use a simpler GUP based solution for the backport] Cc: <stable@vger.kernel.org> # Depends on https://lore.kernel.org/all/20260504205924.536382-1-rick.p.edgecombe@intel.com/ Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 21159d8 commit e2c2b04

1 file changed

Lines changed: 29 additions & 17 deletions

File tree

arch/x86/kernel/shstk.c

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/sizes.h>
1919
#include <linux/user.h>
2020
#include <linux/syscalls.h>
21+
#include <linux/highmem.h>
2122
#include <asm/msr.h>
2223
#include <asm/fpu/xstate.h>
2324
#include <asm/fpu/types.h>
@@ -262,11 +263,29 @@ static int put_shstk_data(u64 __user *addr, u64 data)
262263
return 0;
263264
}
264265

266+
/* Copy from aligned address in userspace without risk of page fault. */
267+
static int shstk_copy_user_gup(unsigned long *ldata, unsigned long __user *addr)
268+
{
269+
struct page *page;
270+
void *kaddr;
271+
272+
mmap_assert_locked(current->mm);
273+
if (get_user_pages((unsigned long)addr, 1, 0, &page) != 1)
274+
return -EFAULT;
275+
276+
kaddr = kmap_local_page(page);
277+
*ldata = *(unsigned long *)(kaddr + offset_in_page(addr));
278+
kunmap_local(kaddr);
279+
put_page(page);
280+
281+
return 0;
282+
}
283+
265284
static int get_shstk_data(unsigned long *data, unsigned long __user *addr)
266285
{
267286
unsigned long ldata;
268287

269-
if (unlikely(get_user(ldata, addr)))
288+
if (shstk_copy_user_gup(&ldata, addr))
270289
return -EFAULT;
271290

272291
if (!(ldata & SHSTK_DATA_BIT))
@@ -296,7 +315,6 @@ static int shstk_pop_sigframe(unsigned long *ssp)
296315
{
297316
struct vm_area_struct *vma;
298317
unsigned long token_addr;
299-
bool need_to_check_vma;
300318
int err = 1;
301319

302320
/*
@@ -308,26 +326,21 @@ static int shstk_pop_sigframe(unsigned long *ssp)
308326
if (!IS_ALIGNED(*ssp, 8))
309327
return -EINVAL;
310328

311-
need_to_check_vma = PAGE_ALIGN(*ssp) == *ssp;
312-
313-
if (need_to_check_vma)
314-
if (mmap_read_lock_killable(current->mm))
315-
return -EINTR;
329+
if (mmap_read_lock_killable(current->mm))
330+
return -EINTR;
316331

317332
err = get_shstk_data(&token_addr, (unsigned long __user *)*ssp);
318333
if (unlikely(err))
319334
goto out_err;
320335

321-
if (need_to_check_vma) {
322-
vma = find_vma(current->mm, *ssp);
323-
if (!vma || !(vma->vm_flags & VM_SHADOW_STACK)) {
324-
err = -EFAULT;
325-
goto out_err;
326-
}
327-
328-
mmap_read_unlock(current->mm);
336+
vma = find_vma(current->mm, *ssp);
337+
if (!vma || !(vma->vm_flags & VM_SHADOW_STACK)) {
338+
err = -EFAULT;
339+
goto out_err;
329340
}
330341

342+
mmap_read_unlock(current->mm);
343+
331344
/* Restore SSP aligned? */
332345
if (unlikely(!IS_ALIGNED(token_addr, 8)))
333346
return -EINVAL;
@@ -340,8 +353,7 @@ static int shstk_pop_sigframe(unsigned long *ssp)
340353

341354
return 0;
342355
out_err:
343-
if (need_to_check_vma)
344-
mmap_read_unlock(current->mm);
356+
mmap_read_unlock(current->mm);
345357
return err;
346358
}
347359

0 commit comments

Comments
 (0)