Skip to content

Commit 0fef65e

Browse files
oleg-nesterovgregkh
authored andcommitted
uprobes: ensure that uprobe->offset and ->ref_ctr_offset are properly aligned
commit 013b2de upstream. 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 <torvalds@linux-foundation.org> Suggested-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Oleg Nesterov <oleg@redhat.com> Reviewed-by: Srikar Dronamraju <srikar@linux.vnet.ibm.com> Acked-by: Christian Borntraeger <borntraeger@de.ibm.com> Tested-by: Sven Schnelle <svens@linux.ibm.com> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: stable@vger.kernel.org Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 1e6cd20 commit 0fef65e

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

‎kernel/events/uprobes.c‎

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

870-
/* uprobe_write_opcode() assumes we don't cross page boundary */
871-
BUG_ON((uprobe->offset & ~PAGE_MASK) +
872-
UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
873-
874870
smp_wmb(); /* pairs with the smp_rmb() in handle_swbp() */
875871
set_bit(UPROBE_COPY_INSN, &uprobe->flags);
876872

@@ -1166,6 +1162,15 @@ static int __uprobe_register(struct inode *inode, loff_t offset,
11661162
if (offset > i_size_read(inode))
11671163
return -EINVAL;
11681164

1165+
/*
1166+
* This ensures that copy_from_page(), copy_to_page() and
1167+
* __update_ref_ctr() can't cross page boundary.
1168+
*/
1169+
if (!IS_ALIGNED(offset, UPROBE_SWBP_INSN_SIZE))
1170+
return -EINVAL;
1171+
if (!IS_ALIGNED(ref_ctr_offset, sizeof(short)))
1172+
return -EINVAL;
1173+
11691174
retry:
11701175
uprobe = alloc_uprobe(inode, offset, ref_ctr_offset);
11711176
if (!uprobe)
@@ -2014,6 +2019,9 @@ static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr)
20142019
uprobe_opcode_t opcode;
20152020
int result;
20162021

2022+
if (WARN_ON_ONCE(!IS_ALIGNED(vaddr, UPROBE_SWBP_INSN_SIZE)))
2023+
return -EINVAL;
2024+
20172025
pagefault_disable();
20182026
result = __get_user(opcode, (uprobe_opcode_t __user *)vaddr);
20192027
pagefault_enable();

0 commit comments

Comments
 (0)