linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] arm64: mm: check for upper PAGE_SHIFT bits in pfn_valid()
@ 2018-08-13 19:30 Greg Hackmann
  2018-08-14 10:40 ` Will Deacon
  0 siblings, 1 reply; 5+ messages in thread
From: Greg Hackmann @ 2018-08-13 19:30 UTC (permalink / raw)
  To: linux-arm-kernel
  Cc: kernel-team, Greg Hackmann, Catalin Marinas, Will Deacon,
	Andrew Morton, Robin Murphy, Laura Abbott, Steve Capper,
	Kristina Martsenko, Stefan Agner, CHANDAN VN, Johannes Weiner,
	linux-kernel

ARM64's pfn_valid() shifts away the upper PAGE_SHIFT bits of the input
before seeing if the PFN is valid.  This leads to false positives when
some of the upper bits are set, but the lower bits match a valid PFN.

For example, the following userspace code looks up a bogus entry in
/proc/kpageflags:

    int pagemap = open("/proc/self/pagemap", O_RDONLY);
    int pageflags = open("/proc/kpageflags", O_RDONLY);
    uint64_t pfn, val;

    lseek64(pagemap, [...], SEEK_SET);
    read(pagemap, &pfn, sizeof(pfn));
    if (pfn & (1UL << 63)) {        /* valid PFN */
        pfn &= ((1UL << 55) - 1);   /* clear flag bits */
        pfn |= (1UL << 55);
        lseek64(pageflags, pfn * sizeof(uint64_t), SEEK_SET);
        read(pageflags, &val, sizeof(val));
    }

On ARM64 this causes the userspace process to crash with SIGSEGV rather
than reading (1 << KPF_NOPAGE).  kpageflags_read() treats the offset as
valid, and stable_page_flags() will try to access an address between the
user and kernel address ranges.

Signed-off-by: Greg Hackmann <ghackmann@google.com>
---
 arch/arm64/mm/init.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c
index 9abf8a1e7b25..787e27964ab9 100644
--- a/arch/arm64/mm/init.c
+++ b/arch/arm64/mm/init.c
@@ -287,7 +287,11 @@ static void __init zone_sizes_init(unsigned long min, unsigned long max)
 #ifdef CONFIG_HAVE_ARCH_PFN_VALID
 int pfn_valid(unsigned long pfn)
 {
-	return memblock_is_map_memory(pfn << PAGE_SHIFT);
+	phys_addr_t addr = pfn << PAGE_SHIFT;
+
+	if ((addr >> PAGE_SHIFT) != pfn)
+		return 0;
+	return memblock_is_map_memory(addr);
 }
 EXPORT_SYMBOL(pfn_valid);
 #endif
-- 
2.18.0.597.ga71716f1ad-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] arm64: mm: check for upper PAGE_SHIFT bits in pfn_valid()
  2018-08-13 19:30 [PATCH] arm64: mm: check for upper PAGE_SHIFT bits in pfn_valid() Greg Hackmann
@ 2018-08-14 10:40 ` Will Deacon
  2018-08-14 15:17   ` Greg Hackmann
  0 siblings, 1 reply; 5+ messages in thread
From: Will Deacon @ 2018-08-14 10:40 UTC (permalink / raw)
  To: Greg Hackmann
  Cc: linux-arm-kernel, kernel-team, Greg Hackmann, Catalin Marinas,
	Andrew Morton, Robin Murphy, Laura Abbott, Steve Capper,
	Kristina Martsenko, Stefan Agner, CHANDAN VN, Johannes Weiner,
	linux-kernel

Hi Greg,

On Mon, Aug 13, 2018 at 12:30:11PM -0700, Greg Hackmann wrote:
> ARM64's pfn_valid() shifts away the upper PAGE_SHIFT bits of the input
> before seeing if the PFN is valid.  This leads to false positives when
> some of the upper bits are set, but the lower bits match a valid PFN.
> 
> For example, the following userspace code looks up a bogus entry in
> /proc/kpageflags:
> 
>     int pagemap = open("/proc/self/pagemap", O_RDONLY);
>     int pageflags = open("/proc/kpageflags", O_RDONLY);
>     uint64_t pfn, val;
> 
>     lseek64(pagemap, [...], SEEK_SET);
>     read(pagemap, &pfn, sizeof(pfn));
>     if (pfn & (1UL << 63)) {        /* valid PFN */
>         pfn &= ((1UL << 55) - 1);   /* clear flag bits */
>         pfn |= (1UL << 55);
>         lseek64(pageflags, pfn * sizeof(uint64_t), SEEK_SET);
>         read(pageflags, &val, sizeof(val));
>     }
> 
> On ARM64 this causes the userspace process to crash with SIGSEGV rather
> than reading (1 << KPF_NOPAGE).  kpageflags_read() treats the offset as
> valid, and stable_page_flags() will try to access an address between the
> user and kernel address ranges.
> 
> Signed-off-by: Greg Hackmann <ghackmann@google.com>
> ---
>  arch/arm64/mm/init.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)

Thanks, this looks like a sensible fix to me. Do you think it warrants a
CC stable?

Will

> diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c
> index 9abf8a1e7b25..787e27964ab9 100644
> --- a/arch/arm64/mm/init.c
> +++ b/arch/arm64/mm/init.c
> @@ -287,7 +287,11 @@ static void __init zone_sizes_init(unsigned long min, unsigned long max)
>  #ifdef CONFIG_HAVE_ARCH_PFN_VALID
>  int pfn_valid(unsigned long pfn)
>  {
> -	return memblock_is_map_memory(pfn << PAGE_SHIFT);
> +	phys_addr_t addr = pfn << PAGE_SHIFT;
> +
> +	if ((addr >> PAGE_SHIFT) != pfn)
> +		return 0;
> +	return memblock_is_map_memory(addr);
>  }
>  EXPORT_SYMBOL(pfn_valid);
>  #endif
> -- 
> 2.18.0.597.ga71716f1ad-goog
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] arm64: mm: check for upper PAGE_SHIFT bits in pfn_valid()
  2018-08-14 10:40 ` Will Deacon
@ 2018-08-14 15:17   ` Greg Hackmann
  2018-08-14 15:29     ` Will Deacon
  0 siblings, 1 reply; 5+ messages in thread
From: Greg Hackmann @ 2018-08-14 15:17 UTC (permalink / raw)
  To: Will Deacon, Greg Hackmann
  Cc: linux-arm-kernel, kernel-team, Catalin Marinas, Andrew Morton,
	Robin Murphy, Laura Abbott, Steve Capper, Kristina Martsenko,
	Stefan Agner, CHANDAN VN, Johannes Weiner, linux-kernel

On 08/14/2018 03:40 AM, Will Deacon wrote:
> Hi Greg,
> 
> On Mon, Aug 13, 2018 at 12:30:11PM -0700, Greg Hackmann wrote:
>> ARM64's pfn_valid() shifts away the upper PAGE_SHIFT bits of the input
>> before seeing if the PFN is valid.  This leads to false positives when
>> some of the upper bits are set, but the lower bits match a valid PFN.
>>
>> For example, the following userspace code looks up a bogus entry in
>> /proc/kpageflags:
>>
>>     int pagemap = open("/proc/self/pagemap", O_RDONLY);
>>     int pageflags = open("/proc/kpageflags", O_RDONLY);
>>     uint64_t pfn, val;
>>
>>     lseek64(pagemap, [...], SEEK_SET);
>>     read(pagemap, &pfn, sizeof(pfn));
>>     if (pfn & (1UL << 63)) {        /* valid PFN */
>>         pfn &= ((1UL << 55) - 1);   /* clear flag bits */
>>         pfn |= (1UL << 55);
>>         lseek64(pageflags, pfn * sizeof(uint64_t), SEEK_SET);
>>         read(pageflags, &val, sizeof(val));
>>     }
>>
>> On ARM64 this causes the userspace process to crash with SIGSEGV rather
>> than reading (1 << KPF_NOPAGE).  kpageflags_read() treats the offset as
>> valid, and stable_page_flags() will try to access an address between the
>> user and kernel address ranges.
>>
>> Signed-off-by: Greg Hackmann <ghackmann@google.com>
>> ---
>>  arch/arm64/mm/init.c | 6 +++++-
>>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> Thanks, this looks like a sensible fix to me. Do you think it warrants a
> CC stable?
> 
> Will

Yes, I think so.  Should I resend with a "Fixes" field?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] arm64: mm: check for upper PAGE_SHIFT bits in pfn_valid()
  2018-08-14 15:17   ` Greg Hackmann
@ 2018-08-14 15:29     ` Will Deacon
  2018-08-15 19:30       ` Greg Hackmann
  0 siblings, 1 reply; 5+ messages in thread
From: Will Deacon @ 2018-08-14 15:29 UTC (permalink / raw)
  To: Greg Hackmann
  Cc: Greg Hackmann, linux-arm-kernel, kernel-team, Catalin Marinas,
	Andrew Morton, Robin Murphy, Laura Abbott, Steve Capper,
	Kristina Martsenko, Stefan Agner, CHANDAN VN, Johannes Weiner,
	linux-kernel

On Tue, Aug 14, 2018 at 08:17:48AM -0700, Greg Hackmann wrote:
> On 08/14/2018 03:40 AM, Will Deacon wrote:
> > Hi Greg,
> > 
> > On Mon, Aug 13, 2018 at 12:30:11PM -0700, Greg Hackmann wrote:
> >> ARM64's pfn_valid() shifts away the upper PAGE_SHIFT bits of the input
> >> before seeing if the PFN is valid.  This leads to false positives when
> >> some of the upper bits are set, but the lower bits match a valid PFN.
> >>
> >> For example, the following userspace code looks up a bogus entry in
> >> /proc/kpageflags:
> >>
> >>     int pagemap = open("/proc/self/pagemap", O_RDONLY);
> >>     int pageflags = open("/proc/kpageflags", O_RDONLY);
> >>     uint64_t pfn, val;
> >>
> >>     lseek64(pagemap, [...], SEEK_SET);
> >>     read(pagemap, &pfn, sizeof(pfn));
> >>     if (pfn & (1UL << 63)) {        /* valid PFN */
> >>         pfn &= ((1UL << 55) - 1);   /* clear flag bits */
> >>         pfn |= (1UL << 55);
> >>         lseek64(pageflags, pfn * sizeof(uint64_t), SEEK_SET);
> >>         read(pageflags, &val, sizeof(val));
> >>     }
> >>
> >> On ARM64 this causes the userspace process to crash with SIGSEGV rather
> >> than reading (1 << KPF_NOPAGE).  kpageflags_read() treats the offset as
> >> valid, and stable_page_flags() will try to access an address between the
> >> user and kernel address ranges.
> >>
> >> Signed-off-by: Greg Hackmann <ghackmann@google.com>
> >> ---
> >>  arch/arm64/mm/init.c | 6 +++++-
> >>  1 file changed, 5 insertions(+), 1 deletion(-)
> > 
> > Thanks, this looks like a sensible fix to me. Do you think it warrants a
> > CC stable?
> > 
> > Will
> 
> Yes, I think so.  Should I resend with a "Fixes" field?

Could do, but I think this goes all the way back to day 1! Doesn't arch/arm/
also suffer from the same issue?

Will

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] arm64: mm: check for upper PAGE_SHIFT bits in pfn_valid()
  2018-08-14 15:29     ` Will Deacon
@ 2018-08-15 19:30       ` Greg Hackmann
  0 siblings, 0 replies; 5+ messages in thread
From: Greg Hackmann @ 2018-08-15 19:30 UTC (permalink / raw)
  To: Will Deacon
  Cc: Greg Hackmann, linux-arm-kernel, kernel-team, Catalin Marinas,
	Andrew Morton, Robin Murphy, Laura Abbott, Steve Capper,
	Kristina Martsenko, Stefan Agner, CHANDAN VN, Johannes Weiner,
	linux-kernel

On 08/14/2018 08:29 AM, Will Deacon wrote:
> On Tue, Aug 14, 2018 at 08:17:48AM -0700, Greg Hackmann wrote:
>> On 08/14/2018 03:40 AM, Will Deacon wrote:
>>> Hi Greg,
>>>
>>> On Mon, Aug 13, 2018 at 12:30:11PM -0700, Greg Hackmann wrote:
>>>> ARM64's pfn_valid() shifts away the upper PAGE_SHIFT bits of the input
>>>> before seeing if the PFN is valid.  This leads to false positives when
>>>> some of the upper bits are set, but the lower bits match a valid PFN.
>>>>
>>>> For example, the following userspace code looks up a bogus entry in
>>>> /proc/kpageflags:
>>>>
>>>>     int pagemap = open("/proc/self/pagemap", O_RDONLY);
>>>>     int pageflags = open("/proc/kpageflags", O_RDONLY);
>>>>     uint64_t pfn, val;
>>>>
>>>>     lseek64(pagemap, [...], SEEK_SET);
>>>>     read(pagemap, &pfn, sizeof(pfn));
>>>>     if (pfn & (1UL << 63)) {        /* valid PFN */
>>>>         pfn &= ((1UL << 55) - 1);   /* clear flag bits */
>>>>         pfn |= (1UL << 55);
>>>>         lseek64(pageflags, pfn * sizeof(uint64_t), SEEK_SET);
>>>>         read(pageflags, &val, sizeof(val));
>>>>     }
>>>>
>>>> On ARM64 this causes the userspace process to crash with SIGSEGV rather
>>>> than reading (1 << KPF_NOPAGE).  kpageflags_read() treats the offset as
>>>> valid, and stable_page_flags() will try to access an address between the
>>>> user and kernel address ranges.
>>>>
>>>> Signed-off-by: Greg Hackmann <ghackmann@google.com>
>>>> ---
>>>>  arch/arm64/mm/init.c | 6 +++++-
>>>>  1 file changed, 5 insertions(+), 1 deletion(-)
>>>
>>> Thanks, this looks like a sensible fix to me. Do you think it warrants a
>>> CC stable?
>>>
>>> Will
>>
>> Yes, I think so.  Should I resend with a "Fixes" field?
> 
> Could do, but I think this goes all the way back to day 1! Doesn't arch/arm/
> also suffer from the same issue?
> 
> Will
> 

Yeah, it looks like this happens on non-LPAE 32-bit kernels too.  LPAE
kernels aren't affected since __pfn_to_phys() promotes to a 64-bit type.

I can submit a fix for that too while I'm at it.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-08-15 19:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-08-13 19:30 [PATCH] arm64: mm: check for upper PAGE_SHIFT bits in pfn_valid() Greg Hackmann
2018-08-14 10:40 ` Will Deacon
2018-08-14 15:17   ` Greg Hackmann
2018-08-14 15:29     ` Will Deacon
2018-08-15 19:30       ` Greg Hackmann

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).