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.5 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 0F253C33C9E for ; Mon, 3 Feb 2020 04:55:48 +0000 (UTC) Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B35D52073C for ; Mon, 3 Feb 2020 04:55:47 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=russell.cc header.i=@russell.cc header.b="TotxA/Mk"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="iMY/H/Cy" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B35D52073C Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=russell.cc Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 489wWP1HpJzDqVZ for ; Mon, 3 Feb 2020 15:55:45 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=russell.cc (client-ip=64.147.123.19; helo=wout3-smtp.messagingengine.com; envelope-from=ruscur@russell.cc; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=russell.cc Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=russell.cc header.i=@russell.cc header.a=rsa-sha256 header.s=fm1 header.b=TotxA/Mk; dkim=pass (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm1 header.b=iMY/H/Cy; dkim-atps=neutral Received: from wout3-smtp.messagingengine.com (wout3-smtp.messagingengine.com [64.147.123.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 489wPK5GSDzDqQQ for ; Mon, 3 Feb 2020 15:50:29 +1100 (AEDT) Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 21891725; Sun, 2 Feb 2020 23:50:26 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Sun, 02 Feb 2020 23:50:26 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=russell.cc; h= message-id:subject:from:to:cc:date:in-reply-to:references :content-type:mime-version:content-transfer-encoding; s=fm1; bh= bFOUHt7V3C4f1TpU4gQyO6wPuUUc6O2MFNH7/EMga9Y=; b=TotxA/MkYWmTcJAk iEa8IfjfrpQHh/fIOtqge0pfHDmUhttrkia5OwjxpGNFlvfc5jc5KN7qmEif5CnF tg4mSF9rOj/BFlu8/gO/cQeevwy5Ri1GJEKf4bcWkOyT4PFq5/AL7lGIfLAl6p1z RwUq2J9kt/X45MZVQA7qnDjxX79QQ0Ry7lAAIP0Kii6BOJ2M0e7wSCrfVwDorsB7 nlSHDAT34qPuxfNizWOR/TmVuSiBABN7QM8uJOrpJ4SHgcxpmdVtuWdSIw6YddT5 0yiBurE32MxAyGDPLFvXnzjqnJ+yZquS4UWEZQJICXE0KMmyauAgRLbbHJnI8MAT C6X1iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=bFOUHt7V3C4f1TpU4gQyO6wPuUUc6O2MFNH7/EMga 9Y=; b=iMY/H/Cyr70rWB30iy2bxK99ytKSA+1JzLtXQRUzGpg2v8YUhtuMe+nDY AuwlKz5elc9XaW9hH57P/xTa0FOP5X5lrq3Viom3XLtIycWZuOdG6Ln0TFt/5wo6 Lk7aL8iLYmsK61grO1iZlVXBEUZO/7IQ+ZJTc01ocBkc4m3Ewuz6x8Z/sxjMiEvF ysE35gQCdJ8Q+F+vSoPAMSORUrEjCEX7xAUPix1V8zxx87GyUgBIRgm7jssGhs8f ckgzs2djm5nNnZTuG+Kw6rFwd+mLGj0LGdBhmRiDYD1pVtBuASxBUy+m+g2vCXfV hGP6Gp3enxMZsYxMqO3RWVP5ocQQg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrgeeigdejgecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenfg hrlhcuvffnffculddutddmnecujfgurhepkffuhffvffgjfhgtfggggfesthejredttder jeenucfhrhhomheptfhushhsvghllhcuvehurhhrvgihuceorhhushgtuhhrsehruhhssh gvlhhlrdgttgeqnecukfhppeduvddvrdelledrkedvrddutdenucevlhhushhtvghrufhi iigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehruhhstghurhesrhhushhsvghllh drtggt X-ME-Proxy: Received: from crackle.ozlabs.ibm.com (unknown [122.99.82.10]) by mail.messagingengine.com (Postfix) with ESMTPA id 5C8A4328005D; Sun, 2 Feb 2020 23:50:22 -0500 (EST) Message-ID: <3078df74c232e54aef3e8bb3523587a3053ab0ec.camel@russell.cc> Subject: Re: [PATCH v2 2/7] powerpc/kprobes: Mark newly allocated probes as RO From: Russell Currey To: Christophe Leroy , Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman Date: Mon, 03 Feb 2020 15:50:19 +1100 In-Reply-To: <01fdf1b7375b3e1e43a634bf6719b576c4c5db11.1580477672.git.christophe.leroy@c-s.fr> References: <84be5ad6a996adf5693260749dcb4d8c69182073.1580477672.git.christophe.leroy@c-s.fr> <01fdf1b7375b3e1e43a634bf6719b576c4c5db11.1580477672.git.christophe.leroy@c-s.fr> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.34.3 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org Errors-To: linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Sender: "Linuxppc-dev" On Fri, 2020-01-31 at 13:34 +0000, Christophe Leroy wrote: > With CONFIG_STRICT_KERNEL_RWX=y and CONFIG_KPROBES=y, there will be > one > W+X page at boot by default. This can be tested with > CONFIG_PPC_PTDUMP=y and CONFIG_PPC_DEBUG_WX=y set, and checking the > kernel log during boot. > > powerpc doesn't implement its own alloc() for kprobes like other > architectures do, but we couldn't immediately mark RO anyway since we > do > a memcpy to the page we allocate later. After that, nothing should > be > allowed to modify the page, and write permissions are removed well > before the kprobe is armed. > > The memcpy() would fail if >1 probes were allocated, so use > patch_instruction() instead which is safe for RO. > > Reviewed-by: Daniel Axtens > Signed-off-by: Russell Currey > Signed-off-by: Christophe Leroy > --- > v2: removed the redundant flush > --- > arch/powerpc/kernel/kprobes.c | 8 ++++---- > 1 file changed, 4 insertions(+), 4 deletions(-) > > diff --git a/arch/powerpc/kernel/kprobes.c > b/arch/powerpc/kernel/kprobes.c > index 2d27ec4feee4..d3e594e6094c 100644 > --- a/arch/powerpc/kernel/kprobes.c > +++ b/arch/powerpc/kernel/kprobes.c > @@ -24,6 +24,7 @@ > #include > #include > #include > +#include > > DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; > DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); > @@ -124,13 +125,12 @@ int arch_prepare_kprobe(struct kprobe *p) > } > > if (!ret) { > - memcpy(p->ainsn.insn, p->addr, > - MAX_INSN_SIZE * > sizeof(kprobe_opcode_t)); > + patch_instruction(p->ainsn.insn, *p->addr); > p->opcode = *p->addr; > - flush_icache_range((unsigned long)p->ainsn.insn, > - (unsigned long)p->ainsn.insn + > sizeof(kprobe_opcode_t)); > } > > + set_memory_ro((unsigned long)p->ainsn.insn, 1); > + Since this can be called multiple times on the same page, can avoid by implementing: void *alloc_insn_page(void) { void *page; page = vmalloc_exec(PAGE_SIZE); if (page) set_memory_ro((unsigned long)page, 1); return page; } Which is pretty much the same as what's in arm64. Works for me and passes ftracetest, I was originally doing this but cut it because it broke with the memcpy, but works with patch_instruction(). > p->ainsn.boostable = 0; > return ret; > }