Received: by 2002:a17:90a:9307:0:0:0:0 with SMTP id p7csp3950315pjo; Tue, 3 Mar 2020 09:58:56 -0800 (PST) X-Google-Smtp-Source: ADFU+vuuEWqvXyyIba+moioQ3D5EztL98NxH9HVz8tKqhpYPJ4kCX/mVL7cpfAiDBsKAP6HrNCVA X-Received: by 2002:a54:410e:: with SMTP id l14mr3219758oic.42.1583258336227; Tue, 03 Mar 2020 09:58:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583258336; cv=none; d=google.com; s=arc-20160816; b=it3m/03eOT65WkQuNT39K/6MfWI4X1/HHzcHklIwS8LQj5IFRSyJW1zXToJRmc+mwv Hw27NEGsZNJIlkhAeDit0OfKz3G/veGdgSn4iTcpHT2ZxrUIFN1HIptY1sks0FxcX5Mr CK9THgL61FJeOXqiqwJgJspkWGGloN0eWEfYeLIUf1mRW3zCVr22nBkGCnJRwD16T8Sf ORwaOb5KCTzFiIptzE/xdSvyHNWnjcmUN9tHfxVXHitFY/o+pN7GLPFYl8K6nIm9bHTd m6lrMK8iXpWpeEHMRNjh+Dtqo1Gi1MszECmwcP4GSDn6XpRqfwbq2OtHg4CXm0Y0L8bx 6HhA== 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:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=3yxgozz6smISzUo7zAfweT1FlHUL5Zl5TuXHAy/nSKE=; b=idOe6KDfKyE5VLNZUY/jKSic/raBBZMUceDhHJaiPtDAtS+Fz+UPDllcWBbDR1hd3B KY6UjtUOHnjBS8KRRZEbqIYJe+uA0mvIg+auM4MOXi/KQMwGDOlf5Xn6XF6e0AaJhLoW XEukPfBZHWhVrurwZvpnANy6TM7iA7ygQIuFmV/MxKXn9AjzCVTwQLnvZ6KZnFbA5uMg AzXcN9CdpJJ92kF22pk7HT+8AzB3wskLl209zKMiEFbsuGhhj1FItviqQRF1d+j3rBFM gMmVs9NKVK7pwmnaCtcZJa1zaz53efxuPIT0SgbTpk8sfqs5RtWrSS55I0hI59NOvSGV MdIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=scKyqlXv; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g35si1670282otg.32.2020.03.03.09.58.43; Tue, 03 Mar 2020 09:58:56 -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=@kernel.org header.s=default header.b=scKyqlXv; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733107AbgCCR5f (ORCPT + 99 others); Tue, 3 Mar 2020 12:57:35 -0500 Received: from mail.kernel.org ([198.145.29.99]:40142 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733100AbgCCR5e (ORCPT ); Tue, 3 Mar 2020 12:57:34 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 41D9720656; Tue, 3 Mar 2020 17:57:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1583258253; bh=ul3BD1JVY1ikCvVn58M0Rw/k60t1PMUufy4IbsK5uNk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=scKyqlXvfLj82FemR4G7sVoIguC1xPTxRiZhUhHdI8W9ltUL6MrvhVJepLzTIicNz 4ADHMR+V2WDMSvyCv0HBdUzazUCSijuRkX13TocJYrP0ouR2N3tQJgt0xTfAdMDWH+ eP/3MMlqpHCw+k4a7o1I0Ta1ZawgIKEkbG1MyRNo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alexei Starovoitov , Masami Hiramatsu , Andy Lutomirski , Borislav Petkov , Linus Torvalds , Peter Zijlstra , Steven Rostedt , Thomas Gleixner , bristot@redhat.com, Ingo Molnar Subject: [PATCH 5.4 128/152] kprobes: Set unoptimized flag after unoptimizing code Date: Tue, 3 Mar 2020 18:43:46 +0100 Message-Id: <20200303174317.356855024@linuxfoundation.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200303174302.523080016@linuxfoundation.org> References: <20200303174302.523080016@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Masami Hiramatsu commit f66c0447cca1281116224d474cdb37d6a18e4b5b upstream. Set the unoptimized flag after confirming the code is completely unoptimized. Without this fix, when a kprobe hits the intermediate modified instruction (the first byte is replaced by an INT3, but later bytes can still be a jump address operand) while unoptimizing, it can return to the middle byte of the modified code, which causes an invalid instruction exception in the kernel. Usually, this is a rare case, but if we put a probe on the function call while text patching, it always causes a kernel panic as below: # echo p text_poke+5 > kprobe_events # echo 1 > events/kprobes/enable # echo 0 > events/kprobes/enable invalid opcode: 0000 [#1] PREEMPT SMP PTI RIP: 0010:text_poke+0x9/0x50 Call Trace: arch_unoptimize_kprobe+0x22/0x28 arch_unoptimize_kprobes+0x39/0x87 kprobe_optimizer+0x6e/0x290 process_one_work+0x2a0/0x610 worker_thread+0x28/0x3d0 ? process_one_work+0x610/0x610 kthread+0x10d/0x130 ? kthread_park+0x80/0x80 ret_from_fork+0x3a/0x50 text_poke() is used for patching the code in optprobes. This can happen even if we blacklist text_poke() and other functions, because there is a small time window during which we show the intermediate code to other CPUs. [ mingo: Edited the changelog. ] Tested-by: Alexei Starovoitov Signed-off-by: Masami Hiramatsu Cc: Andy Lutomirski Cc: Borislav Petkov Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Thomas Gleixner Cc: bristot@redhat.com Fixes: 6274de4984a6 ("kprobes: Support delayed unoptimizing") Link: https://lkml.kernel.org/r/157483422375.25881.13508326028469515760.stgit@devnote2 Signed-off-by: Ingo Molnar Signed-off-by: Greg Kroah-Hartman --- kernel/kprobes.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -510,6 +510,8 @@ static void do_unoptimize_kprobes(void) arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list); /* Loop free_list for disarming */ list_for_each_entry_safe(op, tmp, &freeing_list, list) { + /* Switching from detour code to origin */ + op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; /* Disarm probes if marked disabled */ if (kprobe_disabled(&op->kp)) arch_disarm_kprobe(&op->kp); @@ -649,6 +651,7 @@ static void force_unoptimize_kprobe(stru { lockdep_assert_cpus_held(); arch_unoptimize_kprobe(op); + op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; if (kprobe_disabled(&op->kp)) arch_disarm_kprobe(&op->kp); } @@ -676,7 +679,6 @@ static void unoptimize_kprobe(struct kpr return; } - op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; if (!list_empty(&op->list)) { /* Dequeue from the optimization queue */ list_del_init(&op->list);