Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1649665

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

From tip-bot for Mateusz Jurczyk <tipbot@zytor.com>
Newsgroups linux.kernel
Subject [tip:x86/urgent] x86/alternatives: Prevent uninitialized stack byte read in apply_alternatives()
Date 2017-05-24 16:30 +0200
Message-ID <tKFsu-3ss-27@gated-at.bofh.it> (permalink)
References <tKEZr-33S-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


Commit-ID:  fc152d22d6e9fac95a9a990e6c29510bdf1b9425
Gitweb:     http://git.kernel.org/tip/fc152d22d6e9fac95a9a990e6c29510bdf1b9425
Author:     Mateusz Jurczyk <mjurczyk@google.com>
AuthorDate: Wed, 24 May 2017 15:55:00 +0200
Committer:  Thomas Gleixner <tglx@linutronix.de>
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.

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>
---
 arch/x86/kernel/alternative.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index c5b8f76..32e14d1 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,
 		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),

Back to linux.kernel | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

[PATCH] x86: Prevent uninitialized stack byte read in apply_alternatives() Mateusz Jurczyk <mjurczyk@google.com> - 2017-05-24 15:00 +0200
  Re: [PATCH] x86: Prevent uninitialized stack byte read in  apply_alternatives() Borislav Petkov <bp@suse.de> - 2017-05-24 15:10 +0200
    [PATCH v2] x86: Prevent uninitialized stack byte read in apply_alternatives() Mateusz Jurczyk <mjurczyk@google.com> - 2017-05-24 16:00 +0200
      Re: [PATCH v2] x86: Prevent uninitialized stack byte read in  apply_alternatives() Borislav Petkov <bp@suse.de> - 2017-05-24 16:10 +0200
      [tip:x86/urgent] x86/alternatives: Prevent uninitialized stack byte  read in apply_alternatives() tip-bot for Mateusz Jurczyk <tipbot@zytor.com> - 2017-05-24 16:30 +0200

csiph-web