[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <YPH2qRkkG6m0FT2X@google.com>
Date: Fri, 16 Jul 2021 21:14:17 +0000
From: Sean Christopherson <seanjc@...gle.com>
To: Brijesh Singh <brijesh.singh@....com>
Cc: x86@...nel.org, linux-kernel@...r.kernel.org, kvm@...r.kernel.org,
linux-efi@...r.kernel.org, platform-driver-x86@...r.kernel.org,
linux-coco@...ts.linux.dev, linux-mm@...ck.org,
linux-crypto@...r.kernel.org, Thomas Gleixner <tglx@...utronix.de>,
Ingo Molnar <mingo@...hat.com>, Joerg Roedel <jroedel@...e.de>,
Tom Lendacky <thomas.lendacky@....com>,
"H. Peter Anvin" <hpa@...or.com>, Ard Biesheuvel <ardb@...nel.org>,
Paolo Bonzini <pbonzini@...hat.com>,
Vitaly Kuznetsov <vkuznets@...hat.com>,
Wanpeng Li <wanpengli@...cent.com>,
Jim Mattson <jmattson@...gle.com>,
Andy Lutomirski <luto@...nel.org>,
Dave Hansen <dave.hansen@...ux.intel.com>,
Sergio Lopez <slp@...hat.com>, Peter Gonda <pgonda@...gle.com>,
Peter Zijlstra <peterz@...radead.org>,
Srinivas Pandruvada <srinivas.pandruvada@...ux.intel.com>,
David Rientjes <rientjes@...gle.com>,
Dov Murik <dovmurik@...ux.ibm.com>,
Tobin Feldman-Fitzthum <tobin@....com>,
Borislav Petkov <bp@...en8.de>,
Michael Roth <michael.roth@....com>,
Vlastimil Babka <vbabka@...e.cz>, tony.luck@...el.com,
npmccallum@...hat.com, brijesh.ksingh@...il.com
Subject: Re: [PATCH Part2 RFC v4 34/40] KVM: SVM: Add support to handle Page
State Change VMGEXIT
On Wed, Jul 07, 2021, Brijesh Singh wrote:
> +static unsigned long snp_handle_psc(struct vcpu_svm *svm, struct ghcb *ghcb)
> +{
> + struct kvm_vcpu *vcpu = &svm->vcpu;
> + int level, op, rc = PSC_UNDEF_ERR;
> + struct snp_psc_desc *info;
> + struct psc_entry *entry;
> + gpa_t gpa;
> +
> + if (!sev_snp_guest(vcpu->kvm))
> + goto out;
> +
> + if (!setup_vmgexit_scratch(svm, true, sizeof(ghcb->save.sw_scratch))) {
> + pr_err("vmgexit: scratch area is not setup.\n");
> + rc = PSC_INVALID_HDR;
> + goto out;
> + }
> +
> + info = (struct snp_psc_desc *)svm->ghcb_sa;
> + entry = &info->entries[info->hdr.cur_entry];
Grabbing "entry" here is unnecessary and confusing.
> +
> + if ((info->hdr.cur_entry >= VMGEXIT_PSC_MAX_ENTRY) ||
> + (info->hdr.end_entry >= VMGEXIT_PSC_MAX_ENTRY) ||
> + (info->hdr.cur_entry > info->hdr.end_entry)) {
There's a TOCTOU bug here if the guest uses the GHCB instead of a scratch area.
If the guest uses the scratch area, then KVM makes a full copy into kernel memory.
But if the guest uses the GHCB, then KVM maps the GHCB into kernel address space
but doesn't make a full copy, i.e. the guest can modify the data while it's being
processed by KVM.
IIRC, Peter and I discussed the sketchiness of the GHCB mapping offline a few
times, but determined that there were no existing SEV-ES bugs because the guest
could only submarine its own emulation request. But here, it could coerce KVM
into running off the end of a buffer.
I think you can get away with capturing cur_entry/end_entry locally, though
copying the GHCB would be more robust. That would also make the code a bit
prettier, e.g.
cur = info->hdr.cur_entry;
end = info->hdr.end_entry;
> + rc = PSC_INVALID_ENTRY;
> + goto out;
> + }
> +
> + while (info->hdr.cur_entry <= info->hdr.end_entry) {
Make this a for loop?
for ( ; cur_entry < end_entry; cur_entry++)
> + entry = &info->entries[info->hdr.cur_entry];
Does this need array_index_nospec() treatment?
> + gpa = gfn_to_gpa(entry->gfn);
> + level = RMP_TO_X86_PG_LEVEL(entry->pagesize);
> + op = entry->operation;
> +
> + if (!IS_ALIGNED(gpa, page_level_size(level))) {
> + rc = PSC_INVALID_ENTRY;
> + goto out;
> + }
> +
> + rc = __snp_handle_psc(vcpu, op, gpa, level);
> + if (rc)
> + goto out;
> +
> + info->hdr.cur_entry++;
> + }
> +
> +out:
And for the copy case:
info->hdr.cur_entry = cur;
> + return rc ? map_to_psc_vmgexit_code(rc) : 0;
> +}
Powered by blists - more mailing lists