Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp626756lqm; Wed, 1 May 2024 10:35:46 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXBIeHse2c5iOsiuvkjFuK4NDOe5b1p7n9Rr9hwT59cZLNOfSU4wpMWtNvgT6Sb/ueqtONV87kwcYGMkaq7qQ00JfWFF0I8yNR1jJIs3Q== X-Google-Smtp-Source: AGHT+IGmI64iKdSh6sOVwCAq2LY/u0ze3bwZBFq4En1nCLD/Z1ozvLs8MTLR/J9/hT4o227MXAvh X-Received: by 2002:a17:90a:7f93:b0:2b1:3cc7:ae83 with SMTP id m19-20020a17090a7f9300b002b13cc7ae83mr2843887pjl.32.1714584946678; Wed, 01 May 2024 10:35:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714584946; cv=pass; d=google.com; s=arc-20160816; b=Do32VrT33GkoiUEvFO/YFNHwJNIM8DvT4bCjr5QWteh0iyZFw777QLawpREXOC76wr vVuCQs/NYTlTkVtOpka0cci1CTRh2wlzzwv9t2t9vSpBtCi/iiR5sj4Wdv6+jF04Ikd0 s9W+drehBYJ/evYYWJJtneXKsSsX/OITGzy8Vg9CujFGYfXszuYPkf59rAC30wm7tBEp DfpA1sis6OHawJQPxu31VeY/I4gBjRZlrtLef1lqTZ1VSLkUyzYZKFjnVaT7AkhQMMPk l2CAJfHPj2MmzV3zVMEriJw1eAd9z4ABZPCYgefb0vrP+NDolPpIwOJJRarWNItwueFi GpuQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=ReE0u306fVU60pYooK4V25TMuwmnJBt+knAmZgaAyQo=; fh=nf71chx+a0/TXdltyoGALPrugqluF+OuqITn6tLvM3E=; b=NYJnj3j6cOVNmgP/OtC3aQiUcvWNp/zTAqiLsmj0W+4B7R5/5tjl4kMu2M2T57uGpg /uzA5xPPuwxFtM5jFfcmuDjn3fzog8WsjeYgKK8eZKp35DvCyf/hqotM+CKmopHyC1Im 8y2Hj4DplRb6om94Z7khh+OBN7lqfKCI0c2xPIafCZ4nkatkbJw5vpc1QQ77GQ5aHtXh mIX2AMbqbiY+xeWSrQfWR6HYd0iahktk0iHY9hoA19dWgK8vG1R3WnEOhAieZt/Vi+ef rgg2U1JcLb+f+MVWa0I7LIO8mA8JCVQS6L0fuv4NtDyiWrFNwHyym5Ugc+mS1BLS8Zie VRvA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="n7eUQT/8"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-165655-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165655-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id cu5-20020a17090afa8500b002a290e2a581si1868462pjb.140.2024.05.01.10.35.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 10:35:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-165655-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="n7eUQT/8"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-165655-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165655-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id C5C88B22017 for ; Wed, 1 May 2024 17:35:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 325DC12FF89; Wed, 1 May 2024 17:35:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="n7eUQT/8" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DB46817C9B; Wed, 1 May 2024 17:35:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714584930; cv=none; b=kG7iDTDQTCM6hmB1ZKwAurAqXTfd5cLsj0yU7dIQHimCCWEVUNGRRQSCWM80f7Unc0SOE4yq0Roa542K7pIndKcw4dmLmx9GyONpS54AMj7/XHHcx3lcUvJa8D3BJRmzBWQ1adGtjkNrqaZYcWvoEQgyNq+VKBJx4lvJKzsRT6A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714584930; c=relaxed/simple; bh=i5Yel4re/gpYyg9W0x9D7AVuSGIzMUN9waF0CV8LGiA=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=bIsCORz6v+EIjbCjLZD+AvHLt2E7wOuvBEqRKtTeuXLxqogCBYWBmYDTbQJk29wWCUDIxV2ZXyj2hEORKRSPa/VmOLdy1UUs7ecvvXfQXWR6sGkOvKDywLJ2z/9T53vG0rBBdhdwICuvSQGpw2yr6c49tzFC5Sd3xQZA+6lHMhw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=n7eUQT/8; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 451F6C4AF18; Wed, 1 May 2024 17:35:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714584929; bh=i5Yel4re/gpYyg9W0x9D7AVuSGIzMUN9waF0CV8LGiA=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=n7eUQT/8mER933KsE9ph6iqTihSCWywhDiBnKKtOM/MK9itKMnUHn1SiKy9b5d2e+ d/m3W3goFdpo7WY0O06tLDaUK1tDBUuZVupR5/uMWFW699UBcoDeztC4L6IbAYrWMw OHmZ44uYNxK+s4Cv+wY8Ufe4ruAguL8j6uctiUJ98z6N1OywGYhimrx85YHHgvD5GW CaVDf04rnNdFEH3W/TrrI6oJBy+5s/DV8P2GpOv7XLIXBIN/0CXL3Cp5UH3gqNxkMT rpJ+C40Esk8W9E5qwKIuqSmPzWM+Rqji5d08uOxb9aRE0frNfdwGsOcIsQFR9X65Se tsbmpDVKgkeFg== Received: by mail-ed1-f41.google.com with SMTP id 4fb4d7f45d1cf-572af0b12b8so654488a12.2; Wed, 01 May 2024 10:35:29 -0700 (PDT) X-Forwarded-Encrypted: i=1; AJvYcCXSlxFypLulBhcFQmzid5j4FtjnGcgRkxxPd/HyhvTfcDggHO5pJ9NRIL58W9zFWSH4rqTU27bO2dAqvDdtGG9DGYr4IevRf+BHAZx5ODQWPJzb2QzqblcjEBVRIa1SuQpxCJ+rHhBmsQ97owlpfWg45efUuyZ3bQX4nxmOjDMJavfGJofuXSycikI9q0B10MS5oXA8hIgPipqFu6S9zM8u4ua0hJHnFVn0xPPk3k4jPR06Ceql+QJAWRMcZCoVzaC+ll12iN8= X-Gm-Message-State: AOJu0YyORGou7V6HbOUAe/v1QzpvmUsPGj0r4+v86srXS5fhc1iSE95O nWEKcAkg7ZOPGEKgvNUzXdnAOgQkmfnrk15qsVvv7V7E71WDOKVRezP+lnW1tQ5vazeJDL0fdax xt7Pn155pLPGl4YepFZUmiwoHFa0= X-Received: by 2002:a05:6402:11c8:b0:572:315a:b2d3 with SMTP id j8-20020a05640211c800b00572315ab2d3mr1958514edw.15.1714584927605; Wed, 01 May 2024 10:35:27 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240501162956.229427-1-stephen.s.brennan@oracle.com> In-Reply-To: <20240501162956.229427-1-stephen.s.brennan@oracle.com> From: Guo Ren Date: Thu, 2 May 2024 01:35:16 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3] kprobe/ftrace: bail out if ftrace was killed To: Stephen Brennan Cc: Steven Rostedt , Masami Hiramatsu , Mark Rutland , Huacai Chen , WANG Xuerui , "James E.J. Bottomley" , Helge Deller , Michael Ellerman , Nicholas Piggin , Christophe Leroy , "Aneesh Kumar K.V" , "Naveen N. Rao" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-csky@vger.kernel.org, loongarch@lists.linux.dev, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, May 2, 2024 at 12:30=E2=80=AFAM Stephen Brennan wrote: > > If an error happens in ftrace, ftrace_kill() will prevent disarming > kprobes. Eventually, the ftrace_ops associated with the kprobes will be > freed, yet the kprobes will still be active, and when triggered, they > will use the freed memory, likely resulting in a page fault and panic. > > This behavior can be reproduced quite easily, by creating a kprobe and > then triggering a ftrace_kill(). For simplicity, we can simulate an > ftrace error with a kernel module like [1]: > > [1]: https://github.com/brenns10/kernel_stuff/tree/master/ftrace_killer > > sudo perf probe --add commit_creds > sudo perf trace -e probe:commit_creds > # In another terminal > make > sudo insmod ftrace_killer.ko # calls ftrace_kill(), simulating bug > # Back to perf terminal > # ctrl-c > sudo perf probe --del commit_creds > > After a short period, a page fault and panic would occur as the kprobe > continues to execute and uses the freed ftrace_ops. While ftrace_kill() > is supposed to be used only in extreme circumstances, it is invoked in > FTRACE_WARN_ON() and so there are many places where an unexpected bug > could be triggered, yet the system may continue operating, possibly > without the administrator noticing. If ftrace_kill() does not panic the > system, then we should do everything we can to continue operating, > rather than leave a ticking time bomb. > > Signed-off-by: Stephen Brennan > --- > Changes in v3: > Don't expose ftrace_is_dead(). Create a "kprobe_ftrace_disabled" > variable and check it directly in the kprobe handlers. > Link to v1/v2 discussion: > https://lore.kernel.org/all/20240426225834.993353-1-stephen.s.brennan@o= racle.com/ > > arch/csky/kernel/probes/ftrace.c | 3 +++ > arch/loongarch/kernel/ftrace_dyn.c | 3 +++ > arch/parisc/kernel/ftrace.c | 3 +++ > arch/powerpc/kernel/kprobes-ftrace.c | 3 +++ > arch/riscv/kernel/probes/ftrace.c | 3 +++ > arch/s390/kernel/ftrace.c | 3 +++ > arch/x86/kernel/kprobes/ftrace.c | 3 +++ > include/linux/kprobes.h | 7 +++++++ > kernel/kprobes.c | 6 ++++++ > kernel/trace/ftrace.c | 1 + > 10 files changed, 35 insertions(+) > > diff --git a/arch/csky/kernel/probes/ftrace.c b/arch/csky/kernel/probes/f= trace.c > index 834cffcfbce3..7ba4b98076de 100644 > --- a/arch/csky/kernel/probes/ftrace.c > +++ b/arch/csky/kernel/probes/ftrace.c > @@ -12,6 +12,9 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned l= ong parent_ip, > struct kprobe_ctlblk *kcb; > struct pt_regs *regs; > > + if (unlikely(kprobe_ftrace_disabled)) > + return; > + For csky part. Acked-by: Guo Ren > bit =3D ftrace_test_recursion_trylock(ip, parent_ip); > if (bit < 0) > return; > diff --git a/arch/loongarch/kernel/ftrace_dyn.c b/arch/loongarch/kernel/f= trace_dyn.c > index 73858c9029cc..bff058317062 100644 > --- a/arch/loongarch/kernel/ftrace_dyn.c > +++ b/arch/loongarch/kernel/ftrace_dyn.c > @@ -287,6 +287,9 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned= long parent_ip, > struct kprobe *p; > struct kprobe_ctlblk *kcb; > > + if (unlikely(kprobe_ftrace_disabled)) > + return; > + > bit =3D ftrace_test_recursion_trylock(ip, parent_ip); > if (bit < 0) > return; > diff --git a/arch/parisc/kernel/ftrace.c b/arch/parisc/kernel/ftrace.c > index 621a4b386ae4..c91f9c2e61ed 100644 > --- a/arch/parisc/kernel/ftrace.c > +++ b/arch/parisc/kernel/ftrace.c > @@ -206,6 +206,9 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned= long parent_ip, > struct kprobe *p; > int bit; > > + if (unlikely(kprobe_ftrace_disabled)) > + return; > + > bit =3D ftrace_test_recursion_trylock(ip, parent_ip); > if (bit < 0) > return; > diff --git a/arch/powerpc/kernel/kprobes-ftrace.c b/arch/powerpc/kernel/k= probes-ftrace.c > index 072ebe7f290b..f8208c027148 100644 > --- a/arch/powerpc/kernel/kprobes-ftrace.c > +++ b/arch/powerpc/kernel/kprobes-ftrace.c > @@ -21,6 +21,9 @@ void kprobe_ftrace_handler(unsigned long nip, unsigned = long parent_nip, > struct pt_regs *regs; > int bit; > > + if (unlikely(kprobe_ftrace_disabled)) > + return; > + > bit =3D ftrace_test_recursion_trylock(nip, parent_nip); > if (bit < 0) > return; > diff --git a/arch/riscv/kernel/probes/ftrace.c b/arch/riscv/kernel/probes= /ftrace.c > index 7142ec42e889..a69dfa610aa8 100644 > --- a/arch/riscv/kernel/probes/ftrace.c > +++ b/arch/riscv/kernel/probes/ftrace.c > @@ -11,6 +11,9 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned l= ong parent_ip, > struct kprobe_ctlblk *kcb; > int bit; > > + if (unlikely(kprobe_ftrace_disabled)) > + return; > + > bit =3D ftrace_test_recursion_trylock(ip, parent_ip); > if (bit < 0) > return; > diff --git a/arch/s390/kernel/ftrace.c b/arch/s390/kernel/ftrace.c > index c46381ea04ec..7f6f8c438c26 100644 > --- a/arch/s390/kernel/ftrace.c > +++ b/arch/s390/kernel/ftrace.c > @@ -296,6 +296,9 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned= long parent_ip, > struct kprobe *p; > int bit; > > + if (unlikely(kprobe_ftrace_disabled)) > + return; > + > bit =3D ftrace_test_recursion_trylock(ip, parent_ip); > if (bit < 0) > return; > diff --git a/arch/x86/kernel/kprobes/ftrace.c b/arch/x86/kernel/kprobes/f= trace.c > index dd2ec14adb77..15af7e98e161 100644 > --- a/arch/x86/kernel/kprobes/ftrace.c > +++ b/arch/x86/kernel/kprobes/ftrace.c > @@ -21,6 +21,9 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned l= ong parent_ip, > struct kprobe_ctlblk *kcb; > int bit; > > + if (unlikely(kprobe_ftrace_disabled)) > + return; > + > bit =3D ftrace_test_recursion_trylock(ip, parent_ip); > if (bit < 0) > return; > diff --git a/include/linux/kprobes.h b/include/linux/kprobes.h > index 0ff44d6633e3..5fcbc254d186 100644 > --- a/include/linux/kprobes.h > +++ b/include/linux/kprobes.h > @@ -378,11 +378,15 @@ static inline void wait_for_kprobe_optimizer(void) = { } > extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent= _ip, > struct ftrace_ops *ops, struct ftrace_r= egs *fregs); > extern int arch_prepare_kprobe_ftrace(struct kprobe *p); > +/* Set when ftrace has been killed: kprobes on ftrace must be disabled f= or safety */ > +extern bool kprobe_ftrace_disabled __read_mostly; > +extern void kprobe_ftrace_kill(void); > #else > static inline int arch_prepare_kprobe_ftrace(struct kprobe *p) > { > return -EINVAL; > } > +static inline void kprobe_ftrace_kill(void) {} > #endif /* CONFIG_KPROBES_ON_FTRACE */ > > /* Get the kprobe at this addr (if any) - called with preemption disable= d */ > @@ -495,6 +499,9 @@ static inline void kprobe_flush_task(struct task_stru= ct *tk) > static inline void kprobe_free_init_mem(void) > { > } > +static inline void kprobe_ftrace_kill(void) > +{ > +} > static inline int disable_kprobe(struct kprobe *kp) > { > return -EOPNOTSUPP; > diff --git a/kernel/kprobes.c b/kernel/kprobes.c > index 65adc815fc6e..166ebf81dc45 100644 > --- a/kernel/kprobes.c > +++ b/kernel/kprobes.c > @@ -1068,6 +1068,7 @@ static struct ftrace_ops kprobe_ipmodify_ops __read= _mostly =3D { > > static int kprobe_ipmodify_enabled; > static int kprobe_ftrace_enabled; > +bool kprobe_ftrace_disabled; > > static int __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops, > int *cnt) > @@ -1136,6 +1137,11 @@ static int disarm_kprobe_ftrace(struct kprobe *p) > ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops, > ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enab= led); > } > + > +void kprobe_ftrace_kill() > +{ > + kprobe_ftrace_disabled =3D true; > +} > #else /* !CONFIG_KPROBES_ON_FTRACE */ > static inline int arm_kprobe_ftrace(struct kprobe *p) > { > diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c > index da1710499698..96db99c347b3 100644 > --- a/kernel/trace/ftrace.c > +++ b/kernel/trace/ftrace.c > @@ -7895,6 +7895,7 @@ void ftrace_kill(void) > ftrace_disabled =3D 1; > ftrace_enabled =3D 0; > ftrace_trace_function =3D ftrace_stub; > + kprobe_ftrace_kill(); > } > > /** > -- > 2.39.3 > --=20 Best Regards Guo Ren