Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1jEWdZ-0005lE-VL for mharc-grub-devel@gnu.org; Wed, 18 Mar 2020 07:13:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39073) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jEWdW-0005iu-O1 for grub-devel@gnu.org; Wed, 18 Mar 2020 07:13:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jEWdU-0000vi-Pg for grub-devel@gnu.org; Wed, 18 Mar 2020 07:13:22 -0400 Received: from mo6-p00-ob.smtp.rzone.de ([2a01:238:20a:202:5300::7]:23753) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jEWdT-0000nQ-GJ for grub-devel@gnu.org; Wed, 18 Mar 2020 07:13:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1584529996; s=strato-dkim-0002; d=aepfle.de; h=Message-Id:Date:Subject:Cc:To:From:X-RZG-CLASS-ID:X-RZG-AUTH:From: Subject:Sender; bh=z+iETTw3s36WFEVBg0LR8abiZ44J8Okm1JUhjnFveUo=; b=WVDDkmENgGEGaa4EV0iBq4xGw2IkdGV30ooYfn2eD//R/zqz28AFwuLjKlynW3YTga KoFKulqtAmSIKYpk7kiGDuARaL06ItxBMq7V/h27SgsKAcxv59mzhClxg0hRCQJSH6+p k9a8M0Y0XWL+j6pO9a4MwaS9STAq7Grp7qvvF0Hw1vqV86R9hdWY6kvitok4bucEaZzu w6giYzBkc2FxvruZcyrXZEOGZ1XNH8yZMvJ+gbUqELHZ3JBZKwBpsme7+38rZStMQU0y Qns7MbiqcgXEVDQbYBoD3PiBO+hhSzYktfsBHRNzxEqy14oEqiBDX63r7RfkcV/sr7AR aGDA== X-RZG-AUTH: ":P2EQZWCpfu+qG7CngxMFH1J+3q8wa/QXkBR9MXjAuzBW/OdlBZQ4AHSS329Ojw==" X-RZG-CLASS-ID: mo00 Received: from sender by smtp.strato.de (RZmta 46.2.0 DYNA|AUTH) with ESMTPSA id u021a0w2IBDFGt1 (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256 bits)) (Client did not present a certificate); Wed, 18 Mar 2020 12:13:15 +0100 (CET) From: Olaf Hering To: grub-devel@gnu.org Cc: Olaf Hering Subject: [PATCH v1] fix kernel cmdline corruption Date: Wed, 18 Mar 2020 12:13:12 +0100 Message-Id: <20200318111312.21684-1-olaf@aepfle.de> X-Mailer: git-send-email 2.16.4 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a01:238:20a:202:5300::7 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 Mar 2020 11:13:24 -0000 The functions grub_create_loader_cmdline and its sibling grub_loader_cmdline_size add extra, unexpected characters to the kernel cmdline. Both functions are supposed to copy each argv[] element verbatim. It is up to the caller of the kernel command ('linux' for example) to add desired quoting or escaping characters for the consumers of the kernel command line. The built-in grub shell gives enough opportunities to construct such argv[] elements. After this change, these arguments result in the following kernel cmdline: var='val' var="val" var=\'v1 v2\' var=\"v1 v2\" var=\\\"v1 v2\\\" x var=val var=val var='v1 v2' var="v1 v2" var=\"v1 v2\" x Fixes commit 25953e10553dad2e378541a68686fc094603ec54 Signed-off-by: Olaf Hering --- grub-core/lib/cmdline.c | 58 ++++++++----------------------------------------- 1 file changed, 9 insertions(+), 49 deletions(-) diff --git a/grub-core/lib/cmdline.c b/grub-core/lib/cmdline.c index ed0b149dc..b572a367f 100644 --- a/grub-core/lib/cmdline.c +++ b/grub-core/lib/cmdline.c @@ -20,31 +20,6 @@ #include #include -static unsigned int check_arg (char *c, int *has_space) -{ - int space = 0; - unsigned int size = 0; - - while (*c) - { - if (*c == '\\' || *c == '\'' || *c == '"') - size++; - else if (*c == ' ') - space = 1; - - size++; - c++; - } - - if (space) - size += 2; - - if (has_space) - *has_space = space; - - return size; -} - unsigned int grub_loader_cmdline_size (int argc, char *argv[]) { int i; @@ -52,7 +27,7 @@ unsigned int grub_loader_cmdline_size (int argc, char *argv[]) for (i = 0; i < argc; i++) { - size += check_arg (argv[i], 0); + size += grub_strlen(argv[i]); size++; /* Separator space or NULL. */ } @@ -66,36 +41,21 @@ grub_err_t grub_create_loader_cmdline (int argc, char *argv[], char *buf, grub_size_t size, enum grub_verify_string_type type) { - int i, space; + int i; unsigned int arg_size; - char *c, *orig_buf = buf; + char *orig_buf = buf; for (i = 0; i < argc; i++) { - c = argv[i]; - arg_size = check_arg(argv[i], &space); - arg_size++; /* Separator space or NULL. */ + arg_size = grub_strlen(argv[i]); - if (size < arg_size) + /* Separator space or NULL. */ + if (size < arg_size + 1) break; - size -= arg_size; - - if (space) - *buf++ = '"'; - - while (*c) - { - if (*c == '\\' || *c == '\'' || *c == '"') - *buf++ = '\\'; - - *buf++ = *c; - c++; - } - - if (space) - *buf++ = '"'; - + grub_memcpy(buf, argv[i], arg_size); + size -= arg_size + 1; + buf += arg_size; *buf++ = ' '; }