Skip to contentSkip to conversation

perf disasm: Fix potential use-after-free on fileloc

PATCH Superseded Ian Rogers 2 participants 3 messages Mar 6 – Mar 21 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf disasm: Fix potential use-after-free on fileloc

The fileloc is a copy of a pointer to a string but in places like symbol_disassemble__llvm this string appears to be freed setting up potential use-after-frees:

llvm.c:

```
		dl = disasm_line__new(args);
		if (dl == NULL)
			goto err;

		annotation_line__add(&dl->al, &notes->src->source);

		free(args->fileloc);
```
disasm.c:
```
static void annotation_line__init(struct annotation_line *al,
				  struct annotate_args *args,
				  int nr)
{
	al->offset = args->offset;
	al->line = strdup(args->line);
	al->line_nr = args->line_nr;
	al->fileloc = args->fileloc;
	al->data_nr = nr;
}

struct disasm_line *disasm_line__new(struct annotate_args *args)
{
	struct disasm_line *dl = NULL;
	struct annotation *notes = symbol__annotation(args->ms->sym);
	int nr = notes->src->nr_events;

	dl = zalloc(disasm_line_size(nr));
	if (!dl)
		return NULL;

	annotation_line__init(&dl->al, args, nr);
```

Fix this by making the fileloc a copy of the underlying string in its init/exit.

---
 tools/perf/util/disasm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/disasm.c +2 -1
index ddcc488f2e5f..b83bc14f82e1 100644
@@ -908,13 +908,14 @@ static void annotation_line__init(struct annotation_line *al,
 	al->offset = args->offset;
 	al->line = strdup(args->line);
 	al->line_nr = args->line_nr;
-	al->fileloc = args->fileloc;
+	al->fileloc = args->fileloc ? strdup(args->fileloc) : NULL;
 	al->data_nr = nr;
 }
 
 static void annotation_line__exit(struct annotation_line *al)
 {
 	zfree_srcline(&al->path);
+	zfree(&al->fileloc);
 	zfree(&al->line);
 	zfree(&al->cycles);
 	zfree(&al->br_cntr);
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
author reviewer
#1 lore raw
Quoted 41 lines · Ian Rogers

The fileloc is a copy of a pointer to a string but in places like symbol_disassemble__llvm this string appears to be freed setting up potential use-after-frees:

llvm.c:

```
                dl = disasm_line__new(args);
                if (dl == NULL)
                        goto err;

                annotation_line__add(&dl->al, &notes->src->source);

                free(args->fileloc);
```
disasm.c:
```
static void annotation_line__init(struct annotation_line *al,
                                  struct annotate_args *args,
                                  int nr)
{
        al->offset = args->offset;
        al->line = strdup(args->line);
        al->line_nr = args->line_nr;
        al->fileloc = args->fileloc;
        al->data_nr = nr;
}

struct disasm_line *disasm_line__new(struct annotate_args *args)
{
        struct disasm_line *dl = NULL;
        struct annotation *notes = symbol__annotation(args->ms->sym);
        int nr = notes->src->nr_events;

        dl = zalloc(disasm_line_size(nr));
        if (!dl)
                return NULL;

        annotation_line__init(&dl->al, args, nr);
```
Quoted 4 lines

Fix this by making the fileloc a copy of the underlying string in its init/exit.

Ping.

Thanks,
Ian

Quoted 27 lines
---
 tools/perf/util/disasm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/disasm.c +2 -2
index ddcc488f2e5f..b83bc14f82e1 100644
@@ -908,13 +908,14 @@ static void annotation_line__init(struct annotation_line *al,
        al->offset = args->offset;
        al->line = strdup(args->line);
        al->line_nr = args->line_nr;
-       al->fileloc = args->fileloc;
+       al->fileloc = args->fileloc ? strdup(args->fileloc) : NULL;
        al->data_nr = nr;
 }
 
 static void annotation_line__exit(struct annotation_line *al)
 {
        zfree_srcline(&al->path);
+       zfree(&al->fileloc);
        zfree(&al->line);
        zfree(&al->cycles);
        zfree(&al->br_cntr);
--

2.53.0.473.g4a7958ca14-goog

Reviewed-by on v1 1/1
#2 lore raw
In reply to Ian Rogers
Quoted 59 lines · Ian Rogers

The fileloc is a copy of a pointer to a string but in places like symbol_disassemble__llvm this string appears to be freed setting up potential use-after-frees:

llvm.c:

```
dl = disasm_line__new(args);
if (dl == NULL)
goto err;

annotation_line__add(&dl->al, &notes->src->source);

free(args->fileloc);
```
disasm.c:
```
static void annotation_line__init(struct annotation_line *al,
 struct annotate_args *args,
 int nr)
{
al->offset = args->offset;
al->line = strdup(args->line);
al->line_nr = args->line_nr;
al->fileloc = args->fileloc;
al->data_nr = nr;
}

struct disasm_line *disasm_line__new(struct annotate_args *args)
{
struct disasm_line *dl = NULL;
struct annotation *notes = symbol__annotation(args->ms->sym);
int nr = notes->src->nr_events;

dl = zalloc(disasm_line_size(nr));
if (!dl)
return NULL;

annotation_line__init(&dl->al, args, nr);
```

Fix this by making the fileloc a copy of the underlying string in its init/exit.

---
tools/perf/util/disasm.c | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/disasm.c +0 -0
index ddcc488f2e5f..b83bc14f82e1 100644
@@ -908,13 +908,14 @@ static void annotation_line__init(struct annotation_line *al,
 al->offset = args->offset;
 al->line = strdup(args->line);

tools/perf/util/disasm.c static void annotation_line__init(struct annotation_line *al, · line 918

Quoted 10 lines
tools/perf/util/disasm.c +2 -1
 al->line_nr = args->line_nr;
- al->fileloc = args->fileloc;
+ al->fileloc = args->fileloc ? strdup(args->fileloc) : NULL;
 al->data_nr = nr;
 }
 
 static void annotation_line__exit(struct annotation_line *al)
 {
 zfree_srcline(&al->path);
+ zfree(&al->fileloc);

Looks good to me.

Reviewed-by Athira Rajeev <[email protected] <mailto:[email protected]>>

Thanks
Athira

Quoted 6 lines
tools/perf/util/disasm.c +0 -0
 zfree(&al->line);
 zfree(&al->cycles);
 zfree(&al->br_cntr);
Signature · 1 line
2.53.0.473.g4a7958ca14-goog