From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 34C2EC433E0 for ; Tue, 9 Jun 2020 18:23:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 10B292067B for ; Tue, 9 Jun 2020 18:23:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1591727032; bh=xdwFVTfgP3vZ5Wz9kYYn9FvzA9/8botzxGW8McRJN6A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=s0F0UDlVTvbmM6TDlR6gebE2fEWsWBRYZu+Xtxn8ura+D9qLONfkvuc/y8oZqHAt8 U8q4scwB2oI9g1BDgB32To149sTtX3LfmyzY6WXa0wDCA5MU+gW7Y5uAZo6h6jCK1B JZzOeb7toBks0+SDRu2s8+2C9Fd7O/7lFWYxvGU8= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388827AbgFISXu (ORCPT ); Tue, 9 Jun 2020 14:23:50 -0400 Received: from mail.kernel.org ([198.145.29.99]:58452 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732071AbgFIRrP (ORCPT ); Tue, 9 Jun 2020 13:47:15 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1317C20820; Tue, 9 Jun 2020 17:47:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1591724835; bh=xdwFVTfgP3vZ5Wz9kYYn9FvzA9/8botzxGW8McRJN6A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Pc6Ad2TEHRHn8PS6XMWVgKFRgmp8t9d7NDQ5XLG8m61aHiFQzUnDj+3CTfglXoXz9 yrus80aB6o+c0bmfOTdukhc+FUHi2HxrwBVeeKZiCqYPXRGUkx/qSBxq0kbbuQYsk6 k4wiipzbLjC3T4BhJDUf523pdk3cw41TpKsA1bKQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Linus Torvalds , Oleg Nesterov , Srikar Dronamraju , Christian Borntraeger , Sven Schnelle , Steven Rostedt Subject: [PATCH 4.4 36/36] uprobes: ensure that uprobe->offset and ->ref_ctr_offset are properly aligned Date: Tue, 9 Jun 2020 19:44:36 +0200 Message-Id: <20200609173935.927158590@linuxfoundation.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200609173933.288044334@linuxfoundation.org> References: <20200609173933.288044334@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Oleg Nesterov commit 013b2deba9a6b80ca02f4fafd7dedf875e9b4450 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 Suggested-by: Linus Torvalds Signed-off-by: Oleg Nesterov Reviewed-by: Srikar Dronamraju Acked-by: Christian Borntraeger Tested-by: Sven Schnelle Cc: Steven Rostedt Cc: stable@vger.kernel.org Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- kernel/events/uprobes.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -602,10 +602,6 @@ static int prepare_uprobe(struct uprobe if (ret) goto out; - /* uprobe_write_opcode() assumes we don't cross page boundary */ - BUG_ON((uprobe->offset & ~PAGE_MASK) + - UPROBE_SWBP_INSN_SIZE > PAGE_SIZE); - smp_wmb(); /* pairs with the smp_rmb() in handle_swbp() */ set_bit(UPROBE_COPY_INSN, &uprobe->flags); @@ -884,6 +880,15 @@ int uprobe_register(struct inode *inode, if (offset > i_size_read(inode)) return -EINVAL; + /* + * This ensures that copy_from_page(), copy_to_page() and + * __update_ref_ctr() can't cross page boundary. + */ + if (!IS_ALIGNED(offset, UPROBE_SWBP_INSN_SIZE)) + return -EINVAL; + if (!IS_ALIGNED(ref_ctr_offset, sizeof(short))) + return -EINVAL; + retry: uprobe = alloc_uprobe(inode, offset); if (!uprobe) @@ -1692,6 +1697,9 @@ static int is_trap_at_addr(struct mm_str uprobe_opcode_t opcode; int result; + if (WARN_ON_ONCE(!IS_ALIGNED(vaddr, UPROBE_SWBP_INSN_SIZE))) + return -EINVAL; + pagefault_disable(); result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr, sizeof(opcode));