From: Markus Armbruster <armbru@redhat.com>
To: Wolfgang Bumiller <w.bumiller@proxmox.com>
Cc: P J P <ppandit@redhat.com>, Michael Tokarev <mjt@tls.msk.ru>,
qemu-devel@nongnu.org, Ling Liu <liuling-it@360.cn>
Subject: Re: [Qemu-devel] [PATCH] hmp: avoid redundant null termination of buffer
Date: Tue, 12 Jan 2016 17:00:59 +0100 [thread overview]
Message-ID: <87h9iirdms.fsf@blackfin.pond.sub.org> (raw)
In-Reply-To: <1786291473.38.33445737-8c34-4632-b55f-f565652bb5d3.open-xchange@webmail.proxmox.com> (Wolfgang Bumiller's message of "Tue, 12 Jan 2016 10:27:43 +0100 (CET)")
Wolfgang Bumiller <w.bumiller@proxmox.com> writes:
>> On January 12, 2016 at 9:45 AM Markus Armbruster <armbru@redhat.com> wrote:
>>
>> Wolfgang Bumiller <w.bumiller@proxmox.com> writes:
>>
>> > When processing 'sendkey' command, hmp_sendkey routine null
>> > terminates the 'keyname_buf' array. This results in an OOB
>>
>> Well, it technically doesn't terminate,
>
> It does for combined keys where it replaces the '-' sign (eg. ctrl-alt-f1).
>
>> > @@ -1749,6 +1749,8 @@ void hmp_sendkey(Monitor *mon, const QDict *qdict)
>> > while (1) {
>> > separator = strchr(keys, '-');
>> > keyname_len = separator ? separator - keys : strlen(keys);
>> > + if (keyname_len >= sizeof(keyname_buf))
>> > + goto err_out;
>>
>> Style nit: missing braces.
>>
>> Works because the longest member of QKeyCode_lookup[] is 13 characters,
>> and therefore truncation implies "no match". But it's not obvious.
>> No worse than before, but "you touch it, you own it".
>>
>> Options:
>>
>> * Add a comments
>>
>> char keyname_buf[16]; /* can hold any QKeyCode */
>>
>> and
>>
>> if (keyname_len >= sizeof(keyname_buf)) {
>> /* too long to match any QKeyCode */
>> goto err_out;
>> }
>>
>> * Make index_from_key() take pointer into string and size instead of a
>> string.
>
> That actually looks like a good idea.
>
>> * Get rid of the static buffer and malloc the sucker already.
>>
>> > pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
>> >
>> > /* Be compatible with old interface, convert user inputted "<" */
>> if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
>> pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
>> keyname_len = 4;
>> }
>> keyname_buf[keyname_len] = 0;
>>
>> Why keep this assignment?
>
> See above, it terminates keys when using combined keys.
You're right. We copy out up to 15 characters, then zap the '-':
separator = strchr(keys, '-');
keyname_len = separator ? separator - keys : strlen(keys);
pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
[...]
keyname_buf[keyname_len] = 0;
This is stupid. If we already know how many characters we need, we
should copy exactly those:
separator = strchr(keys, '-');
keyname_len = separator ? separator - keys : strlen(keys);
if (keyname_len >= sizeof(keyname_buf))
goto err_out;
memcpy(keyname_buf, keyname_len, keys);
keyname_buf[keyname_len] = 0;
But I'd simply dispense with the static buffer, and do something like
separator = strchr(keys, '-');
key = separator ? g_strndup(keys, separator - keys) : g_strdup(keys);
[...]
g_free(key);
This is advice, not recommendation.
>> > @@ -1800,7 +1802,7 @@ out:
>> > return;
>> >
>> > err_out:
>> > - monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
>> > + monitor_printf(mon, "invalid parameter: %s\n", keys);
>> > goto out;
>> > }
>>
>> Before your patch, the message shows the (possibly truncated) offending
>> key name. With your patch, it shows the tail of the argument starting
>> with the offending key name. Why is that an improvement?
>
> I guess that's a matter of preference and the if() can be put after the
> pstrcpy() without changing the error output.
>
>> Could use %.*s to print exactly the offending key name.
>
> Does that work on all supported platforms? (I see windows-related files
> in the code base and last time I checked it doesn't do everything.)
> If so then this + changing index_from_key() as you suggested above seems
> to be the simplest option.
git-grep -F '%.*s' shows several instances, at least one of them in
Windows code.
If we strdup the key, we can simply print it, of course.
>> What's wrong with Prasad's simple fix?
>
> See above, breaks combined keys and eg. 'ctrl-alt-f1' then errors.
Will you prepare a revised patch?
next prev parent reply other threads:[~2016-01-12 16:01 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-12-17 12:40 [Qemu-devel] [PATCH] hmp: avoid redundant null termination of buffer P J P
2015-12-18 3:46 ` 刘令
2015-12-18 4:34 ` P J P
2015-12-22 18:48 ` Luiz Capitulino
2016-01-12 8:41 ` Markus Armbruster
2016-01-08 9:19 ` Wolfgang Bumiller
2016-01-08 12:19 ` P J P
2016-01-08 13:02 ` Wolfgang Bumiller
2016-01-08 13:59 ` P J P
2016-01-08 14:38 ` Wolfgang Bumiller
2016-01-08 17:32 ` P J P
2016-01-09 9:31 ` Wolfgang Bumiller
2016-01-09 13:03 ` P J P
2016-01-10 7:56 ` Michael Tokarev
2016-01-11 7:00 ` P J P
2016-01-11 7:59 ` Wolfgang Bumiller
2016-01-11 8:22 ` P J P
2016-01-12 8:45 ` Markus Armbruster
2016-01-12 9:27 ` Wolfgang Bumiller
2016-01-12 16:00 ` Markus Armbruster [this message]
2016-01-12 16:25 ` Wolfgang Bumiller
2016-01-12 16:52 ` Markus Armbruster
2016-01-13 8:09 ` Wolfgang Bumiller
2016-01-18 13:02 ` Markus Armbruster
2016-01-18 13:38 ` Wolfgang Bumiller
2016-01-18 14:23 ` Markus Armbruster
2016-01-26 9:36 ` Michael Tokarev
2016-01-28 10:52 ` Michael Tokarev
2016-01-28 14:45 ` Markus Armbruster
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=87h9iirdms.fsf@blackfin.pond.sub.org \
--to=armbru@redhat.com \
--cc=liuling-it@360.cn \
--cc=mjt@tls.msk.ru \
--cc=ppandit@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=w.bumiller@proxmox.com \
/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.