Skip to content

Commit c16e2fd

Browse files
oleg-nesterovPeter Zijlstra
authored andcommitted
uprobes: deny mremap(xol_vma)
kernel/events/uprobes.c assumes that xol_area->vaddr is always correct but a malicious application can remap its "[uprobes]" vma to another adress to confuse the kernel. Introduce xol_mremap() to make this impossible. With this change utask->xol_vaddr in xol_free_insn_slot() can't be invalid, we can turn the offset check into WARN_ON_ONCE(offset >= PAGE_SIZE). Signed-off-by: Oleg Nesterov <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent c5356ab commit c16e2fd

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

kernel/events/uprobes.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,9 +1472,15 @@ static vm_fault_t xol_fault(const struct vm_special_mapping *sm,
14721472
return 0;
14731473
}
14741474

1475+
static int xol_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma)
1476+
{
1477+
return -EPERM;
1478+
}
1479+
14751480
static const struct vm_special_mapping xol_mapping = {
14761481
.name = "[uprobes]",
14771482
.fault = xol_fault,
1483+
.mremap = xol_mremap,
14781484
};
14791485

14801486
/* Slot allocation for XOL */
@@ -1667,21 +1673,19 @@ static void xol_free_insn_slot(struct uprobe_task *utask)
16671673
{
16681674
struct xol_area *area = current->mm->uprobes_state.xol_area;
16691675
unsigned long offset = utask->xol_vaddr - area->vaddr;
1676+
unsigned int slot_nr;
16701677

16711678
utask->xol_vaddr = 0;
1672-
/*
1673-
* xol_vaddr must fit into [area->vaddr, area->vaddr + PAGE_SIZE).
1674-
* This check can only fail if the "[uprobes]" vma was mremap'ed.
1675-
*/
1676-
if (offset < PAGE_SIZE) {
1677-
int slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
1678-
1679-
clear_bit(slot_nr, area->bitmap);
1680-
atomic_dec(&area->slot_count);
1681-
smp_mb__after_atomic(); /* pairs with prepare_to_wait() */
1682-
if (waitqueue_active(&area->wq))
1683-
wake_up(&area->wq);
1684-
}
1679+
/* xol_vaddr must fit into [area->vaddr, area->vaddr + PAGE_SIZE) */
1680+
if (WARN_ON_ONCE(offset >= PAGE_SIZE))
1681+
return;
1682+
1683+
slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
1684+
clear_bit(slot_nr, area->bitmap);
1685+
atomic_dec(&area->slot_count);
1686+
smp_mb__after_atomic(); /* pairs with prepare_to_wait() */
1687+
if (waitqueue_active(&area->wq))
1688+
wake_up(&area->wq);
16851689
}
16861690

16871691
void __weak arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,

0 commit comments

Comments
 (0)