From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19011 invoked by alias); 15 Sep 2009 05:19:57 -0000 Received: (qmail 18996 invoked by uid 22791); 15 Sep 2009 05:19:56 -0000 X-SWARE-Spam-Status: No, hits=-0.6 required=5.0 tests=AWL,BAYES_50,SPF_SOFTFAIL X-Spam-Check-By: sourceware.org Received: from e34.co.us.ibm.com (HELO e34.co.us.ibm.com) (32.97.110.152) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 15 Sep 2009 05:19:52 +0000 Received: from d03relay02.boulder.ibm.com (d03relay02.boulder.ibm.com [9.17.195.227]) by e34.co.us.ibm.com (8.14.3/8.13.1) with ESMTP id n8F58voH002715 for ; Mon, 14 Sep 2009 23:08:57 -0600 Received: from d03av02.boulder.ibm.com (d03av02.boulder.ibm.com [9.17.195.168]) by d03relay02.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id n8F5DD3j236080 for ; Mon, 14 Sep 2009 23:13:13 -0600 Received: from d03av02.boulder.ibm.com (loopback [127.0.0.1]) by d03av02.boulder.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id n8F5DBhp006922 for ; Mon, 14 Sep 2009 23:13:13 -0600 Received: from thinktux.in.ibm.com ([9.124.35.36]) by d03av02.boulder.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id n8F5D87A006851; Mon, 14 Sep 2009 23:13:09 -0600 Received: by thinktux.in.ibm.com (Postfix, from userid 500) id 52BBA89755; Tue, 15 Sep 2009 10:43:07 +0530 (IST) Date: Tue, 15 Sep 2009 05:19:00 -0000 From: Ananth N Mavinakayanahalli To: Masami Hiramatsu Cc: Frederic Weisbecker , Steven Rostedt , Ingo Molnar , lkml , systemtap , DLE , Jim Keniston , Andi Kleen , Christoph Hellwig , "Frank Ch. Eigler" , "H. Peter Anvin" , Jason Baron , "K.Prasad" , Lai Jiangshan , Li Zefan , Peter Zijlstra , Srikar Dronamraju , Tom Zanussi Subject: Re: [BUGFIX] kprobes: prevent re-registration of the same kprobe - take2 Message-ID: <20090915051307.GB26458@in.ibm.com> Reply-To: ananth@in.ibm.com References: <20090910235258.22412.29317.stgit@dhcp-100-2-132.bos.redhat.com> <20090910235329.22412.94731.stgit@dhcp-100-2-132.bos.redhat.com> <20090911031253.GD16396@nowhere> <20090913100713.GB14251@in.ibm.com> <4AADA0BB.4030307@redhat.com> <20090914100459.GA4446@in.ibm.com> <4AAE6E85.9020002@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4AAE6E85.9020002@redhat.com> User-Agent: Mutt/1.5.17 (2007-11-01) X-IsSubscribed: yes Mailing-List: contact systemtap-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Post: List-Help: , Sender: systemtap-owner@sourceware.org X-SW-Source: 2009-q3/txt/msg00720.txt.bz2 On Mon, Sep 14, 2009 at 12:25:41PM -0400, Masami Hiramatsu wrote: > Ananth N Mavinakayanahalli wrote: >> On Sun, Sep 13, 2009 at 09:47:39PM -0400, Masami Hiramatsu wrote: >>> Ananth N Mavinakayanahalli wrote: >>>> On Fri, Sep 11, 2009 at 05:12:54AM +0200, Frederic Weisbecker wrote: >>>>> On Thu, Sep 10, 2009 at 07:53:30PM -0400, Masami Hiramatsu wrote: ... >> +static inline int check_kprobe_rereg(struct kprobe *p) >> +{ >> + int ret = 0; >> + struct kprobe *old_p; >> + >> + mutex_lock(&kprobe_mutex); >> + old_p = __get_valid_kprobe(p); >> + if (old_p == p) > > Here, since __get_valid_kprobe(p) will return aggr_kprobe of 'p', > you just need to check old_p != NULL. Right! --- Prevent re-registration of the same kprobe. This situation, though unlikely, needs to be flagged since it can lead to a system crash if its not handled. The core change itself is small, but the helper routine needed to be moved around a bit; hence the diffstat. Signed-off-by: Ananth N Mavinakayanahalli --- kernel/kprobes.c | 58 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 20 deletions(-) Index: linux-2.6.31/kernel/kprobes.c =================================================================== --- linux-2.6.31.orig/kernel/kprobes.c +++ linux-2.6.31/kernel/kprobes.c @@ -681,6 +681,40 @@ static kprobe_opcode_t __kprobes *kprobe return (kprobe_opcode_t *)(((char *)addr) + p->offset); } +/* Check passed kprobe is valid and return kprobe in kprobe_table. */ +static struct kprobe * __kprobes __get_valid_kprobe(struct kprobe *p) +{ + struct kprobe *old_p, *list_p; + + old_p = get_kprobe(p->addr); + if (unlikely(!old_p)) + return NULL; + + if (p != old_p) { + list_for_each_entry_rcu(list_p, &old_p->list, list) + if (list_p == p) + /* kprobe p is a valid probe */ + goto valid; + return NULL; + } +valid: + return old_p; +} + +/* Return error if the kprobe is being re-registered */ +static inline int check_kprobe_rereg(struct kprobe *p) +{ + int ret = 0; + struct kprobe *old_p; + + mutex_lock(&kprobe_mutex); + old_p = __get_valid_kprobe(p); + if (old_p) + ret = -EINVAL; + mutex_unlock(&kprobe_mutex); + return ret; +} + int __kprobes register_kprobe(struct kprobe *p) { int ret = 0; @@ -693,6 +727,10 @@ int __kprobes register_kprobe(struct kpr return -EINVAL; p->addr = addr; + ret = check_kprobe_rereg(p); + if (ret) + return ret; + preempt_disable(); if (!kernel_text_address((unsigned long) p->addr) || in_kprobes_functions((unsigned long) p->addr)) { @@ -762,26 +800,6 @@ out: } EXPORT_SYMBOL_GPL(register_kprobe); -/* Check passed kprobe is valid and return kprobe in kprobe_table. */ -static struct kprobe * __kprobes __get_valid_kprobe(struct kprobe *p) -{ - struct kprobe *old_p, *list_p; - - old_p = get_kprobe(p->addr); - if (unlikely(!old_p)) - return NULL; - - if (p != old_p) { - list_for_each_entry_rcu(list_p, &old_p->list, list) - if (list_p == p) - /* kprobe p is a valid probe */ - goto valid; - return NULL; - } -valid: - return old_p; -} - /* * Unregister a kprobe without a scheduler synchronization. */