Skip to contentSkip to conversation

perf: Don't read the original event's pmu_ctx in inherit_event()

PATCH New Daniel Wade 2 participants 2 messages Nov 16 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf: Don't read the original event's pmu_ctx in inherit_event()

inherit_event() walks @parent_event up to the original, top-level event and takes the PMU from its pmu_ctx. perf_event_init_context() holds the mutex of the forking task's own context, but that original is attached to the context it was opened in, whose mutex and lock are both unheld, and the fork is not in an RCU read-side section. The move_group path of perf_event_open() drops that pmu_ctx with put_pmu_ctx(), which can free it through call_rcu(), and reinstalls the pointer only two loops later:

G: perf_event_open(group_fd = P) M, a child of G: fork()
-------------------------------- ----------------------------
put_pmu_ctx(P->pmu_ctx)
call_rcu(free_epc_rcu)
[ grace period -> kfree() ] inherit_event(C)
parent_event = P P->pmu_ctx->pmu <-- freed
P->pmu_ctx = pmu_ctx

The value is used as a struct pmu. No privilege is needed: a software leader, an inheriting hardware event on the caller's own task, and a fork.

Take the PMU from the event inherit_event() is given, before the replacement; it is attached to @parent_ctx, whose mutex is held. The result differs only when the group was moved after the context was cloned from it.

Fixes 4b9ce6719606 ("perf: Make sure to use pmu_ctx->pmu for groups")

---
Tested on mainline b3dfbde62cf1 (kernel/events identical to v7.4-rc2), x86-64 with KASAN and lockdep, as uid 65534 at perf_event_paranoid 2.

With synchronize_rcu(), rcu_barrier() and msleep(3000) after that put_pmu_ctx() to hold the window open, and no patch: "BUG: KASAN:
slab-use-after-free in inherit_event", freed from an RCU callback queued by put_pmu_ctx(), then an oops in perf_ctx_disable(). With the delay and this patch, and with this patch and no delay: clean.

Not tested without the delay: only a task whose own context is not the leader's can race it, since perf_event_init_context() takes the forking task's ctx->mutex and perf_event_open() takes the leader's.

4b9ce6719606 is in 6.6, 6.12, 6.18 and 6.19 stable. Jun Yang's unmerged "perf/core: Remove group siblings before the leader in move_group" reorders that block; it shortens this window but does not close it.

 kernel/events/core.c |   11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
kernel/events/core.c +10 -1
index f5f2261f1fe..544a2c10409 100644
@@ -14974,6 +14974,15 @@ inherit_event(struct perf_event *parent_event,
 	struct perf_event_pmu_context *pmu_ctx;
 	struct perf_event *child_event;
 	unsigned long flags;
+	struct pmu *pmu;
+
+	/*
+	 * @parent_event is attached to @parent_ctx, whose mutex is held, so
+	 * its pmu_ctx is stable. The original it is replaced with below can
+	 * be in another context, where perf_event_open()'s move_group path
+	 * may have dropped the pmu_ctx and not yet installed the new one.
+	 */
+	pmu = parent_event->pmu_ctx->pmu;
 
 	/*
 	 * Instead of creating recursive hierarchies of events,
@@ -15003,7 +15012,7 @@ inherit_event(struct perf_event *parent_event,
 	get_ctx(child_ctx);
 	child_event->ctx = child_ctx;
 
-	pmu_ctx = find_get_pmu_context(parent_event->pmu_ctx->pmu, child_ctx, child_event);
+	pmu_ctx = find_get_pmu_context(pmu, child_ctx, child_event);
 	if (IS_ERR(pmu_ctx)) {
 		free_event(child_event);
 		return ERR_CAST(pmu_ctx);
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 20 lines

inherit_event() walks @parent_event up to the original, top-level event and takes the PMU from its pmu_ctx. perf_event_init_context() holds the mutex of the forking task's own context, but that original is attached to the context it was opened in, whose mutex and lock are both unheld, and the fork is not in an RCU read-side section. The move_group path of perf_event_open() drops that pmu_ctx with put_pmu_ctx(), which can free it through call_rcu(), and reinstalls the pointer only two loops later:

G: perf_event_open(group_fd = P) M, a child of G: fork()
-------------------------------- ----------------------------
put_pmu_ctx(P->pmu_ctx)
call_rcu(free_epc_rcu)
[ grace period -> kfree() ] inherit_event(C)
parent_event = P P->pmu_ctx->pmu <-- freed
P->pmu_ctx = pmu_ctx

The value is used as a struct pmu. No privilege is needed: a software leader, an inheriting hardware event on the caller's own task, and a fork.

Quoted 6 lines

Take the PMU from the event inherit_event() is given, before the replacement; it is attached to @parent_ctx, whose mutex is held. The result differs only when the group was moved after the context was cloned from it.

Fixes 4b9ce6719606 ("perf: Make sure to use pmu_ctx->pmu for groups")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1