linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kprobes: avoid the kprobe being re-registered
@ 2017-10-26 12:11 Zhou Chengming
  2017-10-26 14:39 ` Masami Hiramatsu
  0 siblings, 1 reply; 3+ messages in thread
From: Zhou Chengming @ 2017-10-26 12:11 UTC (permalink / raw)
  To: ananth, anil.s.keshavamurthy, davem, mhiramat
  Cc: linux-kernel, zhouchengming1

Old code use check_kprobe_rereg() to check if the kprobe has been
registered already, but check_kprobe_rereg() will release the
kprobe_mutex then, so maybe two paths will pass the check and
register the same kprobe. This patch put the check inside the mutex.

Signed-off-by: Zhou Chengming <zhouchengming1@huawei.com>
---
 kernel/kprobes.c | 23 ++++++-----------------
 1 file changed, 6 insertions(+), 17 deletions(-)

diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index a1606a4..2a4873a 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -1443,19 +1443,6 @@ static struct kprobe *__get_valid_kprobe(struct kprobe *p)
 	return ap;
 }
 
-/* Return error if the kprobe is being re-registered */
-static inline int check_kprobe_rereg(struct kprobe *p)
-{
-	int ret = 0;
-
-	mutex_lock(&kprobe_mutex);
-	if (__get_valid_kprobe(p))
-		ret = -EINVAL;
-	mutex_unlock(&kprobe_mutex);
-
-	return ret;
-}
-
 int __weak arch_check_ftrace_location(struct kprobe *p)
 {
 	unsigned long ftrace_addr;
@@ -1536,10 +1523,6 @@ int register_kprobe(struct kprobe *p)
 		return PTR_ERR(addr);
 	p->addr = addr;
 
-	ret = check_kprobe_rereg(p);
-	if (ret)
-		return ret;
-
 	/* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
 	p->flags &= KPROBE_FLAG_DISABLED;
 	p->nmissed = 0;
@@ -1551,6 +1534,12 @@ int register_kprobe(struct kprobe *p)
 
 	mutex_lock(&kprobe_mutex);
 
+	/* Return error if the kprobe is being re-registered */
+	if (__get_valid_kprobe(p)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
 	old_p = get_kprobe(p->addr);
 	if (old_p) {
 		/* Since this may unoptimize old_p, locking text_mutex. */
-- 
1.8.3.1

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] kprobes: avoid the kprobe being re-registered
  2017-10-26 12:11 [PATCH] kprobes: avoid the kprobe being re-registered Zhou Chengming
@ 2017-10-26 14:39 ` Masami Hiramatsu
  2017-10-27  1:24   ` zhouchengming
  0 siblings, 1 reply; 3+ messages in thread
From: Masami Hiramatsu @ 2017-10-26 14:39 UTC (permalink / raw)
  To: Zhou Chengming; +Cc: ananth, anil.s.keshavamurthy, davem, linux-kernel

On Thu, 26 Oct 2017 20:11:25 +0800
Zhou Chengming <zhouchengming1@huawei.com> wrote:

> Old code use check_kprobe_rereg() to check if the kprobe has been
> registered already, but check_kprobe_rereg() will release the
> kprobe_mutex then, so maybe two paths will pass the check and
> register the same kprobe. This patch put the check inside the mutex.

Still no good, see below comment.

> 
> Signed-off-by: Zhou Chengming <zhouchengming1@huawei.com>
> ---
>  kernel/kprobes.c | 23 ++++++-----------------
>  1 file changed, 6 insertions(+), 17 deletions(-)
> 
> diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> index a1606a4..2a4873a 100644
> --- a/kernel/kprobes.c
> +++ b/kernel/kprobes.c
> @@ -1443,19 +1443,6 @@ static struct kprobe *__get_valid_kprobe(struct kprobe *p)
>  	return ap;
>  }
>  
> -/* Return error if the kprobe is being re-registered */
> -static inline int check_kprobe_rereg(struct kprobe *p)
> -{
> -	int ret = 0;
> -
> -	mutex_lock(&kprobe_mutex);
> -	if (__get_valid_kprobe(p))
> -		ret = -EINVAL;
> -	mutex_unlock(&kprobe_mutex);
> -
> -	return ret;
> -}
> -
>  int __weak arch_check_ftrace_location(struct kprobe *p)
>  {
>  	unsigned long ftrace_addr;
> @@ -1536,10 +1523,6 @@ int register_kprobe(struct kprobe *p)
>  		return PTR_ERR(addr);
>  	p->addr = addr;
>  
> -	ret = check_kprobe_rereg(p);
> -	if (ret)
> -		return ret;
> -
>  	/* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
>  	p->flags &= KPROBE_FLAG_DISABLED;
>  	p->nmissed = 0;

here, we already modifies the kprobe. We need to check and reject before modifying it.

Thank you,

> @@ -1551,6 +1534,12 @@ int register_kprobe(struct kprobe *p)
>  
>  	mutex_lock(&kprobe_mutex);
>  
> +	/* Return error if the kprobe is being re-registered */
> +	if (__get_valid_kprobe(p)) {
> +		ret = -EINVAL;
> +		goto out;
> +	}
> +
>  	old_p = get_kprobe(p->addr);
>  	if (old_p) {
>  		/* Since this may unoptimize old_p, locking text_mutex. */
> -- 
> 1.8.3.1
> 


-- 
Masami Hiramatsu <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] kprobes: avoid the kprobe being re-registered
  2017-10-26 14:39 ` Masami Hiramatsu
@ 2017-10-27  1:24   ` zhouchengming
  0 siblings, 0 replies; 3+ messages in thread
From: zhouchengming @ 2017-10-27  1:24 UTC (permalink / raw)
  To: Masami Hiramatsu; +Cc: ananth, anil.s.keshavamurthy, davem, linux-kernel

On 2017/10/26 22:39, Masami Hiramatsu wrote:
> On Thu, 26 Oct 2017 20:11:25 +0800
> Zhou Chengming<zhouchengming1@huawei.com>  wrote:
>
>> Old code use check_kprobe_rereg() to check if the kprobe has been
>> registered already, but check_kprobe_rereg() will release the
>> kprobe_mutex then, so maybe two paths will pass the check and
>> register the same kprobe. This patch put the check inside the mutex.
> Still no good, see below comment.
>
>> Signed-off-by: Zhou Chengming<zhouchengming1@huawei.com>
>> ---
>>   kernel/kprobes.c | 23 ++++++-----------------
>>   1 file changed, 6 insertions(+), 17 deletions(-)
>>
>> diff --git a/kernel/kprobes.c b/kernel/kprobes.c
>> index a1606a4..2a4873a 100644
>> --- a/kernel/kprobes.c
>> +++ b/kernel/kprobes.c
>> @@ -1443,19 +1443,6 @@ static struct kprobe *__get_valid_kprobe(struct kprobe *p)
>>   	return ap;
>>   }
>>
>> -/* Return error if the kprobe is being re-registered */
>> -static inline int check_kprobe_rereg(struct kprobe *p)
>> -{
>> -	int ret = 0;
>> -
>> -	mutex_lock(&kprobe_mutex);
>> -	if (__get_valid_kprobe(p))
>> -		ret = -EINVAL;
>> -	mutex_unlock(&kprobe_mutex);
>> -
>> -	return ret;
>> -}
>> -
>>   int __weak arch_check_ftrace_location(struct kprobe *p)
>>   {
>>   	unsigned long ftrace_addr;
>> @@ -1536,10 +1523,6 @@ int register_kprobe(struct kprobe *p)
>>   		return PTR_ERR(addr);
>>   	p->addr = addr;
>>
>> -	ret = check_kprobe_rereg(p);
>> -	if (ret)
>> -		return ret;
>> -
>>   	/* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
>>   	p->flags&= KPROBE_FLAG_DISABLED;
>>   	p->nmissed = 0;
> here, we already modifies the kprobe. We need to check and reject before modifying it.
>
> Thank you,

Ah, right. We should put the modifies after the re-reg check. I will send a patch-v2.

Thank you.

>> @@ -1551,6 +1534,12 @@ int register_kprobe(struct kprobe *p)
>>
>>   	mutex_lock(&kprobe_mutex);
>>
>> +	/* Return error if the kprobe is being re-registered */
>> +	if (__get_valid_kprobe(p)) {
>> +		ret = -EINVAL;
>> +		goto out;
>> +	}
>> +
>>   	old_p = get_kprobe(p->addr);
>>   	if (old_p) {
>>   		/* Since this may unoptimize old_p, locking text_mutex. */
>> -- 
>> 1.8.3.1
>>
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-10-27  1:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-26 12:11 [PATCH] kprobes: avoid the kprobe being re-registered Zhou Chengming
2017-10-26 14:39 ` Masami Hiramatsu
2017-10-27  1:24   ` zhouchengming

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).