linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: jhugo@codeaurora.org (Jeffrey Hugo)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH] arm64: mm: Fix false positives in W+X checking
Date: Wed, 25 Apr 2018 10:36:25 -0600	[thread overview]
Message-ID: <3a105d2c-19b8-11fc-88d3-2da4fa846296@codeaurora.org> (raw)
In-Reply-To: <CAGXu5j+WhNdY09s_tPQ-BKqEqi_bbbp8XnRSnoGTN-HOPSJFPQ@mail.gmail.com>

On 4/25/2018 10:23 AM, Kees Cook wrote:
> On Wed, Apr 25, 2018 at 8:12 AM, Jeffrey Hugo <jhugo@codeaurora.org> wrote:
>> On 4/25/2018 9:03 AM, Will Deacon wrote:
>>>
>>> Hi Jeffrey,
>>>
>>> Thanks for the patch. Comment below.
>>>
>>> On Wed, Apr 25, 2018 at 08:13:31AM -0600, Jeffrey Hugo wrote:
>>>>
>>>> load_module() creates W+X mappings via __vmalloc_node_range() (from
>>>> layout_and_allocate()->move_module()->module_alloc()) by using
>>>> PAGE_KERNEL_EXEC.  These mappings are later cleaned up via
>>>> "call_rcu_sched(&freeinit->rcu, do_free_init)" from do_init_module().
>>>>
>>>> This is a problem because call_rcu_sched() queues work, which can be run
>>>> after debug_checkwx() is run, resulting in a race condition.  If hit, the
>>>> race results in a nasty splat about insecure W+X mappings, which results
>>>> in a poor user experience as these are not the mappings that
>>>> debug_checkwx() is intended to catch.
>>>>
>>>> Address the race by flushing the queued work before running
>>>> debug_checkwx().
>>>>
>>>> Reported-by: Timur Tabi <timur@codeaurora.org>
>>>> Reported-by: Jan Glauber <jan.glauber@caviumnetworks.com>
>>>> Fixes: 1404d6f13e47 ("arm64: dump: Add checking for writable and
>>>> exectuable pages")
>>>> Signed-off-by: Jeffrey Hugo <jhugo@codeaurora.org>
>>>> ---
>>>>    arch/arm64/mm/mmu.c | 6 ++++++
>>>>    1 file changed, 6 insertions(+)
>>>>
>>>> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
>>>> index 2dbb2c9..40d45fd 100644
>>>> --- a/arch/arm64/mm/mmu.c
>>>> +++ b/arch/arm64/mm/mmu.c
>>>> @@ -503,6 +503,12 @@ void mark_rodata_ro(void)
>>>>          update_mapping_prot(__pa_symbol(__start_rodata), (unsigned
>>>> long)__start_rodata,
>>>>                              section_size, PAGE_KERNEL_RO);
>>>>    +     /*
>>>> +        * load_module() results in W+X mappings, which are cleaned up
>>>> with
>>>> +        * call_rcu_sched().  Let's make sure that queued work is flushed
>>>> so
>>>> +        * that we don't hit false positives.
>>>> +        */
>>>> +       rcu_barrier_sched();
>>>>          debug_checkwx();
>>>>    }
>>>
>>>
>>> Whilst this looks correct to me, it looks to me like other architectures
>>> (e.g. x86) would be affected by this problem as well. Perhaps it would be
>>> better to solve it in the core code before invoking mark_rodata_ro, or is
>>> there some reason that we only run into this on arm64?
>>>
>>
>> Thanks for the review.
>>
>> I was actually pondering pushing this into ptdump_check_wx() so that the
>> "barrier" hit is only observed with CONFIG_DEBUG_WX.
>>
>> I agree, in principal this is not an arm64 specific issue.  I do not have
>> sufficient equipment to validate other architectures.  On QDF2400, the
>> reproduction rate is very low, roughly 1-3 instances in 2000-3000 reboots.
>> I do have a system simulator which happens to repro the issue 100% of the
>> time, which is what I used to debug and test this fix, before applying it to
>> QDF2400.
>>
>> I'm waffling.  I see the benefit of fixing this in common code, but the
>> "core" functionality of mark_rodata_ro doesn't need this barrier...
>>
>> I suppose I can push it up to core code, and see what the rest of the
>> community says.  Is that what you recommend?
> 
> I think fixing this in the general case makes sense.

Ok.  I'll give it until Monday to see if Laura has any insights into 
x86, and then spin a v2.

-- 
Jeffrey Hugo
Qualcomm Datacenter Technologies as an affiliate of Qualcomm 
Technologies, Inc.
Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.

  reply	other threads:[~2018-04-25 16:36 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-25 14:13 [PATCH] arm64: mm: Fix false positives in W+X checking Jeffrey Hugo
2018-04-25 15:03 ` Will Deacon
2018-04-25 15:12   ` Jeffrey Hugo
2018-04-25 15:41     ` Laura Abbott
2018-04-25 16:23     ` Kees Cook
2018-04-25 16:36       ` Jeffrey Hugo [this message]
2018-04-25 16:37         ` Will Deacon
2018-04-27 19:30           ` Laura Abbott
2018-04-27 20:01             ` Jeffrey Hugo

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=3a105d2c-19b8-11fc-88d3-2da4fa846296@codeaurora.org \
    --to=jhugo@codeaurora.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    /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;
as well as URLs for NNTP newsgroup(s).