Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp1643697pxv; Fri, 16 Jul 2021 14:14:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzUO/wltMQ8aGUTrM3RAUz4RQermEpZebORZAbJIUMLc7EXQIhxGcmwtfBo39oDYz0mPZb9 X-Received: by 2002:a05:6638:c58:: with SMTP id g24mr10504454jal.63.1626470086848; Fri, 16 Jul 2021 14:14:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626470086; cv=none; d=google.com; s=arc-20160816; b=AS52l5vum1lUSWglK6K98Fa4EtuoIcQQdAO8U8Mwn0PkjjZvetcJDrpGstYL1uJ4OZ GiXbw+iwz4S5MYKSxMJqs4xrhWuA3eciU5+9BFViCTMo9fi2z1WpJ11ZpEJCPkDySVu6 lDOxBKh3rXRe7SnYikH+xnKzP6a5V633oNe5MWMlSYN9VP7PNJV7RJDJFkY7kBCHpm2o A4JHQ8WS+kcNFfpTrGH1ilp8CwOGjxwuEe0+TI8oTG57o1EnWgolmYK/GKzp7Ty3/d9D K1VFstVfoY8oTG0l9NLayroSVJPyYT1JQmIf5JdEmCSzXuczo+rZQfCIq8WWpmc7adZY P0TA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=wHhC/u+Tz+YyCWTdNKVYJX34gL1qM4KNQ1J0r6VozT0=; b=mACiqfwCCQcyagGIY9sCowzOqtmOiqNIWoxOkmNhQUBFpI0iQkPuWjj8i425XDbTTQ p66SO2kDlbotSSZPl1bD/2XZ7qSCoyF6tinzWOqn18/ulfmBpjMAib0/pORIFdqVAcpE 5Z6WaMpep1NEd4wjZyZJa7/MEOWjJg66jJdolwj7YldsZOYvUN4RwToXPsCmB6bkKhNw BOiLqtU7V0j2lWnYIBLGurR3YlI3MQjQvcJFUuUoSjP2D3chnEHNRD5Iq3hrKClOavdR rnpXimbeOXw1u54dmveA6UnQmBZrL1Oi1Rzlt4ZqmuxBDve0KcfXXHGGWeHhVRvHjGXV 1mpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="AGRyq/qL"; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d6si10671201ilr.154.2021.07.16.14.14.26; Fri, 16 Jul 2021 14:14:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="AGRyq/qL"; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234984AbhGPVRS (ORCPT + 99 others); Fri, 16 Jul 2021 17:17:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230084AbhGPVRR (ORCPT ); Fri, 16 Jul 2021 17:17:17 -0400 Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18571C06175F for ; Fri, 16 Jul 2021 14:14:22 -0700 (PDT) Received: by mail-pf1-x435.google.com with SMTP id j9so9949943pfc.5 for ; Fri, 16 Jul 2021 14:14:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=wHhC/u+Tz+YyCWTdNKVYJX34gL1qM4KNQ1J0r6VozT0=; b=AGRyq/qLXmbKze3lJ72pJN2QN/c76K/csTSS8PXICwMCVK/EaEAzMwZxA3o3PZ1Qtt gC0Bhr5bbahQFHLe/HTA6RMZAOcKRBBJeVVUC+uHdTDfBmW/V36F3wEFvo67Bt9rc8r3 5HwAsQBrgzKeb91PrTNi048jDDwGveiQTTWtvGKvulqf0D3cOo9WsQCK1BD78rEMgTGJ cOfgcmwQiKsnj/PXZm6smL5sk3hlLIxtkYPebjodPnBHLoBG5088hMqzEaq2LfRwsuAq bP919qg9TSZR+H2/ajzxyrUq9XVZ59DW4JTE/NKIYZ8LNzjsMBfzte6WfHhE0oFyV2rN E0Bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=wHhC/u+Tz+YyCWTdNKVYJX34gL1qM4KNQ1J0r6VozT0=; b=qhelyI8DzD5mYifjJSI/uSsSIC+T03njJbQbmVkSpiUceITCqbNuUjNlWBACDDbWZv xA28Kvip1tcDVcP5sY4n4HFj1zcedO8FJtjP6DWPU+yRABK8m46d2xw84+mS5O0sRyNh BC1tIwrwHUs65WAONyNhLB8kZ7IqWP8295b3EdpzpB+m886gf9DLO0LNLwTIiFtZs4rm 03ZiujOJxUmnG+PdetZsdvTZOsEarGDArlJlHfHo4Dfnlb3CXW4Qnjr4LgK80TAwZ5Lw NHhrEOObIahDoLiW8Lwjf0bVftFwDIP/qL7Kenb8Po+YsyVVmB2boUaFc49wgfKRZjHF JP3A== X-Gm-Message-State: AOAM532refWhQ4VGh8560+nQzy4aWQJe44CXhTeNpzk1e9kt5m1x8gVv X5MznCcupyc51f07lyeoqS69Rg== X-Received: by 2002:aa7:90cd:0:b029:333:baa9:87b7 with SMTP id k13-20020aa790cd0000b0290333baa987b7mr7471263pfk.23.1626470061392; Fri, 16 Jul 2021 14:14:21 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id b1sm12659509pjn.11.2021.07.16.14.14.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Jul 2021 14:14:20 -0700 (PDT) Date: Fri, 16 Jul 2021 21:14:17 +0000 From: Sean Christopherson To: Brijesh Singh Cc: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com Subject: Re: [PATCH Part2 RFC v4 34/40] KVM: SVM: Add support to handle Page State Change VMGEXIT Message-ID: References: <20210707183616.5620-1-brijesh.singh@amd.com> <20210707183616.5620-35-brijesh.singh@amd.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210707183616.5620-35-brijesh.singh@amd.com> Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org 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; > +}