Skip to content

Commit 5872705

Browse files
captain5050gregkh
authored andcommitted
perf cgroup: Update metric leader in evlist__expand_cgroup
[ Upstream commit c9ef786 ] When the evlist is expanded the metric leader wasn't being updated. As the original evsel is deleted this creates a use-after-free in stat-shadow's prepare_metric. This was detected running the "perf stat --bpf-counters --for-each-cgroup test" with sanitizers. The change itself puts the copied evsel into the priv field (known unused because of evsel__clone use) and then in a second pass over the list updates the copied values using the priv pointer. Fixes: d1c5a0e ("perf stat: Add --for-each-cgroup option") Signed-off-by: Ian Rogers <irogers@google.com> Acked-by: Sun Jian <sun.jian.kdev@gmail.com> Signed-off-by: Namhyung Kim <namhyung@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 48fc9a3 commit 5872705

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

tools/perf/util/cgroup.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,6 @@ static bool has_pattern_string(const char *str)
417417
int evlist__expand_cgroup(struct evlist *evlist, const char *str, bool open_cgroup)
418418
{
419419
struct evlist *orig_list, *tmp_list;
420-
struct evsel *pos, *evsel, *leader;
421420
struct rblist orig_metric_events;
422421
struct cgroup *cgrp = NULL;
423422
struct cgroup_name *cn;
@@ -452,6 +451,7 @@ int evlist__expand_cgroup(struct evlist *evlist, const char *str, bool open_cgro
452451
goto out_err;
453452

454453
list_for_each_entry(cn, &cgroup_list, list) {
454+
struct evsel *pos;
455455
char *name;
456456

457457
if (!cn->used)
@@ -467,21 +467,37 @@ int evlist__expand_cgroup(struct evlist *evlist, const char *str, bool open_cgro
467467
if (cgrp == NULL)
468468
continue;
469469

470-
leader = NULL;
470+
/* copy the list and set to the new cgroup. */
471471
evlist__for_each_entry(orig_list, pos) {
472-
evsel = evsel__clone(/*dest=*/NULL, pos);
472+
struct evsel *evsel = evsel__clone(/*dest=*/NULL, pos);
473+
473474
if (evsel == NULL)
474475
goto out_err;
475476

477+
/* stash the copy during the copying. */
478+
pos->priv = evsel;
476479
cgroup__put(evsel->cgrp);
477480
evsel->cgrp = cgroup__get(cgrp);
478481

479-
if (evsel__is_group_leader(pos))
480-
leader = evsel;
481-
evsel__set_leader(evsel, leader);
482-
483482
evlist__add(tmp_list, evsel);
484483
}
484+
/* update leader information using stashed pointer to copy. */
485+
evlist__for_each_entry(orig_list, pos) {
486+
struct evsel *evsel = pos->priv;
487+
488+
if (evsel__leader(pos))
489+
evsel__set_leader(evsel, evsel__leader(pos)->priv);
490+
491+
if (pos->metric_leader)
492+
evsel->metric_leader = pos->metric_leader->priv;
493+
494+
if (pos->first_wildcard_match)
495+
evsel->first_wildcard_match = pos->first_wildcard_match->priv;
496+
}
497+
/* the stashed copy is no longer used. */
498+
evlist__for_each_entry(orig_list, pos)
499+
pos->priv = NULL;
500+
485501
/* cgroup__new() has a refcount, release it here */
486502
cgroup__put(cgrp);
487503
nr_cgroups++;

0 commit comments

Comments
 (0)