Received: by 2002:ac0:aa62:0:0:0:0:0 with SMTP id w31-v6csp2075179ima; Mon, 22 Oct 2018 03:57:36 -0700 (PDT) X-Google-Smtp-Source: ACcGV62qnNNRvG+aocjcU8HaPHe1djb73VgI30ocNdV8INFKjTjfGIX6U8oIg6Ogkv39yOXnbGwT X-Received: by 2002:aa7:80cd:: with SMTP id a13-v6mr44678830pfn.86.1540205856582; Mon, 22 Oct 2018 03:57:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540205856; cv=none; d=google.com; s=arc-20160816; b=PXsNb/rTN3eo0bafno7D0nKaiNW3L5aBxuanappiKG8sbBRg+/b95cUGOWzl5bcOh6 eU1VC5y2kwom8R6Y8j6tNnxenJmanHYFGD+gScT9rbxq1QL0G/97WgkuFqy6lOp8jPgR u/3Q40I57lvuJB9rGGw1U4VFoxQmcW6obdBKhacPwE2HfOI7Ft7SR2uH2ygMYrwTBUMN VBB1EW6DqM6tTJflEn5BA+O1BzvjoTEm/8zUa+ShevsJ4vmAfMjEPtRYG3HW2nQlWpdy 6dEuqS1otNZAHdPB1mp4b5bew25KPEDL2LXSB1zAIxp/y+G0vebRzOrNy6+HGU3NyCNJ ZRig== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=FxUq16/lM/BuGprTt1NGFyhvtVuKC7FchER62kSGApc=; b=RhTwlajiPPB65vjcRZx4jLnBQpCIpDzYzpuXaiik1QLZ+70PmW7rRdsDp1iwi1GzS1 t12Ath5hHqJDyXDnnOpEm0Ilh57t719h8YTzfFdnIixjLW5ZLmM/Qr2H6lCPZCBkzsCC 7H0ymqKMl83ekTXxjY33RKTcdR1Xc0rXG2IloIfmc0KK7SsuesN2mvFhG02JIzDXe2AQ vKauTv8zaTSCfAMMeQYlTKGriZGZLr5o9cBg3axkyNPum85VDiFe4hJbpLXkw13yyZEL xjflLzMTOpDVwImVX51Yjcx4uUUrl6/9TLaQFoUqqGO/PAeRLsWq8cbW6IM4kQhiBpcy o0zg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c22-v6si32823018pgk.292.2018.10.22.03.57.21; Mon, 22 Oct 2018 03:57:36 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728775AbeJVTOZ (ORCPT + 99 others); Mon, 22 Oct 2018 15:14:25 -0400 Received: from mail-wm1-f67.google.com ([209.85.128.67]:34709 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727857AbeJVTOZ (ORCPT ); Mon, 22 Oct 2018 15:14:25 -0400 Received: by mail-wm1-f67.google.com with SMTP id z25-v6so8922009wmf.1 for ; Mon, 22 Oct 2018 03:56:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=FxUq16/lM/BuGprTt1NGFyhvtVuKC7FchER62kSGApc=; b=sp2QjzQLwYRwXsqdEJfYQqP4q5Z3bin326mFk5uqBVwylvQJnoMlKP3FOSeKqKlgob zmor5SSC4hGd3+T8+7foDRFWUKTnF85Y/a7H5C8uWnmmmkTRQY4qXuCdFNggR74S/FUH GFkMa13nNid+u+9HuzDiOYyCv0wFZsXOG0Sbzjtc4KdIvFBFhfMyYmOL5x5AAiq4+Niz UDBTjokiMXgCS2LK2LBO611plu+4eY6ycgDcCxXGSWPhzdyHMHQAoDe1fhSe5vJfcp0I nVG7msE+/C8XDI5ELJxNR0wT7/S/VihTYxKMzQLrghafXxo1EvFQg+rg8lCdkE3eTrxq kTxQ== X-Gm-Message-State: ABuFfoga9kGMb2MSkyKJIfPtZPM1zfoSdAqZNj7PYIkUA3tSzo6VFuLa 5ox44W1jQ2xHtLXx4LAbWxdm2Kzgb14= X-Received: by 2002:a1c:930c:: with SMTP id v12-v6mr16065587wmd.9.1540205779727; Mon, 22 Oct 2018 03:56:19 -0700 (PDT) Received: from [172.22.36.222] ([167.98.65.38]) by smtp.gmail.com with ESMTPSA id n7-v6sm479947wrx.17.2018.10.22.03.56.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 22 Oct 2018 03:56:18 -0700 (PDT) Subject: Re: [PATCH] arm64: kprobe: make page to RO mode when allocate it To: Anders Roxell , catalin.marinas@arm.com, will.deacon@arm.com Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Arnd Bergmann , Ard Biesheuvel References: <20181015111600.5479-1-anders.roxell@linaro.org> From: Laura Abbott Message-ID: <071b035a-4eac-525c-f073-3e63867078c3@redhat.com> Date: Mon, 22 Oct 2018 03:56:18 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <20181015111600.5479-1-anders.roxell@linaro.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 10/15/2018 04:16 AM, Anders Roxell wrote: > 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()"). > Reviewed-by: Laura Abbott > 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 > --- > 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 */ >