* [PATCH 1/2] tee: avoid phys_addr_t truncation in static protected memory pool
[not found] <20261206113813.9768-1-1045704958@qq.com>
@ 2026-12-06 11:38 ` Jingyi Sun
2026-12-06 11:38 ` [PATCH 2/2] optee: avoid phys_addr_t truncation in dynamic " Jingyi Sun
1 sibling, 0 replies; 2+ messages in thread
From: Jingyi Sun @ 2026-12-06 11:38 UTC (permalink / raw)
To: Jens Wiklander, Sumit Garg; +Cc: op-tee, linux-kernel, Jingyi Sun, stable
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
^ permalink raw reply [flat|nested] 2+ messages in thread* [PATCH 2/2] optee: avoid phys_addr_t truncation in dynamic protected memory pool
[not found] <20261206113813.9768-1-1045704958@qq.com>
2026-12-06 11:38 ` [PATCH 1/2] tee: avoid phys_addr_t truncation in static protected memory pool Jingyi Sun
@ 2026-12-06 11:38 ` Jingyi Sun
1 sibling, 0 replies; 2+ messages in thread
From: Jingyi Sun @ 2026-12-06 11:38 UTC (permalink / raw)
To: Jens Wiklander, Sumit Garg; +Cc: op-tee, linux-kernel, Jingyi Sun, stable
init_dyn_protmem() allocates DMA pages via tee_shm_alloc_dma_mem(), which
sets rp->protmem->paddr from page_to_phys(page) (a 64-bit phys_addr_t on
32-bit kernels with LPAE, where optee_set_dma_mask() explicitly enables
pa_width > 32). It then passes rp->protmem->paddr directly to
gen_pool_add(), and protmem_pool_op_dyn_alloc() /
protmem_pool_op_dyn_free() pass physical addresses directly through
gen_pool_alloc() and gen_pool_free().
Because gen_pool stores addresses as unsigned long and treats 0 as an
allocation failure, a dynamic protected memory pool above 4 GiB on a
32-bit LPAE kernel has its upper 32 bits truncated: allocations either
fail with -ENOMEM when the low 32 bits are zero, or return a bogus low
physical address that corrupts phys_to_page(pa) and underflows
*offs = pa - rp->protmem->paddr.
Use gen_pool_add_virt() with PAGE_SIZE as a non-zero synthetic virtual
base, obtain the full phys_addr_t via gen_pool_virt_to_phys(), and map
sg_phys(sg) back to the synthetic address on free. Also pass the aligned
size sz on the allocation error path to match gen_pool_alloc().
Fixes: 2b78d79cdf96 ("optee: FF-A: dynamic protected memory allocation")
Cc: stable@vger.kernel.org
Signed-off-by: Jingyi Sun <1045704958@qq.com>
---
drivers/tee/optee/protmem.c | 21 +++++++++++++++------
1 file changed, 15 insertions(+), 6 deletions(-)
diff --git a/drivers/tee/optee/protmem.c b/drivers/tee/optee/protmem.c
index bf2c678108df..716e2b257ec0 100644
--- a/drivers/tee/optee/protmem.c
+++ b/drivers/tee/optee/protmem.c
@@ -57,14 +57,19 @@ static int init_dyn_protmem(struct optee_protmem_dyn_pool *rp)
goto err_put_shm;
rp->protmem->flags |= TEE_SHM_DYNAMIC;
+ if (!rp->protmem->size || rp->protmem->size > ULONG_MAX - PAGE_SIZE) {
+ rc = -EINVAL;
+ goto err_reclaim;
+ }
+
rp->gen_pool = gen_pool_create(PAGE_SHIFT, -1);
if (!rp->gen_pool) {
rc = -ENOMEM;
goto err_reclaim;
}
- rc = gen_pool_add(rp->gen_pool, rp->protmem->paddr,
- rp->protmem->size, -1);
+ rc = gen_pool_add_virt(rp->gen_pool, PAGE_SIZE, rp->protmem->paddr,
+ rp->protmem->size, -1);
if (rc)
goto err_free_pool;
@@ -135,6 +140,7 @@ static int protmem_pool_op_dyn_alloc(struct tee_protmem_pool *pool,
{
struct optee_protmem_dyn_pool *rp = to_protmem_dyn_pool(pool);
size_t sz = ALIGN(size, PAGE_SIZE);
+ unsigned long va;
phys_addr_t pa;
int rc;
@@ -142,8 +148,8 @@ static int protmem_pool_op_dyn_alloc(struct tee_protmem_pool *pool,
if (rc)
return rc;
- pa = gen_pool_alloc(rp->gen_pool, sz);
- if (!pa) {
+ va = gen_pool_alloc(rp->gen_pool, sz);
+ if (!va) {
rc = -ENOMEM;
goto err_put;
}
@@ -152,12 +158,13 @@ static int protmem_pool_op_dyn_alloc(struct tee_protmem_pool *pool,
if (rc)
goto err_free;
+ pa = gen_pool_virt_to_phys(rp->gen_pool, va);
sg_set_page(sgt->sgl, phys_to_page(pa), size, 0);
*offs = pa - rp->protmem->paddr;
return 0;
err_free:
- gen_pool_free(rp->gen_pool, pa, size);
+ gen_pool_free(rp->gen_pool, va, sz);
err_put:
put_dyn_protmem(rp);
@@ -172,7 +179,9 @@ static void protmem_pool_op_dyn_free(struct tee_protmem_pool *pool,
int i;
for_each_sgtable_sg(sgt, sg, i)
- gen_pool_free(rp->gen_pool, sg_phys(sg), sg->length);
+ gen_pool_free(rp->gen_pool,
+ sg_phys(sg) - rp->protmem->paddr + PAGE_SIZE,
+ sg->length);
sg_free_table(sgt);
put_dyn_protmem(rp);
}
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread