Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2030309imm; Tue, 10 Jul 2018 11:56:41 -0700 (PDT) X-Google-Smtp-Source: AAOMgpckuZ44tYk4D8wsVE/oRihhpHiQ9K6upz6/3x0PoUsWE8r28O6RDhvqyPbAzRBCE0qQQZiF X-Received: by 2002:a63:b74a:: with SMTP id w10-v6mr23562883pgt.266.1531249000939; Tue, 10 Jul 2018 11:56:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531249000; cv=none; d=google.com; s=arc-20160816; b=uRSyryvgMo8I/Hv4FlkpO4la0Jo1tGjBEIGS0GeIML/lx/BO0kJUJ1Cq1/Bn09KqIh EiMdxRxfL1oXUeg1/i7DXu++c3MZeVR9HINOeVW3jg23d67q5bMTBO1WacPackySLbJ2 u3QzAymAtDaY7nU5fH3oILq5liJzi9oR0y++6LlSIbbkAKU1Gvk4klgf0ZI+HehD71sa 2tNLOQdfoP39QKtEbwa+DH69b51lGLeVkxsz5mtfWI7edSu9dH8AqZL5GWQpXuwimH5V FLJn666Czq+96gacQlnaYGRGdCtVfUSVKfDKQhhML2CYyAASawrxNnbQCYQ7hm95/Bi4 V2GQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=QVRaL4AqO5UY6YUN1HaD7y7jx6BWOSqOinqG7vvyJNs=; b=vT5/Lg/MfXSoaYn5ueDHg1VUljFFU8H/ey3orRxWg5JIGHxpq2SeipniYmaWbN1SAQ +pHvEbAVipBngQ7EvraktgKA7Kt6huor9vEwehnkO158EOuDEmQYeSpub1grCUViRDRN epu2u5OC0m3zoExxsBcNdeJ8XOnHD7VpcEFR0ueTmB50An0ZQwgFaIVmd09hpm6emkts FSZNil37B0QCSoWUVIgrNEjGxnmtEGn9L6WvNQWUw+8lLkJGm9elzDNY+mm6SxEiHHcY GIjRGq3rEcJDGYnYfmKbo1bsejAVLo56GM3SnNGAxpStZrFnIzOW8cILPx8iGjz/KA9S 6pKQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 13-v6si9620489pgt.638.2018.07.10.11.56.25; Tue, 10 Jul 2018 11:56:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732953AbeGJS1D (ORCPT + 99 others); Tue, 10 Jul 2018 14:27:03 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:44668 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732943AbeGJS1C (ORCPT ); Tue, 10 Jul 2018 14:27:02 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 13016DB4; Tue, 10 Jul 2018 18:26:51 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dave Hansen , Borislav Petkov , Andy Lutomirski , Borislav Petkov , Brian Gerst , Denys Vlasenko , "H. Peter Anvin" , Linus Torvalds , Peter Zijlstra , Thomas Gleixner , fenghua.yu@intel.com, yu-cheng.yu@intel.com, Ingo Molnar , Ben Hutchings Subject: [PATCH 3.18 03/23] x86/boot: Fix early command-line parsing when matching at end Date: Tue, 10 Jul 2018 20:24:36 +0200 Message-Id: <20180710182309.025696403@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180710182308.877332304@linuxfoundation.org> References: <20180710182308.877332304@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Dave Hansen commit 02afeaae9843733a39cd9b11053748b2d1dc5ae7 upstream. The x86 early command line parsing in cmdline_find_option_bool() is buggy. If it matches a specified 'option' all the way to the end of the command-line, it will consider it a match. For instance, cmdline = "foo"; cmdline_find_option_bool(cmdline, "fool"); will return 1. This is particularly annoying since we have actual FPU options like "noxsave" and "noxsaves" So, command-line "foo bar noxsave" will match *BOTH* a "noxsave" and "noxsaves". (This turns out not to be an actual problem because "noxsave" implies "noxsaves", but it's still confusing.) To fix this, we simplify the code and stop tracking 'len'. 'len' was trying to indicate either the NULL terminator *OR* the end of a non-NULL-terminated command line at 'COMMAND_LINE_SIZE'. But, each of the three states is *already* checking 'cmdline' for a NULL terminator. We _only_ need to check if we have overrun 'COMMAND_LINE_SIZE', and that we can do without keeping 'len' around. Also add some commends to clarify what is going on. Signed-off-by: Dave Hansen Signed-off-by: Borislav Petkov Cc: Andy Lutomirski Cc: Borislav Petkov Cc: Brian Gerst Cc: Denys Vlasenko Cc: H. Peter Anvin Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: fenghua.yu@intel.com Cc: yu-cheng.yu@intel.com Link: http://lkml.kernel.org/r/20151222225238.9AEB560C@viggo.jf.intel.com Signed-off-by: Ingo Molnar Cc: Ben Hutchings Signed-off-by: Greg Kroah-Hartman --- arch/x86/lib/cmdline.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) --- a/arch/x86/lib/cmdline.c +++ b/arch/x86/lib/cmdline.c @@ -21,12 +21,14 @@ static inline int myisspace(u8 c) * @option: option string to look for * * Returns the position of that @option (starts counting with 1) - * or 0 on not found. + * or 0 on not found. @option will only be found if it is found + * as an entire word in @cmdline. For instance, if @option="car" + * then a cmdline which contains "cart" will not match. */ int cmdline_find_option_bool(const char *cmdline, const char *option) { char c; - int len, pos = 0, wstart = 0; + int pos = 0, wstart = 0; const char *opptr = NULL; enum { st_wordstart = 0, /* Start of word/after whitespace */ @@ -37,11 +39,14 @@ int cmdline_find_option_bool(const char if (!cmdline) return -1; /* No command line */ - len = min_t(int, strlen(cmdline), COMMAND_LINE_SIZE); - if (!len) + if (!strlen(cmdline)) return 0; - while (len--) { + /* + * This 'pos' check ensures we do not overrun + * a non-NULL-terminated 'cmdline' + */ + while (pos < COMMAND_LINE_SIZE) { c = *(char *)cmdline++; pos++; @@ -58,17 +63,26 @@ int cmdline_find_option_bool(const char /* fall through */ case st_wordcmp: - if (!*opptr) + if (!*opptr) { + /* + * We matched all the way to the end of the + * option we were looking for. If the + * command-line has a space _or_ ends, then + * we matched! + */ if (!c || myisspace(c)) return wstart; else state = st_wordskip; - else if (!c) + } else if (!c) { + /* + * Hit the NULL terminator on the end of + * cmdline. + */ return 0; - else if (c != *opptr++) + } else if (c != *opptr++) { state = st_wordskip; - else if (!len) /* last word and is matching */ - return wstart; + } break; case st_wordskip: