Skip to contentSkip to conversation

kexec jump: check code size in control page

PATCH In review Huang Ying 3 participants 6 messages Aug 7, 2008 – Aug 8, 2008

Subsystems (3)
lore
Share & tools
lore

Huang Ying

author
#0 lore raw

PATCH 2/6 kexec jump: check code size in control page

Kexec/Kexec-jump requires code size in control page is less than PAGE_SIZE/2. This patch adds runtime checking for this.

---
 arch/x86/kernel/machine_kexec_32.c   |    4 ++++
 arch/x86/kernel/relocate_kernel_32.S |    3 +++
 include/asm-x86/kexec.h              |    1 +
 3 files changed, 8 insertions(+)
arch/x86/kernel/machine_kexec_32.c +4 -0
@@ -92,6 +92,10 @@ int machine_kexec_prepare(struct kimage 
 {
 	if (nx_enabled)
 		set_pages_x(image->control_code_page, 1);
+
+	BUG_ON((unsigned long)kexec_control_page_code_end - \
+	       (unsigned long)relocate_kernel >= PAGE_SIZE/2);
+
 	return 0;
 }
 
arch/x86/kernel/relocate_kernel_32.S +3 -0
@@ -376,3 +376,6 @@ swap_pages:
 	popl	%ebx
 	popl	%ebp
 	ret
+
+	.globl kexec_control_page_code_end
+kexec_control_page_code_end:
include/asm-x86/kexec.h +1 -0
@@ -159,6 +159,7 @@ relocate_kernel(unsigned long indirectio
 		unsigned long start_address,
 		unsigned int has_pae,
 		unsigned int preserve_context);
+void kexec_control_page_code_end(void);
 #else
 NORET_TYPE void
 relocate_kernel(unsigned long indirection_page,

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#1 lore raw
Quoted 4 lines

Kexec/Kexec-jump requires code size in control page is less than PAGE_SIZE/2. This patch adds runtime checking for this.

ACK.

Signature · 7 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#2 lore raw
In reply to Huang Ying
Quoted 14 lines · Huang Ying

Kexec/Kexec-jump requires code size in control page is less than PAGE_SIZE/2. This patch adds runtime checking for this.

---
 arch/x86/kernel/machine_kexec_32.c   |    4 ++++
 arch/x86/kernel/relocate_kernel_32.S |    3 +++
 include/asm-x86/kexec.h              |    1 +
 3 files changed, 8 insertions(+)

arch/x86/kernel/machine_kexec_32.c int machine_kexec_prepare(struct kimage · line 98

Quoted 7 lines
arch/x86/kernel/machine_kexec_32.c +4 -0
@@ -92,6 +92,10 @@ int machine_kexec_prepare(struct kimage 
 {
 	if (nx_enabled)
 		set_pages_x(image->control_code_page, 1);
+
+	BUG_ON((unsigned long)kexec_control_page_code_end - \
+	       (unsigned long)relocate_kernel >= PAGE_SIZE/2);
+

Hi Huang,

Run time check is better than nothing but I think in this case it would be better if we can catch it at compile time.

One of the methods will be to write a small program of your own and put in script/ and at build time check for the size and flag error. May be there are other better ways to do this.

Thanks
Vivek

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#3 lore raw

Hi!

Quoted 3 lines
Quoted 3 lines

PAGE_SIZE/2. This patch adds runtime checking for this.

...

Quoted 12 lines

arch/x86/kernel/machine_kexec_32.c int machine_kexec_prepare(struct kimage · line 98

Quoted 7 lines

{

arch/x86/kernel/machine_kexec_32.c +4 -0
 	if (nx_enabled)
 		set_pages_x(image->control_code_page, 1);
+
+	BUG_ON((unsigned long)kexec_control_page_code_end - \
+	       (unsigned long)relocate_kernel >= PAGE_SIZE/2);
+

Run time check is better than nothing but I think in this case it would be better if we can catch it at compile time.

Quoted 3 lines

One of the methods will be to write a small program of your own and put in script/ and at build time check for the size and flag error. May be there are other better ways to do this.

BUILD_BUG_ON()?

Signature · 7 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
author
#4 lore raw
Quoted 16 lines · Pavel Machek

Hi!

Quoted 3 lines
Quoted 3 lines

PAGE_SIZE/2. This patch adds runtime checking for this.

...

Quoted 8 lines
Quoted 7 lines

{

(quoted diff) +4 -0
 	if (nx_enabled)
 		set_pages_x(image->control_code_page, 1);
+
+	BUG_ON((unsigned long)kexec_control_page_code_end - \
+	       (unsigned long)relocate_kernel >= PAGE_SIZE/2);
+
Quoted 8 lines
Quoted 6 lines

Run time check is better than nothing but I think in this case it would be better if we can catch it at compile time.

One of the methods will be to write a small program of your own and put in script/ and at build time check for the size and flag error. May be there are other better ways to do this.

BUILD_BUG_ON()?

I tried with BUILD_BUG_ON(), and compiling is OK for both of following statement:

BUILD_BUG_ON((unsigned long)kexec_control_page_code_end - \
	     (unsigned long)relocate_kernel >= PAGE_SIZE/2);
BUILD_BUG_ON((unsigned long)kexec_control_page_code_end - \
	     (unsigned long)relocate_kernel < PAGE_SIZE/2);

In general, I think value of kexec_control_page_code_end and relocate_kernel is not determined during compiling time. So BUILD_BUG_ON() doesn't work.

Another idea, use ASSERT() command of ld link script as in the following patch:

arch/x86/kernel/vmlinux_32.lds.S +2 -0
@@ -209,3 +209,5 @@ SECTIONS
 
   DWARF_DEBUG
 }
+
+#include "vmlinux_check_32.lds.S"
arch/x86/kernel/vmlinux_check_32.lds.S new file +3 -0
@@ -0,0 +1,3 @@
+#include <asm/kexec.h>
+
+ASSERT(kexec_control_page_code_end - relocate_kernel >= 2048, "kexec control page code size is too big")

It works for me. What do you think about that?

Best Regards,
Huang Ying

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#5 lore raw
Quoted 55 lines · Huang Ying
Quoted 16 lines · Pavel Machek

Hi!

Quoted 3 lines
Quoted 3 lines

PAGE_SIZE/2. This patch adds runtime checking for this.

...

Quoted 8 lines
Quoted 7 lines

{

(quoted diff) +4 -0
 	if (nx_enabled)
 		set_pages_x(image->control_code_page, 1);
+
+	BUG_ON((unsigned long)kexec_control_page_code_end - \
+	       (unsigned long)relocate_kernel >= PAGE_SIZE/2);
+
Quoted 8 lines
Quoted 6 lines

Run time check is better than nothing but I think in this case it would be better if we can catch it at compile time.

One of the methods will be to write a small program of your own and put in script/ and at build time check for the size and flag error. May be there are other better ways to do this.

BUILD_BUG_ON()?

I tried with BUILD_BUG_ON(), and compiling is OK for both of following statement:

BUILD_BUG_ON((unsigned long)kexec_control_page_code_end - \
	     (unsigned long)relocate_kernel >= PAGE_SIZE/2);
BUILD_BUG_ON((unsigned long)kexec_control_page_code_end - \
	     (unsigned long)relocate_kernel < PAGE_SIZE/2);

In general, I think value of kexec_control_page_code_end and relocate_kernel is not determined during compiling time. So BUILD_BUG_ON() doesn't work.

Another idea, use ASSERT() command of ld link script as in the following patch:

arch/x86/kernel/vmlinux_32.lds.S +2 -0
@@ -209,3 +209,5 @@ SECTIONS
 
   DWARF_DEBUG
 }
+
+#include "vmlinux_check_32.lds.S"

arch/x86/kernel/vmlinux_check_32.lds.S line 3

Quoted 3 lines
arch/x86/kernel/vmlinux_check_32.lds.S new file +3 -0
@@ -0,0 +1,3 @@
+#include <asm/kexec.h>
+
+ASSERT(kexec_control_page_code_end - relocate_kernel >= 2048, "kexec control page code size is too big")

Use of ASSERT() looks good to me. I think creation of extra file for a single ASSERT() can be avoided. Instead, we can just put this assert in vmlinux_32.lds.S itself?

Thansk
Vivek

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec