Skip to contentSkip to conversation

x86/KASLR: move aligning of minimal address to choose_random_location()

PATCH In review Alexander Kuleshov 2 participants 3 messages Jan 4, 2018 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/KASLR: move aligning of minimal address to choose_random_location()

we align minimal possible address during randomization to CONFIG_PHYSICAL_ALIGN two times: during getting of random physical address and virtual address (only for x86_64).

Let's move this to choose_random_location() to not duplicate code in find_random_virt_addr() and find_random_phys_addr().

---
 arch/x86/boot/compressed/kaslr.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
arch/x86/boot/compressed/kaslr.c +3 -5
index 8199a6187251..8c93e399fdfd 100644
@@ -673,9 +673,6 @@ static unsigned long find_random_phys_addr(unsigned long minimum,
 		return 0;
 	}
 
-	/* Make sure minimum is aligned. */
-	minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
-
 	if (process_efi_entries(minimum, image_size))
 		return slots_fetch_random();
 
@@ -688,8 +685,6 @@ static unsigned long find_random_virt_addr(unsigned long minimum,
 {
 	unsigned long slots, random_addr;
 
-	/* Make sure minimum is aligned. */
-	minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
 	/* Align image_size for easy slot calculations. */
 	image_size = ALIGN(image_size, CONFIG_PHYSICAL_ALIGN);
 
@@ -738,6 +733,9 @@ void choose_random_location(unsigned long input,
 	 */
 	min_addr = min(*output, 512UL << 20);
 
+	/* Make sure minimum is aligned. */
+	min_addr = ALIGN(min_addr, CONFIG_PHYSICAL_ALIGN);
+
 	/* Walk available memory entries to find a random address. */
 	random_addr = find_random_phys_addr(min_addr, output_size);
 	if (!random_addr) {
Signature · 1 line
2.14.3
#1 lore raw
Quoted 36 lines · Alexander Kuleshov

we align minimal possible address during randomization to CONFIG_PHYSICAL_ALIGN two times: during getting of random physical address and virtual address (only for x86_64).

Let's move this to choose_random_location() to not duplicate code in find_random_virt_addr() and find_random_phys_addr().

---
 arch/x86/boot/compressed/kaslr.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
arch/x86/boot/compressed/kaslr.c +0 -5
index 8199a6187251..8c93e399fdfd 100644
@@ -673,9 +673,6 @@ static unsigned long find_random_phys_addr(unsigned long minimum,
 		return 0;
 	}
 
-	/* Make sure minimum is aligned. */
-	minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
-
 	if (process_efi_entries(minimum, image_size))
 		return slots_fetch_random();
 
@@ -688,8 +685,6 @@ static unsigned long find_random_virt_addr(unsigned long minimum,
 {
 	unsigned long slots, random_addr;
 
-	/* Make sure minimum is aligned. */
-	minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
 	/* Align image_size for easy slot calculations. */
 	image_size = ALIGN(image_size, CONFIG_PHYSICAL_ALIGN);
 

arch/x86/boot/compressed/kaslr.c void choose_random_location(unsigned long input, · line 737

Quoted 5 lines
arch/x86/boot/compressed/kaslr.c +2 -0
@@ -738,6 +733,9 @@ void choose_random_location(unsigned long input,
 	 */
 	min_addr = min(*output, 512UL << 20);
 
+	/* Make sure minimum is aligned. */
+	min_addr = ALIGN(min_addr, CONFIG_PHYSICAL_ALIGN);

Hmm, the min_addr is passed to find_random_phys_addr(), but not to find_random_virt_addr().

Quoted 7 lines
arch/x86/boot/compressed/kaslr.c +1 -0
+
 	/* Walk available memory entries to find a random address. */
 	random_addr = find_random_phys_addr(min_addr, output_size);
 	if (!random_addr) {
Signature · 1 line
2.14.3
#2 lore raw
Quoted 40 lines · Baoquan He
Quoted 39 lines · Alexander Kuleshov

we align minimal possible address during randomization to CONFIG_PHYSICAL_ALIGN two times: during getting of random physical address and virtual address (only for x86_64).

Let's move this to choose_random_location() to not duplicate code in find_random_virt_addr() and find_random_phys_addr().

---
 arch/x86/boot/compressed/kaslr.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
arch/x86/boot/compressed/kaslr.c +0 -5
index 8199a6187251..8c93e399fdfd 100644
@@ -673,9 +673,6 @@ static unsigned long find_random_phys_addr(unsigned long minimum,
 		return 0;
 	}
 
-	/* Make sure minimum is aligned. */
-	minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
-
 	if (process_efi_entries(minimum, image_size))
 		return slots_fetch_random();
 
@@ -688,8 +685,6 @@ static unsigned long find_random_virt_addr(unsigned long minimum,
 {
 	unsigned long slots, random_addr;
 
-	/* Make sure minimum is aligned. */
-	minimum = ALIGN(minimum, CONFIG_PHYSICAL_ALIGN);
 	/* Align image_size for easy slot calculations. */
 	image_size = ALIGN(image_size, CONFIG_PHYSICAL_ALIGN);
 
@@ -738,6 +733,9 @@ void choose_random_location(unsigned long input,
 	 */
 	min_addr = min(*output, 512UL << 20);
 
Quoted 5 lines

arch/x86/boot/compressed/kaslr.c void choose_random_location(unsigned long input, · line 737

Quoted 2 lines
arch/x86/boot/compressed/kaslr.c +2 -0
+	/* Make sure minimum is aligned. */
+	min_addr = ALIGN(min_addr, CONFIG_PHYSICAL_ALIGN);

Hmm, the min_addr is passed to find_random_phys_addr(), but not to find_random_virt_addr().

ah, yes, sorry for noise then