Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3138362pxj; Mon, 14 Jun 2021 15:36:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwhRD9WF4AnIrtSFhCzaJZu5KG4SVtJjUbfaAw0t4Zgjk4ddhlNgWZUcFlp7XM/gJbk5vci X-Received: by 2002:a17:906:eb17:: with SMTP id mb23mr17765994ejb.239.1623710198074; Mon, 14 Jun 2021 15:36:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623710198; cv=none; d=google.com; s=arc-20160816; b=XHtXPVCf+B+H6wi02zP/pUBxnIZM4lrQNrAn5O5RbmNkGWS0VAzfnZCKiJOJkbsKic yc62hRSbHsu7/gnmtupKx1f7vAQcD3v9d447jDI7YQQ3BO3NsYZJPIJXLj4UwCsmvrSf 5DuMFYrRdOxNbWWPCTdyloIoOhgm3hax3F4MBqrxd1DRdTwsxPz8M/rFEYAl1895nghM yzJx4XyHPIKYRI1g4/KI6kyuTtiO+1Kv9/13pjqbswc/PS2l0A0CLzekSm7+kCGrl7tZ 0LHREKFYu8K7RcEA59iWJgytxZQX5f+oiyqGTchzVP+xYJrHxazl1He3Thn/evWoxj0h XHsA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-filter; bh=ovzjhUAHZMUAXxwPnn8UpOWfyMHiT8Tau4YH/N0hSac=; b=D4GBGyRQ+L098p+3391Py7+P+QtlH7amcRyEkC8l85c3RxdbnE/fdArgKISdaaSKBO MnKN0NympwWxEYxulU7uE7WZ5pf/AJpY2+TB5ubQIG2WaoXX0LtFiuTfmRDaOp0OTXb1 bKqmgyzC6WhAkVv/z/hU4qwBkYcMe/VeQZnysq+wR4F+zyxE+XEjCVeGyjpfPHGK4SMy emXT8LjX3En1FUvy55FRJ+bYJNDRCkjQcPlEzl/JSRRXNN8Nds5BhHVghd2G+AgGlby3 TKcJQmS0tcBfwXlb2ELni5aUVpc75fXO8U6hVPMzQ7QMgmfNXUfGYopmzTuFBEXZ8RBw nTyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=jXE6leNz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h9si12296962edw.186.2021.06.14.15.36.15; Mon, 14 Jun 2021 15:36:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@linux.microsoft.com header.s=default header.b=jXE6leNz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231742AbhFNWfs (ORCPT + 99 others); Mon, 14 Jun 2021 18:35:48 -0400 Received: from linux.microsoft.com ([13.77.154.182]:56108 "EHLO linux.microsoft.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231674AbhFNWfl (ORCPT ); Mon, 14 Jun 2021 18:35:41 -0400 Received: from sequoia.work.tihix.com (162-237-133-238.lightspeed.rcsntx.sbcglobal.net [162.237.133.238]) by linux.microsoft.com (Postfix) with ESMTPSA id 422CE20B83CB; Mon, 14 Jun 2021 15:33:37 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 422CE20B83CB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1623710018; bh=ovzjhUAHZMUAXxwPnn8UpOWfyMHiT8Tau4YH/N0hSac=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jXE6leNzv4aWPewhbdj1JDdeG1hXyxsffs4eNPSWIu9paaelY2CjgKRxM0EPibOe4 uosh7V00cSO+hXQePLpS5wPajLoWBeQHvwBvzNIZcpHmZOi/t2tHQJ3H4GWdHeh66l 0osgJuKCu+0ORGhrVPiyVeejQ0iwKnluYbvSGRp4= From: Tyler Hicks To: Jens Wiklander , Allen Pais , Sumit Garg , Peter Huewe , Jarkko Sakkinen , Jason Gunthorpe , Vikas Gupta Cc: Thirupathaiah Annapureddy , Pavel Tatashin , =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= , op-tee@lists.trustedfirmware.org, linux-integrity@vger.kernel.org, bcm-kernel-feedback-list@broadcom.com, linux-mips@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 4/8] optee: Clear stale cache entries during initialization Date: Mon, 14 Jun 2021 17:33:13 -0500 Message-Id: <20210614223317.999867-5-tyhicks@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210614223317.999867-1-tyhicks@linux.microsoft.com> References: <20210614223317.999867-1-tyhicks@linux.microsoft.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The shm cache could contain invalid addresses if optee_disable_shm_cache() was not called from the .shutdown hook of the previous kernel before a kexec. These addresses could be unmapped or they could point to mapped but unintended locations in memory. Clear the shared memory cache, while being careful to not translate the addresses returned from OPTEE_SMC_DISABLE_SHM_CACHE, during driver initialization. Once all pre-cache shm objects are removed, proceed with enabling the cache so that we know that we can handle cached shm objects with confidence later in the .shutdown hook. Cc: stable@vger.kernel.org Signed-off-by: Tyler Hicks --- drivers/tee/optee/call.c | 36 ++++++++++++++++++++++++++++--- drivers/tee/optee/core.c | 9 ++++++++ drivers/tee/optee/optee_private.h | 1 + 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/drivers/tee/optee/call.c b/drivers/tee/optee/call.c index 6e6eb836e9b6..387e94768182 100644 --- a/drivers/tee/optee/call.c +++ b/drivers/tee/optee/call.c @@ -416,11 +416,13 @@ void optee_enable_shm_cache(struct optee *optee) } /** - * optee_disable_shm_cache() - Disables caching of some shared memory allocation - * in OP-TEE + * __optee_disable_shm_cache() - Disables caching of some shared memory + * allocation in OP-TEE * @optee: main service struct + * @is_mapped: true if the cached shared memory addresses were mapped by this + * kernel, are safe to dereference, and should be freed */ -void optee_disable_shm_cache(struct optee *optee) +static void __optee_disable_shm_cache(struct optee *optee, bool is_mapped) { struct optee_call_waiter w; @@ -439,6 +441,13 @@ void optee_disable_shm_cache(struct optee *optee) if (res.result.status == OPTEE_SMC_RETURN_OK) { struct tee_shm *shm; + /* + * Shared memory references that were not mapped by + * this kernel must be ignored to prevent a crash. + */ + if (!is_mapped) + continue; + shm = reg_pair_to_ptr(res.result.shm_upper32, res.result.shm_lower32); tee_shm_free(shm); @@ -449,6 +458,27 @@ void optee_disable_shm_cache(struct optee *optee) optee_cq_wait_final(&optee->call_queue, &w); } +/** + * optee_disable_shm_cache() - Disables caching of mapped shared memory + * allocations in OP-TEE + * @optee: main service struct + */ +void optee_disable_shm_cache(struct optee *optee) +{ + return __optee_disable_shm_cache(optee, true); +} + +/** + * optee_disable_unmapped_shm_cache() - Disables caching of shared memory + * allocations in OP-TEE which are not + * currently mapped + * @optee: main service struct + */ +void optee_disable_unmapped_shm_cache(struct optee *optee) +{ + return __optee_disable_shm_cache(optee, false); +} + #define PAGELIST_ENTRIES_PER_PAGE \ ((OPTEE_MSG_NONCONTIG_PAGE_SIZE / sizeof(u64)) - 1) diff --git a/drivers/tee/optee/core.c b/drivers/tee/optee/core.c index 0987074d7ed0..651d49b53d3b 100644 --- a/drivers/tee/optee/core.c +++ b/drivers/tee/optee/core.c @@ -716,6 +716,15 @@ static int optee_probe(struct platform_device *pdev) optee->memremaped_shm = memremaped_shm; optee->pool = pool; + /* + * Ensure that there are no pre-existing shm objects before enabling + * the shm cache so that there's no chance of receiving an invalid + * address during shutdown. This could occur, for example, if we're + * kexec booting from an older kernel that did not properly cleanup the + * shm cache. + */ + optee_disable_unmapped_shm_cache(optee); + optee_enable_shm_cache(optee); if (optee->sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM) diff --git a/drivers/tee/optee/optee_private.h b/drivers/tee/optee/optee_private.h index e25b216a14ef..dbdd367be156 100644 --- a/drivers/tee/optee/optee_private.h +++ b/drivers/tee/optee/optee_private.h @@ -159,6 +159,7 @@ int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session); void optee_enable_shm_cache(struct optee *optee); void optee_disable_shm_cache(struct optee *optee); +void optee_disable_unmapped_shm_cache(struct optee *optee); int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm, struct page **pages, size_t num_pages, -- 2.25.1