linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
To: Bert Karwatzki <spasswolf@web.de>
Cc: "Liam R . Howlett" <Liam.Howlett@oracle.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v8 14/21] mm/mmap: Avoid zeroing vma tree in mmap_region()
Date: Mon, 14 Oct 2024 10:46:51 +0100	[thread overview]
Message-ID: <02217d08-bc08-4ffe-8e63-07878561f9b7@lucifer.local> (raw)
In-Reply-To: <20241013223601.3823-1-spasswolf@web.de>

On Mon, Oct 14, 2024 at 12:35:59AM +0200, Bert Karwatzki wrote:
> I created a program which can trigger the bug on newer kernel (after the
> "Avoid zeroing vma tree in mmap_region()" patch and before the fix).
> My original goal was to trigger the bug on older kernels,
> but that does not work, yet.
>
> Bert Karwatzki

Thanks, that's great!

For older kernels the problem should still be present, the fundamental
thing that changed from the point of view of this bug is that merge won't
contribute to the number of VMAs being overwritten at once.

To trigger prior to commit f8d112a4e657 ("mm/mmap: avoid zeroing vma tree
in mmap_region()") you would need to create a situation where the _clear_
triggers the bug, i.e. you must consistute all the VMAs that are being
overwritten by the store from existing VMAs you are overwriting with a
MAP_FIXED.

So some tweaks should get you there...

>
> #define _GNU_SOURCE
> #include <stdlib.h>
> #include <stdio.h>
> #include <fcntl.h>
> #include <unistd.h>
> #include <errno.h>
> #include <sys/mman.h>
>
> int main()
> {
> 	int ret, prot;
> 	void *addr, *tmp = NULL;
>
> 	// Create a lot of consecutive mappings to create a sufficiently deep maple tree
> 	for (int i = 0; i < 224; i++) {
> 		// We're creating mappings with different PROT_ to
> 		// avoid the vmas getting merged.
> 		if (i % 2)
> 			prot = PROT_READ;
> 		else
> 			prot = PROT_WRITE;
>
> 			// These mappings are all at very low addresses in the virtual address space so
> 			// they are mapped before the text and data sections of the executable and
> 			// the library and stack mappings
> 			tmp = mmap(tmp + 0x100000, 0x100000, prot, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
> 	}
>
> 	//
> 	// The maple node we're targetting has the range 0x7800000-0x86fffff (and 15 entries of size 0x100000 each)
> 	//
> 	//    Here is the layout of the tree before the spanning store:
> 	//
>         //                     [0 - ffffffffffffffff]
> 	//			/		\
> 	//		       /		 \
> 	//		[0-86fffff]		[8700000-ffffffffffffffff]
> 	//	       /   |	   \               /         |
> 	//	      /    |	    \		  /          |
> 	//	   ... [6900000-  [7800000-     [8700000-   ...
> 	//	     	77fffff]   86fffff]      87fffff]
> 	//
> 	// Do we always need a spanning_store AND a merge? Yes, and we must be carefull that we do not merge
> 	// with the first vma of the next node.
> 	//
> 	// This gives a spanning_store because the newly created mapping can be merge with
> 	// with the last mapping (0x7700000-0x77fffff) in the previous node as both have PROT_WRITE.
> 	// No corruption here! Why? This merges with the next node, too! (0x8700000-0x87fffff is PROT_WRITE, too)
> 	//addr = mmap((void *) 0x7800000, 0x1000000 - 0x100000, PROT_WRITE, MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
>
> 	// This give a spanning_store, but no merge as the PROT_ flags do not fit, no maple tree corruption here!
> 	//addr = mmap((void *) 0x7700000, 0x1000000, PROT_NONE, MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
>
> 	// this give a spanning store, but no merge, no corruption here!
> 	//addr = mmap((void *) 0x7700000, 0x1000000, PROT_WRITE, MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
>
> 	// This last example give the maple tree corruption and the validate_mm() error:
>
> 	// The mapping from 0x7600000 to 0x7700000 has PROT_READ, so this gives the needed merge
> 	addr = mmap((void *) 0x7700000, 0x1000000, PROT_READ, MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
>
> 	// Just for waiting (to examine the mappings in /proc/PID/maps)
> 	for (;;) {
> 	}
>
> 	return 0;
> }
>
>
>


  reply	other threads:[~2024-10-14  9:47 UTC|newest]

Thread overview: 71+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-13 22:35 Bert Karwatzki
2024-10-14  9:46 ` Lorenzo Stoakes [this message]
2024-10-16 10:28   ` Bert Karwatzki
2024-10-16 11:16     ` Lorenzo Stoakes
2024-10-16 14:13     ` Liam R. Howlett
  -- strict thread matches above, loose matches on Subject: below --
2024-10-04  9:35 Bert Karwatzki
2024-10-04  9:58 ` Lorenzo Stoakes
2024-10-04 14:23 ` Lorenzo Stoakes
2024-10-04 14:26   ` Lorenzo Stoakes
2024-10-04 14:32     ` Lorenzo Stoakes
2024-10-04 14:58       ` Lorenzo Stoakes
2024-10-04 22:41 ` Lorenzo Stoakes
2024-10-05  0:56   ` Bert Karwatzki
2024-10-05  6:21     ` Lorenzo Stoakes
2024-10-05  8:57       ` Bert Karwatzki
2024-10-05 11:11         ` Lorenzo Stoakes
2024-10-04  8:51 Bert Karwatzki
2024-10-04  8:59 ` Lorenzo Stoakes
2024-10-03 17:07 Bert Karwatzki
2024-10-03 17:24 ` Lorenzo Stoakes
2024-10-03 19:32 ` Lorenzo Stoakes
2024-10-04  8:36 ` Lorenzo Stoakes
2024-10-03 13:09 Bert Karwatzki
2024-10-03 13:34 ` Lorenzo Stoakes
2024-10-03 10:51 Bert Karwatzki
2024-10-03 11:17 ` Lorenzo Stoakes
2024-10-03 10:41 Bert Karwatzki
2024-10-03 10:46 ` Lorenzo Stoakes
2024-10-03  8:59 Bert Karwatzki
2024-10-03  9:04 ` Lorenzo Stoakes
2024-10-03  9:27 ` Lorenzo Stoakes
2024-10-02 22:58 Bert Karwatzki
2024-10-03  7:43 ` Lorenzo Stoakes
2024-10-02 22:57 Bert Karwatzki
2024-10-03  8:06 ` Lorenzo Stoakes
2024-10-02 21:58 Bert Karwatzki
2024-10-02 21:48 Bert Karwatzki
2024-10-02 21:41 Bert Karwatzki
     [not found] <20241002105131.4545-1-spasswolf@web.de>
2024-10-02 11:19 ` Lorenzo Stoakes
2024-10-01  2:34 Bert Karwatzki
2024-10-01  8:02 ` Lorenzo Stoakes
2024-10-01  8:38   ` Bert Karwatzki
2024-10-01  8:49     ` Lorenzo Stoakes
2024-10-01  8:55       ` Bert Karwatzki
2024-10-01  8:59         ` Lorenzo Stoakes
2024-10-01  9:10           ` Bert Karwatzki
2024-10-01  9:20             ` Lorenzo Stoakes
2024-10-01  9:49               ` Lorenzo Stoakes
2024-10-01  9:57                 ` Bert Karwatzki
2024-10-01 10:02                   ` Lorenzo Stoakes
2024-10-01 10:22                     ` Bert Karwatzki
2024-10-01 10:33                       ` Lorenzo Stoakes
2024-10-01 10:42                         ` Bert Karwatzki
2024-10-01 11:23                           ` Lorenzo Stoakes
2024-10-01 11:56 ` Lorenzo Stoakes
2024-10-01 16:43   ` Bert Karwatzki
2024-10-01 18:01     ` Lorenzo Stoakes
2024-10-02  8:39       ` Lorenzo Stoakes
2024-10-02  8:48         ` Lorenzo Stoakes
2024-10-02 12:13 ` Lorenzo Stoakes
2024-10-02 13:23   ` Lorenzo Stoakes
2024-10-02 16:13     ` Bert Karwatzki
2024-10-02 17:19       ` Lorenzo Stoakes
2024-10-02 18:28         ` Lorenzo Stoakes
2024-10-02 18:54           ` Lorenzo Stoakes
2024-10-02 20:06           ` Bert Karwatzki
2024-10-02 20:22             ` Lorenzo Stoakes
2024-10-02 20:39               ` Bert Karwatzki
2024-10-02 20:44                 ` Lorenzo Stoakes
2024-10-02 21:13                   ` Lorenzo Stoakes
2024-08-30  4:00 [PATCH v8 00/21] Avoid MAP_FIXED gap exposure Liam R. Howlett
2024-08-30  4:00 ` [PATCH v8 14/21] mm/mmap: Avoid zeroing vma tree in mmap_region() Liam R. Howlett

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=02217d08-bc08-4ffe-8e63-07878561f9b7@lucifer.local \
    --to=lorenzo.stoakes@oracle.com \
    --cc=Liam.Howlett@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=spasswolf@web.de \
    /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