From: Paolo Bonzini <pbonzini@redhat.com>
To: Mark Brown <broonie@kernel.org>, Shuah Khan <shuah@kernel.org>,
Haibo Xu <haibo1.xu@intel.com>,
Andrew Jones <ajones@ventanamicro.com>,
Anup Patel <anup@brainfault.org>
Cc: kvm@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH RESEND v3] KVM: selftests: Initialise dynamically allocated configuration names
Date: Wed, 13 Dec 2023 14:31:26 +0100 [thread overview]
Message-ID: <a2e25f7c-dd5b-4c88-b25a-4d8ddc8b7f29@redhat.com> (raw)
In-Reply-To: <20231211-kvm-get-reg-list-str-init-v3-1-6554c71c77b1@kernel.org>
On 12/11/23 14:08, Mark Brown wrote:
> When we dynamically generate a name for a configuration in get-reg-list
> we use strcat() to append to a buffer allocated using malloc() but we
> never initialise that buffer. Since malloc() offers no guarantees
> regarding the contents of the memory it returns this can lead to us
> corrupting, and likely overflowing, the buffer:
>
> vregs: PASS
> vregs+pmu: PASS
> sve: PASS
> sve+pmu: PASS
> vregs+pauth_address+pauth_generic: PASS
> X�vr+gspauth_addre+spauth_generi+pmu: PASS
>
> Initialise the buffer to an empty string to avoid this.
> diff --git a/tools/testing/selftests/kvm/get-reg-list.c b/tools/testing/selftests/kvm/get-reg-list.c
> index be7bf5224434..dd62a6976c0d 100644
> --- a/tools/testing/selftests/kvm/get-reg-list.c
> +++ b/tools/testing/selftests/kvm/get-reg-list.c
> @@ -67,6 +67,7 @@ static const char *config_name(struct vcpu_reg_list *c)
>
> c->name = malloc(len);
>
> + c->name[0] = '\0';
> len = 0;
> for_each_sublist(c, s) {
> if (!strcmp(s->name, "base"))
> continue;
> strcat(c->name + len, s->name);
This can be fixed just by s/strcat/strcpy/, but there's also an ugly
hidden assumption that for_each_sublist runs at least one iteration of
the loop; otherwise, the loop ends with a c->name[-1] = '\0';
> len += strlen(s->name) + 1;
> c->name[len - 1] = '+';
> }
> c->name[len - 1] = '\0';
Now this *is* a bit academic, but it remains the fact that all the
invariants are screwed up and while we're fixing it we might at least
fix it well.
So let's make the invariant that c->name[0..len-1] is initialized. Then
every write is done with either strcpy of c->name[len++] = '...'.
> ---
> base-commit: b85ea95d086471afb4ad062012a4d73cd328fa86
> change-id: 20231012-kvm-get-reg-list-str-init-76c8ed4e19d6
>
> Best regards,
prev parent reply other threads:[~2023-12-13 13:31 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-11 13:08 Mark Brown
2023-12-13 13:31 ` Paolo Bonzini [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=a2e25f7c-dd5b-4c88-b25a-4d8ddc8b7f29@redhat.com \
--to=pbonzini@redhat.com \
--cc=ajones@ventanamicro.com \
--cc=anup@brainfault.org \
--cc=broonie@kernel.org \
--cc=haibo1.xu@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=shuah@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®