Skip to contentSkip to conversation

x86: Relocate the compat vdso per process

PATCH In review Andy Lutomirski 2 participants 9 messages Mar 11, 2014 – Mar 12, 2014

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

v2 cover x86: Relocate the compat vdso per process

The meat of this patch series is in patch 1. Patch 2 is split out for improved bisectability.

Changes from v1: Split into two patches and fixed a comment.

Andy Lutomirski (2):
x86: Dynamically relocate the compat vdso
x86_32: Remove user bit from identity map PDE

 Documentation/kernel-parameters.txt  |  18 +++-
 arch/x86/Kconfig                     |  24 +++--
 arch/x86/include/asm/elf.h           |   4 -
 arch/x86/include/asm/fixmap.h        |   8 --
 arch/x86/include/asm/pgtable_types.h |   7 +-
 arch/x86/include/asm/vdso.h          |   5 +-
 arch/x86/vdso/vdso-layout.lds.S      |   2 +-
 arch/x86/vdso/vdso32-setup.c         | 173 ++++++++++++++++-------------------
 arch/x86/vdso/vdso32/vdso32.lds.S    |   2 -
 9 files changed, 111 insertions(+), 132 deletions(-)
Signature · 1 line
1.8.5.3
#1 lore raw

PATCH v2 1/2 x86: Dynamically relocate the compat vdso

This removes the compat vdso from the fixmap and instead relocates it each time it's mapped. It also documents the reason for the compat vDSO.

Before this patch, using the compat vDSO partially defeated ASLR. With this patch, ASLR is left alone, but starting a 32-bit process with the compat vDSO enabled takes slightly longer and consumes one additional page of memory.

IMO this makes the code cleaner. There are currently two separate code paths to set up the 32-bit vDSO depending on the compat setting. This removes the fixmap version.

The main observable effect should be that CONFIG_COMPAT_VDSO users get an extra line in their kernel log at startup. Perhaps a few of them will stop using CONFIG_COMPAT_VDSO as a result.

This does not actually add support for vDSO images >4k, but it will make it much easier to do so in the future.

---
 Documentation/kernel-parameters.txt |  18 ++--
 arch/x86/Kconfig                    |  24 +++--
 arch/x86/include/asm/elf.h          |   4 -
 arch/x86/include/asm/fixmap.h       |   8 --
 arch/x86/include/asm/vdso.h         |   5 +-
 arch/x86/vdso/vdso-layout.lds.S     |   2 +-
 arch/x86/vdso/vdso32-setup.c        | 173 ++++++++++++++++--------------------
 arch/x86/vdso/vdso32/vdso32.lds.S   |   2 -
 8 files changed, 110 insertions(+), 126 deletions(-)
Documentation/kernel-parameters.txt +13 -5
index 7116fda..3d33150 100644
@@ -3409,14 +3409,22 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 					of CONFIG_HIGHPTE.
 
 	vdso=		[X86,SH]
-			vdso=2: enable compat VDSO (default with COMPAT_VDSO)
+			On x86_32, this is an alias for vdso32=.  Otherwise:
+
 			vdso=1: enable VDSO (default)
 			vdso=0: disable VDSO mapping
 
-	vdso32=		[X86]
-			vdso32=2: enable compat VDSO (default with COMPAT_VDSO)
-			vdso32=1: enable 32-bit VDSO (default)
-			vdso32=0: disable 32-bit VDSO mapping
+	vdso32=		[X86] Control the 32-bit vDSO
+			vdso=2: enable VDSO with workaround
+			vdso=1: enable VDSO (default unless CONFIG_COMPAT_VDSO)
+			vdso=0: disable VDSO mapping
+
+			See the help text for CONFIG_COMPAT_VDSO for more
+			details.  If CONFIG_COMPAT_VDSO is set, the default
+			changes to 2.  vdso32=1 is the best choice unless you
+			experience crashes that say:
+
+			dl_main: Assertion `(void *) ph->p_vaddr == _rtld_local._dl_sysinfo_dso' failed!
 
 	vector=		[IA-64,SMP]
 			vector=percpu: enable percpu vector domain
arch/x86/Kconfig +18 -6
index 0af5250..e91d770 100644
@@ -1837,16 +1837,28 @@ config DEBUG_HOTPLUG_CPU0
 
 config COMPAT_VDSO
 	def_bool y
-	prompt "Compat VDSO support"
+	prompt "Compatibility with 32-bit glibc 2.3.3"
 	depends on X86_32 || IA32_EMULATION
 	---help---
-	  Map the 32-bit VDSO to the predictable old-style address too.
+	  Certain buggy versions of glibc will crash if they are
+          presented with a 32-bit vDSO that not mapped at the address
+          indicated in its segment table.
 
-	  Say N here if you are running a sufficiently recent glibc
-	  version (2.3.3 or later), to remove the high-mapped
-	  VDSO mapping and to exclusively use the randomized VDSO.
+	  The bug was introduced by f866314b89d56845f55e6f365e18b31ec978ec3a
+	  and fixed by 3b3ddb4f7db98ec9e912ccdf54d35df4aa30e04a and
+	  49ad572a70b8aeb91e57483a11dd1b77e31c4468.  Glibc 2.3.3 is
+	  the only released version with the bug, but OpenSUSE 9
+	  contains a buggy "glibc 2.3.2".
 
-	  If unsure, say Y.
+	  The symptom of the bug is that everything crashes on startup, saying:
+	  dl_main: Assertion `(void *) ph->p_vaddr == _rtld_local._dl_sysinfo_dso' failed!
+
+	  This option changes the default value of the vdso32 boot option
+	  from 1 to 2, which enables a workaround.  This workaround causes
+	  every 32-bit process to use an additional 4k of memory.
+
+	  If unsure, say N: if you are compiling your own kernel, you
+	  are unlikely to be using a buggy version of glibc.
 
 config CMDLINE_BOOL
 	bool "Built-in kernel command line"
arch/x86/include/asm/elf.h +0 -4
index 9c999c1..2c71182 100644
@@ -281,16 +281,12 @@ do {									\
 
 #define STACK_RND_MASK (0x7ff)
 
-#define VDSO_HIGH_BASE		(__fix_to_virt(FIX_VDSO))
-
 #define ARCH_DLINFO		ARCH_DLINFO_IA32(vdso_enabled)
 
 /* update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT entries changes */
 
 #else /* CONFIG_X86_32 */
 
-#define VDSO_HIGH_BASE		0xffffe000U /* CONFIG_COMPAT_VDSO address */
-
 /* 1GB for 64bit, 8MB for 32bit */
 #define STACK_RND_MASK (test_thread_flag(TIF_ADDR32) ? 0x7ff : 0x3fffff)
 
arch/x86/include/asm/fixmap.h +0 -8
index 7252cd3..2377f56 100644
@@ -40,15 +40,8 @@
  */
 extern unsigned long __FIXADDR_TOP;
 #define FIXADDR_TOP	((unsigned long)__FIXADDR_TOP)
-
-#define FIXADDR_USER_START     __fix_to_virt(FIX_VDSO)
-#define FIXADDR_USER_END       __fix_to_virt(FIX_VDSO - 1)
 #else
 #define FIXADDR_TOP	(VSYSCALL_END-PAGE_SIZE)
-
-/* Only covers 32bit vsyscalls currently. Need another set for 64bit. */
-#define FIXADDR_USER_START	((unsigned long)VSYSCALL32_VSYSCALL)
-#define FIXADDR_USER_END	(FIXADDR_USER_START + PAGE_SIZE)
 #endif
 
 
@@ -74,7 +67,6 @@ extern unsigned long __FIXADDR_TOP;
 enum fixed_addresses {
 #ifdef CONFIG_X86_32
 	FIX_HOLE,
-	FIX_VDSO,
 #else
 	VSYSCALL_LAST_PAGE,
 	VSYSCALL_FIRST_PAGE = VSYSCALL_LAST_PAGE
arch/x86/include/asm/vdso.h +1 -4
index fddb53d..5594e84 100644
@@ -2,8 +2,6 @@
 #define _ASM_X86_VDSO_H
 
 #if defined CONFIG_X86_32 || defined CONFIG_COMPAT
-extern const char VDSO32_PRELINK[];
-
 /*
  * Given a pointer to the vDSO image, find the pointer to VDSO32_name
  * as that symbol is defined in the vDSO sources or linker script.
@@ -11,8 +9,7 @@ extern const char VDSO32_PRELINK[];
 #define VDSO32_SYMBOL(base, name)					\
 ({									\
 	extern const char VDSO32_##name[];				\
-	(void __user *)(VDSO32_##name - VDSO32_PRELINK +		\
-			(unsigned long)(base));				\
+	(void __user *)(VDSO32_##name + (unsigned long)(base));		\
 })
 #endif
 
arch/x86/vdso/vdso-layout.lds.S +1 -1
index 634a2cf..8c550c1 100644
@@ -6,7 +6,7 @@
 
 SECTIONS
 {
-	. = VDSO_PRELINK + SIZEOF_HEADERS;
+	. = SIZEOF_HEADERS;
 
 	.hash		: { *(.hash) }			:text
 	.gnu.hash	: { *(.gnu.hash) }
arch/x86/vdso/vdso32-setup.c +77 -96

296 lines not on this page · raw message

arch/x86/vdso/vdso32/vdso32.lds.S +0 -2
index 976124b..90e7aa9 100644
@@ -8,7 +8,6 @@
  * values visible using the asm-x86/vdso.h macros from the kernel proper.
  */
 
-#define VDSO_PRELINK 0
 #include "../vdso-layout.lds.S"
 
 /* The ELF entry point can be used to set the AT_SYSINFO value.  */
@@ -31,7 +30,6 @@ VERSION
 /*
  * Symbols we define here called VDSO* get their values into vdso32-syms.h.
  */
-VDSO32_PRELINK		= VDSO_PRELINK;
 VDSO32_vsyscall		= __kernel_vsyscall;
 VDSO32_sigreturn	= __kernel_sigreturn;
 VDSO32_rt_sigreturn	= __kernel_rt_sigreturn;
Signature · 1 line
1.8.5.3
#2 lore raw

PATCH v2 2/2 x86_32: Remove user bit from identity map PDE

In reply to Andy Lutomirski

The only reason that the user bit was set was to support userspace access to the compat vDSO in the fixmap. The compat vDSO now lives in an ordinary vma, so the user bit can be removed.

---
 arch/x86/include/asm/pgtable_types.h | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
arch/x86/include/asm/pgtable_types.h +1 -6
index 1aa9ccd..757f9c4 100644
@@ -214,13 +214,8 @@
 #ifdef CONFIG_X86_64
 #define __PAGE_KERNEL_IDENT_LARGE_EXEC	__PAGE_KERNEL_LARGE_EXEC
 #else
-/*
- * For PDE_IDENT_ATTR include USER bit. As the PDE and PTE protection
- * bits are combined, this will alow user to access the high address mapped
- * VDSO in the presence of CONFIG_COMPAT_VDSO
- */
 #define PTE_IDENT_ATTR	 0x003		/* PRESENT+RW */
-#define PDE_IDENT_ATTR	 0x067		/* PRESENT+RW+USER+DIRTY+ACCESSED */
+#define PDE_IDENT_ATTR	 0x063		/* PRESENT+RW+DIRTY+ACCESSED */
 #define PGD_IDENT_ATTR	 0x001		/* PRESENT (no other attributes) */
 #endif
 
Signature · 1 line
1.8.5.3
#3 lore raw
In reply to Andy Lutomirski
Quoted 9 lines · Andy Lutomirski

The meat of this patch series is in patch 1. Patch 2 is split out for improved bisectability.

Changes from v1: Split into two patches and fixed a comment.

Andy Lutomirski (2):
x86: Dynamically relocate the compat vdso
x86_32: Remove user bit from identity map PDE

Quoted 11 lines
  Documentation/kernel-parameters.txt  |  18 +++-
  arch/x86/Kconfig                     |  24 +++--
  arch/x86/include/asm/elf.h           |   4 -
  arch/x86/include/asm/fixmap.h        |   8 --
  arch/x86/include/asm/pgtable_types.h |   7 +-
  arch/x86/include/asm/vdso.h          |   5 +-
  arch/x86/vdso/vdso-layout.lds.S      |   2 +-
  arch/x86/vdso/vdso32-setup.c         | 173 ++++++++++++++++-------------------
  arch/x86/vdso/vdso32/vdso32.lds.S    |   2 -
  9 files changed, 111 insertions(+), 132 deletions(-)

Why per process? *If* we have compat vdso turned on, can't we just put it in one place system-wide?

-hpa

#4 lore raw
Quoted 24 lines · H. Peter Anvin
Quoted 10 lines · Andy Lutomirski

The meat of this patch series is in patch 1. Patch 2 is split out for improved bisectability.

Changes from v1: Split into two patches and fixed a comment.

Andy Lutomirski (2):
x86: Dynamically relocate the compat vdso
x86_32: Remove user bit from identity map PDE

Quoted 11 lines
  Documentation/kernel-parameters.txt  |  18 +++-
  arch/x86/Kconfig                     |  24 +++--
  arch/x86/include/asm/elf.h           |   4 -
  arch/x86/include/asm/fixmap.h        |   8 --
  arch/x86/include/asm/pgtable_types.h |   7 +-
  arch/x86/include/asm/vdso.h          |   5 +-
  arch/x86/vdso/vdso-layout.lds.S      |   2 +-
  arch/x86/vdso/vdso32-setup.c         | 173 ++++++++++++++++-------------------
  arch/x86/vdso/vdso32/vdso32.lds.S    |   2 -
  9 files changed, 111 insertions(+), 132 deletions(-)
Quoted 1 line

Why per process? *If* we have compat vdso turned on, can't we just put it in one place system-wide?

My machine has ASLR turned on, and the vma really does end up somewhere different each time. I suspect that most (?) ASLR users would happily accept the 4k hit to keep ASLR working fully.

Another downside of trying to have a common address for all processes:
someone needs to figure out what that address is. What happens if an ELF binary (or interpreter) wants to load something at the chosen address?

--Andy

#5 lore raw

There is one sensible address: end of address space perhaps minus some small offset. Unlikely to be used by anything specific.

Quoted 40 lines · Andy Lutomirski
Quoted 4 lines · H. Peter Anvin
Quoted 2 lines · Andy Lutomirski

The meat of this patch series is in patch 1. Patch 2 is split out

for

Quoted 8 lines
Quoted 8 lines

improved bisectability.

Changes from v1: Split into two patches and fixed a comment.

Andy Lutomirski (2):
x86: Dynamically relocate the compat vdso
x86_32: Remove user bit from identity map PDE

Quoted 8 lines
Quoted 8 lines
  Documentation/kernel-parameters.txt  |  18 +++-
  arch/x86/Kconfig                     |  24 +++--
  arch/x86/include/asm/elf.h           |   4 -
  arch/x86/include/asm/fixmap.h        |   8 --
  arch/x86/include/asm/pgtable_types.h |   7 +-
  arch/x86/include/asm/vdso.h          |   5 +-
  arch/x86/vdso/vdso-layout.lds.S      |   2 +-
  arch/x86/vdso/vdso32-setup.c         | 173

++++++++++++++++-------------------

Quoted 5 lines
Quoted 3 lines
  arch/x86/vdso/vdso32/vdso32.lds.S    |   2 -
  9 files changed, 111 insertions(+), 132 deletions(-)

Why per process? *If* we have compat vdso turned on, can't we just

put it in one place system-wide?

My machine has ASLR turned on, and the vma really does end up somewhere different each time. I suspect that most (?) ASLR users would happily accept the 4k hit to keep ASLR working fully.

Another downside of trying to have a common address for all processes:
someone needs to figure out what that address is. What happens if an ELF binary (or interpreter) wants to load something at the chosen address?

--Andy

Signature · 1 line
Sent from my mobile phone.  Please pardon brevity and lack of formatting.
#6 lore raw
Quoted 1 line · H. Peter Anvin

There is one sensible address: end of address space perhaps minus some small offset. Unlikely to be used by anything specific.

Meh. The compat vdso logic will end up being either:

1) Try to map at the preferred address. If it fails, don't map it. Force ASLR off for the vdso

2) Try to map at the preferred address. If it fails, map somewhere else and relocate.

3) Try to map at the preferred address unless ASLR is on. If it fails or ASLR is on, map somewhere else and relocate.

since the whole point is to simplify this stuff, I don't really like any of these choices. That's a lot of code to save 4k per process, and when it breaks (which I suspect it will the next time anyone touches the address space layout) so one will notice that 4k gets wasted again.

Quoted 45 lines · Andy Lutomirski
Quoted 34 lines · H. Peter Anvin
Quoted 4 lines · Andy Lutomirski
Quoted 2 lines

The meat of this patch series is in patch 1. Patch 2 is split out

for

Quoted 8 lines
Quoted 8 lines

improved bisectability.

Changes from v1: Split into two patches and fixed a comment.

Andy Lutomirski (2):
x86: Dynamically relocate the compat vdso
x86_32: Remove user bit from identity map PDE

Quoted 8 lines
Quoted 8 lines
  Documentation/kernel-parameters.txt  |  18 +++-
  arch/x86/Kconfig                     |  24 +++--
  arch/x86/include/asm/elf.h           |   4 -
  arch/x86/include/asm/fixmap.h        |   8 --
  arch/x86/include/asm/pgtable_types.h |   7 +-
  arch/x86/include/asm/vdso.h          |   5 +-
  arch/x86/vdso/vdso-layout.lds.S      |   2 +-
  arch/x86/vdso/vdso32-setup.c         | 173

++++++++++++++++-------------------

Quoted 5 lines
Quoted 3 lines
  arch/x86/vdso/vdso32/vdso32.lds.S    |   2 -
  9 files changed, 111 insertions(+), 132 deletions(-)

Why per process? *If* we have compat vdso turned on, can't we just

put it in one place system-wide?

My machine has ASLR turned on, and the vma really does end up somewhere different each time. I suspect that most (?) ASLR users would happily accept the 4k hit to keep ASLR working fully.

Quoted 6 lines

Another downside of trying to have a common address for all processes:
someone needs to figure out what that address is. What happens if an ELF binary (or interpreter) wants to load something at the chosen address?

--Andy

--
Sent from my mobile phone. Please pardon brevity and lack of formatting.

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC
#7 lore raw

In taking about 1 here.

Quoted 69 lines · Andy Lutomirski
Quoted 1 line · H. Peter Anvin

There is one sensible address: end of address space perhaps minus

some small offset. Unlikely to be used by anything specific.

Meh. The compat vdso logic will end up being either:

1) Try to map at the preferred address. If it fails, don't map it. Force ASLR off for the vdso

2) Try to map at the preferred address. If it fails, map somewhere else and relocate.

3) Try to map at the preferred address unless ASLR is on. If it fails or ASLR is on, map somewhere else and relocate.

since the whole point is to simplify this stuff, I don't really like any of these choices. That's a lot of code to save 4k per process, and when it breaks (which I suspect it will the next time anyone touches the address space layout) so one will notice that 4k gets wasted again.

Quoted 2 lines

On March 11, 2014 11:09:11 PM PDT, Andy Lutomirski

Quoted 27 lines · <[email protected]> wrote:
Quoted 27 lines · H. Peter Anvin
Quoted 4 lines · Andy Lutomirski
Quoted 2 lines

The meat of this patch series is in patch 1. Patch 2 is split out

for

Quoted 8 lines
Quoted 8 lines

improved bisectability.

Changes from v1: Split into two patches and fixed a comment.

Andy Lutomirski (2):
x86: Dynamically relocate the compat vdso
x86_32: Remove user bit from identity map PDE

Quoted 8 lines
Quoted 8 lines
  Documentation/kernel-parameters.txt  |  18 +++-
  arch/x86/Kconfig                     |  24 +++--
  arch/x86/include/asm/elf.h           |   4 -
  arch/x86/include/asm/fixmap.h        |   8 --
  arch/x86/include/asm/pgtable_types.h |   7 +-
  arch/x86/include/asm/vdso.h          |   5 +-
  arch/x86/vdso/vdso-layout.lds.S      |   2 +-
  arch/x86/vdso/vdso32-setup.c         | 173

++++++++++++++++-------------------

Quoted 4 lines
Quoted 3 lines
  arch/x86/vdso/vdso32/vdso32.lds.S    |   2 -
  9 files changed, 111 insertions(+), 132 deletions(-)
Quoted 8 lines
Quoted 8 lines
Quoted 1 line

Why per process? *If* we have compat vdso turned on, can't we just

put it in one place system-wide?

My machine has ASLR turned on, and the vma really does end up somewhere different each time. I suspect that most (?) ASLR users would happily accept the 4k hit to keep ASLR working fully.

Another downside of trying to have a common address for all

processes:

Quoted 8 lines
Quoted 5 lines

someone needs to figure out what that address is. What happens if an ELF binary (or interpreter) wants to load something at the chosen address?

--Andy

--
Sent from my mobile phone. Please pardon brevity and lack of

formatting.

Signature · 1 line
Sent from my mobile phone.  Please pardon brevity and lack of formatting.
#8 lore raw
Quoted 1 line · H. Peter Anvin

In taking about 1 here.

I'm still unconvinced that this is worth the effort. I'm okay with killing the compat vdso entirely, and I'm okay with adding a minimal, fully functional fix (like this patch set), but I don't really want to add funny interactions with ASLR to support such a narrow use case.

Quoted 74 lines · Andy Lutomirski
Quoted 64 lines · H. Peter Anvin
Quoted 1 line

There is one sensible address: end of address space perhaps minus

some small offset. Unlikely to be used by anything specific.

Meh. The compat vdso logic will end up being either:

1) Try to map at the preferred address. If it fails, don't map it. Force ASLR off for the vdso

2) Try to map at the preferred address. If it fails, map somewhere else and relocate.

3) Try to map at the preferred address unless ASLR is on. If it fails or ASLR is on, map somewhere else and relocate.

since the whole point is to simplify this stuff, I don't really like any of these choices. That's a lot of code to save 4k per process, and when it breaks (which I suspect it will the next time anyone touches the address space layout) so one will notice that 4k gets wasted again.

Quoted 2 lines

On March 11, 2014 11:09:11 PM PDT, Andy Lutomirski

Quoted 27 lines · <[email protected]> wrote:
Quoted 27 lines · H. Peter Anvin
Quoted 4 lines · Andy Lutomirski
Quoted 2 lines

The meat of this patch series is in patch 1. Patch 2 is split out

for

Quoted 8 lines
Quoted 8 lines

improved bisectability.

Changes from v1: Split into two patches and fixed a comment.

Andy Lutomirski (2):
x86: Dynamically relocate the compat vdso
x86_32: Remove user bit from identity map PDE

Quoted 8 lines
Quoted 8 lines
  Documentation/kernel-parameters.txt  |  18 +++-
  arch/x86/Kconfig                     |  24 +++--
  arch/x86/include/asm/elf.h           |   4 -
  arch/x86/include/asm/fixmap.h        |   8 --
  arch/x86/include/asm/pgtable_types.h |   7 +-
  arch/x86/include/asm/vdso.h          |   5 +-
  arch/x86/vdso/vdso-layout.lds.S      |   2 +-
  arch/x86/vdso/vdso32-setup.c         | 173

++++++++++++++++-------------------

Quoted 4 lines
Quoted 3 lines
  arch/x86/vdso/vdso32/vdso32.lds.S    |   2 -
  9 files changed, 111 insertions(+), 132 deletions(-)
Quoted 8 lines
Quoted 8 lines
Quoted 1 line

Why per process? *If* we have compat vdso turned on, can't we just

put it in one place system-wide?

My machine has ASLR turned on, and the vma really does end up somewhere different each time. I suspect that most (?) ASLR users would happily accept the 4k hit to keep ASLR working fully.

Another downside of trying to have a common address for all

processes:

Quoted 4 lines
Quoted 4 lines

someone needs to figure out what that address is. What happens if an ELF binary (or interpreter) wants to load something at the chosen address?

Quoted 5 lines
Quoted 4 lines
Quoted 1 line

--Andy

--
Sent from my mobile phone. Please pardon brevity and lack of

formatting.

--
Sent from my mobile phone. Please pardon brevity and lack of formatting.

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC