Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp5613558imu; Mon, 26 Nov 2018 03:01:42 -0800 (PST) X-Google-Smtp-Source: AJdET5c5JG87GuLSIRFhuSgRHELIc0kj1xidd3/YnrZENwQ1Jh41tRlF+E1a8UBEOQvrhRWksfcc X-Received: by 2002:a62:2606:: with SMTP id m6mr26734447pfm.133.1543230101883; Mon, 26 Nov 2018 03:01:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543230101; cv=none; d=google.com; s=arc-20160816; b=smRsXjs+kxxiKQGBARfKrgqQMQVGPh5bYkB23Koi1c8lZWz+JoE76tYsw6r1YoVpHo JI8sLLzbunoerntPNw00sG5xwisxWM5GSJRgYNCzf8T7wmwZeu3UA8iqt7GJNycREhV0 61fG99Y29T68VaGoqk1xApjYtpJFriPRUFqrIagoIUjaX3R/2Bnv3rc5EhAhDOjGyXFN JP/clAEDs4jGZ/3eAIFTgIH3xXf2Z1dQk6CRma/5KFS82ML2e2gti/TomFFSgFCeGlRR AY9G473VyoDyf3qJO1s0ARoWn+XQRxl5QH/RUlHECZiPeuoVO5K3MJZE68yplHii1sTv N+Jw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=R05ZNS+MB3cPW/6LUhqO+C2zTYrz+m2+W/7ABDWuknQ=; b=LjEhSZYjnpwcUt5Dknsv9vJM+qVH+BVdiQ4DddLTzOnRP0/VhjRBtaJSOKRf3CHdrG sxBGi6Po54a3q/0x7/3vwEepJhqUYHW1ni2XTb1HPtm97uQBCjHUkJqVCHcI2GLCKu6M TN/nYrQnbBlZ4r/RK2SB90fMgIawOp5pXsqFy78adnzviP2EM0HkfPN3r2Di/zeWKuU9 GjjflxrIcMkoeZrJLu6dWUG8NK6+BSncskbQHjU6sLa+oAmhKu3rpd7VmoSVotPbCfIp j2f+kv3YHTrSGYfQDbC9QNn3wGEAH6k2hOTr3JzCsfxOxYITpLpjNYMKskAFX9LWGirb qBgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=FrE7aV0h; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h3si40540069pgl.468.2018.11.26.03.01.19; Mon, 26 Nov 2018 03:01:41 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=FrE7aV0h; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729658AbeKZVxi (ORCPT + 99 others); Mon, 26 Nov 2018 16:53:38 -0500 Received: from mail.kernel.org ([198.145.29.99]:35404 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729648AbeKZVxi (ORCPT ); Mon, 26 Nov 2018 16:53:38 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.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 212102089F; Mon, 26 Nov 2018 10:59:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1543229992; bh=gcl4dKD/iQTZW4VRvVK/Pli48iTyXU6tDSRNFyzJ0s0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FrE7aV0hngg1DqLyTUdHKRLyKZIdsUWOmQoyUCq/Fb63aKyzYNEeeetIIZ7yrR6ek /GCbQGN2wTVnklhrIYlQubUXFGqMWwaUJ8TPCsA7Mt8dz98n6scrRIEoR9oMGwrGhz 7iAxHBciJfKMfjnhhXKUXRiGRHlVf6ojA2B3Dork= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Arnd Bergmann , Ard Biesheuvel , Will Deacon , Masami Hiramatsu , Laura Abbott , Anders Roxell , Catalin Marinas , Sasha Levin Subject: [PATCH 4.14 03/62] arm64: kprobe: make page to RO mode when allocate it Date: Mon, 26 Nov 2018 11:50:44 +0100 Message-Id: <20181126105051.213025360@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181126105050.592727680@linuxfoundation.org> References: <20181126105050.592727680@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review 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 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 966866892cf89d606544bca22d584ba2ef9ec208 ] Commit 1404d6f13e47 ("arm64: dump: Add checking for writable and exectuable pages") has successfully identified code that leaves a page with W+X permissions. [ 3.245140] arm64/mm: Found insecure W+X mapping at address (____ptrval____)/0xffff000000d90000 [ 3.245771] WARNING: CPU: 0 PID: 1 at ../arch/arm64/mm/dump.c:232 note_page+0x410/0x420 [ 3.246141] Modules linked in: [ 3.246653] CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.19.0-rc5-next-20180928-00001-ge70ae259b853-dirty #62 [ 3.247008] Hardware name: linux,dummy-virt (DT) [ 3.247347] pstate: 80000005 (Nzcv daif -PAN -UAO) [ 3.247623] pc : note_page+0x410/0x420 [ 3.247898] lr : note_page+0x410/0x420 [ 3.248071] sp : ffff00000804bcd0 [ 3.248254] x29: ffff00000804bcd0 x28: ffff000009274000 [ 3.248578] x27: ffff00000921a000 x26: ffff80007dfff000 [ 3.248845] x25: ffff0000093f5000 x24: ffff000009526f6a [ 3.249109] x23: 0000000000000004 x22: ffff000000d91000 [ 3.249396] x21: ffff000000d90000 x20: 0000000000000000 [ 3.249661] x19: ffff00000804bde8 x18: 0000000000000400 [ 3.249924] x17: 0000000000000000 x16: 0000000000000000 [ 3.250271] x15: ffffffffffffffff x14: 295f5f5f5f6c6176 [ 3.250594] x13: 7274705f5f5f5f28 x12: 2073736572646461 [ 3.250941] x11: 20746120676e6970 x10: 70616d20582b5720 [ 3.251252] x9 : 6572756365736e69 x8 : 3039643030303030 [ 3.251519] x7 : 306666666678302f x6 : ffff0000095467b2 [ 3.251802] x5 : 0000000000000000 x4 : 0000000000000000 [ 3.252060] x3 : 0000000000000000 x2 : ffffffffffffffff [ 3.252323] x1 : 4d151327adc50b00 x0 : 0000000000000000 [ 3.252664] Call trace: [ 3.252953] note_page+0x410/0x420 [ 3.253186] walk_pgd+0x12c/0x238 [ 3.253417] ptdump_check_wx+0x68/0xf8 [ 3.253637] mark_rodata_ro+0x68/0x98 [ 3.253847] kernel_init+0x38/0x160 [ 3.254103] ret_from_fork+0x10/0x18 kprobes allocates a writable executable page with module_alloc() in order to store executable code. Reworked to that when allocate a page it sets mode RO. Inspired by commit 63fef14fc98a ("kprobes/x86: Make insn buffer always ROX and use text_poke()"). Suggested-by: Arnd Bergmann Suggested-by: Ard Biesheuvel Acked-by: Will Deacon Acked-by: Masami Hiramatsu Reviewed-by: Laura Abbott Signed-off-by: Anders Roxell [catalin.marinas@arm.com: removed unnecessary casts] Signed-off-by: Catalin Marinas Signed-off-by: Sasha Levin --- arch/arm64/kernel/probes/kprobes.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c index 22a5921562c7..0417c929d21a 100644 --- a/arch/arm64/kernel/probes/kprobes.c +++ b/arch/arm64/kernel/probes/kprobes.c @@ -23,7 +23,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -42,10 +44,21 @@ DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); static void __kprobes post_kprobe_handler(struct kprobe_ctlblk *, struct pt_regs *); +static int __kprobes patch_text(kprobe_opcode_t *addr, u32 opcode) +{ + void *addrs[1]; + u32 insns[1]; + + addrs[0] = addr; + insns[0] = opcode; + + return aarch64_insn_patch_text(addrs, insns, 1); +} + static void __kprobes arch_prepare_ss_slot(struct kprobe *p) { /* prepare insn slot */ - p->ainsn.api.insn[0] = cpu_to_le32(p->opcode); + patch_text(p->ainsn.api.insn, p->opcode); flush_icache_range((uintptr_t) (p->ainsn.api.insn), (uintptr_t) (p->ainsn.api.insn) + @@ -118,15 +131,15 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p) return 0; } -static int __kprobes patch_text(kprobe_opcode_t *addr, u32 opcode) +void *alloc_insn_page(void) { - void *addrs[1]; - u32 insns[1]; + void *page; - addrs[0] = (void *)addr; - insns[0] = (u32)opcode; + page = vmalloc_exec(PAGE_SIZE); + if (page) + set_memory_ro((unsigned long)page, 1); - return aarch64_insn_patch_text(addrs, insns, 1); + return page; } /* arm kprobe: install breakpoint in text */ -- 2.17.1