Skip to contentSkip to conversation

x86: kaslr to avoid setup_data regions

PATCH In review Dave Young 2 participants 4 messages May 14, 2014 x86

lore
Share & tools
lore

Dave Young

author
#0 lore raw

PATCH v2 1/1 x86: kaslr to avoid setup_data regions

X86 will pass setup_data region while necessary, these regions could be overwitten by kernel due to kaslr.

Thus iterate and add setup regions to mem_avoid[] in this patch. Set max mem avoid entries 32, hopefully it will be enough.

---
 arch/x86/boot/compressed/aslr.c |   30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)
arch/x86/boot/compressed/aslr.c +28 -2
Index: linux-2.6/arch/x86/boot/compressed/aslr.c
===================================================================
@@ -110,8 +110,9 @@ struct mem_vector {
 	unsigned long size;
 };
 
-#define MEM_AVOID_MAX 5
+#define MEM_AVOID_MAX 32
 static struct mem_vector mem_avoid[MEM_AVOID_MAX];
+static int mem_avoid_nr;
 
 static bool mem_contains(struct mem_vector *region, struct mem_vector *item)
 {
@@ -135,6 +136,28 @@ static bool mem_overlaps(struct mem_vect
 	return true;
 }
 
+static void mem_avoid_setup_data(void)
+{
+	struct setup_data *data;
+	u64 pa_data;
+
+	if (mem_avoid_nr >= MEM_AVOID_MAX) {
+		debug_putstr("KASLR: too many setup_data ranges.\n");
+		return;
+	}
+
+	pa_data = real_mode->hdr.setup_data;
+	while (pa_data) {
+		data = (struct setup_data *)pa_data;
+		if (pa_data < CONFIG_RANDOMIZE_BASE_MAX_OFFSET) {
+			mem_avoid[mem_avoid_nr].start = pa_data;
+			mem_avoid[mem_avoid_nr].size = sizeof(*data) + data->len;
+			mem_avoid_nr++;
+		}
+		pa_data = data->next;
+	}
+}
+
 static void mem_avoid_init(unsigned long input, unsigned long input_size,
 			   unsigned long output, unsigned long output_size)
 {
@@ -177,6 +200,9 @@ static void mem_avoid_init(unsigned long
 	/* Avoid stack memory. */
 	mem_avoid[4].start = (unsigned long)free_mem_end_ptr;
 	mem_avoid[4].size = BOOT_STACK_SIZE;
+	mem_avoid_nr = 5;
+
+	mem_avoid_setup_data();
 }
 
 /* Does this memory vector overlap a known avoided area? */
@@ -184,7 +210,7 @@ static bool mem_avoid_overlap(struct mem
 {
 	int i;
 
-	for (i = 0; i < MEM_AVOID_MAX; i++) {
+	for (i = 0; i < mem_avoid_nr; i++) {
 		if (mem_overlaps(img, &mem_avoid[i]))
 			return true;
 	}
#1 lore raw
Quoted 42 lines · Dave Young

X86 will pass setup_data region while necessary, these regions could be overwitten by kernel due to kaslr.

Thus iterate and add setup regions to mem_avoid[] in this patch. Set max mem avoid entries 32, hopefully it will be enough.

---
 arch/x86/boot/compressed/aslr.c |   30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

Index: linux-2.6/arch/x86/boot/compressed/aslr.c
===================================================================

arch/x86/boot/compressed/aslr.c +14 -1
@@ -110,8 +110,9 @@ struct mem_vector {
 	unsigned long size;
 };
 
-#define MEM_AVOID_MAX 5
+#define MEM_AVOID_MAX 32
 static struct mem_vector mem_avoid[MEM_AVOID_MAX];
+static int mem_avoid_nr;
 
 static bool mem_contains(struct mem_vector *region, struct mem_vector *item)
 {
@@ -135,6 +136,28 @@ static bool mem_overlaps(struct mem_vect
 	return true;
 }
 
+static void mem_avoid_setup_data(void)
+{
+	struct setup_data *data;
+	u64 pa_data;
+
+	if (mem_avoid_nr >= MEM_AVOID_MAX) {
+		debug_putstr("KASLR: too many setup_data ranges.\n");
+		return;
+	}
+
+	pa_data = real_mode->hdr.setup_data;
+	while (pa_data) {

arch/x86/boot/compressed/aslr.c static bool mem_overlaps(struct mem_vect · line 160

Quoted 10 lines
arch/x86/boot/compressed/aslr.c +10 -0
+		data = (struct setup_data *)pa_data;
+		if (pa_data < CONFIG_RANDOMIZE_BASE_MAX_OFFSET) {
+			mem_avoid[mem_avoid_nr].start = pa_data;
+			mem_avoid[mem_avoid_nr].size = sizeof(*data) + data->len;
+			mem_avoid_nr++;
+		}
+		pa_data = data->next;
+	}
+}
+

I have no idea if this real_mode->hdr.setup_data examination is correct, so not commenting on that.

What prevents the loop from overflowing the table?

Additinally reaching the limit should at least result in a big fat warning.

Signature · 1 line
Mateusz Guzik
author
#2 lore raw
Quoted 57 lines · Mateusz Guzik
Quoted 42 lines · Dave Young

X86 will pass setup_data region while necessary, these regions could be overwitten by kernel due to kaslr.

Thus iterate and add setup regions to mem_avoid[] in this patch. Set max mem avoid entries 32, hopefully it will be enough.

---
 arch/x86/boot/compressed/aslr.c |   30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

Index: linux-2.6/arch/x86/boot/compressed/aslr.c
===================================================================

arch/x86/boot/compressed/aslr.c +14 -1
@@ -110,8 +110,9 @@ struct mem_vector {
 	unsigned long size;
 };
 
-#define MEM_AVOID_MAX 5
+#define MEM_AVOID_MAX 32
 static struct mem_vector mem_avoid[MEM_AVOID_MAX];
+static int mem_avoid_nr;
 
 static bool mem_contains(struct mem_vector *region, struct mem_vector *item)
 {
@@ -135,6 +136,28 @@ static bool mem_overlaps(struct mem_vect
 	return true;
 }
 
+static void mem_avoid_setup_data(void)
+{
+	struct setup_data *data;
+	u64 pa_data;
+
+	if (mem_avoid_nr >= MEM_AVOID_MAX) {
+		debug_putstr("KASLR: too many setup_data ranges.\n");
+		return;
+	}
+
+	pa_data = real_mode->hdr.setup_data;
+	while (pa_data) {

arch/x86/boot/compressed/aslr.c static bool mem_overlaps(struct mem_vect · line 160

Quoted 10 lines
arch/x86/boot/compressed/aslr.c +10 -0
+		data = (struct setup_data *)pa_data;
+		if (pa_data < CONFIG_RANDOMIZE_BASE_MAX_OFFSET) {
+			mem_avoid[mem_avoid_nr].start = pa_data;
+			mem_avoid[mem_avoid_nr].size = sizeof(*data) + data->len;
+			mem_avoid_nr++;
+		}
+		pa_data = data->next;
+	}
+}
+

I have no idea if this real_mode->hdr.setup_data examination is correct, so not commenting on that.

Quoted 1 line

What prevents the loop from overflowing the table?

Ooooops, the checking should be in the loop instead of our of it. Will repost. Thanks for catching.

Dave

author
#3 lore raw
Quoted 49 lines · Dave Young
Quoted 48 lines · Mateusz Guzik
Quoted 47 lines · Dave Young

X86 will pass setup_data region while necessary, these regions could be overwitten by kernel due to kaslr.

Thus iterate and add setup regions to mem_avoid[] in this patch. Set max mem avoid entries 32, hopefully it will be enough.

---
 arch/x86/boot/compressed/aslr.c |   30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

Index: linux-2.6/arch/x86/boot/compressed/aslr.c
===================================================================

arch/x86/boot/compressed/aslr.c +19 -1
@@ -110,8 +110,9 @@ struct mem_vector {
 	unsigned long size;
 };
 
-#define MEM_AVOID_MAX 5
+#define MEM_AVOID_MAX 32
 static struct mem_vector mem_avoid[MEM_AVOID_MAX];
+static int mem_avoid_nr;
 
 static bool mem_contains(struct mem_vector *region, struct mem_vector *item)
 {
@@ -135,6 +136,28 @@ static bool mem_overlaps(struct mem_vect
 	return true;
 }
 
+static void mem_avoid_setup_data(void)
+{
+	struct setup_data *data;
+	u64 pa_data;
+
+	if (mem_avoid_nr >= MEM_AVOID_MAX) {
+		debug_putstr("KASLR: too many setup_data ranges.\n");
+		return;
+	}
+
+	pa_data = real_mode->hdr.setup_data;
+	while (pa_data) {
+		data = (struct setup_data *)pa_data;
+		if (pa_data < CONFIG_RANDOMIZE_BASE_MAX_OFFSET) {
+			mem_avoid[mem_avoid_nr].start = pa_data;
+			mem_avoid[mem_avoid_nr].size = sizeof(*data) + data->len;
+			mem_avoid_nr++;
Quoted 8 lines
Quoted 8 lines

arch/x86/boot/compressed/aslr.c static bool mem_overlaps(struct mem_vect · line 160

Quoted 5 lines
arch/x86/boot/compressed/aslr.c +5 -0
+		}
+		pa_data = data->next;
+	}
+}
+

I have no idea if this real_mode->hdr.setup_data examination is correct, so not commenting on that.

Probably should return early when pa_data > 4G because of 4G early mapping. Not sure for 32bit though. I'm not familar with the detail of x86 early boot. Help will be appreciate.

Quoted 7 lines
Quoted 2 lines

What prevents the loop from overflowing the table?

Ooooops, the checking should be in the loop instead of our of it. Will repost. Thanks for catching.

Dave