Skip to content

Commit 013b2de

Browse files
oleg-nesterovtorvalds
authored andcommitted
uprobes: ensure that uprobe->offset and ->ref_ctr_offset are properly aligned
uprobe_write_opcode() must not cross page boundary; prepare_uprobe() relies on arch_uprobe_analyze_insn() which should validate "vaddr" but some architectures (csky, s390, and sparc) don't do this. We can remove the BUG_ON() check in prepare_uprobe() and validate the offset early in __uprobe_register(). The new IS_ALIGNED() check matches the alignment check in arch_prepare_kprobe() on supported architectures, so I think that all insns must be aligned to UPROBE_SWBP_INSN_SIZE. Another problem is __update_ref_ctr() which was wrong from the very beginning, it can read/write outside of kmap'ed page unless "vaddr" is aligned to sizeof(short), __uprobe_register() should check this too. Reported-by: Linus Torvalds <[email protected]> Suggested-by: Linus Torvalds <[email protected]> Signed-off-by: Oleg Nesterov <[email protected]> Reviewed-by: Srikar Dronamraju <[email protected]> Acked-by: Christian Borntraeger <[email protected]> Tested-by: Sven Schnelle <[email protected]> Cc: Steven Rostedt <[email protected]> Cc: [email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent 8b4d37d commit 013b2de

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

kernel/events/uprobes.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -861,10 +861,6 @@ static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
861861
if (ret)
862862
goto out;
863863

864-
/* uprobe_write_opcode() assumes we don't cross page boundary */
865-
BUG_ON((uprobe->offset & ~PAGE_MASK) +
866-
UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
867-
868864
smp_wmb(); /* pairs with the smp_rmb() in handle_swbp() */
869865
set_bit(UPROBE_COPY_INSN, &uprobe->flags);
870866

@@ -1160,6 +1156,15 @@ static int __uprobe_register(struct inode *inode, loff_t offset,
11601156
if (offset > i_size_read(inode))
11611157
return -EINVAL;
11621158

1159+
/*
1160+
* This ensures that copy_from_page(), copy_to_page() and
1161+
* __update_ref_ctr() can't cross page boundary.
1162+
*/
1163+
if (!IS_ALIGNED(offset, UPROBE_SWBP_INSN_SIZE))
1164+
return -EINVAL;
1165+
if (!IS_ALIGNED(ref_ctr_offset, sizeof(short)))
1166+
return -EINVAL;
1167+
11631168
retry:
11641169
uprobe = alloc_uprobe(inode, offset, ref_ctr_offset);
11651170
if (!uprobe)
@@ -2008,6 +2013,9 @@ static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr)
20082013
uprobe_opcode_t opcode;
20092014
int result;
20102015

2016+
if (WARN_ON_ONCE(!IS_ALIGNED(vaddr, UPROBE_SWBP_INSN_SIZE)))
2017+
return -EINVAL;
2018+
20112019
pagefault_disable();
20122020
result = __get_user(opcode, (uprobe_opcode_t __user *)vaddr);
20132021
pagefault_enable();

0 commit comments

Comments
 (0)