From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754764AbeAJL2a (ORCPT + 1 other); Wed, 10 Jan 2018 06:28:30 -0500 Received: from mail.skyhub.de ([5.9.137.197]:55188 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751409AbeAJL23 (ORCPT ); Wed, 10 Jan 2018 06:28:29 -0500 Date: Wed, 10 Jan 2018 12:28:16 +0100 From: Borislav Petkov To: David Woodhouse Cc: Linus Torvalds , Andi Kleen , Thomas Gleixner , the arch/x86 maintainers , Linux Kernel Mailing List , Paul Turner , Andrew Lutomirski , Peter Zijlstra , Tom Lendacky , Tim Chen , Greg Kroah-Hartman , Dave Hansen , Jiri Kosina , Andi Kleen Subject: [PATCH] x86/alternatives: Fix optimize_nops() checking Message-ID: <20180110112815.mgciyf5acwacphkq@pd.tnic> References: <20180110003139.10531-1-andi@firstfloor.org> <1515568506.22302.72.camel@infradead.org> <1515578735.22302.91.camel@infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <1515578735.22302.91.camel@infradead.org> User-Agent: NeoMutt/20170609 (1.8.3) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: From: Borislav Petkov Date: Wed, 10 Jan 2018 12:14:07 +0100 We check only the first byte whether it is a NOP but if David Woodhouse wants to do some crazy experiments with slapping NOPs in front of the payload and having the actual instructions after it, this "optimized" test breaks. :-) Make sure we scan all bytes before we decide to optimize the NOPs in there. Reported-by: David Woodhouse Signed-off-by: Borislav Petkov Cc: x86@kernel.org --- arch/x86/kernel/alternative.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 3344d3382e91..78932b283915 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -344,9 +344,11 @@ recompute_jump(struct alt_instr *a, u8 *orig_insn, u8 *repl_insn, u8 *insnbuf) static void __init_or_module noinline optimize_nops(struct alt_instr *a, u8 *instr) { unsigned long flags; + int i; - if (instr[0] != 0x90) - return; + for (i = 0; i < a->padlen; i++) + if (instr[i] != 0x90) + return; local_irq_save(flags); add_nops(instr + (a->instrlen - a->padlen), a->padlen); -- 2.13.0 -- Regards/Gruss, Boris. Good mailing practices for 400: avoid top-posting and trim the reply.