Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3828082imu; Mon, 7 Jan 2019 10:09:30 -0800 (PST) X-Google-Smtp-Source: ALg8bN6qD3lO66yMp8T4R631Yv6iZCejnJJ+fjKre4qfvRdCeWE3/BvxrJ4he39c9mYtftaq0T5u X-Received: by 2002:a63:94:: with SMTP id 142mr57732578pga.74.1546884570809; Mon, 07 Jan 2019 10:09:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546884570; cv=none; d=google.com; s=arc-20160816; b=LR1rP524AqFHBcQCzCDXB1lrLSdpwzYwm8ZScQQsBQNH2+H7wvVAwXdyktfTcLggpd yfIzux0n3JJRxeFV3YSxWc+iFeCdLxS82lOC/ahUOD2KIl2v9C42OZA9xc4fwwFJlNII vS7jNGeCdITRWXsFp39ueK6Kr4dP45kMjZ/7XSBdkfR96JFdKI2Muz2MBkIcJ36lc/bm pQVSj1TIzl4Xgo7hakL0HkkouSr5iCunlsRI3V4nNl9xdI7c/ZNt8lNtI9V+Ajy6RcIg bmT05AC4j58CpA8sEmtx/LF6w9Abvg/HybHEh9UDnYDXyLDeokkFrEiomKCR+4h+ZevJ Z5dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=5CQKOvPWttDZY0VU1jt6gpwFAxeXcKCJjLZJt7iyGWM=; b=wEfA1YJ5TXCwHBew+EX7w8JS805F+j/hpyAt0+VaxSmjsCsk/xJlFshCTTJoQsapzE cxAoFf98BM7keL3ZeFJQlyeG9AT2MtD0rLgjlUfQ8Bh0Q7dT2ofdOcw9EYSfsGVwpd9u cyxIYYBa0nUthjwGSGNNkCDLkCxywbyIhnXnSzISo7YQIX7smJd+puoapnXnpz+SalQh iV+PqmaoVkDEQglQ8pncwn+pechOF5ZozLFlv33q6w6TzSaJn3V+tJDHO77061xv1q6z Ofl7ZCRP6FMUPD/TV5eEH64MOifr/KC0MSaJKMUEUKcjojeuqJVsRIIWsIa+aEgY5Bpy IRhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Insi8KhD; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 61si13762378plz.117.2019.01.07.10.09.15; Mon, 07 Jan 2019 10:09:30 -0800 (PST) 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=@gmail.com header.s=20161025 header.b=Insi8KhD; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727827AbfAGR2e (ORCPT + 99 others); Mon, 7 Jan 2019 12:28:34 -0500 Received: from mail-wm1-f65.google.com ([209.85.128.65]:36196 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727223AbfAGR2e (ORCPT ); Mon, 7 Jan 2019 12:28:34 -0500 Received: by mail-wm1-f65.google.com with SMTP id p6so1718816wmc.1 for ; Mon, 07 Jan 2019 09:28:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=5CQKOvPWttDZY0VU1jt6gpwFAxeXcKCJjLZJt7iyGWM=; b=Insi8KhDGSFVIYljffZe18Dxn8LST09VxRvMWsG9kIJ1RxN75TQDuLw1b83COfuPWN 3GHfxhZVKdwQ2RYgZmidcGrWRdrlIYxp+DKUnqTH+v25Ai/adhr9cf73vvY+Pp8ML8tc Dh0q9vCNrrXny/3AEQsxrw1QotV2umVNEXDTsEyI+79IRCgN+s9aQRzBr/HawzWGDE1o 4RuiNf+dN0AFis5Xd0Xp0LWcobGOaCnntFRsqk4dB+awrFIT4IFW1AHJcJ7vuZnmDmNw 5M5DmH//I8bFCc+MKIckN+r2GM1snjdzAoHv63wKHJLSQxIiq8YmrDabSc1MZ5eorhh8 RiSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=5CQKOvPWttDZY0VU1jt6gpwFAxeXcKCJjLZJt7iyGWM=; b=nKIGnX1rzE4PjgMI2fvIfZKcMMj7Eh2IIgNey+z8N5n3DHhI1WHi+0O2dT75RTzzgv Asdv5T2PtNLeThzjX5giOQaG+s3eRc9vrUAAxhdfapFbuOaS51Skuw+/0cM+WPd94uVr 6Huopa0xDCKXPc673ce2AJyC8fvXlGvY53p0xSjVfb1SMuQd7wOZ9k+UQruTaiHOvC6D UW1u0q0ptRF1FkPVMTZ6L23EdCdl1LSLtjLTiNU/4seLY45m3MHCmZq+8ym2bezQeHcB 6OtO6zZt/HsGCCsPNAQIjcaIDpFBr+w3kF1RBxoPGZkrlc6hjvKNDOUCQSI4dwtBZewi Cw+g== X-Gm-Message-State: AJcUukfW5UVjWKVi7Ug9V/xGiSlfeNCUP6TJh4vdmts/BJUh8BVesCZQ XnUii6q3TzroIvIuCMqLUw== X-Received: by 2002:a1c:de57:: with SMTP id v84mr9281939wmg.55.1546882111098; Mon, 07 Jan 2019 09:28:31 -0800 (PST) Received: from localhost (host89-130-dynamic.43-79-r.retail.telecomitalia.it. [79.43.130.89]) by smtp.gmail.com with ESMTPSA id e16sm59709081wrn.72.2019.01.07.09.28.30 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 Jan 2019 09:28:30 -0800 (PST) Date: Mon, 7 Jan 2019 18:28:29 +0100 From: Andrea Righi To: Masami Hiramatsu Cc: Ingo Molnar , peterz@infradead.org, Mathieu Desnoyers , linux-kernel , Steven Rostedt Subject: Re: [PATCH 0/2] kprobes: Fix kretprobe incorrect stacking order problem Message-ID: <20190107172829.GB1944@xps-13> References: <154686789378.15479.2886543882215785247.stgit@devbox> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <154686789378.15479.2886543882215785247.stgit@devbox> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jan 07, 2019 at 10:31:34PM +0900, Masami Hiramatsu wrote: > Hello, > > On recent talk with Andrea, I started more precise investigation on > the kernel panic with kretprobes on notrace functions, which Francis > had been reported last year ( https://lkml.org/lkml/2017/7/14/466 ). > > At first, I tried to reproduce the issue. I picked up __fdget and > ftrace_ops_assist_func as probed functions. > With CONFIG_KPROBE_EVENTS_ON_NOTRACE=y, I could reproduce the kernel > panic as below. > > ===== > /sys/kernel/debug/tracing # echo "r:event_1 __fdget" >> kprobe_events > /sys/kernel/debug/tracing # echo "r:event_2 ftrace_ops_assist_func" >> kprobe_events > /sys/kernel/debug/tracing # echo 1 > events/kprobes/enable > [ 70.491856] BUG: unable to handle kernel NULL pointer dereference at 0000000000000010 > [ 70.493203] PGD 800000001c62e067 P4D 800000001c62e067 PUD 1b5bf067 PMD 0 > [ 70.494247] Oops: 0010 [#1] PREEMPT SMP PTI > [ 70.494918] CPU: 6 PID: 1210 Comm: sh Not tainted 4.20.0-rc3+ #58 > [ 70.495931] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.10.1-0-g8891697-prebuilt.qemu-project.org 04/01/2014 > [ 70.497906] RIP: 0010:0x10 > [ 70.498465] Code: Bad RIP value. > [ 70.499077] RSP: 0018:ffffb1d4c0347e78 EFLAGS: 00010246 > [ 70.499959] RAX: 00000000fffffff7 RBX: 0000000000000000 RCX: 0000000000000000 > [ 70.501383] RDX: ffff88f19f9c4f80 RSI: ffffffffb7d75e12 RDI: ffffffffb7d0ede7 > [ 70.502501] RBP: 00007ffc7061af20 R08: 0000000080000002 R09: ffff88f19f9c4f80 > [ 70.503698] R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000005401 > [ 70.504810] R13: 0000000000000000 R14: ffffb1d4c0347f58 R15: 0000000000000000 > [ 70.506028] FS: 0000000000922880(0000) GS:ffff88f19d380000(0000) knlGS:0000000000000000 > [ 70.507354] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > [ 70.508271] CR2: ffffffffffffffe6 CR3: 000000001f916000 CR4: 00000000000006e0 > [ 70.509419] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 > [ 70.510803] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 > [ 70.511748] Call Trace: > [ 70.512225] ? ksys_ioctl+0x70/0x70 > [ 70.512884] ? __fdget+0x5/0x10 > [ 70.513454] ? __fdget+0x5/0x10 > [ 70.513980] ? copy_oldmem_page_encrypted+0x20/0x20 > [ 70.514815] ? __x64_sys_ioctl+0x16/0x20 > [ 70.515596] ? do_syscall_64+0x50/0x100 > [ 70.516229] ? entry_SYSCALL_64_after_hwframe+0x49/0xbe > [ 70.517143] Modules linked in: > [ 70.517806] CR2: 0000000000000010 > [ 70.518527] ---[ end trace ece844ac05189f10 ]--- > [ 70.519417] RIP: 0010:0x10 > [ 70.520026] Code: Bad RIP value. > [ 70.520800] RSP: 0018:ffffb1d4c0347e78 EFLAGS: 00010246 > [ 70.521948] RAX: 00000000fffffff7 RBX: 0000000000000000 RCX: 0000000000000000 > [ 70.523315] RDX: ffff88f19f9c4f80 RSI: ffffffffb7d75e12 RDI: ffffffffb7d0ede7 > [ 70.524515] RBP: 00007ffc7061af20 R08: 0000000080000002 R09: ffff88f19f9c4f80 > [ 70.525702] R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000005401 > [ 70.526715] R13: 0000000000000000 R14: ffffb1d4c0347f58 R15: 0000000000000000 > [ 70.527673] FS: 0000000000922880(0000) GS:ffff88f19d380000(0000) knlGS:0000000000000000 > [ 70.528896] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > [ 70.529851] CR2: ffffffffffffffe6 CR3: 000000001f916000 CR4: 00000000000006e0 > [ 70.530922] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 > [ 70.531907] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 > Killed > ===== > > This seems the kernel is trying to execute incorrect address. > > Next, I focused on the combination of probes. From Francis's report, > this issue caused by the combination of kretprobes, not kprobes. > I ensured that was true. > > r __fdget & r ftrace_ops_assist_func => NG > p __fdget & p ftrace_ops_assist_func => OK > p __fdget & r ftrace_ops_assist_func => OK > r __fdget & p ftrace_ops_assist_func => OK > > r: kretprobe, p: kprobe > > This gave me a hint of what happened. I can explain the cause of this > issue as below; > > Correct processing of kretprobe on probed-function. > > > -> > ->fentry > ->ftrace_ops_assist_func() > ->kprobe_ftrace_handler() > ...->pre_handler_kretprobe() > push the return address (caller) of probed-function to top of the > kretprobe list and replace it with kretprobe_trampoline. > <-(ftrace_ops_assist_func()) > <-(fentry) > <-(probed-function) > [kretprobe_trampoline] > ->tampoline_handler() > pop the return address (caller) from top of the kretprobe list > <-(trampoline_handler()) > > > When we put a kretprobe on ftrace_ops_assist_func(), below happens > > > -> > ->fentry > ->ftrace_ops_assist_func() > ->int3 > ->kprobe_int3_handler() > ...->pre_handler_kretprobe() > push the return address (*fentry*) of ftrace_ops_assist_func() to > top of the kretprobe list and replace it with kretprobe_trampoline. > <-kprobe_int3_handler() > <-(int3) > ->kprobe_ftrace_handler() > ...->pre_handler_kretprobe() > push the return address (caller) of probed-function to top of the > kretprobe list and replace it with kretprobe_trampoline. > <-(kprobe_ftrace_handler()) > <-(ftrace_ops_assist_func()) > [kretprobe_trampoline] > ->tampoline_handler() > pop the return address (caller) from top of the kretprobe list > <-(trampoline_handler()) > > [run caller with incorrect stack information] > <-() > !!KERNEL PANIC!! > > Therefore, this kernel panic happens only when we put 2 k*ret*probes on > ftrace_ops_assist_func() and other functions. If we put kprobes, it > doesn't cause any issue, since it doesn't change the return address. > > To fix (or just avoid) this issue, we can introduce a frame pointer > verification to skip wrong order entries. And I also would like to > blacklist those functions because those are part of ftrace-based > kprobe handling routine. > > BTW, this is not all of issues. To remove CONFIG_KPROBE_EVENTS_ON_NOTRACE > I'm trying to find out other notrace functions which can cause > kernel crash by probing. Mostly done on x86, so I'll post it > after this series. > > Thank you, Apart than the missing include in PATCH 2/2 everything else looks good to me. Tested-by: Andrea Righi Thanks! -Andrea