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 2/2] optee: avoid phys_addr_t truncation in dynamic protected memory pool
Date: Sun, 6 Dec 2026 19:38:13 +0800 [thread overview]
Message-ID: <tencent_503B7C2FDA544BCD29B3FBB7AE5E059C9006@qq.com> (raw)
In-Reply-To: <20261206113813.9768-1-1045704958@qq.com>
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
prev 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 ` [PATCH 1/2] tee: avoid phys_addr_t truncation in static " Jingyi Sun
2026-12-06 11:38 ` Jingyi Sun [this message]
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_503B7C2FDA544BCD29B3FBB7AE5E059C9006@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®