Skip to content

Commit 2f7df9d

Browse files
committed
core: tee_ree_fs: preserve physical offsets on 32-bit builds
REE FS backing file offsets can exceed 4 GiB within the valid logical position range. However, get_offs_size() stored physical offsets in size_t. On 32-bit builds, offsets above SIZE_MAX (0xffffffff) therefore wrapped before being passed to the 64-bit read and write RPC interface. The truncate length could wrap in the same way. Calculate physical offsets with 64-bit arithmetic and carry them as tee_fs_off_t through read, write, and truncate RPC requests. Fixes: a238b74 ("core: REE FS: use the new hash tree interface") Signed-off-by: Sungbae Yoo <sungbaey@nvidia.com>
1 parent 449a9b9 commit 2f7df9d

3 files changed

Lines changed: 16 additions & 12 deletions

File tree

core/include/tee/tee_fs_rpc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ TEE_Result tee_fs_rpc_write_init(struct tee_fs_rpc_operation *op,
4343
TEE_Result tee_fs_rpc_write_final(struct tee_fs_rpc_operation *op);
4444

4545

46-
TEE_Result tee_fs_rpc_truncate(uint32_t id, int fd, size_t len);
46+
TEE_Result tee_fs_rpc_truncate(uint32_t id, int fd, tee_fs_off_t len);
4747
TEE_Result tee_fs_rpc_remove_dfh(uint32_t id,
4848
const struct tee_fs_dirfile_fileh *dfh);
4949
#endif /* __TEE_TEE_FS_RPC_H */

core/tee/tee_fs_rpc.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ TEE_Result tee_fs_rpc_write_final(struct tee_fs_rpc_operation *op)
177177
return operation_commit(op);
178178
}
179179

180-
TEE_Result tee_fs_rpc_truncate(uint32_t id, int fd, size_t len)
180+
TEE_Result tee_fs_rpc_truncate(uint32_t id, int fd, tee_fs_off_t len)
181181
{
182182
struct tee_fs_rpc_operation op = {
183183
.id = id, .num_params = 1, .params = {
@@ -186,6 +186,9 @@ TEE_Result tee_fs_rpc_truncate(uint32_t id, int fd, size_t len)
186186
}
187187
};
188188

189+
if (len < 0)
190+
return TEE_ERROR_BAD_PARAMETERS;
191+
189192
return operation_commit(&op);
190193
}
191194

core/tee/tee_ree_fs.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,13 @@ static TEE_Result out_of_place_write(struct tee_fs_fd *fdp, size_t pos,
142142
}
143143

144144
static TEE_Result get_offs_size(enum tee_fs_htree_type type, size_t idx,
145-
uint8_t vers, size_t *offs, size_t *size)
145+
uint8_t vers, tee_fs_off_t *offs, size_t *size)
146146
{
147147
const size_t node_size = sizeof(struct tee_fs_htree_node_image);
148148
const size_t block_nodes = BLOCK_SIZE / (node_size * 2);
149-
size_t pbn;
150-
size_t bidx;
149+
const uint64_t idx64 = idx;
150+
uint64_t pbn = 0;
151+
uint64_t bidx = 0;
151152

152153
assert(vers == 0 || vers == 1);
153154

@@ -207,14 +208,14 @@ static TEE_Result get_offs_size(enum tee_fs_htree_type type, size_t idx,
207208
*size = sizeof(struct tee_fs_htree_image);
208209
return TEE_SUCCESS;
209210
case TEE_FS_HTREE_TYPE_NODE:
210-
pbn = 1 + ((idx / block_nodes) * block_nodes * 2);
211+
pbn = 1 + (idx64 / block_nodes) * block_nodes * 2;
211212
*offs = pbn * BLOCK_SIZE +
212-
2 * node_size * (idx % block_nodes) +
213+
2 * node_size * (idx64 % block_nodes) +
213214
node_size * vers;
214215
*size = node_size;
215216
return TEE_SUCCESS;
216217
case TEE_FS_HTREE_TYPE_BLOCK:
217-
bidx = 2 * idx + vers;
218+
bidx = 2 * idx64 + vers;
218219
pbn = 2 + bidx + bidx / (block_nodes * 2 - 1);
219220
*offs = pbn * BLOCK_SIZE;
220221
*size = BLOCK_SIZE;
@@ -231,7 +232,7 @@ static TEE_Result ree_fs_rpc_read_init(void *aux,
231232
{
232233
struct tee_fs_fd *fdp = aux;
233234
TEE_Result res;
234-
size_t offs;
235+
tee_fs_off_t offs = 0;
235236
size_t size;
236237

237238
res = get_offs_size(type, idx, vers, &offs, &size);
@@ -249,7 +250,7 @@ static TEE_Result ree_fs_rpc_write_init(void *aux,
249250
{
250251
struct tee_fs_fd *fdp = aux;
251252
TEE_Result res;
252-
size_t offs;
253+
tee_fs_off_t offs = 0;
253254
size_t size;
254255

255256
res = get_offs_size(type, idx, vers, &offs, &size);
@@ -287,7 +288,7 @@ static TEE_Result ree_fs_ftruncate_internal(struct tee_fs_fd *fdp,
287288
if (res != TEE_SUCCESS)
288289
return res;
289290
} else {
290-
size_t offs;
291+
tee_fs_off_t offs = 0;
291292
size_t sz;
292293

293294
res = get_offs_size(TEE_FS_HTREE_TYPE_BLOCK,
@@ -302,7 +303,7 @@ static TEE_Result ree_fs_ftruncate_internal(struct tee_fs_fd *fdp,
302303
return res;
303304

304305
res = tee_fs_rpc_truncate(OPTEE_RPC_CMD_FS, fdp->fd,
305-
offs + sz);
306+
offs + (tee_fs_off_t)sz);
306307
if (res != TEE_SUCCESS)
307308
return res;
308309

0 commit comments

Comments
 (0)