Skip to content

Commit fec0270

Browse files
committed
llext: add a check to llext_manager_mod_find()
Add a check to llext_manager_mod_find() in case scanning the array reached the last element, that the index indeed is within that element's range. Return an error otherwise. Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent edf8c95 commit fec0270

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

src/include/sof/lib_manager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ struct lib_manager_module {
114114
struct llext *llext; /* Zephyr loadable extension context */
115115
struct llext_buf_loader *ebl; /* Zephyr loadable extension buffer loader */
116116
unsigned int n_dependent; /* For auxiliary modules: number of dependents */
117+
unsigned int n_mod;
117118
bool mapped;
118119
bool domain_dp;
119120
struct lib_manager_segment_desc segment[LIB_MANAGER_N_SEGMENTS];

src/library_manager/llext_manager.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ static int llext_manager_link(const char *name,
473473
if (ret >= 0) {
474474
llext_get_section_info(ldr, *llext, ret, &hdr, NULL, NULL);
475475
*mod_manifest = llext_peek(ldr, hdr->sh_offset);
476+
mctx->n_mod = hdr->sh_size / sizeof(struct sof_man_module_manifest);
476477
}
477478

478479
return *buildinfo && *mod_manifest ? 0 : -EPROTO;
@@ -533,14 +534,17 @@ static int llext_manager_mod_init(struct lib_manager_mod_ctx *ctx,
533534
}
534535

535536
/* Find a module context, containing the driver with the supplied index */
536-
static unsigned int llext_manager_mod_find(const struct lib_manager_mod_ctx *ctx, unsigned int idx)
537+
static int llext_manager_mod_find(const struct lib_manager_mod_ctx *ctx, unsigned int idx)
537538
{
538539
unsigned int i;
539540

540541
for (i = 0; i < ctx->n_mod; i++)
541542
if (ctx->mod[i].start_idx > idx)
542543
break;
543544

545+
if (i == ctx->n_mod && ctx->mod[i - 1].start_idx + ctx->mod[i - 1].n_mod <= idx)
546+
return -ENOENT;
547+
544548
return i - 1;
545549
}
546550

@@ -562,7 +566,11 @@ static int llext_manager_link_single(uint32_t module_id, const struct sof_man_fw
562566
return -EINVAL;
563567
}
564568

565-
unsigned int mod_ctx_idx = llext_manager_mod_find(ctx, entry_index);
569+
int mod_ctx_idx = llext_manager_mod_find(ctx, entry_index);
570+
571+
if (mod_ctx_idx < 0)
572+
return mod_ctx_idx;
573+
566574
struct lib_manager_module *mctx = ctx->mod + mod_ctx_idx;
567575
size_t mod_size;
568576
int i, inst_idx;
@@ -950,7 +958,11 @@ int llext_manager_add_domain(const uint32_t component_id, struct k_mem_domain *d
950958
const uint32_t module_id = IPC4_MOD_ID(component_id);
951959
struct lib_manager_mod_ctx *ctx = lib_manager_get_mod_ctx(module_id);
952960
const uint32_t entry_index = LIB_MANAGER_GET_MODULE_INDEX(module_id);
953-
const unsigned int mod_idx = llext_manager_mod_find(ctx, entry_index);
961+
const int mod_idx = llext_manager_mod_find(ctx, entry_index);
962+
963+
if (mod_idx < 0)
964+
return mod_idx;
965+
954966
struct lib_manager_module *mctx = ctx->mod + mod_idx;
955967

956968
/* FIXME: handle dependencies */
@@ -1031,7 +1043,11 @@ int llext_manager_rm_domain(const uint32_t component_id, struct k_mem_domain *do
10311043
const uint32_t module_id = IPC4_MOD_ID(component_id);
10321044
struct lib_manager_mod_ctx *ctx = lib_manager_get_mod_ctx(module_id);
10331045
const uint32_t entry_index = LIB_MANAGER_GET_MODULE_INDEX(module_id);
1034-
const unsigned int mod_idx = llext_manager_mod_find(ctx, entry_index);
1046+
const int mod_idx = llext_manager_mod_find(ctx, entry_index);
1047+
1048+
if (mod_idx < 0)
1049+
return mod_idx;
1050+
10351051
struct lib_manager_module *mctx = ctx->mod + mod_idx;
10361052

10371053
return llext_manager_rm_mod_domain(mctx, domain);
@@ -1056,7 +1072,11 @@ int llext_manager_free_module(const uint32_t component_id)
10561072
return -ENOENT;
10571073
}
10581074

1059-
unsigned int mod_idx = llext_manager_mod_find(ctx, entry_index);
1075+
int mod_idx = llext_manager_mod_find(ctx, entry_index);
1076+
1077+
if (mod_idx < 0)
1078+
return mod_idx;
1079+
10601080
struct lib_manager_module *mctx = ctx->mod + mod_idx;
10611081

10621082
/* Protected by IPC serialization */

0 commit comments

Comments
 (0)