public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
To: Petr Mladek <pmladek@suse.cz>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>,
	Seth Jennings <sjenning@redhat.com>,
	Jiri Kosina <jkosina@suse.cz>, Vojtech Pavlik <vojtech@suse.cz>,
	live-patching@vger.kernel.org, linux-kernel@vger.kernel.org,
	Rusty Russell <rusty@rustcorp.com.au>,
	Miroslav Benes <mbenes@suse.cz>,
	mingo@kernel.org, mathieu.desnoyers@efficios.com,
	oleg@redhat.com, paulmck@linux.vnet.ibm.com, andi@firstfloor.org,
	rostedt@goodmis.org, tglx@linutronix.de
Subject: Re: Re: Re: [PATCH 2/2] livepatch: fix patched module loading race
Date: Fri, 06 Mar 2015 20:37:26 +0900	[thread overview]
Message-ID: <54F99176.3090104@hitachi.com> (raw)
In-Reply-To: <20150306105159.GJ15177@pathway.suse.cz>

(2015/03/06 19:51), Petr Mladek wrote:
> On Fri 2015-03-06 10:24:27, Masami Hiramatsu wrote:
>> (2015/03/05 23:18), Josh Poimboeuf wrote:
>>> On Thu, Mar 05, 2015 at 09:52:41AM +0900, Masami Hiramatsu wrote:
>>>> (2015/03/04 22:17), Petr Mladek wrote:
>>>>> On Tue 2015-03-03 17:02:22, Josh Poimboeuf wrote:
>>>>>> It's possible for klp_register_patch() to see a module before the COMING
>>>>>> notifier is called, or after the GOING notifier is called.
>>>>>>
>>>>>> That can cause all kinds of ugly races.  As Pter Mladek reported:
>>>>>>
>>>>>>   "The problem is that we do not keep the klp_mutex lock all the time when
>>>>>>   the module is being added or removed.
>>>>>>
>>>>>>   First, the module is visible even before ftrace is ready. If we enable a patch
>>>>>>   in this time frame, adding ftrace ops will fail and the patch will get rejected
>>>>>>   just because bad timing.
>>>>>
>>>>> Ah, this is not true after all. I did not properly check when
>>>>> MODULE_STATE_COMING was set. I though that it was before ftrace was
>>>>> initialized but it was not true.
>>>>>
>>>>>
>>>>>>   Second, if we are "lucky" and enable the patch for the coming module when the
>>>>>>   ftrace is ready but before the module notifier has been called. The notifier
>>>>>>   will try to enable the patch as well. It will detect that it is already patched,
>>>>>>   return error, and the module will get rejected just because bad
>>>>>>   timing. The more serious problem is that it will not call the notifier for
>>>>>>   going module, so that the mess will stay there and we wont be able to load
>>>>>>   the module later.
>>>>>
>>>>> Ah, the race is there but the effect is not that serious in the
>>>>> end. It seems that errors from module notifiers are ignored. In fact,
>>>>> we do not propagate the error from klp_module_notify_coming(). It means
>>>>> that WARN() from klp_enable_object() will be printed but the module
>>>>> will be loaded and patched.
>>>>>
>>>>> I am sorry, I was confused by kGraft where kgr_module_init() was
>>>>> called directly from module_load(). The errors were propagated. It
>>>>> means that kGraft rejects module when the patch cannot be applied.
>>>>>
>>>>> Note that the current solution is perfectly fine for the simple
>>>>> consistency model.
>>>>>
>>>>>
>>>>>>   Third, similar problems are there for going module. If a patch is enabled after
>>>>>>   the notifier finishes but before the module is removed from the list of modules,
>>>>>>   the new patch will be applied to the module. The module might disappear at
>>>>>>   anytime when the patch enabling is in progress, so there might be an access out
>>>>>>   of memory. Or the whole patch might be applied and some mess will be left,
>>>>>>   so it will not be possible to load/patch the module again."
>>>>>
>>>>> This is true.
>>>>
>>>> No, that's not true if you try_get_module() before patching. After the
>>>> module state goes GOING (more correctly say, after try_release_module_ref()
>>>> succeeded), all try_get_module() must fail :)
>>>> So, please make sure to get module when applying patches.
>>>
>>> Hi Masami,
>>>
>>> As Jikos pointed out elsewhere, try_get_module() won't solve all the
>>> GOING races.
>>>
>>> The module can be in GOING before mod->exit() is called.  If we apply a
>>> patch between GOING getting set and mod->exit(), try_module_get() will
>>> fail and the module won't be patched.  But module code can still run
>>> before or during mod->exit(), so the unpatched module code might
>>> interact badly with new patched code elsewhere.
>>
>> Hmm, in that case, we'd better have new GONE state for the module.
>> At least kprobe needs it.
> 
> What is the exact problem with kprobes, please?

Ah, sorry, I miss understood the issue.


> Note that the notifiers for MODULE_STATE_GOING are called
> after mod->exit(). Therefore it is safe to kill kprobes
> the fast way when using the notifier.

Right.

        /* Stop the machine so refcounts can't move and disable module. */
        ret = try_stop_module(mod, flags, &forced);
        if (ret != 0)
                goto out;

        mutex_unlock(&module_mutex);
        /* Final destruction now no one is using it. */
        if (mod->exit != NULL)
                mod->exit();
        blocking_notifier_call_chain(&module_notify_list,
                                     MODULE_STATE_GOING, mod);
        async_synchronize_full();

And, kprobes also can try to put a probe between mutex_unlock()
and mod->exit() on mod->exit() function.
Since kprobe tries to get module when registering, it will
fail but mod->exit() is called after that fail.

So, there is also a race.

However, I'm not sure that is actual serious issue.
Actually, we can suppose this module unloading context is
not changing universe. thus it is expected behavior, isn't it?

Thank you,

-- 
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: masami.hiramatsu.pt@hitachi.com



  reply	other threads:[~2015-03-06 11:37 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-03 11:38 [RFC PATCH] livepatch/module: Do not patch modules that are not ready Petr Mladek
2015-03-03 14:55 ` Josh Poimboeuf
2015-03-03 15:48   ` Petr Mladek
2015-03-03 16:01     ` Josh Poimboeuf
2015-03-03 17:34   ` Petr Mladek
2015-03-03 19:31     ` Josh Poimboeuf
2015-03-03 19:35       ` Josh Poimboeuf
2015-03-03 23:02       ` [PATCH 0/2] livepatch: fix patch module loading race Josh Poimboeuf
2015-03-03 23:02         ` [PATCH 1/2] livepatch: remove unnecessary call to klp_find_object_module() Josh Poimboeuf
2015-03-04  9:00           ` Petr Mladek
2015-03-04 21:48           ` Jiri Kosina
2015-03-03 23:02         ` [PATCH 2/2] livepatch: fix patched module loading race Josh Poimboeuf
2015-03-04 13:17           ` Petr Mladek
2015-03-04 14:18             ` Petr Mladek
2015-03-04 15:34             ` Josh Poimboeuf
2015-03-04 15:51               ` Jiri Kosina
2015-03-04 16:41                 ` Josh Poimboeuf
2015-03-04 16:36               ` Petr Mladek
2015-03-04 17:57                 ` Josh Poimboeuf
2015-03-04 22:02                   ` Jiri Kosina
2015-03-04 22:45                     ` Josh Poimboeuf
2015-03-05  0:52             ` Masami Hiramatsu
2015-03-05 14:18               ` Josh Poimboeuf
2015-03-06  1:24                 ` Masami Hiramatsu
2015-03-06 10:51                   ` Petr Mladek
2015-03-06 11:37                     ` Masami Hiramatsu [this message]
2015-03-06 13:05                       ` Petr Mladek
2015-03-06 14:43                       ` Josh Poimboeuf
2015-03-05 14:24               ` Petr Mladek

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=54F99176.3090104@hitachi.com \
    --to=masami.hiramatsu.pt@hitachi.com \
    --cc=andi@firstfloor.org \
    --cc=jkosina@suse.cz \
    --cc=jpoimboe@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mbenes@suse.cz \
    --cc=mingo@kernel.org \
    --cc=oleg@redhat.com \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=pmladek@suse.cz \
    --cc=rostedt@goodmis.org \
    --cc=rusty@rustcorp.com.au \
    --cc=sjenning@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=vojtech@suse.cz \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox