Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp1145795imm; Tue, 5 Jun 2018 09:45:11 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKoICQOwRkbXOrEMnSta6Bn5E8DMQws9QFTxoLwei4YEvEipFFCIplMOuLBiiNNisoD5fyN X-Received: by 2002:a63:9b19:: with SMTP id r25-v6mr21189150pgd.197.1528217111595; Tue, 05 Jun 2018 09:45:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528217111; cv=none; d=google.com; s=arc-20160816; b=TmpjoxPP7SqClBHbb6D1hQVvzYgfPliqDyk63llP3AkFjQ9ujRLwfB1JHKob4XYrvu D98PKfScibyqjjlqAMiQlrbx0QcGpN3Tb9Ui2vv4t/3rw58uG1dvoekkGbFW5+Lqo2vQ ZgBJ+EjZu2dmpsxAy0oC9eQPxnqm+fo0VIFY+pjX3Fwt6fXkAAyLTawW3Ea2PRobwLCH Q1xLmTXch1vs6PtmZSzth6fTsLn/R/7aW9AbH5rNGV2L+AYZcS2hI12tfsZOcgtG1f/Z 5QXSlG6v+iYdcSLi85Hj/CigPcPPgbC1X9t/HNLkcFcsN4/VpRBxp0vFuwNdGuqPhojD +xwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:to:from :arc-authentication-results; bh=bXke0AzS7ylM3F6+CTrauP/V9vmQ6j3nKEysFV8FERc=; b=vLsmHmfa7Z6eioBfLyv/IAnuXri9X0tQUSMjZbjN8qgO2x1BRxbZ5npkKy94xvxb/W TokcckeXkJIjuLe/Islhdc1PjszYALjd2eTgeNVS1rR38NTsWrRj69l3n7kjmLrjQqyw avusL5dWvD8P7Rh5u1K79L3EYYGzTh0EgigWTuVHaDTsSdhuOB+AGMv4ohQw9LC9RUtk +jB73EI/oGqK6bEDjFR/9FdB0JUUtFp7I9l4hncpJFkmXQCOsmB43n3uMAxS54rwtChy sWrhOPYMj2nBxfCtuqK6HOS9lUkVB9d+JdtchmoQ5WC2YD10wZh9fiCFuffqtGlo3Kkc 2wWg== 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 b8-v6si51420428pls.261.2018.06.05.09.44.56; Tue, 05 Jun 2018 09:45:11 -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 S1752104AbeFEQnt (ORCPT + 99 others); Tue, 5 Jun 2018 12:43:49 -0400 Received: from mx2.suse.de ([195.135.220.15]:46825 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751791AbeFEQnq (ORCPT ); Tue, 5 Jun 2018 12:43:46 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from relay2.suse.de (charybdis-ext-too.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 2397DAD02; Tue, 5 Jun 2018 16:43:45 +0000 (UTC) From: Michal Suchanek To: Jonathan Corbet , Michal Suchanek , Arnd Bergmann , Frederic Weisbecker , Ingo Molnar , Aaron Wu , Tony Luck , Andrew Morton , Thomas Gleixner , "Steven Rostedt," , Laura Abbott , Dominik Brodowski , Alexey Dobriyan , Tom Lendacky , Jeffrey Hugo , Baoquan He , Ilya Matveychikov , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v10 1/5] lib/cmdline.c: Add backslash support to kernel commandline parsing. Date: Tue, 5 Jun 2018 18:43:08 +0200 Message-Id: <0005cec6acaf7ef629a64a1a72b09f68f84aae4c.1528215659.git.msuchanek@suse.de> X-Mailer: git-send-email 2.13.6 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org - add \ as quoting character in kernel parameters - generalize quoting character removal to be able to remove quoting character at any position in kernel parameter rather than start and end only This allows passing quotes in kernel arguments. It is also useful to have quoting grammar more similar to shells and bootloaders. Signed-off-by: Michal Suchanek --- - more verbose commit message - optimize final quote removal --- lib/cmdline.c | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/lib/cmdline.c b/lib/cmdline.c index 192848d880ea..aa0086dbb082 100644 --- a/lib/cmdline.c +++ b/lib/cmdline.c @@ -191,32 +191,43 @@ bool parse_option_str(const char *str, const char *option) return false; } +#define break_arg_end(i) { \ + if (isspace(args[i]) && !in_quote && !backslash) \ + break; \ + } + /* * Parse a string to get a param value pair. - * You can use " around spaces, but can't escape ". + * You can use " around spaces, and you can escape with \ * Hyphens and underscores equivalent in parameter names. */ char *next_arg(char *args, char **param, char **val, int *state_flags) { unsigned int i, equals = 0; - int in_quote = 0, quoted = 0; + int in_quote = 0, backslash = 0; char *next; - if (*args == '"') { - args++; - in_quote = 1; - quoted = 1; - } - for (i = 0; args[i]; i++) { - if (isspace(args[i]) && !in_quote) - break; - if (equals == 0) { - if (args[i] == '=') - equals = i; + break_arg_end(i); + + if ((equals == 0) && (args[i] == '=')) + equals = i; + + if (!backslash) { + if ((args[i] == '"') || (args[i] == '\\')) { + if (args[i] == '"') + in_quote = !in_quote; + if (args[i] == '\\') + backslash = 1; + + break_arg_end(i + 1); + memmove(args + 1, args, i); + args++; + i--; + } + } else { + backslash = 0; } - if (args[i] == '"') - in_quote = !in_quote; } if (state_flags) @@ -231,16 +242,7 @@ char *next_arg(char *args, char **param, char **val, int *state_flags) else { args[equals] = '\0'; *val = args + equals + 1; - - /* Don't include quotes in value. */ - if (**val == '"') { - (*val)++; - if (args[i-1] == '"') - args[i-1] = '\0'; - } } - if (quoted && args[i-1] == '"') - args[i-1] = '\0'; if (args[i]) { args[i] = '\0'; -- 2.13.6