From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751997AbdJ0GTi (ORCPT ); Fri, 27 Oct 2017 02:19:38 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:56360 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1751050AbdJ0GTh (ORCPT ); Fri, 27 Oct 2017 02:19:37 -0400 Message-ID: <59F2CF5B.3030507@huawei.com> Date: Fri, 27 Oct 2017 14:16:59 +0800 From: zhouchengming User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20120428 Thunderbird/12.0.1 MIME-Version: 1.0 To: Masami Hiramatsu CC: , , , Subject: Re: [PATCH v2] kprobes: avoid the kprobe being re-registered References: <1509069400-133557-1-git-send-email-zhouchengming1@huawei.com> <20171027145721.ef28a8eea41210f05c23f30e@kernel.org> In-Reply-To: <20171027145721.ef28a8eea41210f05c23f30e@kernel.org> Content-Type: text/plain; charset="ISO-8859-1"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.177.236.183] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2017/10/27 13:57, Masami Hiramatsu wrote: > On Fri, 27 Oct 2017 09:56:40 +0800 > Zhou Chengming wrote: > >> Changes from v1: >> - We should put the modifies of the kprobe after the re-reg check. >> - And then the address_safe check. >> - When check_kprobe_address_safe() return fail, the *probed_mod >> should be set to NULL, and no module refcount held. > Could you split this item from this patch (with initializing probe_mod = NULL), > since it is another bug? > > Thank you, Ok, it seems more reasonable. I will split this patch into two patches later. Thank you. >> 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 >> --- >> kernel/kprobes.c | 28 +++++++++------------------- >> 1 file changed, 9 insertions(+), 19 deletions(-) >> >> diff --git a/kernel/kprobes.c b/kernel/kprobes.c >> index a1606a4..f622639 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; >> @@ -1501,6 +1488,7 @@ static int check_kprobe_address_safe(struct kprobe *p, >> * its code to prohibit unexpected unloading. >> */ >> if (unlikely(!try_module_get(*probed_mod))) { >> + *probed_mod = NULL; >> ret = -ENOENT; >> goto out; >> } >> @@ -1536,9 +1524,13 @@ int register_kprobe(struct kprobe *p) >> return PTR_ERR(addr); >> p->addr = addr; >> >> - ret = check_kprobe_rereg(p); >> - if (ret) >> - return ret; >> + mutex_lock(&kprobe_mutex); >> + >> + /* Return error if the kprobe is being re-registered */ >> + if (__get_valid_kprobe(p)) { >> + ret = -EINVAL; >> + goto out; >> + } >> >> /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */ >> p->flags&= KPROBE_FLAG_DISABLED; >> @@ -1547,9 +1539,7 @@ int register_kprobe(struct kprobe *p) >> >> ret = check_kprobe_address_safe(p,&probed_mod); >> if (ret) >> - return ret; >> - >> - mutex_lock(&kprobe_mutex); >> + goto out; >> >> old_p = get_kprobe(p->addr); >> if (old_p) { >> -- >> 1.8.3.1 >> >