Skip to contentSkip to conversation

module: Fix link failure due to invalid relocation on namespace offset

PATCH Applied Will Deacon 4 participants 6 messages Sep 11, 2019 Generic include/asm header files

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 module: Fix link failure due to invalid relocation on namespace offset

Commit 8651ec01daed ("module: add support for symbol namespaces.") broke linking for arm64 defconfig:

  | lib/crypto/arc4.o: In function `__ksymtab_arc4_setkey':
  | arc4.c:(___ksymtab+arc4_setkey+0x8): undefined reference to `no symbol'
  | lib/crypto/arc4.o: In function `__ksymtab_arc4_crypt':
  | arc4.c:(___ksymtab+arc4_crypt+0x8): undefined reference to `no symbol'

This is because the dummy initialisation of the 'namespace_offset' field in 'struct kernel_symbol' when using EXPORT_SYMBOL on architectures with support for PREL32 locations uses an offset from an absolute address (0) in an effort to trick 'offset_to_pointer' into behaving as a NOP, allowing non-namespaced symbols to be treated in the same way as those belonging to a namespace.

Unfortunately, place-relative relocations require a symbol reference rather than an absolute value and, although x86 appears to get away with this due to placing the kernel text at the top of the address space, it almost certainly results in a runtime failure if the kernel is relocated dynamically as a result of KASLR.

Rework 'namespace_offset' so that a value of 0, which cannot occur for a valid namespaced symbol, indicates that the corresponding symbol does not belong to a namespace.

Cc Matthias Maennich <[email protected]>
Cc Jessica Yu <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Catalin Marinas <[email protected]>
Fixes 8651ec01daed ("module: add support for symbol namespaces.")

---

Please note that I've not been able to test this at LPC, but it's been submitted to kernelci.

 include/asm-generic/export.h | 2 +-
 include/linux/export.h       | 2 +-
 kernel/module.c              | 2 ++
 3 files changed, 4 insertions(+), 2 deletions(-)
include/asm-generic/export.h +1 -1
index e2b5d0f569d3..d0912c7ac2fc 100644
@@ -17,7 +17,7 @@
 
 .macro __put, val, name
 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
-	.long	\val - ., \name - ., 0 - .
+	.long	\val - ., \name - ., 0
 #elif defined(CONFIG_64BIT)
 	.quad	\val, \name, 0
 #else
include/linux/export.h +1 -1
index 2c5468d8ea9a..ef5d015d754a 100644
@@ -68,7 +68,7 @@ extern struct module __this_module;
 	    "__ksymtab_" #sym ":				\n"	\
 	    "	.long	" #sym "- .				\n"	\
 	    "	.long	__kstrtab_" #sym "- .			\n"	\
-	    "	.long	0 - .					\n"	\
+	    "	.long	0					\n"	\
 	    "	.previous					\n")
 
 struct kernel_symbol {
kernel/module.c +2 -0
index f76efcf2043e..7ab244c4e1ba 100644
@@ -547,6 +547,8 @@ static const char *kernel_symbol_name(const struct kernel_symbol *sym)
 static const char *kernel_symbol_namespace(const struct kernel_symbol *sym)
 {
 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+	if (!sym->namespace_offset)
+		return NULL;
 	return offset_to_ptr(&sym->namespace_offset);
 #else
 	return sym->namespace;
Signature · 1 line
2.23.0.162.g0b9fbb3734-goog
#1 lore raw

+++ Will Deacon [11/09/19 13:26 +0100]:

Quoted 32 lines

Commit 8651ec01daed ("module: add support for symbol namespaces.") broke linking for arm64 defconfig:

 | lib/crypto/arc4.o: In function `__ksymtab_arc4_setkey':
 | arc4.c:(___ksymtab+arc4_setkey+0x8): undefined reference to `no symbol'
 | lib/crypto/arc4.o: In function `__ksymtab_arc4_crypt':
 | arc4.c:(___ksymtab+arc4_crypt+0x8): undefined reference to `no symbol'

This is because the dummy initialisation of the 'namespace_offset' field in 'struct kernel_symbol' when using EXPORT_SYMBOL on architectures with support for PREL32 locations uses an offset from an absolute address (0) in an effort to trick 'offset_to_pointer' into behaving as a NOP, allowing non-namespaced symbols to be treated in the same way as those belonging to a namespace.

Unfortunately, place-relative relocations require a symbol reference rather than an absolute value and, although x86 appears to get away with this due to placing the kernel text at the top of the address space, it almost certainly results in a runtime failure if the kernel is relocated dynamically as a result of KASLR.

Rework 'namespace_offset' so that a value of 0, which cannot occur for a valid namespaced symbol, indicates that the corresponding symbol does not belong to a namespace.

Cc Matthias Maennich <[email protected]>
Cc Jessica Yu <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Catalin Marinas <[email protected]>
Fixes 8651ec01daed ("module: add support for symbol namespaces.")
Quoted 4 lines

---

Please note that I've not been able to test this at LPC, but it's been submitted to kernelci.

Thanks for fixing this so quickly. I can confirm that this fixes the build for arm64 defconfig and x86 built fine for me as well. I'll wait a bit and apply this at the end of the day in case Matthias or anybody else would like to confirm/test.

Thanks,

Jessica

Quoted 47 lines
include/asm-generic/export.h | 2 +-
include/linux/export.h       | 2 +-
kernel/module.c              | 2 ++
3 files changed, 4 insertions(+), 2 deletions(-)
include/asm-generic/export.h +0 -0
index e2b5d0f569d3..d0912c7ac2fc 100644
@@ -17,7 +17,7 @@
 

.macro __put, val, name
#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS

include/asm-generic/export.h +1 -1
-	.long	\val - ., \name - ., 0 - .
+	.long	\val - ., \name - ., 0

#elif defined(CONFIG_64BIT)

	.quad	\val, \name, 0

#else

include/linux/export.h +1 -1
index 2c5468d8ea9a..ef5d015d754a 100644
@@ -68,7 +68,7 @@ extern struct module __this_module;
 	    "__ksymtab_" #sym ":				\n"	\
 	    "	.long	" #sym "- .				\n"	\
 	    "	.long	__kstrtab_" #sym "- .			\n"	\
-	    "	.long	0 - .					\n"	\
+	    "	.long	0					\n"	\
 	    "	.previous					\n")
 
 struct kernel_symbol {
kernel/module.c +2 -0
index f76efcf2043e..7ab244c4e1ba 100644
@@ -547,6 +547,8 @@ static const char *kernel_symbol_name(const struct kernel_symbol *sym)
 static const char *kernel_symbol_namespace(const struct kernel_symbol *sym)
 {
 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+	if (!sym->namespace_offset)
+		return NULL;
 	return offset_to_ptr(&sym->namespace_offset);
 #else
 	return sym->namespace;
Signature · 1 line
2.23.0.162.g0b9fbb3734-goog
#2 lore raw
Quoted 35 lines · Jessica Yu

+++ Will Deacon [11/09/19 13:26 +0100]:

Quoted 34 lines

Commit 8651ec01daed ("module: add support for symbol namespaces.") broke linking for arm64 defconfig:

 | lib/crypto/arc4.o: In function `__ksymtab_arc4_setkey':
 | arc4.c:(___ksymtab+arc4_setkey+0x8): undefined reference to `no symbol'
 | lib/crypto/arc4.o: In function `__ksymtab_arc4_crypt':
 | arc4.c:(___ksymtab+arc4_crypt+0x8): undefined reference to `no symbol'

This is because the dummy initialisation of the 'namespace_offset' field in 'struct kernel_symbol' when using EXPORT_SYMBOL on architectures with support for PREL32 locations uses an offset from an absolute address (0) in an effort to trick 'offset_to_pointer' into behaving as a NOP, allowing non-namespaced symbols to be treated in the same way as those belonging to a namespace.

Unfortunately, place-relative relocations require a symbol reference rather than an absolute value and, although x86 appears to get away with this due to placing the kernel text at the top of the address space, it almost certainly results in a runtime failure if the kernel is relocated dynamically as a result of KASLR.

Rework 'namespace_offset' so that a value of 0, which cannot occur for a valid namespaced symbol, indicates that the corresponding symbol does not belong to a namespace.

Cc Matthias Maennich <[email protected]>
Cc Jessica Yu <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Catalin Marinas <[email protected]>
Fixes 8651ec01daed ("module: add support for symbol namespaces.")

---

Quoted 7 lines
Quoted 2 lines

Please note that I've not been able to test this at LPC, but it's been submitted to kernelci.

Thanks for fixing this so quickly. I can confirm that this fixes the build for arm64 defconfig and x86 built fine for me as well. I'll wait a bit and apply this at the end of the day in case Matthias or anybody else would like to confirm/test.

FWIW, I've managed to boot arm64 Debian under QEMU and load/unload modules successfully with this patch applied on top of modules-next.

Will

Acked-by Tested-by on v1 1/1
#4 lore raw
In reply to Will Deacon
Quoted 33 lines · Will Deacon

Commit 8651ec01daed ("module: add support for symbol namespaces.") broke linking for arm64 defconfig:

  | lib/crypto/arc4.o: In function `__ksymtab_arc4_setkey':
  | arc4.c:(___ksymtab+arc4_setkey+0x8): undefined reference to `no symbol'
  | lib/crypto/arc4.o: In function `__ksymtab_arc4_crypt':
  | arc4.c:(___ksymtab+arc4_crypt+0x8): undefined reference to `no symbol'

This is because the dummy initialisation of the 'namespace_offset' field in 'struct kernel_symbol' when using EXPORT_SYMBOL on architectures with support for PREL32 locations uses an offset from an absolute address (0) in an effort to trick 'offset_to_pointer' into behaving as a NOP, allowing non-namespaced symbols to be treated in the same way as those belonging to a namespace.

Unfortunately, place-relative relocations require a symbol reference rather than an absolute value and, although x86 appears to get away with this due to placing the kernel text at the top of the address space, it almost certainly results in a runtime failure if the kernel is relocated dynamically as a result of KASLR.

Rework 'namespace_offset' so that a value of 0, which cannot occur for a valid namespaced symbol, indicates that the corresponding symbol does not belong to a namespace.

Cc Matthias Maennich <[email protected]>
Cc Jessica Yu <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Catalin Marinas <[email protected]>
Fixes 8651ec01daed ("module: add support for symbol namespaces.")
Quoted 5 lines

---

Please note that I've not been able to test this at LPC, but it's been submitted to kernelci.

The build tests all passed, and the boot tests that have completed by now all look fine, so

Quoted 47 lines
 include/asm-generic/export.h | 2 +-
 include/linux/export.h       | 2 +-
 kernel/module.c              | 2 ++
 3 files changed, 4 insertions(+), 2 deletions(-)
include/asm-generic/export.h +1 -1
index e2b5d0f569d3..d0912c7ac2fc 100644
@@ -17,7 +17,7 @@
 
 .macro __put, val, name
 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
-       .long   \val - ., \name - ., 0 - .
+       .long   \val - ., \name - ., 0
 #elif defined(CONFIG_64BIT)
        .quad   \val, \name, 0
 #else
include/linux/export.h +1 -1
index 2c5468d8ea9a..ef5d015d754a 100644
@@ -68,7 +68,7 @@ extern struct module __this_module;
            "__ksymtab_" #sym ":                                \n"     \
            "   .long   " #sym "- .                             \n"     \
            "   .long   __kstrtab_" #sym "- .                   \n"     \
-           "   .long   0 - .                                   \n"     \
+           "   .long   0                                       \n"     \
            "   .previous                                       \n")
 
 struct kernel_symbol {
kernel/module.c +2 -1
index f76efcf2043e..7ab244c4e1ba 100644
@@ -547,6 +547,8 @@ static const char *kernel_symbol_name(const struct kernel_symbol *sym)
 static const char *kernel_symbol_namespace(const struct kernel_symbol *sym)
 {
 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+       if (!sym->namespace_offset)
+               return NULL;
        return offset_to_ptr(&sym->namespace_offset);
 #else
        return sym->namespace;
--

2.23.0.162.g0b9fbb3734-goog

Reviewed-by Tested-by
#3 lore raw
In reply to Will Deacon
Quoted 39 lines · Will Deacon
Quoted 38 lines · Jessica Yu

+++ Will Deacon [11/09/19 13:26 +0100]:

Quoted 36 lines

Commit 8651ec01daed ("module: add support for symbol namespaces.") broke linking for arm64 defconfig:

 | lib/crypto/arc4.o: In function `__ksymtab_arc4_setkey':
 | arc4.c:(___ksymtab+arc4_setkey+0x8): undefined reference to `no symbol'
 | lib/crypto/arc4.o: In function `__ksymtab_arc4_crypt':
 | arc4.c:(___ksymtab+arc4_crypt+0x8): undefined reference to `no symbol'

This is because the dummy initialisation of the 'namespace_offset' field in 'struct kernel_symbol' when using EXPORT_SYMBOL on architectures with support for PREL32 locations uses an offset from an absolute address (0) in an effort to trick 'offset_to_pointer' into behaving as a NOP, allowing non-namespaced symbols to be treated in the same way as those belonging to a namespace.

Unfortunately, place-relative relocations require a symbol reference rather than an absolute value and, although x86 appears to get away with this due to placing the kernel text at the top of the address space, it almost certainly results in a runtime failure if the kernel is relocated dynamically as a result of KASLR.

Rework 'namespace_offset' so that a value of 0, which cannot occur for a valid namespaced symbol, indicates that the corresponding symbol does not belong to a namespace.

Cc Matthias Maennich <[email protected]>
Cc Jessica Yu <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Catalin Marinas <[email protected]>
Fixes 8651ec01daed ("module: add support for symbol namespaces.")

---

Please note that I've not been able to test this at LPC, but it's been submitted to kernelci.

Quoted 7 lines
Quoted 4 lines

Thanks for fixing this so quickly. I can confirm that this fixes the build for arm64 defconfig and x86 built fine for me as well. I'll wait a bit and apply this at the end of the day in case Matthias or anybody else would like to confirm/test.

FWIW, I've managed to boot arm64 Debian under QEMU and load/unload modules successfully with this patch applied on top of modules-next.

Thanks Will for fixing this so quickly! The patch looks good to me.

Feel free to add

Cheers,
Matthias

Applied
#5 lore raw
In reply to Will Deacon

+++ Will Deacon [11/09/19 13:26 +0100]:

Quoted 25 lines

Commit 8651ec01daed ("module: add support for symbol namespaces.") broke linking for arm64 defconfig:

 | lib/crypto/arc4.o: In function `__ksymtab_arc4_setkey':
 | arc4.c:(___ksymtab+arc4_setkey+0x8): undefined reference to `no symbol'
 | lib/crypto/arc4.o: In function `__ksymtab_arc4_crypt':
 | arc4.c:(___ksymtab+arc4_crypt+0x8): undefined reference to `no symbol'

This is because the dummy initialisation of the 'namespace_offset' field in 'struct kernel_symbol' when using EXPORT_SYMBOL on architectures with support for PREL32 locations uses an offset from an absolute address (0) in an effort to trick 'offset_to_pointer' into behaving as a NOP, allowing non-namespaced symbols to be treated in the same way as those belonging to a namespace.

Unfortunately, place-relative relocations require a symbol reference rather than an absolute value and, although x86 appears to get away with this due to placing the kernel text at the top of the address space, it almost certainly results in a runtime failure if the kernel is relocated dynamically as a result of KASLR.

Rework 'namespace_offset' so that a value of 0, which cannot occur for a valid namespaced symbol, indicates that the corresponding symbol does not belong to a namespace.

Quoted 7 lines
Cc Matthias Maennich <[email protected]>
Cc Jessica Yu <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Catalin Marinas <[email protected]>
Fixes 8651ec01daed ("module: add support for symbol namespaces.")

Applied, thanks everyone!

Jessica

Quoted 52 lines

---

Please note that I've not been able to test this at LPC, but it's been submitted to kernelci.

include/asm-generic/export.h | 2 +-
include/linux/export.h       | 2 +-
kernel/module.c              | 2 ++
3 files changed, 4 insertions(+), 2 deletions(-)
include/asm-generic/export.h +0 -0
index e2b5d0f569d3..d0912c7ac2fc 100644
@@ -17,7 +17,7 @@
 

.macro __put, val, name
#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS

include/asm-generic/export.h +1 -1
-	.long	\val - ., \name - ., 0 - .
+	.long	\val - ., \name - ., 0

#elif defined(CONFIG_64BIT)

	.quad	\val, \name, 0

#else

include/linux/export.h +1 -1
index 2c5468d8ea9a..ef5d015d754a 100644
@@ -68,7 +68,7 @@ extern struct module __this_module;
 	    "__ksymtab_" #sym ":				\n"	\
 	    "	.long	" #sym "- .				\n"	\
 	    "	.long	__kstrtab_" #sym "- .			\n"	\
-	    "	.long	0 - .					\n"	\
+	    "	.long	0					\n"	\
 	    "	.previous					\n")
 
 struct kernel_symbol {
kernel/module.c +2 -0
index f76efcf2043e..7ab244c4e1ba 100644
@@ -547,6 +547,8 @@ static const char *kernel_symbol_name(const struct kernel_symbol *sym)
 static const char *kernel_symbol_namespace(const struct kernel_symbol *sym)
 {
 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+	if (!sym->namespace_offset)
+		return NULL;
 	return offset_to_ptr(&sym->namespace_offset);
 #else
 	return sym->namespace;
Signature · 1 line
2.23.0.162.g0b9fbb3734-goog