Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2283933iof; Wed, 8 Jun 2022 01:23:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzy4y6WIR5/WLuZCGx/y3PN/BxwwV16XDSvyrF13eUQyx88x+VLX5/34VGYelrNb6ksl6DG X-Received: by 2002:a17:902:f7cf:b0:165:13ac:cac8 with SMTP id h15-20020a170902f7cf00b0016513accac8mr33823194plw.20.1654676628566; Wed, 08 Jun 2022 01:23:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654676628; cv=none; d=google.com; s=arc-20160816; b=BLuQOy80euZeVofO/AZXI29yhJCvsekFjAYPeRxPOfQx4aL/dyf/Z+oZEt1/Bn5xtF JSnS4fqftpS3+HG8E9qq2qzSgg7jqfOLQv6hHdApn5PLZoNC3eg7DCpP1SKNn4L0iNxY 17e2PjLrtM8ilX9lPq74fF3n+e8egb3S8UYIf3wSZkbiA0K4Sd0dt1E2t2BBUb0T+Tx9 JJd0z/B7ARVollnX+UCIj2O3XP/91LTn3Q17lQ0j/QV05+eZSXE/k1AcGUIlyGLG5pwO 5DBEpYZ5Y1SpLU35P6rFBE+y9/bxMUSzmrt6ChIMDG8Oei6xkKRH39OdiRa/LIpTpZSB A4aQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=8XglOtHSoJkBsQzuTP3DXx27/diWkDTW23Svt5U2SyQ=; b=KK9DKKRGlLeNohTYHs9tJjLy81vPS5SUi46/ctrlAN070Dj0IByINre1SLthgwWjW0 a5qu4mDA4lL+y1htWJJ9eX7Ia6Y0u18DLc++gQQVz2cFsnWZ+cyyu4hC4jYT9I8ZwnMJ lXHZEUY2ycmZOdhSlBqhru7m7XHDzV3PS6GDFlkwGncdSBxdNN5A8R8hyGmtP6uCK9U0 H9TwKKklaU86jLKcyYnDNWJBlD6TOBXOEzDXqx2bdhEd+ZlyGnPTxLHLxHsA4ZiDeR4a /WHIVz3O4hVDdkFbTe9+5Lipee7Rxv6PCHy1CtLLcdJT7T4M8sJS+pjPOVqpuNBUU9ue Xupw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rGgvXs7Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id by10-20020a056a00400a00b0051c50aa4266si2445143pfb.41.2022.06.08.01.23.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 01:23:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rGgvXs7Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9908711E481; Wed, 8 Jun 2022 00:54:49 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378110AbiFHBZT (ORCPT + 99 others); Tue, 7 Jun 2022 21:25:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382883AbiFGVwC (ORCPT ); Tue, 7 Jun 2022 17:52:02 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E95C23F234; Tue, 7 Jun 2022 12:09:53 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 58017617DA; Tue, 7 Jun 2022 19:09:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 62AD8C385A2; Tue, 7 Jun 2022 19:09:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654628992; bh=6+X3r+SmwZkR8Bjt3sCvukd/lbvONYbu3LJTaLIiXWY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rGgvXs7YFnGKyHolotee7YLspmwcWqp5s41EV7yHgSOZW2uTZPmQs20a5nj0W37Tf bFOb7V2UUWOGUL3lT1tVzcQOIs+NiyF3gTz6iOpWwQAyMLGNGHr0X1X2kSXIuOFXTj bgh3S/Y76JqJ8m5hSKaObjkY+0OjWtnJaua58OiM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Robin Murphy , Christoph Hellwig , David Rientjes , Sasha Levin Subject: [PATCH 5.18 529/879] dma-direct: dont over-decrypt memory Date: Tue, 7 Jun 2022 19:00:47 +0200 Message-Id: <20220607165018.230271846@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607165002.659942637@linuxfoundation.org> References: <20220607165002.659942637@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Robin Murphy [ Upstream commit 4a37f3dd9a83186cb88d44808ab35b78375082c9 ] The original x86 sev_alloc() only called set_memory_decrypted() on memory returned by alloc_pages_node(), so the page order calculation fell out of that logic. However, the common dma-direct code has several potential allocators, not all of which are guaranteed to round up the underlying allocation to a power-of-two size, so carrying over that calculation for the encryption/decryption size was a mistake. Fix it by rounding to a *number* of pages, rather than an order. Until recently there was an even worse interaction with DMA_DIRECT_REMAP where we could have ended up decrypting part of the next adjacent vmalloc area, only averted by no architecture actually supporting both configs at once. Don't ask how I found that one out... Fixes: c10f07aa27da ("dma/direct: Handle force decryption for DMA coherent buffers in common code") Signed-off-by: Robin Murphy Signed-off-by: Christoph Hellwig Acked-by: David Rientjes Signed-off-by: Sasha Levin --- kernel/dma/direct.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c index 3e7f4aab740e..e978f36e6be8 100644 --- a/kernel/dma/direct.c +++ b/kernel/dma/direct.c @@ -79,7 +79,7 @@ static int dma_set_decrypted(struct device *dev, void *vaddr, size_t size) { if (!force_dma_unencrypted(dev)) return 0; - return set_memory_decrypted((unsigned long)vaddr, 1 << get_order(size)); + return set_memory_decrypted((unsigned long)vaddr, PFN_UP(size)); } static int dma_set_encrypted(struct device *dev, void *vaddr, size_t size) @@ -88,7 +88,7 @@ static int dma_set_encrypted(struct device *dev, void *vaddr, size_t size) if (!force_dma_unencrypted(dev)) return 0; - ret = set_memory_encrypted((unsigned long)vaddr, 1 << get_order(size)); + ret = set_memory_encrypted((unsigned long)vaddr, PFN_UP(size)); if (ret) pr_warn_ratelimited("leaking DMA memory that can't be re-encrypted\n"); return ret; -- 2.35.1