From patchwork Sat Feb 17 01:34:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 202555 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp95813dyc; Fri, 16 Feb 2024 17:35:16 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU5UclQScrZQAIJ2hvzczoNac+pRxvmnPwJdlzkdOdq/NYsCdvSiKCbQIl2obZM2vukqWsdw/jFl20I3YMuyRKDXSi+cg== X-Google-Smtp-Source: AGHT+IHp4ZwFOGJyMEov0GSyV1dzSq0l0kxpOFElsN/1MRII22ho7Qudk6+JsyDAzLoqFxRzlExG X-Received: by 2002:a05:6a20:d703:b0:19c:6fb0:2b02 with SMTP id iz3-20020a056a20d70300b0019c6fb02b02mr6901802pzb.61.1708133716533; Fri, 16 Feb 2024 17:35:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708133716; cv=pass; d=google.com; s=arc-20160816; b=hw1O3AjpY7A3rdMfdxWynVv8KSkVDKScAhmjMj2Jo/t4JtrfN6yC0leP/ChOr83sni 6066P2aeia5BoZDYZRrwVEvEadSLMrVRe6ZoJ9aI5ulFbVAGKHMX/cAlVxoRJGZjdq6C eW59SUPzAXbeOzE37SabXZ1kSTv0bqzOISh8TLtvUWkbv51WZT7D0VQxEIw6EBwLjz0b sGYHSQBrrNsLASWt6Z3kpdkLNivOcuumxmaSKLjWKmOnTiHYRp3jECGcIZyC15yxEaLV HExhWbSLM02G/VU2Fcitvb9TLEoAIur5EO/E8bhh/qZpcrikxonF32bO9CDumi0Un1c0 9SwA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:reply-to:dkim-signature; bh=8V5hHcKHjHM46B61QGfRSvoByqou28ucJRHIA1LXfY4=; fh=4S5si2iqgaapFNBNbAuXgRPAz9mEENymu05qzpWbbUs=; b=no1qHpPRtw1kTLopAAxBBhXnOVNCX4LdFkvIcfASVEDRvsXY+288v3Mbks77ZYyqpi LDlpH5TgzbnGLfuQT86UosUcGWJApI4tOR1pMwxz6gHdalM8XSI/oJ+pcSuFfy2MkZRo DbGIEoH/grN2lo8Ugj9hqtQrNg06CK040P8gYQGYhEZPFcr/xoxxjHwSeu3OF6S+bv4O v7M80GtauVk26S2xS7Vaxd41ILVPjvVgbAoFplF1Z7/5IgwhK9RZp/+QzlSPUngqtkLv 2xKPf1n+HKYnFS683ERxnA1oCO7/ksNOC9aJDtq6tTxY/RJZGG6T9Ht4jhiQUHkaUFMS OUUQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=wAkhB+tE; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-69649-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69649-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id kd6-20020a17090313c600b001da1d3d7971si705055plb.447.2024.02.16.17.35.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 17:35:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-69649-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=wAkhB+tE; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-69649-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69649-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id CEBB72875E5 for ; Sat, 17 Feb 2024 01:34:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 866D01B944; Sat, 17 Feb 2024 01:34:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="wAkhB+tE" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E80D4149DFD for ; Sat, 17 Feb 2024 01:34:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708133678; cv=none; b=qN1NL0EFA9ZEuAMB5OnVTXyesQIQf1OYOqHaRWYXzIu+OJjcbdAIAb9Az5E//ssrfjP4PuwEkj1eLhrj3gYZ4eXXfK2orPi4YoDs2t3RIB9ahTkw0siYiWxMaw7a8QiZaZIDG6GCSRD3KSY470HfthUIsjgkc7CMiW9gzGn/LRU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708133678; c=relaxed/simple; bh=CCqhL6OxPySMxyCsemMfR4OsGPip9sZrMtAildgv/eo=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=o5/Vkz0K3QE+4NiAXyRL+ohkeldemeOnJkwrsnO1xefszNhrTHBxnWdpA4qmu1wiUxT3ID+CzwDl71LRXEHkqipXnr06SlLboob5bsrwEVM6yCXUfM9brT/t5hT5w4AihN9rUeNq1uN/cCgqpJ8uhrbI968ShPIdVzjGmNCWMF8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=wAkhB+tE; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-608107bd8a2so2253317b3.1 for ; Fri, 16 Feb 2024 17:34:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708133675; x=1708738475; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:reply-to:from:to:cc :subject:date:message-id:reply-to; bh=8V5hHcKHjHM46B61QGfRSvoByqou28ucJRHIA1LXfY4=; b=wAkhB+tEONLD159y9q1s+Zosg36Ov8TCx7tHzZi1AR4jTAAeNBujBvtR2UZZWYw0Ai DzMxSjjASzRXaM8yILTkC7ZJ6SswyIaOi+/QRvWoOc4qXUCANQeFvdlMnDppze3sLRUt vltV2m9mSIR0o2oRa9wXeRixwo7t0PcbG2TpcvkxA8U+nSzspj0CVhiVW4FSHPwxFXqF IQOhlaBc1eXJVWsaplRo1xARbBpzMQZTMJy3NM/Ste8iCR45wa5FWR2Kzmrxou9UHWcG kKRDj+X1Yx2iaE2yly9WijygJtYW9rj3K1eGleCyigJSKehOY/vg/aTAKX1vIqSdWzcJ LkUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708133675; x=1708738475; h=cc:to:from:subject:message-id:mime-version:date:reply-to :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8V5hHcKHjHM46B61QGfRSvoByqou28ucJRHIA1LXfY4=; b=uIXPcsJFtyBZskAhth80Pej0NgITUFoxBBUK3qlmeWuFsN5qK3pgRydYBSmWNQoBLg dH3J313r/yHOuAvDeG6q6tSRgP2FL/PYaTBrCZFYcDaen1zLWO2aguJTA6Lx3vVZ01Lo QAqOXRH2A7F1HgF2DJV/ZUu9ev5MUwVUO19CTcbv+l0xDTkg4ISO4HWCOXp8xXa9vAti N+r2+WHHih3vXvAsi/hppVBmjYmld7L7xgJzm4+GbtYjg18FrdxhmKOm8Iaj9GvummBo pLVGYOeVYwejQG+tLnSxEKNlhD/Lw4od15qWJ2ybrPgwUxVcp5RR5VjZBz4GtVwrdlnt kdIQ== X-Forwarded-Encrypted: i=1; AJvYcCUAoWokljRQEwPumGcJlHVFIScjIzN/MDtjkh3QewokUxjArEs4Q7jq1OOHUb8k7TBwlrFnXPhjcy6glp1Vwp44fqNpsPnCCyKTLfJx X-Gm-Message-State: AOJu0Yy6PlxAz1KzvbhJXv0s0f1KhdWe8yeBRdOwdVURk3B1dlR17UH6 +VlrzVoomKx/KDhPPEXuRD6rRqavUOu77cbx2htC0jrTxEh31+SJyTiNmxaDTnpxF8N4BE/OdIe HdA== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a0d:d496:0:b0:608:d1d:d8d6 with SMTP id w144-20020a0dd496000000b006080d1dd8d6mr181981ywd.7.1708133674935; Fri, 16 Feb 2024 17:34:34 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 16 Feb 2024 17:34:30 -0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240217013430.2079561-1-seanjc@google.com> Subject: [PATCH] KVM: SVM: Flush pages under kvm->lock to fix UAF in svm_register_enc_region() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Gabe Kirkpatrick , Josh Eads , Peter Gonda X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791108020131705752 X-GMAIL-MSGID: 1791108020131705752 Do the cache flush of converted pages in svm_register_enc_region() before dropping kvm->lock to fix use-after-free issues where region and/or its array of pages could be freed by a different task, e.g. if userspace has __unregister_enc_region_locked() already queued up for the region. Note, the "obvious" alternative of using local variables doesn't fully resolve the bug, as region->pages is also dynamically allocated. I.e. the region structure itself would be fine, but region->pages could be freed. Flushing multiple pages under kvm->lock is unfortunate, but the entire flow is a rare slow path, and the manual flush is only needed on CPUs that lack coherency for encrypted memory. Fixes: 19a23da53932 ("Fix unsynchronized access to sev members through svm_register_enc_region") Reported-by: Gabe Kirkpatrick Cc: Josh Eads Cc: Peter Gonda Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/sev.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) base-commit: 7455665a3521aa7b56245c0a2810f748adc5fdd4 diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index f06f9e51ad9d..cbc626dc8795 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -1981,20 +1981,22 @@ int sev_mem_enc_register_region(struct kvm *kvm, goto e_free; } - region->uaddr = range->addr; - region->size = range->size; - - list_add_tail(®ion->list, &sev->regions_list); - mutex_unlock(&kvm->lock); - /* * The guest may change the memory encryption attribute from C=0 -> C=1 * or vice versa for this memory range. Lets make sure caches are * flushed to ensure that guest data gets written into memory with - * correct C-bit. + * correct C-bit. Note, this must be done before dropping kvm->lock, + * as region and its array of pages can be freed by a different task + * once kvm->lock is released. */ sev_clflush_pages(region->pages, region->npages); + region->uaddr = range->addr; + region->size = range->size; + + list_add_tail(®ion->list, &sev->regions_list); + mutex_unlock(&kvm->lock); + return ret; e_free: