Skip to contentSkip to conversation

properly account for stack randomization and guard gap

PATCH Applied [email protected] 2 participants 6 messages Jun 22, 2017 – Jun 23, 2017

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

cover properly account for stack randomization and guard gap

When RLIMIT_STACK is larger than the minimum gap enforced by mmap_base(), it is possible for the kernel to place the mmap area where the stack wants to grow, resulting in the stack not being able to use the space that should have been allocated to it through RLIMIT_STACK.

This series ensures that x86, ARM64, and PPC have at least RLIMIT_STACK + stack randomization + the stack guard gap space available for the stack.

s390 seems to be ok. I have not checked other architectures.

#1 lore raw

PATCH 1/3 x86/mmap: properly account for stack randomization in mmap_base

From: Rik van Riel <[email protected]>

When RLIMIT_STACK is, for example, 256MB, the current code results in a gap between the top of the task and mmap_base of 256MB, failing to take into account the amount by which the stack address was randomized. In other words, the stack gets less than RLIMIT_STACK space.

Ensure that the gap between the stack and mmap_base always takes stack randomization and the stack guard gap into account.

Quoted 1 line

From Daniel Micay's linux-hardened tree.

---
 arch/x86/mm/mmap.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/mm/mmap.c +6 -1
index 19ad095b41df..7c35dd73dbd4 100644
@@ -95,13 +95,18 @@ unsigned long arch_mmap_rnd(void)
 static unsigned long mmap_base(unsigned long rnd, unsigned long task_size)
 {
 	unsigned long gap = rlimit(RLIMIT_STACK);
+	unsigned long pad = stack_maxrandom_size(task_size) + stack_guard_gap;
 	unsigned long gap_min, gap_max;
 
+	/* Values close to RLIM_INFINITY can overflow. */
+	if (gap + pad > gap)
+		gap += pad;
+
 	/*
 	 * Top of mmap area (just below the process stack).
 	 * Leave an at least ~128 MB hole with possible stack randomization.
 	 */
-	gap_min = SIZE_128M + stack_maxrandom_size(task_size);
+	gap_min = SIZE_128M;
 	gap_max = (task_size / 6) * 5;
 
 	if (gap < gap_min)
Signature · 1 line
2.9.4
#2 lore raw

PATCH 2/3 arm64/mmap: properly account for stack randomization in mmap_base

In reply to [email protected]

From: Rik van Riel <[email protected]>

When RLIMIT_STACK is, for example, 256MB, the current code results in a gap between the top of the task and mmap_base of 256MB, failing to take into account the amount by which the stack address was randomized. In other words, the stack gets less than RLIMIT_STACK space.

Ensure that the gap between the stack and mmap_base always takes stack randomization and the stack guard gap into account.

Quoted 1 line

From Daniel Micay's linux-hardened tree.

---
 arch/arm64/mm/mmap.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/arm64/mm/mmap.c +6 -1
index 7b0d55756eb1..a0cb6b8ccde7 100644
@@ -34,7 +34,7 @@
  * Leave enough space between the mmap area and the stack to honour ulimit in
  * the face of randomisation.
  */
-#define MIN_GAP (SZ_128M + ((STACK_RND_MASK << PAGE_SHIFT) + 1))
+#define MIN_GAP (SZ_128M)
 #define MAX_GAP	(STACK_TOP/6*5)
 
 static int mmap_is_legacy(void)
@@ -64,6 +64,11 @@ unsigned long arch_mmap_rnd(void)
 static unsigned long mmap_base(unsigned long rnd)
 {
 	unsigned long gap = rlimit(RLIMIT_STACK);
+	unsigned long pad = (STACK_RND_MASK << PAGE_SHIFT) + stack_guard_gap;
+
+	/* Values close to RLIM_INFINITY can overflow. */
+	if (gap + pad > gap)
+		gap += pad;
 
 	if (gap < MIN_GAP)
 		gap = MIN_GAP;
Signature · 1 line
2.9.4
#3 lore raw

PATCH 3/3 powerpc,mmap: properly account for stack randomization in mmap_base

In reply to [email protected]

From: Rik van Riel <[email protected]>

When RLIMIT_STACK is, for example, 256MB, the current code results in a gap between the top of the task and mmap_base of 256MB, failing to take into account the amount by which the stack address was randomized. In other words, the stack gets less than RLIMIT_STACK space.

Ensure that the gap between the stack and mmap_base always takes stack randomization and the stack guard gap into account.

Inspired by Daniel Micay's linux-hardened tree.

---
 arch/powerpc/mm/mmap.c | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)
arch/powerpc/mm/mmap.c +19 -9
index 0ee6be4f1ba4..5d78b193fec4 100644
@@ -34,16 +34,9 @@
 /*
  * Top of mmap area (just below the process stack).
  *
- * Leave at least a ~128 MB hole on 32bit applications.
- *
- * On 64bit applications we randomise the stack by 1GB so we need to
- * space our mmap start address by a further 1GB, otherwise there is a
- * chance the mmap area will end up closer to the stack than our ulimit
- * requires.
+ * Leave at least a ~128 MB hole.
  */
-#define MIN_GAP32 (128*1024*1024)
-#define MIN_GAP64 ((128 + 1024)*1024*1024UL)
-#define MIN_GAP ((is_32bit_task()) ? MIN_GAP32 : MIN_GAP64)
+#define MIN_GAP (128*1024*1024)
 #define MAX_GAP (TASK_SIZE/6*5)
 
 static inline int mmap_is_legacy(void)
@@ -71,9 +64,26 @@ unsigned long arch_mmap_rnd(void)
 	return rnd << PAGE_SHIFT;
 }
 
+static inline unsigned long stack_maxrandom_size(void)
+{
+	if (!(current->flags & PF_RANDOMIZE))
+		return 0;
+
+	/* 8MB for 32bit, 1GB for 64bit */
+	if (is_32bit_task())
+		return (1<<23);
+	else
+		return (1<<30);
+}
+
 static inline unsigned long mmap_base(unsigned long rnd)
 {
 	unsigned long gap = rlimit(RLIMIT_STACK);
+	unsigned long pad = stack_maxrandom_size() + stack_guard_gap;
+
+	/* Values close to RLIM_INFINITY can overflow. */
+	if (gap + pad > gap)
+		gap += pad;
 
 	if (gap < MIN_GAP)
 		gap = MIN_GAP;
Signature · 1 line
2.9.4
Acked-by on the v1 series
#4 lore raw
In reply to [email protected]
Quoted 11 lines · * [email protected] <[email protected]> wrote:

When RLIMIT_STACK is larger than the minimum gap enforced by mmap_base(), it is possible for the kernel to place the mmap area where the stack wants to grow, resulting in the stack not being able to use the space that should have been allocated to it through RLIMIT_STACK.

This series ensures that x86, ARM64, and PPC have at least RLIMIT_STACK + stack randomization + the stack guard gap space available for the stack.

s390 seems to be ok. I have not checked other architectures.

x86 patch LGTM:

... but I suspect this wants to go via -mm or Linus directly?

Thanks,

Ingo

#5 lore raw
Quoted 12 lines · Ingo Molnar
Quoted 10 lines · * [email protected] <[email protected]> wrote:

When RLIMIT_STACK is larger than the minimum gap enforced by mmap_base(), it is possible for the kernel to place the mmap area where the stack wants to grow, resulting in the stack not being able to use the space that should have been allocated to it through RLIMIT_STACK.

This series ensures that x86, ARM64, and PPC have at least RLIMIT_STACK + stack randomization + the stack guard gap space available for the stack.

Quoted 7 lines
Quoted 1 line

s390 seems to be ok. I have not checked other architectures.

x86 patch LGTM:

... but I suspect this wants to go via -mm or Linus directly?

I believe Andrew picked it up yesterday.