mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "José Pekkarinen" <jose.pekkarinen@foxhound.fi>
To: Matthew Wilcox <willy@infradead.org>
Cc: akpm@linux-foundation.org, skhan@linuxfoundation.org,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	linux-kernel-mentees@lists.linux.dev,
	syzbot+89edd67979b52675ddec@syzkaller.appspotmail.com,
	Hugh Dickins <hughd@google.com>
Subject: Re: [PATCH] mm/pgtable: return null if no ptl in __pte_offset_map_lock
Date: Wed, 15 Nov 2023 18:05:30 +0200	[thread overview]
Message-ID: <1c4cb1959829ecf4f0c59691d833618c@foxhound.fi> (raw)
In-Reply-To: <ZVTTbuviH9/RWYyI@casper.infradead.org>

On 2023-11-15 16:19, Matthew Wilcox wrote:
> On Wed, Nov 15, 2023 at 08:55:05AM +0200, José Pekkarinen wrote:
>> Documentation of __pte_offset_map_lock suggest there is situations 
>> where
> 
> You should have cc'd Hugh who changed all this code recently.

     Hi,

     Sorry, he seems to be missing if I run get_maintainer.pl:

$ ./scripts/get_maintainer.pl include/linux/mm.h
Andrew Morton <akpm@linux-foundation.org> (maintainer:MEMORY MANAGEMENT)
linux-mm@kvack.org (open list:MEMORY MANAGEMENT)
linux-kernel@vger.kernel.org (open list)

>> a pmd may not have a corresponding page table, in which case it should
>> return NULL without changing ptlp. Syzbot found its ways to produce a
>> NULL dereference in the function showing this case. This patch will
>> provide the exit path suggested if this unlikely situation turns up. 
>> The
>> output of the kasan null-ptr-report follows:
> 
> There's no need to include all this nonsense in the changelog.

     No problem, we can clean the patch if we find there is something
worth upstreaming.

>>  spin_lock include/linux/spinlock.h:351 [inline]
>>  __pte_offset_map_lock+0x154/0x360 mm/pgtable-generic.c:373
>>  pte_offset_map_lock include/linux/mm.h:2939 [inline]
>>  filemap_map_pages+0x698/0x11f0 mm/filemap.c:3582
> 
> This was the only interesting part.
> 
>> +++ b/include/linux/mm.h
>> @@ -2854,7 +2854,7 @@ void ptlock_free(struct ptdesc *ptdesc);
>> 
>>  static inline spinlock_t *ptlock_ptr(struct ptdesc *ptdesc)
>>  {
>> -	return ptdesc->ptl;
>> +	return (likely(ptdesc)) ? ptdesc->ptl : NULL;
>>  }
> 
> I don't think we should be changing ptlock_ptr().

     This is where the null ptr dereference originates, so the only
alternative I can think of is to protect the life cycle of the ptdesc
to prevent it to die between the pte check and the spin_unlock of
__pte_offset_map_lock. Would that work for you?

>> +++ b/mm/pgtable-generic.c
>> @@ -370,6 +370,8 @@ pte_t *__pte_offset_map_lock(struct mm_struct *mm, 
>> pmd_t *pmd,
>>  	if (unlikely(!pte))
>>  		return pte;
>>  	ptl = pte_lockptr(mm, &pmdval);
>> +	if (unlikely(!ptl))
>> +		return NULL;
>>  	spin_lock(ptl);
> 
> I don't understand how this could possibly solve the problem.  If 
> there's
> no PTE level, then __pte_offset_map() should return NULL and we'd 
> already
> return due to the check for !pte.

     I tested the syzbot reproducer in x86 and it doesn't produce this 
kasan
report anymore.

     José.

  reply	other threads:[~2023-11-15 16:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-15  6:55 José Pekkarinen
2023-11-15 14:19 ` Matthew Wilcox
2023-11-15 16:05   ` José Pekkarinen [this message]
2023-11-15 19:04     ` Matthew Wilcox
2023-11-16  7:23       ` Hugh Dickins
2023-11-16  8:26         ` Hugh Dickins
2023-11-16 10:04         ` José Pekkarinen
2023-11-17  6:13           ` Hugh Dickins
2023-11-17  9:16             ` José Pekkarinen

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=1c4cb1959829ecf4f0c59691d833618c@foxhound.fi \
    --to=jose.pekkarinen@foxhound.fi \
    --cc=akpm@linux-foundation.org \
    --cc=hughd@google.com \
    --cc=linux-kernel-mentees@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=skhan@linuxfoundation.org \
    --cc=syzbot+89edd67979b52675ddec@syzkaller.appspotmail.com \
    --cc=willy@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

all inboxes | Powered by JetHome®