Skip to content

Commit 40aa3ba

Browse files
Jyri Sarhakv2019i
authored andcommitted
tools: tplg_parser: fix stack buffer overflow in tplg_create_graph()
Replace unbounded strcat() calls with snprintf() that tracks remaining buffer capacity. Add a pipeline_string_size parameter to tplg_create_graph() so the function knows the buffer limit. Without this fix, a crafted topology file with many or long graph element names can overflow the caller's fixed 256-byte pipeline_string buffer via repeated strcat(), corrupting the host stack. Signed-off-by: Jyri Sarha <jyri.sarha@intel.com>
1 parent 5e82c8f commit 40aa3ba

3 files changed

Lines changed: 19 additions & 8 deletions

File tree

tools/testbench/topology_ipc3.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ static int tb_register_graph(struct tplg_context *ctx, struct tplg_comp_info *te
9898

9999
for (i = 0; i < num_connections; i++) {
100100
ret = tplg_create_graph(ctx, num_comps, pipeline_id, temp_comp_list,
101-
pipeline_string, &connection, i);
101+
pipeline_string, sizeof(pipeline_string),
102+
&connection, i);
102103
if (ret < 0)
103104
return ret;
104105

tools/tplg_parser/graph.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
/* load pipeline graph DAPM widget*/
2424
int tplg_create_graph(struct tplg_context *ctx, int count, int pipeline_id,
2525
struct tplg_comp_info *temp_comp_list, char *pipeline_string,
26+
size_t pipeline_string_size,
2627
struct sof_ipc_pipe_comp_connect *connection,
2728
int route_num)
2829
{
@@ -64,13 +65,21 @@ int tplg_create_graph(struct tplg_context *ctx, int count, int pipeline_id,
6465

6566
printf("loading route %s -> %s\n", source, sink);
6667

67-
strcat(pipeline_string, graph_elem->source);
68-
strcat(pipeline_string, "->");
69-
70-
if (route_num == (count - 1)) {
71-
strcat(pipeline_string, graph_elem->sink);
72-
strcat(pipeline_string, "\n");
73-
}
68+
size_t cur_len = strnlen(pipeline_string, pipeline_string_size);
69+
size_t remaining = pipeline_string_size > cur_len ?
70+
pipeline_string_size - cur_len : 0;
71+
int written;
72+
73+
if (route_num == (count - 1))
74+
written = snprintf(pipeline_string + cur_len, remaining,
75+
"%s->%s\n", graph_elem->source,
76+
graph_elem->sink);
77+
else
78+
written = snprintf(pipeline_string + cur_len, remaining,
79+
"%s->", graph_elem->source);
80+
81+
if (written < 0 || (size_t)written >= remaining)
82+
fprintf(stderr, "warning: pipeline string truncated\n");
7483

7584
return 0;
7685
}

tools/tplg_parser/include/tplg_parser/topology.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ int tplg_new_process(struct tplg_context *ctx, void *process, size_t process_siz
338338

339339
int tplg_create_graph(struct tplg_context *ctx, int count, int pipeline_id,
340340
struct tplg_comp_info *temp_comp_list, char *pipeline_string,
341+
size_t pipeline_string_size,
341342
struct sof_ipc_pipe_comp_connect *connection,
342343
int route_num);
343344

0 commit comments

Comments
 (0)