[PATCH v5 07/17] perf vdso: Fix unlocked __dsos__add() and race in __machine__addnew_vdso()

From: Ian Rogers

Date: Tue Oct 20 2026 - 12:42:29 EST


__machine__addnew_vdso() called __dsos__add(&machine->dsos, dso) without
holding machine->dsos.lock, and machine__findnew_vdso() and
__machine__findnew_compat() initialized machine->vdso_info and created
temporary VDSO files without synchronization. Acquire
machine->dsos.lock in write mode across machine->vdso_info
initialization, duplicate checking via __dsos__find(), temporary file
creation, dso__set_long_name(), and __dsos__add().

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/dsos.c | 7 +++++-
tools/perf/util/dsos.h | 1 +
tools/perf/util/vdso.c | 52 +++++++++++++++++++++++++++---------------
3 files changed, 40 insertions(+), 20 deletions(-)

diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c
index a2cd37bd1660..40267d3a725d 100644
--- a/tools/perf/util/dsos.c
+++ b/tools/perf/util/dsos.c
@@ -284,12 +284,17 @@ static struct dso *__dsos__find_id(struct dsos *dsos, const char *name, const st
return res;
}

+struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short, bool write_locked)
+{
+ return __dsos__find_id(dsos, name, &dso_id_empty, cmp_short, write_locked);
+}
+
struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
{
struct dso *res;

down_read(&dsos->lock);
- res = __dsos__find_id(dsos, name, &dso_id_empty, cmp_short, /*write_locked=*/false);
+ res = __dsos__find(dsos, name, cmp_short, /*write_locked=*/false);
up_read(&dsos->lock);
return res;
}
diff --git a/tools/perf/util/dsos.h b/tools/perf/util/dsos.h
index a26774950866..857a79fd8805 100644
--- a/tools/perf/util/dsos.h
+++ b/tools/perf/util/dsos.h
@@ -30,6 +30,7 @@ void dsos__exit(struct dsos *dsos);

int __dsos__add(struct dsos *dsos, struct dso *dso);
int dsos__add(struct dsos *dsos, struct dso *dso);
+struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short, bool write_locked);
struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short);

struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, const struct dso_id *id);
diff --git a/tools/perf/util/vdso.c b/tools/perf/util/vdso.c
index c12f5d8c4bf6..93828e660672 100644
--- a/tools/perf/util/vdso.c
+++ b/tools/perf/util/vdso.c
@@ -131,8 +131,13 @@ static struct dso *__machine__addnew_vdso(struct machine *machine, const char *s

dso = dso__new(short_name);
if (dso != NULL) {
- __dsos__add(&machine->dsos, dso);
+ /*
+ * Set the long name before adding to machine->dsos so that
+ * __dsos__add() inserts it in sorted order without needing to
+ * re-take machine->dsos.lock.
+ */
dso__set_long_name(dso, long_name, false);
+ __dsos__add(&machine->dsos, dso);
}

return dso;
@@ -250,15 +255,19 @@ static struct dso *__machine__findnew_compat(struct machine *machine,
const char *file_name;
struct dso *dso;

- dso = dsos__find(&machine->dsos, vdso_file->dso_name, true);
+ down_write(&machine->dsos.lock);
+ dso = __dsos__find(&machine->dsos, vdso_file->dso_name, true, /*write_locked=*/true);
if (dso)
- return dso;
+ goto out_unlock;

file_name = vdso__get_compat_file(vdso_file);
if (!file_name)
- return NULL;
+ goto out_unlock;

- return __machine__addnew_vdso(machine, vdso_file->dso_name, file_name);
+ dso = __machine__addnew_vdso(machine, vdso_file->dso_name, file_name);
+out_unlock:
+ up_write(&machine->dsos.lock);
+ return dso;
}

static int __machine__findnew_vdso_compat(struct machine *machine,
@@ -332,33 +341,38 @@ struct dso *machine__findnew_vdso(struct machine *machine,
{
struct vdso_info *vdso_info;
struct dso *dso = NULL;
- char *file;
+ const char *file;

+ dso = machine__find_vdso(machine, thread);
+ if (dso)
+ return dso;
+
+ down_write(&machine->dsos.lock);
if (!machine->vdso_info)
machine->vdso_info = vdso_info__new();

vdso_info = machine->vdso_info;
- if (!vdso_info)
+ if (!vdso_info) {
+ up_write(&machine->dsos.lock);
return NULL;
-
- dso = machine__find_vdso(machine, thread);
- if (dso)
- return dso;
+ }

#if BITS_PER_LONG == 64
+ up_write(&machine->dsos.lock);
if (__machine__findnew_vdso_compat(machine, thread, vdso_info, &dso))
return dso;
+ down_write(&machine->dsos.lock);
#endif

- dso = dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
- if (dso)
- return dso;
-
- file = get_file(&vdso_info->vdso);
- if (!file)
- return NULL;
+ dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true, /*write_locked=*/true);
+ if (!dso) {
+ file = get_file(&vdso_info->vdso);
+ if (file)
+ dso = __machine__addnew_vdso(machine, DSO__NAME_VDSO, file);
+ }
+ up_write(&machine->dsos.lock);

- return __machine__addnew_vdso(machine, DSO__NAME_VDSO, file);
+ return dso;
}

bool dso__is_vdso(struct dso *dso)
--
2.56.0.212.g129ede20d9-goog