From: Vlastimil Babka <vbabka@suse.cz>
To: Dongyang Zhan <zdyzztq@gmail.com>, linux-mm@kvack.org
Subject: Re: Potential null pointer dereference in remap_vmalloc_range_partial()
Date: Wed, 6 May 2020 18:57:45 +0200 [thread overview]
Message-ID: <f89059b8-21c7-23e1-1ed4-cc9b0041dac8@suse.cz> (raw)
In-Reply-To: <CAFSR4csW7y=dCh+Xe2rJW7toy148dw3N3Dpq2Pb=VLyCUhcBaw@mail.gmail.com>
On 5/3/20 8:03 AM, Dongyang Zhan wrote:
> Hi,
>
> I am a security researcher. I found a potential bug in /mm/sparse.c. I hope you can help me to confirm it.
>
> In Linux 4.10.17,
In general, the upstream community prioritises recent kernels, 4.10 is rather
old. Bugs from older kernels should be verified if they still apply to most
recent version. If they don't, it's a matter of identifying the fixing commit
and sending it to stable.
> remap_vmalloc_range_partial() in /mm/vmalloc.c does not check the validation of allocated memory 'page', which may cause a null pointer dereference bug.
>
> int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
> void *kaddr, unsigned long size)
> {
> ...
> struct page *page = vmalloc_to_page(kaddr); //page is possible to be null
> int ret;
>
> ret = vm_insert_page(vma, uaddr, page); //null pointer dereference of page
> if (ret)
> return ret;
> ...
> }
> Let us see vmalloc_to_page(kaddr) in /mm/vmalloc.c
> struct page *vmalloc_to_page(const void *vmalloc_addr)
> {
> unsigned long addr = (unsigned long) vmalloc_addr;
> struct page *page = NULL;
> pgd_t *pgd = pgd_offset_k(addr);
>
> /*
> * XXX we might need to change this if we add VIRTUAL_BUG_ON for
> * architectures that do not vmalloc module space
> */
> VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
>
> if (!pgd_none(*pgd)) {
> ...
> }
> return page;
> }
> We can find that page is possible to be NULL.
Current version of vmalloc_to_page() is even more explicit:
if (pgd_none(*pgd))
return NULL;
> Then, we can see this function vm_insert_page(vma, uaddr, page) in /mm/memory.c.
> int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
> struct page *page)
> {
> if (addr < vma->vm_start || addr >= vma->vm_end)
> return -EFAULT;
> if (!page_count(page)) //this function can trigger the bug.
> return -EINVAL;
> ...
> }
> page_count() can be found in /include/linux/page_ref.h.
> static inline int page_count(struct page *page)
> {
> return atomic_read(&compound_head(page)->_refcount);
> }
> Directly using the 'page' pointer is not secure.
This all assumes remap_vmalloc_range_partial() was called with kaddr that
doesn't have the page table hierarchy populated. You say 'validation of
allocated memory 'page'' but page is not being allocated, it's looked up from
page tables. If it was valid vmalloc region, the page tables should be populated
and there's no NULL. To show a bug, you would need to show how
remap_vmalloc_range_partial() can be called with kaddr that's not a proper
vmalloc allocation.
HTH,
Vlastimil
prev parent reply other threads:[~2020-05-06 16:57 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-05-03 6:03 Dongyang Zhan
2020-05-06 16:57 ` Vlastimil Babka [this message]
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=f89059b8-21c7-23e1-1ed4-cc9b0041dac8@suse.cz \
--to=vbabka@suse.cz \
--cc=linux-mm@kvack.org \
--cc=zdyzztq@gmail.com \
/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