Skip to contentSkip to conversation

x86: Prevent uninitialized stack byte read in apply_alternatives()

PATCH Superseded Mateusz Jurczyk 3 participants 5 messages May 24, 2017 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: Prevent uninitialized stack byte read in apply_alternatives()

In the current form of the code, if a->replacementlen is 0, the reference to *insnbuf for comparison touches potentially garbage memory. While it doesn't affect the execution flow due to the subsequent a->replacementlen comparison, it is (rightly) detected as use of uninitialized memory by a runtime instrumentation currently under my development, and could be detected as such by other tools in the future, too (e.g. KMSAN).

Fix the "false-positive" by reordering the conditions to first check the replacement instruction length before referencing specific opcode bytes.

---
 arch/x86/kernel/alternative.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/alternative.c +1 -1
index c5b8f760473c..d03ba6bc97d8 100644
@@ -410,7 +410,7 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
 		insnbuf_sz = a->replacementlen;
 
 		/* 0xe8 is a relative jump; fix the offset. */
-		if (*insnbuf == 0xe8 && a->replacementlen == 5) {
+		if (a->replacementlen == 5 && *insnbuf == 0xe8) {
 			*(s32 *)(insnbuf + 1) += replacement - instr;
 			DPRINTK("Fix CALL offset: 0x%x, CALL 0x%lx",
 				*(s32 *)(insnbuf + 1),
Signature · 1 line
2.13.0.219.gdb65acc882-goog
#1 lore raw
Quoted 20 lines · Mateusz Jurczyk

In the current form of the code, if a->replacementlen is 0, the reference to *insnbuf for comparison touches potentially garbage memory. While it doesn't affect the execution flow due to the subsequent a->replacementlen comparison, it is (rightly) detected as use of uninitialized memory by a runtime instrumentation currently under my development, and could be detected as such by other tools in the future, too (e.g. KMSAN).

Fix the "false-positive" by reordering the conditions to first check the replacement instruction length before referencing specific opcode bytes.

---
 arch/x86/kernel/alternative.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/alternative.c +0 -0
index c5b8f760473c..d03ba6bc97d8 100644

arch/x86/kernel/alternative.c void __init_or_module noinline apply_alternatives(struct alt_instr *start, · line 412

Quoted 3 lines
arch/x86/kernel/alternative.c +0 -0
@@ -410,7 +410,7 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
 		insnbuf_sz = a->replacementlen;
 
 		/* 0xe8 is a relative jump; fix the offset. */

Sure but put in short the reason to look at the instruction length first in the comment above so that it is clear why we're doing it this way.

arch/x86/kernel/alternative.c void __init_or_module noinline apply_alternatives(struct alt_instr *start, · line 416

Quoted 6 lines
arch/x86/kernel/alternative.c +1 -1
-		if (*insnbuf == 0xe8 && a->replacementlen == 5) {
+		if (a->replacementlen == 5 && *insnbuf == 0xe8) {
 			*(s32 *)(insnbuf + 1) += replacement - instr;
 			DPRINTK("Fix CALL offset: 0x%x, CALL 0x%lx",
 				*(s32 *)(insnbuf + 1),

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- 
#2 lore raw

PATCH v2 1/1 x86: Prevent uninitialized stack byte read in apply_alternatives()

In the current form of the code, if a->replacementlen is 0, the reference to *insnbuf for comparison touches potentially garbage memory. While it doesn't affect the execution flow due to the subsequent a->replacementlen comparison, it is (rightly) detected as use of uninitialized memory by a runtime instrumentation currently under my development, and could be detected as such by other tools in the future, too (e.g. KMSAN).

Fix the "false-positive" by reordering the conditions to first check the replacement instruction length before referencing specific opcode bytes.

---
Changes in v2:
- Add an explaining comment, as per Borislav Petkov's request.

 arch/x86/kernel/alternative.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
arch/x86/kernel/alternative.c +7 -2
index c5b8f760473c..32e14d137416 100644
@@ -409,8 +409,13 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
 		memcpy(insnbuf, replacement, a->replacementlen);
 		insnbuf_sz = a->replacementlen;
 
-		/* 0xe8 is a relative jump; fix the offset. */
-		if (*insnbuf == 0xe8 && a->replacementlen == 5) {
+		/*
+		 * 0xe8 is a relative jump; fix the offset.
+		 *
+		 * Instruction length is checked before the opcode to avoid
+		 * accessing uninitialized bytes for zero-length replacements.
+		 */
+		if (a->replacementlen == 5 && *insnbuf == 0xe8) {
 			*(s32 *)(insnbuf + 1) += replacement - instr;
 			DPRINTK("Fix CALL offset: 0x%x, CALL 0x%lx",
 				*(s32 *)(insnbuf + 1),
Signature · 1 line
2.13.0.219.gdb65acc882-goog
Reviewed-by on v2 1/1
#3 lore raw
Quoted 29 lines · Mateusz Jurczyk

In the current form of the code, if a->replacementlen is 0, the reference to *insnbuf for comparison touches potentially garbage memory. While it doesn't affect the execution flow due to the subsequent a->replacementlen comparison, it is (rightly) detected as use of uninitialized memory by a runtime instrumentation currently under my development, and could be detected as such by other tools in the future, too (e.g. KMSAN).

Fix the "false-positive" by reordering the conditions to first check the replacement instruction length before referencing specific opcode bytes.

---
Changes in v2:
- Add an explaining comment, as per Borislav Petkov's request.

 arch/x86/kernel/alternative.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
arch/x86/kernel/alternative.c +0 -2
index c5b8f760473c..32e14d137416 100644
@@ -409,8 +409,13 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
 		memcpy(insnbuf, replacement, a->replacementlen);
 		insnbuf_sz = a->replacementlen;
 
-		/* 0xe8 is a relative jump; fix the offset. */
-		if (*insnbuf == 0xe8 && a->replacementlen == 5) {

arch/x86/kernel/alternative.c void __init_or_module noinline apply_alternatives(struct alt_instr *start, · line 421

Quoted 10 lines
arch/x86/kernel/alternative.c +7 -0
+		/*
+		 * 0xe8 is a relative jump; fix the offset.
+		 *
+		 * Instruction length is checked before the opcode to avoid
+		 * accessing uninitialized bytes for zero-length replacements.
+		 */
+		if (a->replacementlen == 5 && *insnbuf == 0xe8) {
 			*(s32 *)(insnbuf + 1) += replacement - instr;
 			DPRINTK("Fix CALL offset: 0x%x, CALL 0x%lx",
 				*(s32 *)(insnbuf + 1),
Signature · 5 lines
Regards/Gruss,
    Boris.

SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- 
#4 lore raw

[tip:x86/urgent] x86/alternatives: Prevent uninitialized stack byte read in apply_alternatives()

In reply to Mateusz Jurczyk
x86/alternatives: Prevent uninitialized stack byte read in apply_alternatives()
Commit-ID:  fc152d22d6e9fac95a9a990e6c29510bdf1b9425
Gitweb:     http://git.kernel.org/tip/fc152d22d6e9fac95a9a990e6c29510bdf1b9425
Author:     Mateusz Jurczyk <[email protected]>
AuthorDate: Wed, 24 May 2017 15:55:00 +0200
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Wed, 24 May 2017 16:18:12 +0200

x86/alternatives: Prevent uninitialized stack byte read in apply_alternatives()

In the current form of the code, if a->replacementlen is 0, the reference to *insnbuf for comparison touches potentially garbage memory. While it doesn't affect the execution flow due to the subsequent a->replacementlen comparison, it is (rightly) detected as use of uninitialized memory by a runtime instrumentation currently under my development, and could be detected as such by other tools in the future, too (e.g. KMSAN).

Fix the "false-positive" by reordering the conditions to first check the replacement instruction length before referencing specific opcode bytes.

---
 arch/x86/kernel/alternative.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
arch/x86/kernel/alternative.c +7 -2
index c5b8f76..32e14d1 100644
@@ -409,8 +409,13 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
 		memcpy(insnbuf, replacement, a->replacementlen);
 		insnbuf_sz = a->replacementlen;
 
-		/* 0xe8 is a relative jump; fix the offset. */
-		if (*insnbuf == 0xe8 && a->replacementlen == 5) {
+		/*
+		 * 0xe8 is a relative jump; fix the offset.
+		 *
+		 * Instruction length is checked before the opcode to avoid
+		 * accessing uninitialized bytes for zero-length replacements.
+		 */
+		if (a->replacementlen == 5 && *insnbuf == 0xe8) {
 			*(s32 *)(insnbuf + 1) += replacement - instr;
 			DPRINTK("Fix CALL offset: 0x%x, CALL 0x%lx",
 				*(s32 *)(insnbuf + 1),