From: Rusty Russell <rusty@rustcorp.com.au>
To: Arthur Gautier <baloo@gandi.net>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH] param: fixup quote parsing of kernel arguments
Date: Wed, 15 Apr 2015 10:24:24 +0930 [thread overview]
Message-ID: <87pp76rz27.fsf@rustcorp.com.au> (raw)
In-Reply-To: <20150408094401.GA10714@khany.gandi.net>
Arthur Gautier <baloo@gandi.net> writes:
> On Wed, Apr 08, 2015 at 03:29:43PM +0930, Rusty Russell wrote:
>> Arthur Gautier <baloo@gandi.net> writes:
>> > When starting kernel with arguments like:
>> > init=/bin/sh -c "echo arguments"
>> > the trailing double quote is not removed which results in following command
>> > being executed:
>> > /bin/sh -c 'echo arguments"'
>> >
>> > This commit removes the trailing double quote.
>> >
>> > Signed-off-by: Arthur Gautier <baloo@gandi.net>
>>
>> Hi Arthur,
>>
>> Thanks, I'd not considered quotes outside '='. But this
>> fixes it in a weird way: we handle quotes below, we just don't do
>> anything for the "raw value" case:
>>
>> for (i = 0; args[i]; i++) {
>> if (isspace(args[i]) && !in_quote)
>> break;
>> if (equals == 0) {
>> if (args[i] == '=')
>> equals = i;
>> }
>> if (args[i] == '"')
>> in_quote = !in_quote;
>> }
>>
>> *param = args;
>> if (!equals)
>> *val = NULL;
>> 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';
>> }
>>
>> The logical fix is to just always remove the close quotes in both
>> cases:
>>
>> diff --git a/kernel/params.c b/kernel/params.c
>> index 728e05b167de..a22d6a759b1a 100644
>> --- a/kernel/params.c
>> +++ b/kernel/params.c
>> @@ -173,9 +173,9 @@ static char *next_arg(char *args, char **param, char **val)
>> if (args[i-1] == '"')
>> args[i-1] = '\0';
>> }
>> - if (quoted && args[i-1] == '"')
>> - args[i-1] = '\0';
>> }
>> + if (quoted && args[i-1] == '"')
>> + args[i-1] = '\0';
>>
>> if (args[i]) {
>> args[i] = '\0';
>>
>> Does this work for you?
>>
>
> Hi Rusty,
>
> This does indeed fixes my issue and I agree with the fix but I've also
> noticed a problem when parsing commands like:
>
> char * input = "var0=\"val=ue\" \"var1\"=value \"var2=value\" \"echo foo\"";
> char buf[255];
> char *args = buf;
> char * param, *val;
>
> memcpy(buf, input, strlen(input)+1);
>
> while(*args)
> {
> args = next_arg(args, ¶m, &val);
> printf("%s=%s\n", param, val);
> }
>
> This parses commandline like:
>
> var0=val=ue
> var1"=value
> var2=value
> echo foo=(null)
>
> As you may notice when using doublequote for keys, the final doublequote
> is not removed. I'm not sure this should be considered as a problem nor
> this should be expected but my patch was fixing this issue as well.
Indeed. And the following isn't parse correctly either:
foo="one ""two"
Though it *looks* like the code handles this, it doesn't: you'll get
'one ""two'.
Your first case was interesting because it was a more practical example.
I've applied my simple fix for now; if you want to do more thorough quote
handling (ie. use memmove), I'd love to see patches.
Thanks!
Rusty.
prev parent reply other threads:[~2015-04-15 3:32 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-04-07 11:20 [PATCH] param: fixup quote parsing of kernel arguments Arthur Gautier
2015-04-08 5:59 ` Rusty Russell
2015-04-08 9:44 ` Arthur Gautier
2015-04-15 0:54 ` Rusty Russell [this message]
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=87pp76rz27.fsf@rustcorp.com.au \
--to=rusty@rustcorp.com.au \
--cc=baloo@gandi.net \
--cc=linux-kernel@vger.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.