Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <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>

authored by

Oleg Nesterov and committed by
Linus Torvalds
013b2deb 8b4d37db

+12 -4
+12 -4
kernel/events/uprobes.c
··· 861 861 if (ret) 862 862 goto out; 863 863 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 - 868 864 smp_wmb(); /* pairs with the smp_rmb() in handle_swbp() */ 869 865 set_bit(UPROBE_COPY_INSN, &uprobe->flags); 870 866 ··· 1154 1158 return -EIO; 1155 1159 /* Racy, just to catch the obvious mistakes */ 1156 1160 if (offset > i_size_read(inode)) 1161 + return -EINVAL; 1162 + 1163 + /* 1164 + * This ensures that copy_from_page(), copy_to_page() and 1165 + * __update_ref_ctr() can't cross page boundary. 1166 + */ 1167 + if (!IS_ALIGNED(offset, UPROBE_SWBP_INSN_SIZE)) 1168 + return -EINVAL; 1169 + if (!IS_ALIGNED(ref_ctr_offset, sizeof(short))) 1157 1170 return -EINVAL; 1158 1171 1159 1172 retry: ··· 2012 2007 struct page *page; 2013 2008 uprobe_opcode_t opcode; 2014 2009 int result; 2010 + 2011 + if (WARN_ON_ONCE(!IS_ALIGNED(vaddr, UPROBE_SWBP_INSN_SIZE))) 2012 + return -EINVAL; 2015 2013 2016 2014 pagefault_disable(); 2017 2015 result = __get_user(opcode, (uprobe_opcode_t __user *)vaddr);