Skip to content

Commit

Permalink
sof-logger: ensure NULL string is not passed to printf/fprintf
Browse files Browse the repository at this point in the history
Due to allocation failures, or invalid content in dictionary,
"params" entries in "struct proc_ldc_entry" may be NULL.

In print_entry_params(), the NULL entries may be passed
as arguments to fprintf(). While e.g. glibc handles these without
error, this is not guaranteed behaviour and may result in segfault
on some platforms.

Fix the issue by aborting program if allocation fails and
explicitly handling the cases when asprintf_entry_text returns
NULL.

Signed-off-by: Kai Vehmanen <[email protected]>
(cherry picked from commit 1a7a36a)
  • Loading branch information
kv2019i authored and marc-hb committed Sep 20, 2023
1 parent 8b5def3 commit ace183c
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions tools/logger/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ static const char *BAD_PTR_STR = "<bad uid ptr 0x%.8x>";
#define UUID_LOWER "%s%s%s<%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x>%s%s%s"
#define UUID_UPPER "%s%s%s<%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X>%s%s%s"

static const char *missing = "<missing>";

static int read_entry_from_ldc_file(struct ldc_entry *entry, uint32_t log_entry_address);

char *format_uid_raw(const struct sof_uuid_entry *uid_entry, int use_colors, int name_first,
Expand Down Expand Up @@ -249,6 +251,8 @@ static void process_params(struct proc_ldc_entry *pe,
/* check for string printing, because it leads to logger crash */
log_err("String printing is not supported\n");
pe->params[i] = (uintptr_t)log_asprintf("<String @ 0x%08x>", raw_param);
if (!pe->params[i])
abort();
pe->subst_mask |= 1 << i;
++i;
p += 2;
Expand All @@ -257,6 +261,8 @@ static void process_params(struct proc_ldc_entry *pe,
/* substitute UUID entry address with formatted string pointer from heap */
pe->params[i] = (uintptr_t)asprintf_uuid(p, raw_param, use_colors,
&uuid_fmt_len);
if (!pe->params[i])
abort();
pe->subst_mask |= 1 << i;
++i;
/* replace uuid formatter with %s */
Expand All @@ -268,7 +274,12 @@ static void process_params(struct proc_ldc_entry *pe,
/* %pQ format specifier */
/* substitute log entry address with formatted entry text */
pe->params[i] = (uintptr_t)asprintf_entry_text(raw_param);
pe->subst_mask |= 1 << i;

if (pe->params[i])
pe->subst_mask |= 1 << i;
else
pe->params[i] = (uintptr_t)missing;

++i;

/* replace entry formatter with %s */
Expand Down Expand Up @@ -1021,7 +1032,7 @@ static int dump_ldc_info(void)
while (remaining > 0) {
name = format_uid_raw(&uid_ptr[cnt], 0, 0, false, false);
uid_addr = get_uuid_key(&uid_ptr[cnt]);
fprintf(out_fd, "\t%p %s\n", (void *)uid_addr, name);
fprintf(out_fd, "\t%p %s\n", (void *)uid_addr, name ? name : missing);

if (name) {
free(name);
Expand Down

0 comments on commit ace183c

Please sign in to comment.