From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CABEAEEAA7E for ; Thu, 14 Sep 2023 23:19:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 132698D0013; Thu, 14 Sep 2023 19:19:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E4AD8D0001; Thu, 14 Sep 2023 19:19:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEC138D0013; Thu, 14 Sep 2023 19:19:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id DF7808D0001 for ; Thu, 14 Sep 2023 19:19:39 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B296EC05EE for ; Thu, 14 Sep 2023 23:19:39 +0000 (UTC) X-FDA: 81236772078.05.5BB5B2F Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf30.hostedemail.com (Postfix) with ESMTP id E9E0B80010 for ; Thu, 14 Sep 2023 23:19:37 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="YUfx/Vg4"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf30.hostedemail.com: domain of 3CJUDZQsKCG8NPXReYRlgaTTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--ackerleytng.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3CJUDZQsKCG8NPXReYRlgaTTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--ackerleytng.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694733578; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4dPufiPQCtPzWz5WHSfvhHWIcWm8zH/JZg221REQo40=; b=RA5GEgguJQcx+ikxYNmfYJg9mHuk6IZhHbegwg0cc+8bKe9p9Hwm7c99OcfBSJyUYW7xAb sEPIFDt0j1uOPmLEfcqhR0KSSNfDr+3A6FLHuLTMV1/dtyxedIi2cMcf4xNdiUVPnidcwe 6Hp6UinRNRMiCVMpyobUQGXdV0OwoDU= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="YUfx/Vg4"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf30.hostedemail.com: domain of 3CJUDZQsKCG8NPXReYRlgaTTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--ackerleytng.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3CJUDZQsKCG8NPXReYRlgaTTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--ackerleytng.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694733578; a=rsa-sha256; cv=none; b=SpjHI8EJb5D0XEgGRjaV0SEdCDiqbTci+1LQT7GSrw/6FA7SKKG7qJPFD8FWwPgDIUjs3T hAll2z2NI8XK3GVJkrKwuBfi7BN+F/XqF0F7ELuPV8QD0Z4Vnw/Zk8aFpXgD4Phny4x5uv H6wJXwwRJDdMYRGUB+AE2Ta1/GUPfiQ= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d8153284d6eso1853931276.3 for ; Thu, 14 Sep 2023 16:19:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694733577; x=1695338377; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4dPufiPQCtPzWz5WHSfvhHWIcWm8zH/JZg221REQo40=; b=YUfx/Vg4o4s/u+My89hOaBJ0gtvwCF8suDDhdqnZurmXzBDqg0NFVymGo0T61LrbsY DeqR4d4sYVFRKwWX76FUwyWsV+UUTgZey2A6i+negH8I16h1qXoJzcQ/oywZWs8LEFIL sd1BSvoOPtbySa3akjEBAW57F2DpYYpiyqwRu7ubJVJldYZhu0FFnW5K5jJO4RcINHFB 9rTtWDxesLt1WneyWE1AWms0/F64+sWZKw/bYYLGThtSetsQFYBbvyqmH7Wrp3Rs+bX5 0HVBcxxravpwQWJ0ZIbntc8g7Wnj2uRZ9r3O2dKU0qcI5Gl//wx2KTZCTJkx44tJmxKT QaAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694733577; x=1695338377; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4dPufiPQCtPzWz5WHSfvhHWIcWm8zH/JZg221REQo40=; b=YKbWO4VzgyViE4yBcH/E/XDS9Gw7QzuXXWXvH/z/n5+eoVkAJs42YAA2umZFNzCYF9 DC1j5XATC75OuFtzq8fyUB/TrH427167PkPVtkD8g8wicAh00HTDrQEm2KW7I5S4/txX b5X9abVLgi6E0Lusl+GEfCUmhqYeBriCFfIpTD7i1gUVzllQIpsAuyRR7fxHsVymo1X7 UgUlTTMeUVpRMjkLC73OETxTqDfMuLlgkB/0dR3EfdBpUnhLG/x490pSSoO+jlF+hCGq xNQzNnWieSSZSdzLlUkjpISHyChkgn5Vrp+aC5lXmJMF1NUCiiVwhoMLaRMLPjIDfKiH h+YA== X-Gm-Message-State: AOJu0Yy+ov/2quxJCmWvCxqgIeCRI5bPO8uuiUzhn4Bt9S++wMxhy78w kyBC66e+oi8JQKGSk7FVek/jhg+neua4/xx5Jw== X-Google-Smtp-Source: AGHT+IGXEzZPfzBJslkSHoMTui4zDBSgnXtUGummxt07D80J/zY1rCAlKy2IgGo0zSECxNr/ULgwmLbLuPQoPz0S1A== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a25:aa83:0:b0:d77:984e:c770 with SMTP id t3-20020a25aa83000000b00d77984ec770mr158034ybi.5.1694733576882; Thu, 14 Sep 2023 16:19:36 -0700 (PDT) Date: Thu, 14 Sep 2023 23:19:34 +0000 In-Reply-To: Mime-Version: 1.0 References: Message-ID: Subject: Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory From: Ackerley Tng To: Sean Christopherson Cc: pbonzini@redhat.com, maz@kernel.org, oliver.upton@linux.dev, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, willy@infradead.org, akpm@linux-foundation.org, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, chao.p.peng@linux.intel.com, tabba@google.com, jarkko@kernel.org, yu.c.zhang@linux.intel.com, vannapurve@google.com, mail@maciej.szmigiero.name, vbabka@suse.cz, david@redhat.com, qperret@google.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com Content-Type: text/plain; charset="UTF-8" X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: E9E0B80010 X-Stat-Signature: k6knonajjkth13qujw4rftcnicsgtiy8 X-HE-T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juQmeUoIMgP2k2Pn3JjIiOOTLvnDbUXG36rhRd1sT8xsAzddF88bxJLUs6J2NpU+6viR596JMs8dRZp+oIgFF/5sQMPJy78A8HtlmBNrUJrhJriYtSkmhERocdWWh2qYP8HQdd/PIzrVTWSazt0caYsjmuUwOdk3BjxGES2tflu+9HriTbSx7gmX9O1/oFRuFePFThIWc8nkMpIRZtlb+35jKtV8xqVDrzUCxQUGBLCVP9ENomXRux8wlPL5xBRvH+vHWY9YPAU8u7VRqC4JaixYmWGBTt9QyUY6n8yb97qsnDB3J/wxLgnQkF1Is4JE411/VFoqnhSAYi8tDgmTyjtGhFgrCERY7xcDyVlhYQM0bIIbmpicL5cLyCTf27hNDK5arhtQ8hP6O9iVy+06+opNQaWsB8au/IXCO9UKj6FlLzFEO2lkdAmgeSweTnmAcXdtwIfstJYgYgTYXx2JWRwcOWZg9K1DY0la21FpUxrTVLd9hXG+aSJg4neKVDrfYbg4hoKOIIS8PM8ZQ9LfshZijzYMganypA5xeY2bLaGet1haWo0Je6zm8TpTLHQrGEiC7YvaDYR7MteYmmsR6aODfKBk4OzZKAEC94gztJVa1yEVJHcc903+XMou+oMRVXSx4Kj8nF/5fnNCmugWokfs+5T1ElxCsm7Lvl3+IddeOWRy04HEUVmz4dCLaKn1uly07ya+mvb4gj5CArPNT2BsSw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Sean Christopherson writes: > On Mon, Aug 28, 2023, Ackerley Tng wrote: >> Sean Christopherson writes: >> >> If we track struct kvm with the inode, then I think (a), (b) and (c) can >> >> be independent of the refcounting method. What do you think? >> > >> > No go. Because again, the inode (physical memory) is coupled to the virtual machine >> > as a thing, not to a "struct kvm". Or more concretely, the inode is coupled to an >> > ASID or an HKID, and there can be multiple "struct kvm" objects associated with a >> > single ASID. And at some point in the future, I suspect we'll have multiple KVM >> > objects per HKID too. >> > >> > The current SEV use case is for the migration helper, where two KVM objects share >> > a single ASID (the "real" VM and the helper). I suspect TDX will end up with >> > similar behavior where helper "VMs" can use the HKID of the "real" VM. For KVM, >> > that means multiple struct kvm objects being associated with a single HKID. >> > >> > To prevent use-after-free, KVM "just" needs to ensure the helper instances can't >> > outlive the real instance, i.e. can't use the HKID/ASID after the owning virtual >> > machine has been destroyed. >> > >> > To put it differently, "struct kvm" is a KVM software construct that _usually_, >> > but not always, is associated 1:1 with a virtual machine. >> > >> > And FWIW, stashing the pointer without holding a reference would not be a complete >> > solution, because it couldn't guard against KVM reusing a pointer. E.g. if a >> > struct kvm was unbound and then freed, KVM could reuse the same memory for a new >> > struct kvm, with a different ASID/HKID, and get a false negative on the rebinding >> > check. >> >> I agree that inode (physical memory) is coupled to the virtual machine >> as a more generic concept. >> >> I was hoping that in the absence of CC hardware providing a HKID/ASID, >> the struct kvm pointer could act as a representation of the "virtual >> machine". You're definitely right that KVM could reuse a pointer and so >> that idea doesn't stand. >> >> I thought about generating UUIDs to represent "virtual machines" in the >> absence of CC hardware, and this UUID could be transferred during >> intra-host migration, but this still doesn't take host userspace out of >> the TCB. A malicious host VMM could just use the migration ioctl to copy >> the UUID to a malicious dumper VM, which would then pass checks with a >> gmem file linked to the malicious dumper VM. This is fine for HKID/ASIDs >> because the memory is encrypted; with UUIDs there's no memory >> encryption. > > I don't understand what problem you're trying to solve. I don't see a need to > provide a single concrete representation/definition of a "virtual machine". E.g. > there's no need for a formal definition to securely perform intrahost migration, > KVM just needs to ensure that the migration doesn't compromise guest security, > functionality, etc. > > That gets a lot more complex if the target KVM instance (module, not "struct kvm") > is a different KVM, e.g. when migrating to a different host. Then there needs to > be a way to attest that the target is trusted and whatnot, but that still doesn't > require there to be a formal definition of a "virtual machine". > >> Circling back to the original topic, was associating the file with >> struct kvm at gmem file creation time meant to constrain the use of the >> gmem file to one struct kvm, or one virtual machine, or something else? > > It's meant to keep things as simple as possible (relatively speaking). A 1:1 > association between a KVM instance and a gmem instance means we don't have to > worry about the edge cases and oddities I pointed out earlier in this thread. > I looked through this thread again and re-read the edge cases and oddities that was pointed out earlier (last paragraph at [1]) and I think I understand better, and I have just one last clarification. It was previously mentioned that binding on creation time simplifies the lifecycle of memory: "(a) prevent a different VM from *ever* binding to the gmem instance" [1] Does this actually mean "prevent a different struct kvm from *ever* binding to this gmem file" ? If so, then binding on creation + Makes the gmem *file* (and just not the bindings xarray) the binding between struct kvm and the file. + Simplifies the KVM-userspace contract to "this gmem file can only be used with this struct kvm" Binding on creation doesn't offer any way to block the contents of the inode from being used with another "virtual machine" though, since we can have more than one gmem file pointing to the same inode, and the other gmem file is associated with another struct kvm. (And a strut kvm isn't associated 1:1 with a virtual machine [2]) The point about an inode needing to be coupled to a virtual machine as a thing [2] led me to try to find a single concrete representation of a "virtual machine". Is locking inode contents to a "virtual machine" outside the scope of gmem? If so, then it is fine to bind on creation time, use a VM ioctl over a system ioctl, and the method of refcounting in gmem v12 is okay. [1] https://lore.kernel.org/lkml/ZNKv9ul2I7A4V7IF@google.com/ [2] https://lore.kernel.org/lkml/ZOO782YGRY0YMuPu@google.com/ >