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 E9AFDCA9EC7 for ; Sat, 2 Nov 2019 10:47:32 +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 9A1D4214AF for ; Sat, 2 Nov 2019 10:47:32 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=ellerman.id.au header.i=@ellerman.id.au header.b="jU8HtMgP" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9A1D4214AF Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=ellerman.id.au Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Received: from bilbo.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 474wkB597RzF5Rr for ; Sat, 2 Nov 2019 21:47:30 +1100 (AEDT) Received: from ozlabs.org (bilbo.ozlabs.org [203.11.71.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 474wgh74JLzF71n for ; Sat, 2 Nov 2019 21:45:20 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=ellerman.id.au Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=ellerman.id.au header.i=@ellerman.id.au header.b="jU8HtMgP"; dkim-atps=neutral Received: from authenticated.ozlabs.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mail.ozlabs.org (Postfix) with ESMTPSA id 474wgf5b5jz9sP3; Sat, 2 Nov 2019 21:45:18 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ellerman.id.au; s=201909; t=1572691520; bh=Jtyqd/wuFAS1+Aoh4ImFCwFhrDrs+nQrk2XwCSuohFM=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=jU8HtMgPJ5LVOjaQcPJMyylrWpWUU1hddriM0diG7HPFhaqMSKxA47FydNm0U3BE2 7BOLrxB9DPxAEzRxBiGhApn4cLGOF/Ml2YMDs3ua2+fUlqmnefxqyeYqeF1EcbgLxi XsnnFrXMxEUbIAzBD1enonawlvRDZ18ZvtL1TZ8Re1el5XkHNn1U9se5Ch4VpbBgYb 6GxBJmnV9ADH6BfdClBQsuQb31o/6yB2HYM1hiLRyoxOf5mB9rMv5GzWdHyA+vvWwP lzzN+hrl024/OdwXL1S/yRGgKO2IlYjC/ba/kMkd/qEstWj8PfWUg+bm58oc10thsw wrAXoNSVTSfGA== From: Michael Ellerman To: Russell Currey , linuxppc-dev@lists.ozlabs.org Subject: Re: [PATCH v5 2/5] powerpc/kprobes: Mark newly allocated probes as RO In-Reply-To: <20191030073111.140493-3-ruscur@russell.cc> References: <20191030073111.140493-1-ruscur@russell.cc> <20191030073111.140493-3-ruscur@russell.cc> Date: Sat, 02 Nov 2019 21:45:18 +1100 Message-ID: <8736f636bl.fsf@mpe.ellerman.id.au> MIME-Version: 1.0 Content-Type: text/plain 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: ajd@linux.ibm.com, kernel-hardening@lists.openwall.com, npiggin@gmail.com, joel@jms.id.au, dja@axtens.net Errors-To: linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Sender: "Linuxppc-dev" Russell Currey writes: > 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. > > Thus mark newly allocated probes as read-only once it's safe to do so. > > Signed-off-by: Russell Currey > --- > arch/powerpc/kernel/kprobes.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/arch/powerpc/kernel/kprobes.c b/arch/powerpc/kernel/kprobes.c > index 2d27ec4feee4..2610496de7c7 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); > @@ -131,6 +132,8 @@ int arch_prepare_kprobe(struct kprobe *p) > (unsigned long)p->ainsn.insn + sizeof(kprobe_opcode_t)); > } > > + set_memory_ro((unsigned long)p->ainsn.insn, 1); > + That comes from: p->ainsn.insn = get_insn_slot(); Which ends up in __get_insn_slot() I think. And that looks very much like it's going to hand out multiple slots per page, which isn't going to work because you've just marked the whole page RO. So I would expect this to crash on the 2nd kprobe that's installed. Have you tested it somehow? I think this code should just use patch_instruction() rather than memcpy(). cheers