aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMateusz Jurczyk <mjurczyk@google.com>2017-05-24 09:55:00 -0400
committerThomas Gleixner <tglx@linutronix.de>2017-05-24 10:18:12 -0400
commitfc152d22d6e9fac95a9a990e6c29510bdf1b9425 (patch)
treec784fbbc87c38aa14979ec3ff7d4a70741761ffa
parentcbed27cdf0e3f7ea3b2259e86b9e34df02be3fe4 (diff)
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. Signed-off-by: Mateusz Jurczyk <mjurczyk@google.com> Reviewed-by: Borislav Petkov <bp@suse.de> Cc: Andy Lutomirski <luto@kernel.org> Link: http://lkml.kernel.org/r/20170524135500.27223-1-mjurczyk@google.com Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
-rw-r--r--arch/x86/kernel/alternative.c9
1 files changed, 7 insertions, 2 deletions
diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index c5b8f760473c..32e14d137416 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -409,8 +409,13 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
409 memcpy(insnbuf, replacement, a->replacementlen); 409 memcpy(insnbuf, replacement, a->replacementlen);
410 insnbuf_sz = a->replacementlen; 410 insnbuf_sz = a->replacementlen;
411 411
412 /* 0xe8 is a relative jump; fix the offset. */ 412 /*
413 if (*insnbuf == 0xe8 && a->replacementlen == 5) { 413 * 0xe8 is a relative jump; fix the offset.
414 *
415 * Instruction length is checked before the opcode to avoid
416 * accessing uninitialized bytes for zero-length replacements.
417 */
418 if (a->replacementlen == 5 && *insnbuf == 0xe8) {
414 *(s32 *)(insnbuf + 1) += replacement - instr; 419 *(s32 *)(insnbuf + 1) += replacement - instr;
415 DPRINTK("Fix CALL offset: 0x%x, CALL 0x%lx", 420 DPRINTK("Fix CALL offset: 0x%x, CALL 0x%lx",
416 *(s32 *)(insnbuf + 1), 421 *(s32 *)(insnbuf + 1),