From: Jingyi Sun <1045704958@qq.com>
To: Jens Wiklander <jenswi@kernel.org>, Sumit Garg <sumit.garg@kernel.org>
Cc: op-tee@lists.trustedfirmware.org, linux-kernel@vger.kernel.org,
Jingyi Sun <1045704958@qq.com>,
stable@vger.kernel.org
Subject: [PATCH 1/2] tee: avoid phys_addr_t truncation in static protected memory pool
Date: Sun, 6 Dec 2026 19:38:12 +0800 [thread overview]
Message-ID: <tencent_808420E165D0F45BE84E71DD3D75EF2BA209@qq.com> (raw)
In-Reply-To: <20261206113813.9768-1-1045704958@qq.com>
tee_protmem_static_pool_alloc() takes a phys_addr_t base address and
registers the pool via gen_pool_add(), then protmem_pool_op_static_alloc()
and protmem_pool_op_static_free() pass physical addresses directly through
gen_pool_alloc() and gen_pool_free().
However, gen_pool tracks addresses as unsigned long (32-bit on 32-bit
kernels) and uses 0 to signal allocation failure. On 32-bit architectures
with a 64-bit phys_addr_t (such as ARM with LPAE), a pool located at or
above 4 GiB has its upper 32 bits silently truncated:
- if the low 32 bits are zero (e.g. 0x100000000), gen_pool_alloc() returns
0 and allocation fails with -ENOMEM;
- otherwise, phys_to_page(pa) maps an unrelated low-memory page and
*offs = pa - stp->pa_base underflows.
This was verified on QEMU ARM32 (virt, cortex-a15, ARM_LPAE, 6G) with
"mem=1G@0x40000000 mem=512M@0x140000000": allocating from a pool at
0x140000000 returned 0x40000000 instead.
Register the pool with gen_pool_add_virt() using PAGE_SIZE as a non-zero
synthetic virtual base (matching tee_shm_pool.c), recover the full 64-bit
physical address with gen_pool_virt_to_phys(), and convert back to the
synthetic address on free. Also reject zero-sized or overflowing ranges.
Fixes: c924c65f52c3 ("tee: implement protected DMA-heap")
Cc: stable@vger.kernel.org
Signed-off-by: Jingyi Sun <1045704958@qq.com>
---
drivers/tee/tee_heap.c | 19 ++++++++++++-------
1 file changed, 12 insertions(+), 7 deletions(-)
diff --git a/drivers/tee/tee_heap.c b/drivers/tee/tee_heap.c
index 007c00156520..db8fb2b69cac 100644
--- a/drivers/tee/tee_heap.c
+++ b/drivers/tee/tee_heap.c
@@ -399,19 +399,21 @@ static int protmem_pool_op_static_alloc(struct tee_protmem_pool *pool,
size_t *offs)
{
struct tee_protmem_static_pool *stp = to_protmem_static_pool(pool);
+ unsigned long va;
phys_addr_t pa;
int ret;
- pa = gen_pool_alloc(stp->gen_pool, size);
- if (!pa)
+ va = gen_pool_alloc(stp->gen_pool, size);
+ if (!va)
return -ENOMEM;
ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
if (ret) {
- gen_pool_free(stp->gen_pool, pa, size);
+ gen_pool_free(stp->gen_pool, va, size);
return ret;
}
+ pa = gen_pool_virt_to_phys(stp->gen_pool, va);
sg_set_page(sgt->sgl, phys_to_page(pa), size, 0);
*offs = pa - stp->pa_base;
@@ -426,7 +428,9 @@ static void protmem_pool_op_static_free(struct tee_protmem_pool *pool,
int i;
for_each_sgtable_sg(sgt, sg, i)
- gen_pool_free(stp->gen_pool, sg_phys(sg), sg->length);
+ gen_pool_free(stp->gen_pool,
+ sg_phys(sg) - stp->pa_base + PAGE_SIZE,
+ sg->length);
sg_free_table(sgt);
}
@@ -465,8 +469,9 @@ struct tee_protmem_pool *tee_protmem_static_pool_alloc(phys_addr_t paddr,
struct tee_protmem_static_pool *stp;
int rc;
- /* Check it's page aligned */
- if ((paddr | size) & page_mask)
+ /* Check it's page aligned and fits in gen_pool's synthetic range */
+ if (!size || ((paddr | size) & page_mask) ||
+ size > ULONG_MAX - PAGE_SIZE)
return ERR_PTR(-EINVAL);
if (!pfn_valid(PHYS_PFN(paddr)))
@@ -482,7 +487,7 @@ struct tee_protmem_pool *tee_protmem_static_pool_alloc(phys_addr_t paddr,
goto err_free;
}
- rc = gen_pool_add(stp->gen_pool, paddr, size, -1);
+ rc = gen_pool_add_virt(stp->gen_pool, PAGE_SIZE, paddr, size, -1);
if (rc)
goto err_free_pool;
--
2.43.0
next parent reply other threads:[~2026-12-06 11:38 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20261206113813.9768-1-1045704958@qq.com>
2026-12-06 11:38 ` Jingyi Sun [this message]
2026-12-06 11:38 ` [PATCH 2/2] optee: avoid phys_addr_t truncation in dynamic " Jingyi Sun
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=tencent_808420E165D0F45BE84E71DD3D75EF2BA209@qq.com \
--to=1045704958@qq.com \
--cc=jenswi@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=op-tee@lists.trustedfirmware.org \
--cc=stable@vger.kernel.org \
--cc=sumit.garg@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®