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 7A7C3C4332F for ; Tue, 31 Oct 2023 00:18:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 18B748D000A; Mon, 30 Oct 2023 20:18:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 114A98D0008; Mon, 30 Oct 2023 20:18:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 002AA8D000A; Mon, 30 Oct 2023 20:18:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id E55818D0008 for ; Mon, 30 Oct 2023 20:18:23 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B6933C09A6 for ; Tue, 31 Oct 2023 00:18:23 +0000 (UTC) X-FDA: 81403844886.25.D25D29A Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf18.hostedemail.com (Postfix) with ESMTP id 016EC1C0015 for ; Tue, 31 Oct 2023 00:18:21 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=rvl6dOwr; spf=pass (imf18.hostedemail.com: domain of 3zUdAZQYKCI4Aws51uy66y3w.u64305CF-442Dsu2.69y@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3zUdAZQYKCI4Aws51uy66y3w.u64305CF-442Dsu2.69y@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698711502; 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=A/LRaTLkejTxBM8pIW/wY9k081He9jXDc1u8/vyc5yU=; b=fkYYd/es39+XRNnQZm36gKzXjcDAE/tx+c3/oUaJ98HbGxk5uzX8iwy2Bm7U5Ju+i0R9dX 6EV1Qz6M+4Cfq1gUELcwcl2BIBTsQCsG5kuB/jQ6tefKxtGffmwmXViY38+kxgrQ2qg9Zh RpwbuaLl8Gxiq61swdRqXK79ZBScFLg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698711502; a=rsa-sha256; cv=none; b=p1c3jnXhR4Lad3fBfUSMJYnjIJHyxhK1USiZyBb9x7UELgRP53UbLRckTWpJhRucby2jTM liB3JL0hH1fqynwYjX0z2yHkj0nZWmPDE++SmIzNTh4+A8+eeItN7R0e67msKIodIXUESF 9CaEzqeaZbJDk9csvondYEWstjqpfOU= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=rvl6dOwr; spf=pass (imf18.hostedemail.com: domain of 3zUdAZQYKCI4Aws51uy66y3w.u64305CF-442Dsu2.69y@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3zUdAZQYKCI4Aws51uy66y3w.u64305CF-442Dsu2.69y@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5af9b0850fdso45068507b3.1 for ; Mon, 30 Oct 2023 17:18:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698711501; x=1699316301; 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=A/LRaTLkejTxBM8pIW/wY9k081He9jXDc1u8/vyc5yU=; b=rvl6dOwr/Ib2X8urh7I1TNX4RdY5G30DY6gMzbEUi9IhxBFtS+uQCx9TYmvDEm48aF Ad3NQm6f4Lb9wtRAEVVsP2QtFg2FNlxBT420uh2P/6pPuqpyihB7f4d2e01hfNcn1nEr GVRWJv+M07DNkFvtv1SGgR2NNlo9JWa5QhHyW9IRFL+961HlNcL2keYOqnWORuInv27a k11fnMxU8tJRjjjNCCNogNDDa+GMAWi4GL4zu8onJoOUeIhJSknu6OiqFS5nnZD7nAFK FV87rArfLw1WDzNlaJYYPGKFDly8xVecg1i7voSBXHtGEzB9Qvb/Ksy3zln0Cc47pBQz wtyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698711501; x=1699316301; 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=A/LRaTLkejTxBM8pIW/wY9k081He9jXDc1u8/vyc5yU=; b=DcZP+dym8QP+6tyuozFewqjrxmP1jwbhd+NJ02C8TL6b9dyDSZKXV1cESTAp4KztKa e9YVzW7nsTMQ5Ct6mewR+CmdexNkmtZlPKcmwtuCu2px9GBjO7pxLUZBXbTysxA/GzXc 6QFAmWgdEHbbSvFSfvgMLkKoyokfFB8LrTbQz3OziLdtccXcoWx2ZN5B+KLUN3OD+rNP gBYUklnrX31G4wyhIlCbdZCbHXkPGAllAS/I99qAE0Prn5z5B5qW/se4IoDUXLanZaLV ZVnSB3VsQsZBOAXXMSf5hspjkV8yDewVbgcNetOoZkzVtUCp36lQjTVr/15RmpHZVGZZ xU/g== X-Gm-Message-State: AOJu0YwpvzGzr13uT+A6hGZXDk1V6FNUJeIdJtuZL0odwYmGcbTUfJLf KcYlqrkaDg0fsyC90mIIFsOQkQOz1Ss= X-Google-Smtp-Source: AGHT+IHXEBSMgWOLLItH3ZyzwVZ95nnvcf8ZeQIGcTFfJ9alEhiQZOfJ5t56nJTNUPAWz2jzQYzE0vm4h+I= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a0d:ca0f:0:b0:5a1:d329:829c with SMTP id m15-20020a0dca0f000000b005a1d329829cmr241205ywd.0.1698711501089; Mon, 30 Oct 2023 17:18:21 -0700 (PDT) Date: Mon, 30 Oct 2023 17:18:19 -0700 In-Reply-To: Mime-Version: 1.0 References: <20231027182217.3615211-1-seanjc@google.com> <20231027182217.3615211-9-seanjc@google.com> <211d093f-4023-4a39-a23f-6d8543512675@redhat.com> Message-ID: Subject: Re: [PATCH v13 08/35] KVM: Introduce KVM_SET_USER_MEMORY_REGION2 From: Sean Christopherson To: Paolo Bonzini Cc: Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexander Viro , Christian Brauner , "Matthew Wilcox (Oracle)" , Andrew Morton , 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-kernel@vger.kernel.org, Xiaoyao Li , Xu Yilun , Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , David Matlack , Yu Zhang , Isaku Yamahata , "=?utf-8?Q?Micka=C3=ABl_Sala=C3=BCn?=" , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" Content-Type: text/plain; charset="us-ascii" X-Stat-Signature: 5p8rigjnskqzifcmsryjro6cc1uemb9s X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 016EC1C0015 X-Rspam-User: X-HE-Tag: 1698711501-301593 X-HE-Meta: U2FsdGVkX18mrPeQn4aB8NS3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Xz9ds6blZHUb9dcUSUmXl4VRGqMQRwDa0HcijzUjO4KUWLmZqW0gVPxwhUQkDM+7uHJB45qsWlHE9eZ+aEkH3XnKfdQ0glLgm9/JaW7D/7ugQqCvO6/nkydkulAOMkNJcqulHX1sCyE2yKZr8AvJdJ9YlNz7I0m00Vn1uaGH9SqzFO35i374kil6OLGGtb56VA1JmYU2iRYbQy/7v0plwxezsokrBTxNX3+2pix7IDlAx3oTCNCOTHP3pGgzp9kJusgy7Tl+RtaAczo6EEcSRZ9F5DTiIMiZTGegpBahtefRCXlQ0kC88/7fyQ4j7sZB6JumUfc9unI9LTbVbOhqMev68rdHocnv86DSSOml6yw5oOR10W4C0EbRNP4xZeJaIEE8oj1+Gnp38KCNpaZcHOt4jIE7UDy5GBvN8MsveVQDWGYjl8F1tmPUw9Ocm7FYowT9/Z1Gr/5o/ZsR6drC3JQKNC1wz0d6IdHtZu0KkAfU+wdeNu163hlgZAYL5HpnZvf1+kr3jnqrT/9c76cRW6RYBunXAY8tzFwJOfE4MXI0JgMUV7A/T5PG449blfZze7RThk3uVjYzh4yUwnFPLGzWGlKLLA= 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: List-Subscribe: List-Unsubscribe: On Tue, Oct 31, 2023, Paolo Bonzini wrote: > On 10/30/23 21:25, Sean Christopherson wrote: > > > Probably worth adding a check on valid flags here. > > > > Definitely needed. There's a very real bug here. But rather than duplicate flags > > checking or plumb @ioctl all the way to __kvm_set_memory_region(), now that we > > have the fancy guard(mutex) and there are no internal calls to kvm_set_memory_region(), > > what if we: > > > > 1. Acquire/release slots_lock in __kvm_set_memory_region() > > 2. Call kvm_set_memory_region() from x86 code for the internal memslots > > 3. Disallow *any* flags for internal memslots > > 4. Open code check_memory_region_flags in kvm_vm_ioctl_set_memory_region() > > I dislike this step, there is a clear point where all paths meet > (ioctl/internal, locked/unlocked) and that's __kvm_set_memory_region(). > I think that's the place where flags should be checked. (I don't mind > the restriction on internal memslots; it's just that to me it's not a > particularly natural way to structure the checks). Yeah, I just don't like the discrepancy it causes where some flags are explicitly checked and allowed, allowed and then later disallowed. > On the other hand, the place where to protect from out-of-bounds > accesses, is the place where you stop caring about struct > kvm_userspace_memory_region vs kvm_userspace_memory_region2 (and > your code gets it right, by dropping "ioctl" as soon as possible). > > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index 87f45aa91ced..fe5a2af14fff 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -1635,6 +1635,14 @@ bool __weak kvm_arch_dirty_log_supported(struct kvm *kvm) > return true; > } > +/* > + * Flags that do not access any of the extra space of struct > + * kvm_userspace_memory_region2. KVM_SET_USER_MEMORY_REGION_FLAGS > + * only allows these. > + */ > +#define KVM_SET_USER_MEMORY_REGION_FLAGS \ Can we name this KVM_SET_USER_MEMORY_REGION_LEGACY_FLAGS, or something equally horrific? As is, this sounds way too much like a generic "allowed flags for any memory region". Or maybe invert the macro? I.e. something to make it more obvious that it's effectively a versioning check, not a generic "what's supported?" check. #define KVM_SET_USER_MEMORY_FLAGS_V2_ONLY \ (~(KVM_MEM_LOG_DIRTY_PAGES | KVM_MEM_READONLY)) > + (KVM_MEM_LOG_DIRTY_PAGES | KVM_MEM_READONLY) > + > static int check_memory_region_flags(struct kvm *kvm, > const struct kvm_userspace_memory_region2 *mem) > { > @@ -5149,10 +5149,16 @@ static long kvm_vm_ioctl(struct file *filp, > struct kvm_userspace_memory_region2 mem; > unsigned long size; > - if (ioctl == KVM_SET_USER_MEMORY_REGION) > + if (ioctl == KVM_SET_USER_MEMORY_REGION) { > + /* > + * Fields beyond struct kvm_userspace_memory_region shouldn't be > + * accessed, but avoid leaking kernel memory in case of a bug. > + */ > + memset(&mem, 0, sizeof(mem)); > size = sizeof(struct kvm_userspace_memory_region); > - else > + } else { > size = sizeof(struct kvm_userspace_memory_region2); > + } > /* Ensure the common parts of the two structs are identical. */ > SANITY_CHECK_MEM_REGION_FIELD(slot); > @@ -5165,6 +5167,11 @@ static long kvm_vm_ioctl(struct file *filp, > if (copy_from_user(&mem, argp, size)) > goto out; > + r = -EINVAL; > + if (ioctl == KVM_SET_USER_MEMORY_REGION && > + (mem->flags & ~KVM_SET_USER_MEMORY_REGION_FLAGS)) > + goto out; > + > r = kvm_vm_ioctl_set_memory_region(kvm, &mem); > break; > } > > > That's a kind of patch that you can't really get wrong (though I have > the brown paper bag ready). > > Maintainance-wise it's fine, since flags are being added at a pace of > roughly one every five years, Heh, true. > and anyway it's also future proof: I placed the #define near > check_memory_region_flags so that in five years we remember to keep it up to > date. But worst case, the new flags will only be allowed by > KVM_SET_USER_MEMORY_REGION2 unnecessarily; there are no security issues > waiting to bite us. > > In sum, this is exactly the only kind of fix that should be in the v13->v14 > delta. Boiling the ocean can be fun too ;-)