* [PATCH v3 1/2] kprobes: avoid the kprobe being re-registered
@ 2017-10-27 9:23 Zhou Chengming
2017-10-27 9:23 ` [PATCH v3 2/2] kprobes: initialize probed_mod to NULL Zhou Chengming
2017-10-28 8:46 ` [PATCH v3 1/2] kprobes: avoid the kprobe being re-registered Masami Hiramatsu
0 siblings, 2 replies; 4+ messages in thread
From: Zhou Chengming @ 2017-10-27 9:23 UTC (permalink / raw)
To: mhiramat, ananth, anil.s.keshavamurthy, davem
Cc: linux-kernel, zhouchengming1
Changes:
- We should put the modifies of the kprobe after the re-reg check.
- And then the address_safe check.
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.
Suggested-by: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Zhou Chengming <zhouchengming1@huawei.com>
---
kernel/kprobes.c | 27 ++++++++-------------------
1 file changed, 8 insertions(+), 19 deletions(-)
diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index a1606a4..1eeedac 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,9 +1523,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 +1538,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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v3 2/2] kprobes: initialize probed_mod to NULL
2017-10-27 9:23 [PATCH v3 1/2] kprobes: avoid the kprobe being re-registered Zhou Chengming
@ 2017-10-27 9:23 ` Zhou Chengming
2017-10-28 8:47 ` Masami Hiramatsu
2017-10-28 8:46 ` [PATCH v3 1/2] kprobes: avoid the kprobe being re-registered Masami Hiramatsu
1 sibling, 1 reply; 4+ messages in thread
From: Zhou Chengming @ 2017-10-27 9:23 UTC (permalink / raw)
To: mhiramat, ananth, anil.s.keshavamurthy, davem
Cc: linux-kernel, zhouchengming1
When check_kprobe_address_safe() return fail, the probed_mod
should be set to NULL, because no module refcount held. And we
initialize probed_mod to NULL in register_kprobe() for the same reason.
Signed-off-by: Zhou Chengming <zhouchengming1@huawei.com>
---
kernel/kprobes.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index 1eeedac..a04588c 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -1488,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;
}
@@ -1514,7 +1515,7 @@ int register_kprobe(struct kprobe *p)
{
int ret;
struct kprobe *old_p;
- struct module *probed_mod;
+ struct module *probed_mod = NULL;
kprobe_opcode_t *addr;
/* Adjust probe address from symbol */
--
1.8.3.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v3 1/2] kprobes: avoid the kprobe being re-registered
2017-10-27 9:23 [PATCH v3 1/2] kprobes: avoid the kprobe being re-registered Zhou Chengming
2017-10-27 9:23 ` [PATCH v3 2/2] kprobes: initialize probed_mod to NULL Zhou Chengming
@ 2017-10-28 8:46 ` Masami Hiramatsu
1 sibling, 0 replies; 4+ messages in thread
From: Masami Hiramatsu @ 2017-10-28 8:46 UTC (permalink / raw)
To: Zhou Chengming; +Cc: ananth, anil.s.keshavamurthy, davem, linux-kernel
On Fri, 27 Oct 2017 17:23:14 +0800
Zhou Chengming <zhouchengming1@huawei.com> wrote:
> Changes:
> - We should put the modifies of the kprobe after the re-reg check.
> - And then the address_safe check.
>
> 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.
>
> Suggested-by: Masami Hiramatsu <mhiramat@kernel.org>
> Signed-off-by: Zhou Chengming <zhouchengming1@huawei.com>
Looks good to me :)
Acked-by: Masami Hiramatsu <mhiramat@kernel.org>
Thanks!
> ---
> kernel/kprobes.c | 27 ++++++++-------------------
> 1 file changed, 8 insertions(+), 19 deletions(-)
>
> diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> index a1606a4..1eeedac 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,9 +1523,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 +1538,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
>
--
Masami Hiramatsu <mhiramat@kernel.org>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v3 2/2] kprobes: initialize probed_mod to NULL
2017-10-27 9:23 ` [PATCH v3 2/2] kprobes: initialize probed_mod to NULL Zhou Chengming
@ 2017-10-28 8:47 ` Masami Hiramatsu
0 siblings, 0 replies; 4+ messages in thread
From: Masami Hiramatsu @ 2017-10-28 8:47 UTC (permalink / raw)
To: Zhou Chengming; +Cc: ananth, anil.s.keshavamurthy, davem, linux-kernel
On Fri, 27 Oct 2017 17:23:15 +0800
Zhou Chengming <zhouchengming1@huawei.com> wrote:
> When check_kprobe_address_safe() return fail, the probed_mod
> should be set to NULL, because no module refcount held. And we
> initialize probed_mod to NULL in register_kprobe() for the same reason.
>
Ok, it's a kind of hardening code but looks good to me.
Acked-by: Masami Hiramatsu <mhiramat@kernel.org>
Thank you!
> Signed-off-by: Zhou Chengming <zhouchengming1@huawei.com>
> ---
> kernel/kprobes.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> index 1eeedac..a04588c 100644
> --- a/kernel/kprobes.c
> +++ b/kernel/kprobes.c
> @@ -1488,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;
> }
> @@ -1514,7 +1515,7 @@ int register_kprobe(struct kprobe *p)
> {
> int ret;
> struct kprobe *old_p;
> - struct module *probed_mod;
> + struct module *probed_mod = NULL;
> kprobe_opcode_t *addr;
>
> /* Adjust probe address from symbol */
> --
> 1.8.3.1
>
--
Masami Hiramatsu <mhiramat@kernel.org>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2017-10-28 8:48 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-27 9:23 [PATCH v3 1/2] kprobes: avoid the kprobe being re-registered Zhou Chengming
2017-10-27 9:23 ` [PATCH v3 2/2] kprobes: initialize probed_mod to NULL Zhou Chengming
2017-10-28 8:47 ` Masami Hiramatsu
2017-10-28 8:46 ` [PATCH v3 1/2] kprobes: avoid the kprobe being re-registered Masami Hiramatsu
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).