Skip to contentSkip to conversation

x86, kaslr: avoid setup_data when picking location

PATCH In review Kees Cook 3 participants 5 messages Sep 11, 2014 – Oct 1, 2014 x86

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 x86, kaslr: avoid setup_data when picking location

The KASLR location-choosing logic needs to avoid the setup_data list memory areas as well. Without this, it would be possible to have the ASLR position stomp on the memory, ultimately causing the boot to fail.

---
 arch/x86/boot/compressed/aslr.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
arch/x86/boot/compressed/aslr.c +15 -0
index fc6091abedb7..d39189ba7f8e 100644
@@ -183,12 +183,27 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size,
 static bool mem_avoid_overlap(struct mem_vector *img)
 {
 	int i;
+	struct setup_data *ptr;
 
 	for (i = 0; i < MEM_AVOID_MAX; i++) {
 		if (mem_overlaps(img, &mem_avoid[i]))
 			return true;
 	}
 
+	/* Avoid all entries in the setup_data linked list. */
+	ptr = (struct setup_data *)(unsigned long)real_mode->hdr.setup_data;
+	while (ptr) {
+		struct mem_vector avoid;
+
+		avoid.start = (u64)ptr;
+		avoid.size = sizeof(*ptr) + ptr->len;
+
+		if (mem_overlaps(img, &avoid))
+			return true;
+
+		ptr = (struct setup_data *)(unsigned long)ptr->next;
+	}
+
 	return false;
 }
 
Signature · 6 lines
1.9.1


-- 
Kees Cook
Chrome OS Security
#1 lore raw

[tip:x86/urgent] x86/kaslr: Avoid the setup_data area when picking location

x86/kaslr: Avoid the setup_data area when picking location
Commit-ID:  0cacbfbeb5077b63d5d3cf6df88b14ac12ad584b
Gitweb:     http://git.kernel.org/tip/0cacbfbeb5077b63d5d3cf6df88b14ac12ad584b
Author:     Kees Cook <[email protected]>
AuthorDate: Thu, 11 Sep 2014 09:19:31 -0700
Committer:  Ingo Molnar <[email protected]>
CommitDate: Fri, 19 Sep 2014 13:04:29 +0200

x86/kaslr: Avoid the setup_data area when picking location

The KASLR location-choosing logic needs to avoid the setup_data list memory areas as well. Without this, it would be possible to have the ASLR position stomp on the memory, ultimately causing the boot to fail.

---
 arch/x86/boot/compressed/aslr.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
arch/x86/boot/compressed/aslr.c +15 -0
index fc6091a..d39189b 100644
@@ -183,12 +183,27 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size,
 static bool mem_avoid_overlap(struct mem_vector *img)
 {
 	int i;
+	struct setup_data *ptr;
 
 	for (i = 0; i < MEM_AVOID_MAX; i++) {
 		if (mem_overlaps(img, &mem_avoid[i]))
 			return true;
 	}
 
+	/* Avoid all entries in the setup_data linked list. */
+	ptr = (struct setup_data *)(unsigned long)real_mode->hdr.setup_data;
+	while (ptr) {
+		struct mem_vector avoid;
+
+		avoid.start = (u64)ptr;
+		avoid.size = sizeof(*ptr) + ptr->len;
+
+		if (mem_overlaps(img, &avoid))
+			return true;
+
+		ptr = (struct setup_data *)(unsigned long)ptr->next;
+	}
+
 	return false;
 }
 
#2 lore raw
In reply to Kees Cook
Quoted 21 lines · Kees Cook
arch/x86/boot/compressed/aslr.c +8 -0
@@ -183,12 +183,27 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size,
 static bool mem_avoid_overlap(struct mem_vector *img)
 {
 	int i;
+	struct setup_data *ptr;
 
 	for (i = 0; i < MEM_AVOID_MAX; i++) {
 		if (mem_overlaps(img, &mem_avoid[i]))
 			return true;
 	}
 
+	/* Avoid all entries in the setup_data linked list. */
+	ptr = (struct setup_data *)(unsigned long)real_mode->hdr.setup_data;
+	while (ptr) {
+		struct mem_vector avoid;
+
+		avoid.start = (u64)ptr;
+		avoid.size = sizeof(*ptr) + ptr->len;

arch/x86/boot/compressed/aslr.c static void mem_avoid_init(unsigned long input, unsigned long input_size, · line 209

Quoted 10 lines
arch/x86/boot/compressed/aslr.c +7 -0
+
+		if (mem_overlaps(img, &avoid))
+			return true;
+
+		ptr = (struct setup_data *)(unsigned long)ptr->next;
+	}
+
 	return false;
 }
 

The use of (u64) in the assignment to avoid.start gives a nuisance warning on 32 bits.

-hpa

author
#3 lore raw
Quoted 27 lines · H. Peter Anvin
Quoted 26 lines · Kees Cook
arch/x86/boot/compressed/aslr.c +14 -0
@@ -183,12 +183,27 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size,
 static bool mem_avoid_overlap(struct mem_vector *img)
 {
      int i;
+     struct setup_data *ptr;
 
      for (i = 0; i < MEM_AVOID_MAX; i++) {
              if (mem_overlaps(img, &mem_avoid[i]))
                      return true;
      }
 
+     /* Avoid all entries in the setup_data linked list. */
+     ptr = (struct setup_data *)(unsigned long)real_mode->hdr.setup_data;
+     while (ptr) {
+             struct mem_vector avoid;
+
+             avoid.start = (u64)ptr;
+             avoid.size = sizeof(*ptr) + ptr->len;
+
+             if (mem_overlaps(img, &avoid))
+                     return true;
+
+             ptr = (struct setup_data *)(unsigned long)ptr->next;
+     }
Quoted 8 lines

arch/x86/boot/compressed/aslr.c static void mem_avoid_init(unsigned long input, unsigned long input_size, · line 209

Quoted 5 lines
arch/x86/boot/compressed/aslr.c +1 -0
+
      return false;
 }
 

The use of (u64) in the assignment to avoid.start gives a nuisance warning on 32 bits.

Ah, good catch! This should be (unsigned long), I think. Shall I send a follow-up patch, or do you want to fix this directly?

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
#4 lore raw
Quoted 7 lines · Kees Cook
Quoted 3 lines

The use of (u64) in the assignment to avoid.start gives a nuisance warning on 32 bits.

Ah, good catch! This should be (unsigned long), I think. Shall I send a follow-up patch, or do you want to fix this directly?

Please do.

In theory it is possible for a bootloader to put an object above 4G even for a 32-bit kernel, but that is likely to break any kind of other things, so I'm not worried about it. You don't need to worry about avoiding such a zone, the problem is mostly that you can't follow the list any further without adding PAE-handling code.

-hpa