[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <751739c3-2d33-4759-b9cf-a02d35391cbb@suse.cz>
Date: Tue, 12 Nov 2024 11:40:00 +0100
From: Vlastimil Babka <vbabka@...e.cz>
To: Jann Horn <jannh@...gle.com>, Andrew Morton <akpm@...ux-foundation.org>
Cc: "Joel Fernandes (Google)" <joel@...lfernandes.org>,
Lorenzo Stoakes <lorenzo.stoakes@...cle.com>,
"Liam R. Howlett" <Liam.Howlett@...cle.com>, linux-mm@...ck.org,
linux-kernel@...r.kernel.org, stable@...r.kernel.org
Subject: Re: [PATCH] mm/mremap: Fix address wraparound in move_page_tables()
On 11/11/24 20:34, Jann Horn wrote:
> On 32-bit platforms, it is possible for the expression
> `len + old_addr < old_end` to be false-positive if `len + old_addr` wraps
> around. `old_addr` is the cursor in the old range up to which page table
> entries have been moved; so if the operation succeeded, `old_addr` is the
> *end* of the old region, and adding `len` to it can wrap.
>
> The overflow causes mremap() to mistakenly believe that PTEs have been
> copied; the consequence is that mremap() bails out, but doesn't move the
> PTEs back before the new VMA is unmapped, causing anonymous pages in the
> region to be lost. So basically if userspace tries to mremap() a
> private-anon region and hits this bug, mremap() will return an error and
> the private-anon region's contents appear to have been zeroed.
>
> The idea of this check is that `old_end - len` is the original start
> address, and writing the check that way also makes it easier to read; so
> fix the check by rearranging the comparison accordingly.
>
> (An alternate fix would be to refactor this function by introducing an
> "orig_old_start" variable or such.)
>
> Cc: stable@...r.kernel.org
> Fixes: af8ca1c14906 ("mm/mremap: optimize the start addresses in move_page_tables()")
> Signed-off-by: Jann Horn <jannh@...gle.com>
Acked-by: Vlastimil Babka <vbabka@...e.cz>
Thanks!
> ---
> Tested in a VM with a 32-bit X86 kernel; without the patch:
>
> ```
> user@...n:~/big_mremap$ cat test.c
> #define _GNU_SOURCE
> #include <stdlib.h>
> #include <stdio.h>
> #include <err.h>
> #include <sys/mman.h>
>
> #define ADDR1 ((void*)0x60000000)
> #define ADDR2 ((void*)0x10000000)
> #define SIZE 0x50000000uL
>
> int main(void) {
> unsigned char *p1 = mmap(ADDR1, SIZE, PROT_READ|PROT_WRITE,
> MAP_ANONYMOUS|MAP_PRIVATE|MAP_FIXED_NOREPLACE, -1, 0);
> if (p1 == MAP_FAILED)
> err(1, "mmap 1");
> unsigned char *p2 = mmap(ADDR2, SIZE, PROT_NONE,
> MAP_ANONYMOUS|MAP_PRIVATE|MAP_FIXED_NOREPLACE, -1, 0);
> if (p2 == MAP_FAILED)
> err(1, "mmap 2");
> *p1 = 0x41;
> printf("first char is 0x%02hhx\n", *p1);
> unsigned char *p3 = mremap(p1, SIZE, SIZE,
> MREMAP_MAYMOVE|MREMAP_FIXED, p2);
> if (p3 == MAP_FAILED) {
> printf("mremap() failed; first char is 0x%02hhx\n", *p1);
> } else {
> printf("mremap() succeeded; first char is 0x%02hhx\n", *p3);
> }
> }
> user@...n:~/big_mremap$ gcc -static -o test test.c
> user@...n:~/big_mremap$ setarch -R ./test
> first char is 0x41
> mremap() failed; first char is 0x00
> ```
>
> With the patch:
>
> ```
> user@...n:~/big_mremap$ setarch -R ./test
> first char is 0x41
> mremap() succeeded; first char is 0x41
> ```
> ---
> mm/mremap.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/mremap.c b/mm/mremap.c
> index dda09e957a5d4c2546934b796e862e5e0213b311..dee98ff2bbd64439200dddac16c4bd054537c2ed 100644
> --- a/mm/mremap.c
> +++ b/mm/mremap.c
> @@ -648,7 +648,7 @@ unsigned long move_page_tables(struct vm_area_struct *vma,
> * Prevent negative return values when {old,new}_addr was realigned
> * but we broke out of the above loop for the first PMD itself.
> */
> - if (len + old_addr < old_end)
> + if (old_addr < old_end - len)
> return 0;
>
> return len + old_addr - old_end; /* how much done */
>
> ---
> base-commit: 2d5404caa8c7bb5c4e0435f94b28834ae5456623
> change-id: 20241111-fix-mremap-32bit-wrap-747105730f20
>
Powered by blists - more mailing lists