Skip to content

Commit 982533e

Browse files
lyakhkv2019i
authored andcommitted
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 e1c266d commit 982533e

2 files changed

Lines changed: 34 additions & 6 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: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -528,21 +528,32 @@ static int llext_manager_mod_init(struct lib_manager_mod_ctx *ctx,
528528
ctx->mod[n_mod].llext = NULL;
529529
ctx->mod[n_mod].ebl = NULL;
530530
ctx->mod[n_mod].n_dependent = 0;
531-
ctx->mod[n_mod++].start_idx = i;
531+
ctx->mod[n_mod].start_idx = i;
532+
if (n_mod)
533+
ctx->mod[n_mod - 1].n_mod = i - ctx->mod[n_mod - 1].start_idx;
534+
n_mod++;
532535
}
533536

537+
ctx->mod[n_mod - 1].n_mod = desc->header.num_module_entries - ctx->mod[n_mod - 1].start_idx;
538+
534539
return 0;
535540
}
536541

537542
/* Find a module context, containing the driver with the supplied index */
538-
static unsigned int llext_manager_mod_find(const struct lib_manager_mod_ctx *ctx, unsigned int idx)
543+
static int llext_manager_mod_find(const struct lib_manager_mod_ctx *ctx, unsigned int idx)
539544
{
540545
unsigned int i;
541546

542547
for (i = 0; i < ctx->n_mod; i++)
543548
if (ctx->mod[i].start_idx > idx)
544549
break;
545550

551+
if (i == ctx->n_mod && ctx->mod[i - 1].start_idx + ctx->mod[i - 1].n_mod <= idx) {
552+
tr_err(&lib_manager_tr, "%u beyond %u + %u after %u", idx,
553+
ctx->mod[i - 1].start_idx, ctx->mod[i - 1].n_mod, i);
554+
return -ENOENT;
555+
}
556+
546557
return i - 1;
547558
}
548559

@@ -564,7 +575,11 @@ static int llext_manager_link_single(uint32_t module_id, const struct sof_man_fw
564575
return -EINVAL;
565576
}
566577

567-
unsigned int mod_ctx_idx = llext_manager_mod_find(ctx, entry_index);
578+
int mod_ctx_idx = llext_manager_mod_find(ctx, entry_index);
579+
580+
if (mod_ctx_idx < 0)
581+
return mod_ctx_idx;
582+
568583
struct lib_manager_module *mctx = ctx->mod + mod_ctx_idx;
569584
size_t mod_size;
570585
int i, inst_idx;
@@ -952,7 +967,11 @@ int llext_manager_add_domain(const uint32_t component_id, struct k_mem_domain *d
952967
const uint32_t module_id = IPC4_MOD_ID(component_id);
953968
struct lib_manager_mod_ctx *ctx = lib_manager_get_mod_ctx(module_id);
954969
const uint32_t entry_index = LIB_MANAGER_GET_MODULE_INDEX(module_id);
955-
const unsigned int mod_idx = llext_manager_mod_find(ctx, entry_index);
970+
const int mod_idx = llext_manager_mod_find(ctx, entry_index);
971+
972+
if (mod_idx < 0)
973+
return mod_idx;
974+
956975
struct lib_manager_module *mctx = ctx->mod + mod_idx;
957976

958977
/* FIXME: handle dependencies */
@@ -1033,7 +1052,11 @@ int llext_manager_rm_domain(const uint32_t component_id, struct k_mem_domain *do
10331052
const uint32_t module_id = IPC4_MOD_ID(component_id);
10341053
struct lib_manager_mod_ctx *ctx = lib_manager_get_mod_ctx(module_id);
10351054
const uint32_t entry_index = LIB_MANAGER_GET_MODULE_INDEX(module_id);
1036-
const unsigned int mod_idx = llext_manager_mod_find(ctx, entry_index);
1055+
const int mod_idx = llext_manager_mod_find(ctx, entry_index);
1056+
1057+
if (mod_idx < 0)
1058+
return mod_idx;
1059+
10371060
struct lib_manager_module *mctx = ctx->mod + mod_idx;
10381061

10391062
return llext_manager_rm_mod_domain(mctx, domain);
@@ -1058,7 +1081,11 @@ int llext_manager_free_module(const uint32_t component_id)
10581081
return -ENOENT;
10591082
}
10601083

1061-
unsigned int mod_idx = llext_manager_mod_find(ctx, entry_index);
1084+
int mod_idx = llext_manager_mod_find(ctx, entry_index);
1085+
1086+
if (mod_idx < 0)
1087+
return mod_idx;
1088+
10621089
struct lib_manager_module *mctx = ctx->mod + mod_idx;
10631090

10641091
/* Protected by IPC serialization */

0 commit comments

Comments
 (0)