Skip to contentSkip to conversation

x86/link: Don't merge .init.text and .init.data

PATCH Superseded Andrew Cooper 3 participants 9 messages Jan 11, 2018 – Feb 27, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/link: Don't merge .init.text and .init.data

c/s 1308f0170c merged .init.text and .init.data, because EFI might properly write-protect r/o sections.

However, this change makes xen-syms unusable for disassembly analysis. In particular, searching for indirect branches as part of the SP2/Spectre mitigation series.

Revert the relevent bits of 1308f0170c and instead modify the EFI relocation code to disable CR0.WP, which is how we deal with relocations in r/o mappings elsewhere.

---
CC: Jan Beulich <[email protected]>

---
 xen/arch/x86/efi/efi-boot.h | 12 ++++++++++++
 xen/arch/x86/efi/mkreloc.c  |  5 -----
 xen/arch/x86/xen.lds.S      |  7 +++----
 3 files changed, 15 insertions(+), 9 deletions(-)
xen/arch/x86/efi/efi-boot.h +12 -0
index d30f688..0b5f218 100644
@@ -47,8 +47,17 @@ extern const struct pe_base_relocs {
 
 static void __init efi_arch_relocate_image(unsigned long delta)
 {
+    unsigned long cr0;
     const struct pe_base_relocs *base_relocs;
 
+    /*
+     * Conditionally disable CR0.WP in case there are relocations present in
+     * read-only mappings.
+     */
+    cr0 = read_cr0();
+    if ( cr0 & X86_CR0_WP )
+        write_cr0(cr0 & ~X86_CR0_WP);
+
     for ( base_relocs = __base_relocs_start; base_relocs < __base_relocs_end; )
     {
         unsigned int i = 0, n;
@@ -96,6 +105,9 @@ static void __init efi_arch_relocate_image(unsigned long delta)
         }
         base_relocs = (const void *)(base_relocs->entries + i + (i & 1));
     }
+
+    if ( cr0 & X86_CR0_WP )
+        write_cr0(cr0);
 }
 
 extern const s32 __trampoline_rel_start[], __trampoline_rel_stop[];
xen/arch/x86/efi/mkreloc.c +0 -5
index 1aca796..509fd83 100644
@@ -267,11 +267,6 @@ static void diff_sections(const unsigned char *ptr1, const unsigned char *ptr2,
             exit(3);
         }
 
-        if ( !(sec->flags & COFF_SECTION_WRITEABLE) )
-            fprintf(stderr,
-                    "Warning: relocation to r/o section %.8s:%08" PRIxFAST32 "\n",
-                    sec->name, i);
-
         printf("\t.word (%u << 12) | 0x%03" PRIxFAST32 "\n",
                reloc, sec->rva + i - disp - rva);
         reloc_size += 2;
xen/arch/x86/xen.lds.S +3 -4
index d5e8821..6a7bbb8 100644
@@ -159,7 +159,7 @@ SECTIONS
   __2M_init_start = .;         /* Start of 2M superpages, mapped RWX (boot only). */
   . = ALIGN(PAGE_SIZE);             /* Init code and data */
   __init_begin = .;
-  .init : {
+  .init.text : {
        _sinittext = .;
        *(.init.text)
        /*
@@ -169,9 +169,8 @@ SECTIONS
         */
        *(.altinstr_replacement)
        _einittext = .;
-
-       . = ALIGN(SMP_CACHE_BYTES);
-
+  } :text
+  .init.data : {
        *(.init.rodata)
        *(.init.rodata.rel)
        *(.init.rodata.str*)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Andrew Cooper

c/s 1308f0170c merged .init.text and .init.data, because EFI might properly write-protect r/o sections.

However, this change makes xen-syms unusable for disassembly analysis. In

s/this makes/that made/

Quoted 8 lines

particular, searching for indirect branches as part of the SP2/Spectre mitigation series.

Revert the relevent bits of 1308f0170c and instead modify the EFI relocation code to disable CR0.WP, which is how we deal with relocations in r/o mappings elsewhere.

with that change above:

Quoted 89 lines

---
CC: Jan Beulich <[email protected]>

---
 xen/arch/x86/efi/efi-boot.h | 12 ++++++++++++
 xen/arch/x86/efi/mkreloc.c  |  5 -----
 xen/arch/x86/xen.lds.S      |  7 +++----
 3 files changed, 15 insertions(+), 9 deletions(-)
xen/arch/x86/efi/efi-boot.h +12 -0
index d30f688..0b5f218 100644
@@ -47,8 +47,17 @@ extern const struct pe_base_relocs {
 
 static void __init efi_arch_relocate_image(unsigned long delta)
 {
+    unsigned long cr0;
     const struct pe_base_relocs *base_relocs;
 
+    /*
+     * Conditionally disable CR0.WP in case there are relocations present in
+     * read-only mappings.
+     */
+    cr0 = read_cr0();
+    if ( cr0 & X86_CR0_WP )
+        write_cr0(cr0 & ~X86_CR0_WP);
+
     for ( base_relocs = __base_relocs_start; base_relocs < __base_relocs_end; )
     {
         unsigned int i = 0, n;
@@ -96,6 +105,9 @@ static void __init efi_arch_relocate_image(unsigned long delta)
         }
         base_relocs = (const void *)(base_relocs->entries + i + (i & 1));
     }
+
+    if ( cr0 & X86_CR0_WP )
+        write_cr0(cr0);
 }
 
 extern const s32 __trampoline_rel_start[], __trampoline_rel_stop[];
xen/arch/x86/efi/mkreloc.c +0 -5
index 1aca796..509fd83 100644
@@ -267,11 +267,6 @@ static void diff_sections(const unsigned char *ptr1, const unsigned char *ptr2,
             exit(3);
         }
 
-        if ( !(sec->flags & COFF_SECTION_WRITEABLE) )
-            fprintf(stderr,
-                    "Warning: relocation to r/o section %.8s:%08" PRIxFAST32 "\n",
-                    sec->name, i);
-
         printf("\t.word (%u << 12) | 0x%03" PRIxFAST32 "\n",
                reloc, sec->rva + i - disp - rva);
         reloc_size += 2;
xen/arch/x86/xen.lds.S +3 -4
index d5e8821..6a7bbb8 100644
@@ -159,7 +159,7 @@ SECTIONS
   __2M_init_start = .;         /* Start of 2M superpages, mapped RWX (boot only). */
   . = ALIGN(PAGE_SIZE);             /* Init code and data */
   __init_begin = .;
-  .init : {
+  .init.text : {
        _sinittext = .;
        *(.init.text)
        /*
@@ -169,9 +169,8 @@ SECTIONS
         */
        *(.altinstr_replacement)
        _einittext = .;
-
-       . = ALIGN(SMP_CACHE_BYTES);
-
+  } :text
+  .init.data : {
        *(.init.rodata)
        *(.init.rodata.rel)
        *(.init.rodata.str*)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 6 lines · Konrad Rzeszutek Wilk
Quoted 4 lines · Andrew Cooper

c/s 1308f0170c merged .init.text and .init.data, because EFI might properly write-protect r/o sections.

However, this change makes xen-syms unusable for disassembly analysis. In

s/this makes/that made/

Erm.  I'm not sure how to apply that regex, but I've changed to

"However, that change makes xen-syms unusable for disassembly analysis."

~Andrew

Quoted 96 lines
Quoted 8 lines

particular, searching for indirect branches as part of the SP2/Spectre mitigation series.

Revert the relevent bits of 1308f0170c and instead modify the EFI relocation code to disable CR0.WP, which is how we deal with relocations in r/o mappings elsewhere.

with that change above:

Quoted 85 lines

---
CC: Jan Beulich <[email protected]>

---
 xen/arch/x86/efi/efi-boot.h | 12 ++++++++++++
 xen/arch/x86/efi/mkreloc.c  |  5 -----
 xen/arch/x86/xen.lds.S      |  7 +++----
 3 files changed, 15 insertions(+), 9 deletions(-)
xen/arch/x86/efi/efi-boot.h +12 -0
index d30f688..0b5f218 100644
@@ -47,8 +47,17 @@ extern const struct pe_base_relocs {
 
 static void __init efi_arch_relocate_image(unsigned long delta)
 {
+    unsigned long cr0;
     const struct pe_base_relocs *base_relocs;
 
+    /*
+     * Conditionally disable CR0.WP in case there are relocations present in
+     * read-only mappings.
+     */
+    cr0 = read_cr0();
+    if ( cr0 & X86_CR0_WP )
+        write_cr0(cr0 & ~X86_CR0_WP);
+
     for ( base_relocs = __base_relocs_start; base_relocs < __base_relocs_end; )
     {
         unsigned int i = 0, n;
@@ -96,6 +105,9 @@ static void __init efi_arch_relocate_image(unsigned long delta)
         }
         base_relocs = (const void *)(base_relocs->entries + i + (i & 1));
     }
+
+    if ( cr0 & X86_CR0_WP )
+        write_cr0(cr0);
 }
 
 extern const s32 __trampoline_rel_start[], __trampoline_rel_stop[];
xen/arch/x86/efi/mkreloc.c +0 -5
index 1aca796..509fd83 100644
@@ -267,11 +267,6 @@ static void diff_sections(const unsigned char *ptr1, const unsigned char *ptr2,
             exit(3);
         }
 
-        if ( !(sec->flags & COFF_SECTION_WRITEABLE) )
-            fprintf(stderr,
-                    "Warning: relocation to r/o section %.8s:%08" PRIxFAST32 "\n",
-                    sec->name, i);
-
         printf("\t.word (%u << 12) | 0x%03" PRIxFAST32 "\n",
                reloc, sec->rva + i - disp - rva);
         reloc_size += 2;
xen/arch/x86/xen.lds.S +3 -4
index d5e8821..6a7bbb8 100644
@@ -159,7 +159,7 @@ SECTIONS
   __2M_init_start = .;         /* Start of 2M superpages, mapped RWX (boot only). */
   . = ALIGN(PAGE_SIZE);             /* Init code and data */
   __init_begin = .;
-  .init : {
+  .init.text : {
        _sinittext = .;
        *(.init.text)
        /*
@@ -169,9 +169,8 @@ SECTIONS
         */
        *(.altinstr_replacement)
        _einittext = .;
-
-       . = ALIGN(SMP_CACHE_BYTES);
-
+  } :text
+  .init.data : {
        *(.init.rodata)
        *(.init.rodata.rel)
        *(.init.rodata.str*)
Signature · 1 line
2.1.4
Quoted 8 lines
Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 11 lines · Andrew Cooper
Quoted 6 lines · Konrad Rzeszutek Wilk
Quoted 4 lines · Andrew Cooper

c/s 1308f0170c merged .init.text and .init.data, because EFI might properly write-protect r/o sections.

However, this change makes xen-syms unusable for disassembly analysis. In

s/this makes/that made/

Erm.  I'm not sure how to apply that regex, but I've changed to

"However, that change makes xen-syms unusable for disassembly analysis."

Perhaps s/makes/made/ ? Just speculating ..

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
In reply to Andrew Cooper
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 11.01.18 at 15:17, <[email protected]> wrote:

c/s 1308f0170c merged .init.text and .init.data, because EFI might properly write-protect r/o sections.

However, this change makes xen-syms unusable for disassembly analysis. In particular, searching for indirect branches as part of the SP2/Spectre mitigation series.

Revert the relevent bits of 1308f0170c and instead modify the EFI relocation code to disable CR0.WP, which is how we deal with relocations in r/o mappings elsewhere.

Doing so is a hack, and I dislike spreading hacks without real need. What's wrong with there being some garbage in the disassembly of .init? Even it you make .init.data its own section again, some garbage will remain (mainly due to the trampoline data). If you care to use tools to find certain patterns in .init, simply discard everything following _einittext before giving the tool a go. IOW I would much prefer for things to be left as they are.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 11.01.18 at 15:17, <[email protected]> wrote:

c/s 1308f0170c merged .init.text and .init.data, because EFI might properly write-protect r/o sections.

Quoted 8 lines
Quoted 7 lines

However, this change makes xen-syms unusable for disassembly analysis. In particular, searching for indirect branches as part of the SP2/Spectre mitigation series.

Revert the relevent bits of 1308f0170c and instead modify the EFI relocation code to disable CR0.WP, which is how we deal with relocations in r/o mappings elsewhere.

Doing so is a hack, and I dislike spreading hacks without real need.

Its a bona-fide feature of the x86 architecture.

Quoted 2 lines

What's wrong with there being some garbage in the disassembly of .init?

objdump -d xen-syms | grep \* gives you a huge quantity of data interpreted as x86 opcode, including the init stringtable.

This isn't the first time I've problems using xen-syms, but it is definitely a blocker to reasonable development of the SP2 mitigation series.

Quoted 2 lines

Even it you make .init.data its own section again, some garbage will remain (mainly due to the trampoline data).

That is far easier to spot an isolate, because it is tiny and reasonably obviously bounded.

Quoted 4 lines

If you
care to use tools to find certain patterns in .init, simply discard everything following _einittext before giving the tool a go. IOW I would much prefer for things to be left as they are.

No.  We should not be deliberately corrupting the binary to work around a theoretical runtime issue for which there is a perfectly fine runtime solution.  This is the real hack.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 12.01.18 at 16:41, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich

Even it you make .init.data its own section again, some garbage will remain (mainly due to the trampoline data).

That is far easier to spot an isolate, because it is tiny and reasonably obviously bounded.

Well, I've pointed out where the easy to spot bounding is:

Quoted 3 lines
Quoted 3 lines

If you
care to use tools to find certain patterns in .init, simply discard everything following _einittext before giving the tool a go. IOW I

^^^

Quoted 5 lines
Quoted 1 line

would much prefer for things to be left as they are.

No. We should not be deliberately corrupting the binary to work around a theoretical runtime issue for which there is a perfectly fine runtime solution. This is the real hack.

Putting all init-time code and data in a single section is a perfectly valid thing to do imo. We don't care about permissions at that point in time, so the resulting RWX section is quite fine (and has a name much better suitable for a PE image). Otherwise the fact that we merge r/o and r/w init time data into a single section would need to be called a hack, too.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw
Quoted 4 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 12.01.18 at 16:41, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich

Even it you make .init.data its own section again, some garbage will remain (mainly due to the trampoline data).

Quoted 8 lines
Quoted 2 lines

That is far easier to spot an isolate, because it is tiny and reasonably obviously bounded.

Well, I've pointed out where the easy to spot bounding is:

Quoted 3 lines
Quoted 3 lines

If you
care to use tools to find certain patterns in .init, simply discard everything following _einittext before giving the tool a go. IOW I

^^^

Perhaps easy if you're reading the disassembly, and are one half of the Xen x86 maintainership.

I know about _einittext, but don't consider this easy, and it certainly isn't reasonable to expect of anyone trying to use xen-syms.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

would much prefer for things to be left as they are.

No. We should not be deliberately corrupting the binary to work around a theoretical runtime issue for which there is a perfectly fine runtime solution. This is the real hack.

Putting all init-time code and data in a single section is a perfectly valid thing to do imo.

Having our debug symbols borderline unusable, isn't valid.

Certainly not to work around what you yourself identify as a theoretical issue in the first place.

Quoted 5 lines

We don't care about permissions at that
point in time, so the resulting RWX section is quite fine (and has a name much better suitable for a PE image). Otherwise the fact that we merge r/o and r/w init time data into a single section would need to be called a hack, too.

It is very much a hack.  I don't see what you're getting at.

Other option would be to #ifdef the section handing based on EFI, or attempt to merge the sections with objcopy before passing mkreloc?

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw
Quoted 8 lines
Quoted 3 lines
Quoted 1 line · On 26.02.18 at 20:01, <[email protected]> wrote:

Putting all init-time code and data in a single section is a perfectly valid thing to do imo.

Having our debug symbols borderline unusable, isn't valid.

Certainly not to work around what you yourself identify as a theoretical issue in the first place.

I wonder what theoretical issue you refer to here. I didn't say anything like that on this thread afaics, nor in the description of the patch that you want to partly undo. EFI implementations write-protecting r/o sections is not just a theoretical issue afaict.

Quoted 2 lines

Other option would be to #ifdef the section handing based on EFI, or attempt to merge the sections with objcopy before passing mkreloc?

Well, as much as I dislike such #ifdef-ary, I think I could (rather hesitantly) accept that.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel