mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Borislav Petkov <bp@suse.de>
To: Mateusz Jurczyk <mjurczyk@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>,
	Andy Lutomirski <luto@kernel.org>,
	x86@kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] x86: Prevent uninitialized stack byte read in apply_alternatives()
Date: Wed, 24 May 2017 16:02:55 +0200	[thread overview]
Message-ID: <20170524140254.GE7275@nazgul.tnic> (raw)
In-Reply-To: <20170524135500.27223-1-mjurczyk@google.com>

On Wed, May 24, 2017 at 03:55:00PM +0200, Mateusz Jurczyk wrote:
> 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>
> ---
> 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(-)
> 
> 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,
>  		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),
> -- 

Reviewed-by: Borislav Petkov <bp@suse.de>

-- 
Regards/Gruss,
    Boris.

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

  reply	other threads:[~2017-05-24 14:03 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-24 12:51 [PATCH] " Mateusz Jurczyk
2017-05-24 13:04 ` Borislav Petkov
2017-05-24 13:55   ` [PATCH v2] " Mateusz Jurczyk
2017-05-24 14:02     ` Borislav Petkov [this message]
2017-05-24 14:25     ` [tip:x86/urgent] x86/alternatives: " tip-bot for Mateusz Jurczyk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170524140254.GE7275@nazgul.tnic \
    --to=bp@suse.de \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mingo@redhat.com \
    --cc=mjurczyk@google.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®