linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] x86, vmlinux.lds: Page-Align end of ..page_aligned sections
@ 2020-07-21  9:34 Joerg Roedel
  2020-07-21 18:44 ` Kees Cook
  2020-07-22  7:43 ` [tip: x86/urgent] x86, vmlinux.lds: Page-align " tip-bot2 for Joerg Roedel
  0 siblings, 2 replies; 5+ messages in thread
From: Joerg Roedel @ 2020-07-21  9:34 UTC (permalink / raw)
  To: x86
  Cc: hpa, Arnd Bergmann, Kees Cook, Heiko Carstens, Joerg Roedel,
	Bob Haarman, hjl.tools, Arvind Sankar, Dmitry Safonov,
	linux-kernel, linux-arch

From: Joerg Roedel <jroedel@suse.de>

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.

Signed-off-by: Joerg Roedel <jroedel@suse.de>
---
 arch/x86/kernel/vmlinux.lds.S     | 1 +
 include/asm-generic/vmlinux.lds.h | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S
index 3bfc8dd8a43d..9a03e5b23135 100644
--- a/arch/x86/kernel/vmlinux.lds.S
+++ b/arch/x86/kernel/vmlinux.lds.S
@@ -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);
diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index 3ceb4b7279ec..bd6302bd1d0f 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -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)						\
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] x86, vmlinux.lds: Page-Align end of ..page_aligned sections
  2020-07-21  9:34 [PATCH] x86, vmlinux.lds: Page-Align end of ..page_aligned sections Joerg Roedel
@ 2020-07-21 18:44 ` Kees Cook
  2020-07-21 20:05   ` Thomas Gleixner
  2020-07-22  7:43 ` [tip: x86/urgent] x86, vmlinux.lds: Page-align " tip-bot2 for Joerg Roedel
  1 sibling, 1 reply; 5+ messages in thread
From: Kees Cook @ 2020-07-21 18:44 UTC (permalink / raw)
  To: Joerg Roedel
  Cc: x86, hpa, Arnd Bergmann, Heiko Carstens, Joerg Roedel,
	Bob Haarman, hjl.tools, Arvind Sankar, Dmitry Safonov,
	linux-kernel, linux-arch

On Tue, Jul 21, 2020 at 11:34:48AM +0200, Joerg Roedel wrote:
> From: Joerg Roedel <jroedel@suse.de>
> 
> 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?)

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] x86, vmlinux.lds: Page-Align end of ..page_aligned sections
  2020-07-21 18:44 ` Kees Cook
@ 2020-07-21 20:05   ` Thomas Gleixner
  2020-07-21 21:18     ` Kees Cook
  0 siblings, 1 reply; 5+ messages in thread
From: Thomas Gleixner @ 2020-07-21 20:05 UTC (permalink / raw)
  To: Kees Cook, Joerg Roedel
  Cc: x86, hpa, Arnd Bergmann, Heiko Carstens, Joerg Roedel,
	Bob Haarman, hjl.tools, Arvind Sankar, Dmitry Safonov,
	linux-kernel, linux-arch

Kees,

Kees Cook <keescook@chromium.org> writes:
> On Tue, Jul 21, 2020 at 11:34:48AM +0200, Joerg Roedel wrote:
>> From: Joerg Roedel <jroedel@suse.de>
>> 
>> 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/87sgdmm8u4.fsf@nanos.tec.linutronix.de

Thanks,

        tglx

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] x86, vmlinux.lds: Page-Align end of ..page_aligned sections
  2020-07-21 20:05   ` Thomas Gleixner
@ 2020-07-21 21:18     ` Kees Cook
  0 siblings, 0 replies; 5+ messages in thread
From: Kees Cook @ 2020-07-21 21:18 UTC (permalink / raw)
  To: Thomas Gleixner
  Cc: Joerg Roedel, x86, hpa, Arnd Bergmann, Heiko Carstens,
	Joerg Roedel, Bob Haarman, hjl.tools, Arvind Sankar,
	Dmitry Safonov, linux-kernel, linux-arch

On Tue, Jul 21, 2020 at 10:05:05PM +0200, Thomas Gleixner wrote:
> Kees,
> 
> Kees Cook <keescook@chromium.org> writes:
> > On Tue, Jul 21, 2020 at 11:34:48AM +0200, Joerg Roedel wrote:
> >> From: Joerg Roedel <jroedel@suse.de>
> >> 
> >> 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/87sgdmm8u4.fsf@nanos.tec.linutronix.de

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).

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [tip: x86/urgent] x86, vmlinux.lds: Page-align end of ..page_aligned sections
  2020-07-21  9:34 [PATCH] x86, vmlinux.lds: Page-Align end of ..page_aligned sections Joerg Roedel
  2020-07-21 18:44 ` Kees Cook
@ 2020-07-22  7:43 ` tip-bot2 for Joerg Roedel
  1 sibling, 0 replies; 5+ messages in thread
From: tip-bot2 for Joerg Roedel @ 2020-07-22  7:43 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Joerg Roedel, Thomas Gleixner, Kees Cook, stable, x86, LKML

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 <jroedel@suse.de>
AuthorDate:    Tue, 21 Jul 2020 11:34:48 +02:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
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 ]

Signed-off-by: Joerg Roedel <jroedel@suse.de>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Kees Cook <keescook@chromium.org>
Cc: stable@vger.kernel.org
Link: https://lkml.kernel.org/r/20200721093448.10417-1-joro@8bytes.org
---
 arch/x86/kernel/vmlinux.lds.S     | 1 +
 include/asm-generic/vmlinux.lds.h | 5 ++++-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S
index 3bfc8dd..9a03e5b 100644
--- a/arch/x86/kernel/vmlinux.lds.S
+++ b/arch/x86/kernel/vmlinux.lds.S
@@ -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);
diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index db600ef..052e0f0 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -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)						\

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-07-22  7:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-21  9:34 [PATCH] x86, vmlinux.lds: Page-Align end of ..page_aligned sections Joerg Roedel
2020-07-21 18:44 ` Kees Cook
2020-07-21 20:05   ` Thomas Gleixner
2020-07-21 21:18     ` Kees Cook
2020-07-22  7:43 ` [tip: x86/urgent] x86, vmlinux.lds: Page-align " tip-bot2 for Joerg Roedel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).