Skip to contentSkip to conversation

mm/vmalloc: Don't allow VM_NO_GUARD on vmap()

PATCH In review Peter Zijlstra 5 participants 5 messages Sep 16, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/vmalloc: Don't allow VM_NO_GUARD on vmap()

The vmalloc guard pages are added on top of each allocation, thereby isolating any two allocations from one another. The top guard of the lower allocation is the bottom guard guard of the higher allocation etc.

Therefore VM_NO_GUARD is dangerous; it breaks the basic premise of isolating separate allocations.

There are only two in-tree users of this flag, neither of which use it through the exported interface. Ensure it stays this way.

---
 include/linux/vmalloc.h | 2 +-
 mm/vmalloc.c            | 7 +++++++
 2 files changed, 8 insertions(+), 1 deletion(-)
include/linux/vmalloc.h +1 -1
index 671d402c3778..10e9571ff0b2 100644
@@ -22,7 +22,7 @@ struct notifier_block;		/* in notifier.h */
 #define VM_USERMAP		0x00000008	/* suitable for remap_vmalloc_range */
 #define VM_DMA_COHERENT		0x00000010	/* dma_alloc_coherent */
 #define VM_UNINITIALIZED	0x00000020	/* vm_struct is not fully initialized */
-#define VM_NO_GUARD		0x00000040      /* don't add guard page */
+#define VM_NO_GUARD		0x00000040      /* ***DANGEROUS*** don't add guard page */
 #define VM_KASAN		0x00000080      /* has allocated kasan shadow memory */
 #define VM_FLUSH_RESET_PERMS	0x00000100	/* reset direct map and flush TLB on unmap, can't be freed in atomic context */
 #define VM_MAP_PUT_PAGES	0x00000200	/* put pages and free array in vfree */
mm/vmalloc.c +7 -0
index d77830ff604c..01927ebea267 100644
@@ -2743,6 +2743,13 @@ void *vmap(struct page **pages, unsigned int count,
 
 	might_sleep();
 
+	/*
+	 * Your top guard is someone else's bottom guard. Not having a top
+	 * guard compromises someone else's mappings too.
+	 */
+	if (WARN_ON_ONCE(flags & VM_NO_GUARD))
+		flags &= ~VM_NO_GUARD;
+
 	if (count > totalram_pages())
 		return NULL;
 
Reviewed-by on v1 1/1
#2 lore raw
In reply to Peter Zijlstra
Quoted 40 lines · Peter Zijlstra

The vmalloc guard pages are added on top of each allocation, thereby isolating any two allocations from one another. The top guard of the lower allocation is the bottom guard guard of the higher allocation etc.

Therefore VM_NO_GUARD is dangerous; it breaks the basic premise of isolating separate allocations.

There are only two in-tree users of this flag, neither of which use it through the exported interface. Ensure it stays this way.

---
  include/linux/vmalloc.h | 2 +-
  mm/vmalloc.c            | 7 +++++++
  2 files changed, 8 insertions(+), 1 deletion(-)
include/linux/vmalloc.h +1 -1
index 671d402c3778..10e9571ff0b2 100644
@@ -22,7 +22,7 @@ struct notifier_block;		/* in notifier.h */
  #define VM_USERMAP		0x00000008	/* suitable for remap_vmalloc_range */
  #define VM_DMA_COHERENT		0x00000010	/* dma_alloc_coherent */
  #define VM_UNINITIALIZED	0x00000020	/* vm_struct is not fully initialized */
-#define VM_NO_GUARD		0x00000040      /* don't add guard page */
+#define VM_NO_GUARD		0x00000040      /* ***DANGEROUS*** don't add guard page */
  #define VM_KASAN		0x00000080      /* has allocated kasan shadow memory */
  #define VM_FLUSH_RESET_PERMS	0x00000100	/* reset direct map and flush TLB on unmap, can't be freed in atomic context */
  #define VM_MAP_PUT_PAGES	0x00000200	/* put pages and free array in vfree */
mm/vmalloc.c +0 -0
index d77830ff604c..01927ebea267 100644
@@ -2743,6 +2743,13 @@ void *vmap(struct page **pages, unsigned int count,
 
  	might_sleep();
 

mm/vmalloc.c void *vmap(struct page **pages, unsigned int count, · line 2755

Quoted 10 lines
mm/vmalloc.c +7 -0
+	/*
+	 * Your top guard is someone else's bottom guard. Not having a top
+	 * guard compromises someone else's mappings too.
+	 */
+	if (WARN_ON_ONCE(flags & VM_NO_GUARD))
+		flags &= ~VM_NO_GUARD;
+
  	if (count > totalram_pages())
  		return NULL;
 
Signature · 3 lines
Thanks,

David / dhildenb
Acked-by on v1 1/1
#3 lore raw
In reply to Peter Zijlstra
Quoted 38 lines · Peter Zijlstra

The vmalloc guard pages are added on top of each allocation, thereby isolating any two allocations from one another. The top guard of the lower allocation is the bottom guard guard of the higher allocation etc.

Therefore VM_NO_GUARD is dangerous; it breaks the basic premise of isolating separate allocations.

There are only two in-tree users of this flag, neither of which use it through the exported interface. Ensure it stays this way.

---
 include/linux/vmalloc.h | 2 +-
 mm/vmalloc.c            | 7 +++++++
 2 files changed, 8 insertions(+), 1 deletion(-)
include/linux/vmalloc.h +1 -1
index 671d402c3778..10e9571ff0b2 100644
@@ -22,7 +22,7 @@ struct notifier_block;		/* in notifier.h */
 #define VM_USERMAP		0x00000008	/* suitable for remap_vmalloc_range */
 #define VM_DMA_COHERENT		0x00000010	/* dma_alloc_coherent */
 #define VM_UNINITIALIZED	0x00000020	/* vm_struct is not fully initialized */
-#define VM_NO_GUARD		0x00000040      /* don't add guard page */
+#define VM_NO_GUARD		0x00000040      /* ***DANGEROUS*** don't add guard page */
 #define VM_KASAN		0x00000080      /* has allocated kasan shadow memory */
 #define VM_FLUSH_RESET_PERMS	0x00000100	/* reset direct map and flush TLB on unmap, can't be freed in atomic context */
 #define VM_MAP_PUT_PAGES	0x00000200	/* put pages and free array in vfree */
mm/vmalloc.c +0 -0
index d77830ff604c..01927ebea267 100644
@@ -2743,6 +2743,13 @@ void *vmap(struct page **pages, unsigned int count,
 
 	might_sleep();

mm/vmalloc.c void *vmap(struct page **pages, unsigned int count, · line 2754

Quoted 10 lines
mm/vmalloc.c +7 -0
 
+	/*
+	 * Your top guard is someone else's bottom guard. Not having a top
+	 * guard compromises someone else's mappings too.
+	 */
+	if (WARN_ON_ONCE(flags & VM_NO_GUARD))
+		flags &= ~VM_NO_GUARD;
+
 	if (count > totalram_pages())
 		return NULL;

Thanks!

Will

Acked-by on v1 1/1
#4 lore raw
In reply to Peter Zijlstra
Quoted 6 lines · Peter Zijlstra

The vmalloc guard pages are added on top of each allocation, thereby isolating any two allocations from one another. The top guard of the lower allocation is the bottom guard guard of the higher allocation etc.

Quoted 7 lines

Therefore VM_NO_GUARD is dangerous; it breaks the basic premise of isolating separate allocations.

There are only two in-tree users of this flag, neither of which use it through the exported interface. Ensure it stays this way.

Yes, please. :)

Signature · 1 line
Kees Cook