Skip to contentSkip to conversation

x86/extable: ensure entries are swapped completely when sorting

PATCH In review Mathias Krause 3 participants 4 messages May 10, 2016 – May 11, 2016 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/extable: ensure entries are swapped completely when sorting

The x86 exception table sorting was changed in commit 29934b0fb8ff ("x86/extable: use generic search and sort routines") to use the arch independent code in lib/extable.c. However, the patch was mangled somehow on its way into the kernel from the last version posted at [1]. The committed version kind of attempted to incorporate the changes of commit 548acf19234d ("x86/mm: Expand the exception table logic to allow new handling options") as in _completely_ _ignoring_ the x86 specific 'handler' member of struct exception_table_entry. This effectively broke the sorting as entries will only partly be swapped now.

Fortunately, the x86 Kconfig selects BUILDTIME_EXTABLE_SORT, so the exception table doesn't need to be sorted at runtime. However, in case that ever changes, we better not break the exception table sorting just because of that.

Fix this by providing a swap_ex_entry_fixup() macro that takes care of the 'handler' member.

[1] https://lkml.org/lkml/2016/1/27/232

Cc Andrew Morton <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Tony Luck <[email protected]>
---
 arch/x86/include/asm/uaccess.h |    8 ++++++++
 1 file changed, 8 insertions(+)
arch/x86/include/asm/uaccess.h +8 -0
index a969ae607be8..b69b7bffb0e0 100644
@@ -108,6 +108,14 @@ struct exception_table_entry {
 
 #define ARCH_HAS_RELATIVE_EXTABLE
 
+#define swap_ex_entry_fixup(a, b, tmp, delta)			\
+	do {							\
+		(a)->fixup = (b)->fixup + (delta);		\
+		(b)->fixup = (tmp).fixup - (delta);		\
+		(a)->handler = (b)->handler + (delta);		\
+		(b)->handler = (tmp).handler - (delta);		\
+	} while (0)
+
 extern int fixup_exception(struct pt_regs *regs, int trapnr);
 extern bool ex_has_fault_handler(unsigned long ip);
 extern int early_fixup_exception(unsigned long *ip);
Signature · 1 line
1.7.10.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Mathias Krause

The x86 exception table sorting was changed in commit 29934b0fb8ff ("x86/extable: use generic search and sort routines") to use the arch independent code in lib/extable.c. However, the patch was mangled somehow on its way into the kernel from the last version posted at [1]. The committed version kind of attempted to incorporate the changes of commit 548acf19234d ("x86/mm: Expand the exception table logic to allow new handling options") as in _completely_ _ignoring_ the x86 specific 'handler' member of struct exception_table_entry. This effectively broke the sorting as entries will only partly be swapped now.

OK, it appears that Tony's patches grew a 'handler' field fairly late in their review cycle, and I didn't pick up on that. Apologies.

Quoted 5 lines

Fortunately, the x86 Kconfig selects BUILDTIME_EXTABLE_SORT, so the exception table doesn't need to be sorted at runtime. However, in case that ever changes, we better not break the exception table sorting just because of that.

BUILDTIME_EXTABLE_SORT applies to the core image only, but we still rely on the sorting routines for modules in that case. So I think this needs to be fixed right away.

Quoted 5 lines

Fix this by providing a swap_ex_entry_fixup() macro that takes care of the 'handler' member.

[1] https://lkml.org/lkml/2016/1/27/232

Quoted 10 lines
Cc Andrew Morton <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Tony Luck <[email protected]>
Fixes 29934b0fb8f ("x86/extable: use generic search and sort routines")

Thanks,
Ard.

Quoted 26 lines
---
 arch/x86/include/asm/uaccess.h |    8 ++++++++
 1 file changed, 8 insertions(+)
arch/x86/include/asm/uaccess.h +8 -1
index a969ae607be8..b69b7bffb0e0 100644
@@ -108,6 +108,14 @@ struct exception_table_entry {
 
 #define ARCH_HAS_RELATIVE_EXTABLE
 
+#define swap_ex_entry_fixup(a, b, tmp, delta)                  \
+       do {                                                    \
+               (a)->fixup = (b)->fixup + (delta);              \
+               (b)->fixup = (tmp).fixup - (delta);             \
+               (a)->handler = (b)->handler + (delta);          \
+               (b)->handler = (tmp).handler - (delta);         \
+       } while (0)
+
 extern int fixup_exception(struct pt_regs *regs, int trapnr);
 extern bool ex_has_fault_handler(unsigned long ip);
 extern int early_fixup_exception(unsigned long *ip);
--

1.7.10.4

#2 lore raw
Quoted 12 lines · Ard Biesheuvel
Quoted 10 lines · Mathias Krause

The x86 exception table sorting was changed in commit 29934b0fb8ff ("x86/extable: use generic search and sort routines") to use the arch independent code in lib/extable.c. However, the patch was mangled somehow on its way into the kernel from the last version posted at [1]. The committed version kind of attempted to incorporate the changes of commit 548acf19234d ("x86/mm: Expand the exception table logic to allow new handling options") as in _completely_ _ignoring_ the x86 specific 'handler' member of struct exception_table_entry. This effectively broke the sorting as entries will only partly be swapped now.

Quoted 2 lines

OK, it appears that Tony's patches grew a 'handler' field fairly late in their review cycle, and I didn't pick up on that. Apologies.

No need to apologize, as your patch *must* have created conflicts for the committer applying it. It's just that those conflicts were resolved wrongly. He/She should have asked you for a new version, but... did not :/

Quoted 9 lines
Quoted 5 lines

Fortunately, the x86 Kconfig selects BUILDTIME_EXTABLE_SORT, so the exception table doesn't need to be sorted at runtime. However, in case that ever changes, we better not break the exception table sorting just because of that.

BUILDTIME_EXTABLE_SORT applies to the core image only, but we still rely on the sorting routines for modules in that case. So I think this needs to be fixed right away.

Good point!

Quoted 17 lines
Quoted 6 lines

Fix this by providing a swap_ex_entry_fixup() macro that takes care of the 'handler' member.

[1] https://lkml.org/lkml/2016/1/27/232

Quoted 10 lines
Cc Andrew Morton <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Tony Luck <[email protected]>
Quoted 6 lines
Fixes 29934b0fb8f ("x86/extable: use generic search and sort routines")

Thanks,
Ard.

Thanks,
Mathias

#3 lore raw

[tip:x86/mm] x86/extable: Ensure entries are swapped completely when sorting

In reply to Mathias Krause
x86/extable: Ensure entries are swapped completely when sorting
Commit-ID:  67d7a982bab6702d84415ea889996fae72a7d3b2
Gitweb:     http://git.kernel.org/tip/67d7a982bab6702d84415ea889996fae72a7d3b2
Author:     Mathias Krause <[email protected]>
AuthorDate: Tue, 10 May 2016 23:07:02 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 11 May 2016 11:14:06 +0200

x86/extable: Ensure entries are swapped completely when sorting

The x86 exception table sorting was changed in this recent commit:

29934b0fb8ff ("x86/extable: use generic search and sort routines")

... to use the arch independent code in lib/extable.c. However, the patch was mangled somehow on its way into the kernel from the last version posted at:

https://lkml.org/lkml/2016/1/27/232

The committed version kind of attempted to incorporate the changes of contemporary commit done in the x86 tree:

548acf19234d ("x86/mm: Expand the exception table logic to allow new handling options")

... as in _completely_ _ignoring_ the x86 specific 'handler' member of struct exception_table_entry. This effectively broke the sorting as entries will only be partly swapped now.

Fortunately, the x86 Kconfig selects BUILDTIME_EXTABLE_SORT, so the exception table doesn't need to be sorted at runtime. However, in case that ever changes, we better not break the exception table sorting just because of that.

Fix this by providing a swap_ex_entry_fixup() macro that takes care of the 'handler' member.

Cc Andrew Morton <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Tony Luck <[email protected]>
---
 arch/x86/include/asm/uaccess.h | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/x86/include/asm/uaccess.h +8 -0
index 8b3fb76..86c48f3 100644
@@ -108,6 +108,14 @@ struct exception_table_entry {
 
 #define ARCH_HAS_RELATIVE_EXTABLE
 
+#define swap_ex_entry_fixup(a, b, tmp, delta)			\
+	do {							\
+		(a)->fixup = (b)->fixup + (delta);		\
+		(b)->fixup = (tmp).fixup - (delta);		\
+		(a)->handler = (b)->handler + (delta);		\
+		(b)->handler = (tmp).handler - (delta);		\
+	} while (0)
+
 extern int fixup_exception(struct pt_regs *regs, int trapnr);
 extern bool ex_has_fault_handler(unsigned long ip);
 extern int early_fixup_exception(unsigned long *ip);