Skip to contentSkip to conversation

x86, vmlinux.lds: Page-Align end of ..page_aligned sections

PATCH Applied Joerg Roedel 4 participants 5 messages Jul 21, 2020 – Jul 22, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86, vmlinux.lds: Page-Align end of ..page_aligned sections

From: Joerg Roedel <[email protected]>

Align the end of the .bss..page_aligned and .data..page_aligned section on page-size too. Otherwise the linker might place other objects on the page of the last ..page_aligned object. This is inconsistent with other objects in those sections, which all have their own page.

---
 arch/x86/kernel/vmlinux.lds.S     | 1 +
 include/asm-generic/vmlinux.lds.h | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)
arch/x86/kernel/vmlinux.lds.S +1 -0
index 3bfc8dd8a43d..9a03e5b23135 100644
@@ -358,6 +358,7 @@ SECTIONS
 	.bss : AT(ADDR(.bss) - LOAD_OFFSET) {
 		__bss_start = .;
 		*(.bss..page_aligned)
+		. = ALIGN(PAGE_SIZE);
 		*(BSS_MAIN)
 		BSS_DECRYPTED
 		. = ALIGN(PAGE_SIZE);
include/asm-generic/vmlinux.lds.h +3 -1
index 3ceb4b7279ec..bd6302bd1d0f 100644
@@ -360,7 +360,8 @@
 
 #define PAGE_ALIGNED_DATA(page_align)					\
 	. = ALIGN(page_align);						\
-	*(.data..page_aligned)
+	*(.data..page_aligned)						\
+	. = ALIGN(page_align);
 
 #define READ_MOSTLY_DATA(align)						\
 	. = ALIGN(align);						\
@@ -758,6 +759,7 @@
 	.bss : AT(ADDR(.bss) - LOAD_OFFSET) {				\
 		BSS_FIRST_SECTIONS					\
 		*(.bss..page_aligned)					\
+		. = ALIGN(PAGE_SIZE);					\
 		*(.dynbss)						\
 		*(BSS_MAIN)						\
 		*(COMMON)						\
Signature · 1 line
2.27.0
#1 lore raw
Quoted 6 lines · Joerg Roedel

From: Joerg Roedel <[email protected]>

Align the end of the .bss..page_aligned and .data..page_aligned section on page-size too. Otherwise the linker might place other objects on the page of the last ..page_aligned object. This is inconsistent with other objects in those sections, which all have their own page.

What problem was actually encountered? (i.e. why is it a problem for the other data to be in the page of the page-aligned data? shouldn't those data have their own, separate, alignment hint?)

Signature · 1 line
Kees Cook
#2 lore raw

Kees,

Quoted 11 lines · Kees Cook
Quoted 6 lines · Joerg Roedel

From: Joerg Roedel <[email protected]>

Align the end of the .bss..page_aligned and .data..page_aligned section on page-size too. Otherwise the linker might place other objects on the page of the last ..page_aligned object. This is inconsistent with other objects in those sections, which all have their own page.

What problem was actually encountered? (i.e. why is it a problem for the other data to be in the page of the page-aligned data? shouldn't those data have their own, separate, alignment hint?)

See: lore.kernel.org/r/[email protected]

Thanks,

tglx

Reviewed-by
#3 lore raw
Quoted 11 lines · Thomas Gleixner

Kees,

Quoted 8 lines · Kees Cook
Quoted 6 lines · Joerg Roedel

From: Joerg Roedel <[email protected]>

Align the end of the .bss..page_aligned and .data..page_aligned section on page-size too. Otherwise the linker might place other objects on the page of the last ..page_aligned object. This is inconsistent with other objects in those sections, which all have their own page.

Quoted 5 lines
Quoted 3 lines

What problem was actually encountered? (i.e. why is it a problem for the other data to be in the page of the page-aligned data? shouldn't those data have their own, separate, alignment hint?)

See: lore.kernel.org/r/[email protected]

Ah-ha: "But with explicit sections which store only page aligned objects there is an implicit guarantee that the object is alone in the page in which it is placed. That works for all objects except the last one. That's inconsistent."

Understood now. The bit in this commit log for "which all have their own page" wasn't clear to me about _why_ (it was coming from .page_aligned, which means both aligned and dedicated).

Signature · 1 line
Kees Cook
merge bot Applied
#4 lore raw

[tip: x86/urgent] x86, vmlinux.lds: Page-align end of ..page_aligned sections

In reply to Joerg Roedel
The following commit has been merged into the x86/urgent branch of tip:

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     de2b41be8fcccb2f5b6c480d35df590476344201
Gitweb:        https://git.kernel.org/tip/de2b41be8fcccb2f5b6c480d35df590476344201
Author:        Joerg Roedel <[email protected]>
AuthorDate:    Tue, 21 Jul 2020 11:34:48 +02:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Wed, 22 Jul 2020 09:38:37 +02:00

x86, vmlinux.lds: Page-align end of ..page_aligned sections

On x86-32 the idt_table with 256 entries needs only 2048 bytes. It is page-aligned, but the end of the .bss..page_aligned section is not guaranteed to be page-aligned.

As a result, objects from other .bss sections may end up on the same 4k page as the idt_table, and will accidentially get mapped read-only during boot, causing unexpected page-faults when the kernel writes to them.

This could be worked around by making the objects in the page aligned sections page sized, but that's wrong.

Explicit sections which store only page aligned objects have an implicit guarantee that the object is alone in the page in which it is placed. That works for all objects except the last one. That's inconsistent.

Enforcing page sized objects for these sections would wreckage memory sanitizers, because the object becomes artificially larger than it should be and out of bound access becomes legit.

Align the end of the .bss..page_aligned and .data..page_aligned section on page-size so all objects places in these sections are guaranteed to have their own page.

[ tglx: Amended changelog ]

---
 arch/x86/kernel/vmlinux.lds.S     | 1 +
 include/asm-generic/vmlinux.lds.h | 5 ++++-
 2 files changed, 5 insertions(+), 1 deletion(-)
arch/x86/kernel/vmlinux.lds.S +1 -0
index 3bfc8dd..9a03e5b 100644
@@ -358,6 +358,7 @@ SECTIONS
 	.bss : AT(ADDR(.bss) - LOAD_OFFSET) {
 		__bss_start = .;
 		*(.bss..page_aligned)
+		. = ALIGN(PAGE_SIZE);
 		*(BSS_MAIN)
 		BSS_DECRYPTED
 		. = ALIGN(PAGE_SIZE);
include/asm-generic/vmlinux.lds.h +4 -1
index db600ef..052e0f0 100644
@@ -341,7 +341,8 @@
 
 #define PAGE_ALIGNED_DATA(page_align)					\
 	. = ALIGN(page_align);						\
-	*(.data..page_aligned)
+	*(.data..page_aligned)						\
+	. = ALIGN(page_align);
 
 #define READ_MOSTLY_DATA(align)						\
 	. = ALIGN(align);						\
@@ -737,7 +738,9 @@
 	. = ALIGN(bss_align);						\
 	.bss : AT(ADDR(.bss) - LOAD_OFFSET) {				\
 		BSS_FIRST_SECTIONS					\
+		. = ALIGN(PAGE_SIZE);					\
 		*(.bss..page_aligned)					\
+		. = ALIGN(PAGE_SIZE);					\
 		*(.dynbss)						\
 		*(BSS_MAIN)						\
 		*(COMMON)						\