LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Masami Hiramatsu <mhiramat@redhat.com>
To: Lai Jiangshan <laijs@cn.fujitsu.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	ananth@in.ibm.com
Cc: David Miller <davem@davemloft.net>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	h-shimamoto@ct.jp.nec.com
Subject: [PATCH] kprobes: disable preempt for module_text_address() and kernel_text_address()
Date: Thu, 06 Nov 2008 10:37:48 -0500	[thread overview]
Message-ID: <49130F4C.6070603@redhat.com> (raw)
In-Reply-To: <49124307.60909@cn.fujitsu.com>

__register_kprobe() can be preempted after checking probing address
but before module_text_address() or try_module_get(), and in this
interval the module can be unloaded. In that case,
try_module_get(probed_mod) will access to invalid address,
or kprobe will probe invalid address.

this patch uses preempt_disable() to protect it and use
__module_text_address() and __kernel_text_address().

Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com>
Signed-off-by: Masami Hiramatsu <mhiramat@redhat.com>
---

>> @@ -718,9 +725,11 @@ static void __kprobes __unregister_kprob
>>  	struct kprobe *old_p;
>>
>>  	if (p->mod_refcounted) {
>> -		mod = module_text_address((unsigned long)p->addr);
>> +		preempt_disable();
>> +		mod = __module_text_address((unsigned long)p->addr);
>>  		if (mod)
>>  			module_put(mod);
>> +		preempt_enable();
>
> this is bad fix, we have had a reference to mod. we don't need
> preempt_disable() before module_put(mod).

Hmm, Indeed.
So let's add a comment there.

 kernel/kprobes.c |   21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

Index: 2.6.28-rc3/kernel/kprobes.c
===================================================================
--- 2.6.28-rc3.orig/kernel/kprobes.c
+++ 2.6.28-rc3/kernel/kprobes.c
@@ -613,30 +613,37 @@ static int __kprobes __register_kprobe(s
 		return -EINVAL;
 	p->addr = addr;

-	if (!kernel_text_address((unsigned long) p->addr) ||
-	    in_kprobes_functions((unsigned long) p->addr))
+	preempt_disable();
+	if (!__kernel_text_address((unsigned long) p->addr) ||
+	    in_kprobes_functions((unsigned long) p->addr)) {
+		preempt_enable();
 		return -EINVAL;
+	}

 	p->mod_refcounted = 0;

 	/*
 	 * Check if are we probing a module.
 	 */
-	probed_mod = module_text_address((unsigned long) p->addr);
+	probed_mod = __module_text_address((unsigned long) p->addr);
 	if (probed_mod) {
-		struct module *calling_mod = module_text_address(called_from);
+		struct module *calling_mod;
+		calling_mod = __module_text_address(called_from);
 		/*
 		 * We must allow modules to probe themself and in this case
 		 * avoid incrementing the module refcount, so as to allow
 		 * unloading of self probing modules.
 		 */
 		if (calling_mod && calling_mod != probed_mod) {
-			if (unlikely(!try_module_get(probed_mod)))
+			if (unlikely(!try_module_get(probed_mod))) {
+				preempt_enable();
 				return -EINVAL;
+			}
 			p->mod_refcounted = 1;
 		} else
 			probed_mod = NULL;
 	}
+	preempt_enable();

 	p->nmissed = 0;
 	INIT_LIST_HEAD(&p->list);
@@ -718,6 +725,10 @@ static void __kprobes __unregister_kprob
 	struct kprobe *old_p;

 	if (p->mod_refcounted) {
+		/*
+		 * Since we've already incremented refcount,
+		 * we don't need to disable preemption.
+		 */
 		mod = module_text_address((unsigned long)p->addr);
 		if (mod)
 			module_put(mod);

-- 
Masami Hiramatsu

Software Engineer
Hitachi Computer Products (America) Inc.
Software Solutions Division

e-mail: mhiramat@redhat.com


  reply	other threads:[~2008-11-06 15:39 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-04  5:56 [PATCH] kprobes: disable preempt for module_text_address() Lai Jiangshan
2008-11-04 14:28 ` Ananth N Mavinakayanahalli
2008-11-05  0:53   ` Lai Jiangshan
2008-11-05  1:27 ` Masami Hiramatsu
2008-11-05  1:47   ` Lai Jiangshan
2008-11-05 19:30     ` Hiroshi Shimamoto
2008-11-05 21:40     ` Masami Hiramatsu
2008-11-05 22:46       ` Hiroshi Shimamoto
2008-11-05 23:07         ` Masami Hiramatsu
2008-11-06  0:06           ` [PATCH] kprobes: bugfix: try_module_get even if calling_mod is NULL Masami Hiramatsu
2008-11-07  1:00             ` Andrew Morton
2008-11-07  2:28               ` Masami Hiramatsu
2008-11-07  2:54                 ` Andrew Morton
2008-11-07  4:46                   ` Ananth N Mavinakayanahalli
2008-11-06  1:06       ` [PATCH] kprobes: disable preempt for module_text_address() Lai Jiangshan
2008-11-06 15:37         ` Masami Hiramatsu [this message]
2008-11-07  0:32           ` [PATCH] kprobes: disable preempt for module_text_address() and kernel_text_address() Lai Jiangshan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=49130F4C.6070603@redhat.com \
    --to=mhiramat@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=ananth@in.ibm.com \
    --cc=davem@davemloft.net \
    --cc=h-shimamoto@ct.jp.nec.com \
    --cc=laijs@cn.fujitsu.com \
    --cc=linux-kernel@vger.kernel.org \
    --subject='Re: [PATCH] kprobes: disable preempt for module_text_address() and kernel_text_address()' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).