Re: [BUGFIX] kprobes: prevent re-registration of the same kprobe- take2

From: Masami Hiramatsu
Date: Tue Sep 15 2009 - 09:28:34 EST


Ananth N Mavinakayanahalli wrote:
> 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<ananth@xxxxxxxxxx>

Acked-by: Masami Hiramatsu <mhiramat@xxxxxxxxxx>

> ---
> 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.
> */

--
Masami Hiramatsu

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

e-mail: mhiramat@xxxxxxxxxx

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/