From patchwork Mon Oct 24 11:31:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 9533 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp483659wru; Mon, 24 Oct 2022 07:27:56 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7RqEHuO50RHxQCFXBk1yVeqGeXtwbqbq32XkPGmbOkWsEk0UrlSsyeBKcqgz4sFTO4PCii X-Received: by 2002:a17:90b:3715:b0:213:2d7:3164 with SMTP id mg21-20020a17090b371500b0021302d73164mr8376052pjb.191.1666621675961; Mon, 24 Oct 2022 07:27:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666621675; cv=none; d=google.com; s=arc-20160816; b=KghxxQsJscE6V1qOxgIkDXHCG3cjdHF8Y3EbqJJiseHfvkhG+WkKveKVxRPu+cVZEQ vW1acZE3mAZMyVJZjJpFkUzS73udrjEvpQwL1KfY+iM84n/Mz8CqRmGulX49UVWnkaMD swA5boqna8oTwkoRupQzTiq5cbb+JD2rceS9tFuoJ+8MMxaXPoHzY/ZLc7pC0TaoS87G 6ssGo0bHZfs85lcRM7pl2wGCwH+o4/6kRmHFUamGAGFN7a/dYMsKacyY8mUjZ3fyVOzL DKdFioTbbGW2j8Oj+kQFB8VZLpE6b32pHa6j6n3IepzOUlbe6E9/An/IAXzNAv4pTyZt ECeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=tAYR5gGf3T0UBEUc5Byh2JF8fRMd6YEDcgf1GBJ8kpQ=; b=nXi86QIawADLAAQw3qZCsw5mcxeE/2Y6Ia3HmWB7jKpC1S9cXaGyPGr5ScpFSYGHka PViB5DcRtg8WnMncy5gGalrIBryYAO356YtWBc8PEe0v6oKvilhCLVaDAaG4aET3vuM6 gvLLzCzcoxgx9jFN9lyEOxVrMl/R9LUYOQd85W5DLPNjKdaAOlfqf2dPa/iyF7NqgB5u QVQZiyVb11wNmrESI2SdW+ZcdAMdlQ5kFnH23Vbz5LChJxIT40cGjJiSMCqeXsGgNNBf gsnSB/OBFNLDX4hAhtu6ceZSOe47LKfx0veD9HZFlMQ0mdO1PXxpHPUSyVwotoigi0eI d+SA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="txC5/mAE"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bd9-20020a056a00278900b005676e2c36b4si18650720pfb.47.2022.10.24.07.27.40; Mon, 24 Oct 2022 07:27:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="txC5/mAE"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236989AbiJXO0P (ORCPT + 99 others); Mon, 24 Oct 2022 10:26:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237054AbiJXOYb (ORCPT ); Mon, 24 Oct 2022 10:24:31 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2290496206; Mon, 24 Oct 2022 05:59:07 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 1601A61297; Mon, 24 Oct 2022 12:49:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F27C9C433C1; Mon, 24 Oct 2022 12:49:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666615753; bh=zc7Mmj+nePzUnH7kV1eXONZglNklXKlQB80i0dc9GCA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=txC5/mAEDETToGKHqFlz+u7t+82fyB6YTUT3H/n0tqJiQuA3/Ni3wXM9xfH17EzCh ZhxEsIj062rBfbbb8/XNCFmTogoXggbfwKr3ihN+1OXo/upwKyVVJY9S+X78VkDrUm AHhB5TyRPJckxJqMGp9SHmbAOiGls7NscTe/Holw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Li Huafei , Michael Ellerman , Sasha Levin Subject: [PATCH 5.15 371/530] powerpc/kprobes: Fix null pointer reference in arch_prepare_kprobe() Date: Mon, 24 Oct 2022 13:31:55 +0200 Message-Id: <20221024113101.834230577@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113044.976326639@linuxfoundation.org> References: <20221024113044.976326639@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747579490379351310?= X-GMAIL-MSGID: =?utf-8?q?1747579490379351310?= From: Li Huafei [ Upstream commit 97f88a3d723162781d6cbfdc7b9617eefab55b19 ] I found a null pointer reference in arch_prepare_kprobe(): # echo 'p cmdline_proc_show' > kprobe_events # echo 'p cmdline_proc_show+16' >> kprobe_events Kernel attempted to read user page (0) - exploit attempt? (uid: 0) BUG: Kernel NULL pointer dereference on read at 0x00000000 Faulting instruction address: 0xc000000000050bfc Oops: Kernel access of bad area, sig: 11 [#1] LE PAGE_SIZE=64K MMU=Radix SMP NR_CPUS=2048 NUMA PowerNV Modules linked in: CPU: 0 PID: 122 Comm: sh Not tainted 6.0.0-rc3-00007-gdcf8e5633e2e #10 NIP: c000000000050bfc LR: c000000000050bec CTR: 0000000000005bdc REGS: c0000000348475b0 TRAP: 0300 Not tainted (6.0.0-rc3-00007-gdcf8e5633e2e) MSR: 9000000000009033 CR: 88002444 XER: 20040006 CFAR: c00000000022d100 DAR: 0000000000000000 DSISR: 40000000 IRQMASK: 0 ... NIP arch_prepare_kprobe+0x10c/0x2d0 LR arch_prepare_kprobe+0xfc/0x2d0 Call Trace: 0xc0000000012f77a0 (unreliable) register_kprobe+0x3c0/0x7a0 __register_trace_kprobe+0x140/0x1a0 __trace_kprobe_create+0x794/0x1040 trace_probe_create+0xc4/0xe0 create_or_delete_trace_kprobe+0x2c/0x80 trace_parse_run_command+0xf0/0x210 probes_write+0x20/0x40 vfs_write+0xfc/0x450 ksys_write+0x84/0x140 system_call_exception+0x17c/0x3a0 system_call_vectored_common+0xe8/0x278 --- interrupt: 3000 at 0x7fffa5682de0 NIP: 00007fffa5682de0 LR: 0000000000000000 CTR: 0000000000000000 REGS: c000000034847e80 TRAP: 3000 Not tainted (6.0.0-rc3-00007-gdcf8e5633e2e) MSR: 900000000280f033 CR: 44002408 XER: 00000000 The address being probed has some special: cmdline_proc_show: Probe based on ftrace cmdline_proc_show+16: Probe for the next instruction at the ftrace location The ftrace-based kprobe does not generate kprobe::ainsn::insn, it gets set to NULL. In arch_prepare_kprobe() it will check for: ... prev = get_kprobe(p->addr - 1); preempt_enable_no_resched(); if (prev && ppc_inst_prefixed(ppc_inst_read(prev->ainsn.insn))) { ... If prev is based on ftrace, 'ppc_inst_read(prev->ainsn.insn)' will occur with a null pointer reference. At this point prev->addr will not be a prefixed instruction, so the check can be skipped. Check if prev is ftrace-based kprobe before reading 'prev->ainsn.insn' to fix this problem. Fixes: b4657f7650ba ("powerpc/kprobes: Don't allow breakpoints on suffixes") Signed-off-by: Li Huafei [mpe: Trim oops] Signed-off-by: Michael Ellerman Link: https://lore.kernel.org/r/20220923093253.177298-1-lihuafei1@huawei.com Signed-off-by: Sasha Levin --- arch/powerpc/kernel/kprobes.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/arch/powerpc/kernel/kprobes.c b/arch/powerpc/kernel/kprobes.c index 7a7cd6bda53e..61552f57db0b 100644 --- a/arch/powerpc/kernel/kprobes.c +++ b/arch/powerpc/kernel/kprobes.c @@ -140,7 +140,13 @@ int arch_prepare_kprobe(struct kprobe *p) preempt_disable(); prev = get_kprobe(p->addr - 1); preempt_enable_no_resched(); - if (prev && ppc_inst_prefixed(ppc_inst_read(prev->ainsn.insn))) { + + /* + * When prev is a ftrace-based kprobe, we don't have an insn, and it + * doesn't probe for prefixed instruction. + */ + if (prev && !kprobe_ftrace(prev) && + ppc_inst_prefixed(ppc_inst_read(prev->ainsn.insn))) { printk("Cannot register a kprobe on the second word of prefixed instruction\n"); ret = -EINVAL; }