From patchwork Mon Oct 15 11:16:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anders Roxell X-Patchwork-Id: 10641563 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 02EFB14E2 for ; Mon, 15 Oct 2018 11:16:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E43BB28BE6 for ; Mon, 15 Oct 2018 11:16:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D368229456; Mon, 15 Oct 2018 11:16:42 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 2730B28BE6 for ; Mon, 15 Oct 2018 11:16:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:To :From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=QNXeRhlnFUZEKvNvlKSsklgMRKRAHcNqxkqNbu/w7GM=; b=OLuqfuT7vFESCR VagJKc19LvJSl4++jR+E0aiKrMCudFujzne1/17aS7uVbX6Joo97iDaFLqOiHX/ThbcOZbwxkp1LC LSSPs2nIh2aDVMrwrVJv0h1ioHQnL0O/ssHOFUpkpi0Q/4TNoaDY3xaWKvxirE/RohdMvooNLhsla o9aIrhPRVkoiRRtQxRrPekoKbxdbByBWc2OATuge+X3FL4QU9TV409QRKt+GN6QDQjIDXGpxvlP6E aY6bYzIxFRsd0/X6yB5lPUBTDF2mQU3E+IVWznCeIDuyJRyHHlm4udblxD/yQELyD8/NBDDD1ER83 D/AInSqrVV5px71zKAQg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gC0rR-0006h1-If; Mon, 15 Oct 2018 11:16:33 +0000 Received: from mail-lj1-x241.google.com ([2a00:1450:4864:20::241]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gC0rN-0006fx-7w for linux-arm-kernel@lists.infradead.org; Mon, 15 Oct 2018 11:16:30 +0000 Received: by mail-lj1-x241.google.com with SMTP id r83-v6so17106007ljr.7 for ; Mon, 15 Oct 2018 04:16:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=KapoO5oMlMVFKcdCw43NU78fcYlzzKItuvdaDSjvtEY=; b=jhoK4P329uM4OgPNBuc+W4XSTNdyijGPQ7tYXpsRzwvnf5sOZn/JeUQlKJbUcBStJC p/KX2drpls/0XTsbU1BTUYGyxuilNKYAC03yGhrOyAD34HGaFqh/sSi6WwkPghYr/IaU kObhyOVBHUK8Wl3202Elwoa4wUyTLHttFT9S8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=KapoO5oMlMVFKcdCw43NU78fcYlzzKItuvdaDSjvtEY=; b=Hxy+rE49GbnKKMhlKQ/AAs55U2Kv8Xv+uHoQC4zoqXjEPCckhl8R9jeLfetIeQXXfd 1BoksvYLzmqhJSgjAFBeoPQhEzKYPrncK/jzIb9BHyFHIrvMzFLQScZV4TWT/A0ecrC+ Y+6kYuHyxYKN0bgPBh4jrLP42htBnDAuIIbxGQv+FziC7jBV4xwSOYB9gWKtJTo+Qx5S 1ez6Wfw5Ca8jR9rUMChjX6cDsrnCVdCLbjCoE+TV8EUQsJnKSYEKaTJ1ow716zlr1Q6J aHbzwd407rA/k+PJ7RQrbhJ36X4GxO96BRnJxESckVqui/DDDsxZgAhSuNKw7QuXIXi3 5bhg== X-Gm-Message-State: ABuFfoiFcmmwJl/b5pjq4DRUEqbUAA9Duevx8DBMa9yVSlquSHWKI/D9 Zz6HLAX+O3KBBjsOfArJIFLJPg== X-Google-Smtp-Source: ACcGV62H04Q0o2Qc3YYrRBzFf9KX/YYxPs4WhluwQ8LWQ6bTZrj77XjSAR1Fcuy+bc+Hf0AgwIlLUQ== X-Received: by 2002:a2e:8606:: with SMTP id a6-v6mr9222957lji.43.1539602175125; Mon, 15 Oct 2018 04:16:15 -0700 (PDT) Received: from localhost (c-732171d5.07-21-73746f28.bbcust.telenor.se. [213.113.33.115]) by smtp.gmail.com with ESMTPSA id l9-v6sm2277055ljb.70.2018.10.15.04.16.14 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 15 Oct 2018 04:16:14 -0700 (PDT) From: Anders Roxell To: catalin.marinas@arm.com, will.deacon@arm.com Subject: [PATCH] arm64: kprobe: make page to RO mode when allocate it Date: Mon, 15 Oct 2018 13:16:00 +0200 Message-Id: <20181015111600.5479-1-anders.roxell@linaro.org> X-Mailer: git-send-email 2.19.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181015_041629_282614_4C5D585D X-CRM114-Status: GOOD ( 14.96 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Anders Roxell , Arnd Bergmann , Ard Biesheuvel , linux-kernel@vger.kernel.org, Laura Abbott , linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP 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 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()"). Cc: Arnd Bergmann Cc: Ard Biesheuvel Cc: Laura Abbott Cc: Catalin Marinas Co-developed-by: Arnd Bergmann Co-developed-by: Ard Biesheuvel Signed-off-by: Anders Roxell Acked-by: Masami Hiramatsu Reviewed-by: Laura Abbott --- 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 9b65132e789a..b842e908b423 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] = (void *)addr; + insns[0] = (u32)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 & PAGE_MASK, 1); - return aarch64_insn_patch_text(addrs, insns, 1); + return page; } /* arm kprobe: install breakpoint in text */