From patchwork Tue Oct 30 11:38:50 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anders Roxell X-Patchwork-Id: 149740 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp5336112ljp; Tue, 30 Oct 2018 04:39:05 -0700 (PDT) X-Google-Smtp-Source: AJdET5fDe6SabfyYtzj9YOZXT4mb2cSs29LZPKfF9IrMr95MeoqczQSKZXSCqJxSXFpk5XNXiedg X-Received: by 2002:a17:902:bc8c:: with SMTP id bb12-v6mr17700994plb.275.1540899545237; Tue, 30 Oct 2018 04:39:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540899545; cv=none; d=google.com; s=arc-20160816; b=qG9LcI50Gb1FtsX7pz3x4d8spFs7Nw1yFDM5IiWLcjRrA4g42ek3TxIKxKwsLXUfni u+b+/+yoLPzM+o642q9OmySXFLZezyUsKd8SOVR1woy47Z3mR8YaflsDGWwBWyBkPqiv s5W1q8IPtrZZMpi3o6ECzMA8r11A7G1xOgoIr51iWjF0S0hAVtAV+UE9p0ghdrgBD8JT /wJSP+ooxa+zpOcisXQ+7zMEl7im/1iHXjtTn7dRrZW1WOEWLaclQEQDFFG9MhICFl+s qk/541s7NV88en2ykniTIFs3/PeLj+qGl4eQJnSn4B/5zf9n0TIQtIXZEV/7AAcMZwEH ZxgQ== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=m1mIk7f03+xCAPFpAQ+ri8nynizG8TXkzIRuRDPgQkU=; b=VU55ghki8wA509akUwR/sMh1Q7oGhZyNk2i6vWuwdJaOnHS4EXJyz8IUNlRZ8wVOqI Oat/DDE4y7xInYBHsr3iRPgU6eNlRq8/RTHe8FaeDNvQc5tF+dhefg8xifpMiSW72zli MjMylRC0924ql8nvaFz8hMUsjVJPZ2FuCbni+G7YMFffVBTgDC3OS37EVLTlwliCzD9M XkP5AIeh6npCNRlWBrLwGKAIwcCib20QEYuWxpl/PgJLIRwA+8B/+Kwxb7GSwRxJ9ro6 wMEfSzmyUI6bY8yQ0PNCLoyMhweqHaxh8zOup/2iZy+Wr32pvLvzov5wb9WHrL+BTblX R5EQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Jwuh5AsF; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z186si11960164pgd.90.2018.10.30.04.39.04; Tue, 30 Oct 2018 04:39:05 -0700 (PDT) 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=@linaro.org header.s=google header.b=Jwuh5AsF; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727620AbeJ3UcK (ORCPT + 32 others); Tue, 30 Oct 2018 16:32:10 -0400 Received: from mail-lj1-f194.google.com ([209.85.208.194]:33365 "EHLO mail-lj1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727216AbeJ3UcK (ORCPT ); Tue, 30 Oct 2018 16:32:10 -0400 Received: by mail-lj1-f194.google.com with SMTP id z21-v6so11008174ljz.0 for ; Tue, 30 Oct 2018 04:39:00 -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=m1mIk7f03+xCAPFpAQ+ri8nynizG8TXkzIRuRDPgQkU=; b=Jwuh5AsFaiuRHjYyFzL3YuMF2QbHogcYyknHZHEgVkN/+AGLQvp3iC/GrzWh4U9vue a6qsC1Y5Y0TmtvTr4qz0XEClCdkrSuU3Tcx1tY0Bd/uFoMLhqCSiQ7fbQXfKbmaFpQ/B yiU+WeM5lIhSnBKWSYJxb7HH83AMtwkGmhiq0= 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=m1mIk7f03+xCAPFpAQ+ri8nynizG8TXkzIRuRDPgQkU=; b=t5uZ1dVQQ1qiCXO2buTP/PR4aApa9dz/pD9jbVfKkmzSVY7de7J0O8sbtLfFME4EBd Fi4WfmDmslzQ+lmZ9iddiBkpQ1b2qo7vc/KupYX4MjO3XUyGTUTASW6UWKoF/l1Z58pg 78W3blf79lr7+mqVXijHACOlmQ6LJeh7B451WGoAG1EdkiGxZ+8Am8a6ExZVI6kgxiyW spHaNIhbyUGPy9FDjQSNbq3R/9RDWpiH3DJz3AQ2lBL8fw+FOydCU3IkYT44BnmnKPK4 joRc6IPwuEihdCtrCcl0EpFyUDa8iEA/5KRi7osrozVKlau3HzhLgtz0A/3YZ1S7P964 CkNw== X-Gm-Message-State: AGRZ1gIOfkuvWgNdbQZz7BHDFu8Kd1dgCmLAXQjCYuaYYUAHNcpDW2Ty dqL0bHgvUrtbXJACs6Jun2o07g== X-Received: by 2002:a2e:45d:: with SMTP id 90-v6mr12053208lje.110.1540899540118; Tue, 30 Oct 2018 04:39:00 -0700 (PDT) Received: from localhost (c-732171d5.07-21-73746f28.bbcust.telenor.se. [213.113.33.115]) by smtp.gmail.com with ESMTPSA id d2-v6sm3593735lfc.11.2018.10.30.04.38.59 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 30 Oct 2018 04:38:59 -0700 (PDT) From: Anders Roxell To: catalin.marinas@arm.com, will.deacon@arm.com Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, masami.hiramatsu@linaro.org, Anders Roxell , Laura Abbott , Arnd Bergmann , Ard Biesheuvel Subject: [PATCH v2] arm64: kprobe: make page to RO mode when allocate it Date: Tue, 30 Oct 2018 12:38:50 +0100 Message-Id: <20181030113850.31150-1-anders.roxell@linaro.org> X-Mailer: git-send-email 2.19.1 MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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()"). Cc: Laura Abbott Cc: Catalin Marinas Co-developed-by: Arnd Bergmann Co-developed-by: Ard Biesheuvel Signed-off-by: Arnd Bergmann Signed-off-by: Ard Biesheuvel Signed-off-by: Anders Roxell --- arch/arm64/kernel/probes/kprobes.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) -- 2.19.1 Acked-by: Will Deacon diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c index 9b65132e789a..decf483b4153 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, 1); - return aarch64_insn_patch_text(addrs, insns, 1); + return page; } /* arm kprobe: install breakpoint in text */