Skip to contentSkip to conversation

perf: Fix tracepoint events permissions check

PATCH In review Jiri Olsa 5 participants 21 messages Jul 11, 2014 – Jul 28, 2014

Subsystems (2)
lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

cover perf: Fix tracepoint events permissions check

hi,
sending fix for bug reported by Alexander Yarygin in here:
http://marc.info/?l=linux-kernel&m=140475133707722&w=2

The main problem was, that the event_init tracepoint callback checked permission of the 'current' task instead of the event owner task.

While this is ok for perf_event_open syscall check, it is wrong once event_init is called during fork to create child events. In this case the permission of the forked task is checked instead of the owner task of the parent event.

Changing tracepoint permission code to check event's owner task, plus some other changes I needed for this.

thanks,
jirka

---
Jiri Olsa (5):
perf: Make perf_init_event function static
perf: Destroy event's children on task exit
perf: Initialize owner before calling event_init callback
perf: Move event owner retrieval into perf_event_get_owner
perf: Check event's owner permission in tracepoint init callback

 include/linux/perf_event.h      |  1 +
 kernel/events/core.c            | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
 kernel/trace/trace_event_perf.c | 19 +++++++++++++++++--
 3 files changed, 73 insertions(+), 19 deletions(-)
author reviewer
#1 lore raw

PATCH 1/5 perf: Make perf_init_event function static

From: Jiri Olsa <[email protected]>

No need for this one to be global.

Cc Alexander Yarygin <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Corey Ashford <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 kernel/events/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/events/core.c +1 -1
index 67e3b9c..71a56ae 100644
@@ -6660,7 +6660,7 @@ void perf_pmu_unregister(struct pmu *pmu)
 }
 EXPORT_SYMBOL_GPL(perf_pmu_unregister);
 
-struct pmu *perf_init_event(struct perf_event *event)
+static struct pmu *perf_init_event(struct perf_event *event)
 {
 	struct pmu *pmu = NULL;
 	int idx;
Signature · 1 line
1.8.3.1
author reviewer
#2 lore raw

PATCH 2/5 perf: Destroy event's children on task exit

In reply to Jiri Olsa

From: Jiri Olsa <[email protected]>

When task exits we close:
1) all events that are installed in task
2) all events owned by task (via file descriptor)

But we don't close children events of 2) events. Those children events stay until the child task exits and are useless with the parent being gone, because we have no way to get to values any more.

Plus if the event stays installed in task even with the owner task gone, it runs the perf callback any time the task forks, for no real reason.

Closing all children events events when the owner task of the parent event is closed.

Cc Alexander Yarygin <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Corey Ashford <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 kernel/events/core.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
kernel/events/core.c +27 -0
index 71a56ae..37797dd 100644
@@ -7535,6 +7535,32 @@ static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
 	put_ctx(child_ctx);
 }
 
+static void perf_event_exit_children(struct perf_event *parent)
+{
+	struct perf_event *child, *tmp;
+
+	mutex_lock(&parent->child_mutex);
+	list_for_each_entry_safe(child, tmp, &parent->child_list,
+				 child_list) {
+		struct perf_event_context *child_ctx = child->ctx;
+
+		/*
+		 * Child events got removed from child_list under
+		 * child_mutex and then freed. So it's safe to access
+		 * childs context in here, because the child holds
+		 * context ref.
+		 */
+		mutex_lock(&child_ctx->mutex);
+		perf_remove_from_context(child, true);
+		mutex_unlock(&child_ctx->mutex);
+
+		list_del_init(&child->child_list);
+		put_event(parent);
+		free_event(child);
+	}
+	mutex_unlock(&parent->child_mutex);
+}
+
 /*
  * When a child task exits, feed back event values to parent events.
  */
@@ -7555,6 +7581,7 @@ void perf_event_exit_task(struct task_struct *child)
 		 */
 		smp_wmb();
 		event->owner = NULL;
+		perf_event_exit_children(event);
 	}
 	mutex_unlock(&child->perf_event_mutex);
 
Signature · 1 line
1.8.3.1
maintainer
#3 lore raw

Re: [PATCH 2/5] perf: Destroy event's children on task exit

Quoted 11 lines · Jiri Olsa

From: Jiri Olsa <[email protected]>

When task exits we close:
1) all events that are installed in task
2) all events owned by task (via file descriptor)

But we don't close children events of 2) events. Those children events stay until the child task exits and are useless with the parent being gone, because we have no way to get to values any more.

Quoted 6 lines

Plus if the event stays installed in task even with the owner task gone, it runs the perf callback any time the task forks, for no real reason.

Closing all children events events when the owner task of the parent event is closed.

So I _think_ the reason we didn't do this is because this is potentially very expensive and keeping them around isn't too much bother, they'll die eventually.

But I can't really remember. Ingo any recollections / opinions?

#4 lore raw

Re: [PATCH 2/5] perf: Destroy event's children on task exit

Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · Jiri Olsa

From: Jiri Olsa <[email protected]>

When task exits we close:
1) all events that are installed in task
2) all events owned by task (via file descriptor)

But we don't close children events of 2) events. Those children events stay until the child task exits and are useless with the parent being gone, because we have no way to get to values any more.

Plus if the event stays installed in task even with the owner task gone, it runs the perf callback any time the task forks, for no real reason.

Quoted 6 lines
Quoted 2 lines

Closing all children events events when the owner task of the parent event is closed.

So I _think_ the reason we didn't do this is because this is potentially very expensive and keeping them around isn't too much bother, they'll die eventually.

yep, they will.. but until then the task they are installed in will continue having them and clone them on each fork

also for each such fork there's the permission check for inherited tracepoint event, which we canot make without the owner task

so I ended up with closing all the children

jirka

#5 lore raw

Re: [PATCH 2/5] perf: Destroy event's children on task exit

In reply to Peter Zijlstra
Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · Jiri Olsa

From: Jiri Olsa <[email protected]>

When task exits we close:
1) all events that are installed in task
2) all events owned by task (via file descriptor)

But we don't close children events of 2) events. Those children events stay until the child task exits and are useless with the parent being gone, because we have no way to get to values any more.

Plus if the event stays installed in task even with the owner task gone, it runs the perf callback any time the task forks, for no real reason.

Quoted 8 lines
Quoted 2 lines

Closing all children events events when the owner task of the parent event is closed.

So I _think_ the reason we didn't do this is because this is potentially very expensive and keeping them around isn't too much bother, they'll die eventually.

But I can't really remember. Ingo any recollections / opinions?

As far as I can remember it's just a historic path dependent implementation detail, not really a conscious choice: initially we didn't remove events (because we couldn't), and then it remained so because it broke nothing.

Thanks,

Ingo

maintainer
#6 lore raw

Re: [PATCH 2/5] perf: Destroy event's children on task exit

In reply to Jiri Olsa
Quoted 11 lines · Jiri Olsa

From: Jiri Olsa <[email protected]>

When task exits we close:
1) all events that are installed in task
2) all events owned by task (via file descriptor)

But we don't close children events of 2) events. Those children events stay until the child task exits and are useless with the parent being gone, because we have no way to get to values any more.

Quoted 6 lines

Plus if the event stays installed in task even with the owner task gone, it runs the perf callback any time the task forks, for no real reason.

Closing all children events events when the owner task of the parent event is closed.

Do we need this for the other patches, or is this an unrelated change?

Quoted 43 lines
---
 kernel/events/core.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
kernel/events/core.c +26 -0
index 71a56ae..37797dd 100644
@@ -7535,6 +7535,32 @@ static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
 	put_ctx(child_ctx);
 }
 
+static void perf_event_exit_children(struct perf_event *parent)
+{
+	struct perf_event *child, *tmp;
+
+	mutex_lock(&parent->child_mutex);
+	list_for_each_entry_safe(child, tmp, &parent->child_list,
+				 child_list) {
+		struct perf_event_context *child_ctx = child->ctx;
+
+		/*
+		 * Child events got removed from child_list under
+		 * child_mutex and then freed. So it's safe to access
+		 * childs context in here, because the child holds
+		 * context ref.
+		 */
+		mutex_lock(&child_ctx->mutex);
+		perf_remove_from_context(child, true);
+		mutex_unlock(&child_ctx->mutex);
+
+		list_del_init(&child->child_list);
+		put_event(parent);
+		free_event(child);
+	}
+	mutex_unlock(&parent->child_mutex);
+}
+
 /*
  * When a child task exits, feed back event values to parent events.
  */

kernel/events/core.c void perf_event_exit_task(struct task_struct *child) · line 7586

Quoted 6 lines
kernel/events/core.c +1 -0
@@ -7555,6 +7581,7 @@ void perf_event_exit_task(struct task_struct *child)
 		 */
 		smp_wmb();
 		event->owner = NULL;
+		perf_event_exit_children(event);
 	}
 	mutex_unlock(&child->perf_event_mutex);

I don't think this is correct, perf_event_init_context() can come in concurrently and the first place it runs into ->child_mutex is after its already allocated and created the (first) child event.

This also means that it not only makes exit() very slow for all tasks that own events -- surmountable I suppose, but also makes clone()/fork() very slow for all decedents -- not so good.

Now I suppose we can fix this by testing event->owner in perf_event_init_context() or somesuch, thereby bypassing event creation when the owner is tearing things down.

But it would be good if we can do all this in a separate series.

#7 lore raw

Re: [PATCH 2/5] perf: Destroy event's children on task exit

Quoted 12 lines · Peter Zijlstra
Quoted 11 lines · Jiri Olsa

From: Jiri Olsa <[email protected]>

When task exits we close:
1) all events that are installed in task
2) all events owned by task (via file descriptor)

But we don't close children events of 2) events. Those children events stay until the child task exits and are useless with the parent being gone, because we have no way to get to values any more.

Quoted 8 lines
Quoted 6 lines

Plus if the event stays installed in task even with the owner task gone, it runs the perf callback any time the task forks, for no real reason.

Closing all children events events when the owner task of the parent event is closed.

Do we need this for the other patches, or is this an unrelated change?

if we dont do it, the event stays installed without owner and perf fork callback will be called and fail on permission checking (because of owner == NULL) ... so yes, I think it's needed

jirka

maintainer
#8 lore raw

Re: [PATCH 2/5] perf: Destroy event's children on task exit

Quoted 17 lines · Jiri Olsa
Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · Jiri Olsa

From: Jiri Olsa <[email protected]>

When task exits we close:
1) all events that are installed in task
2) all events owned by task (via file descriptor)

But we don't close children events of 2) events. Those children events stay until the child task exits and are useless with the parent being gone, because we have no way to get to values any more.

Plus if the event stays installed in task even with the owner task gone, it runs the perf callback any time the task forks, for no real reason.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Closing all children events events when the owner task of the parent event is closed.

Do we need this for the other patches, or is this an unrelated change?

if we dont do it, the event stays installed without owner and perf fork callback will be called and fail on permission checking (because of owner == NULL) ... so yes, I think it's needed

Oh, right. Alternatively, we don't need permission checking for inherits at all, if we're allowed to create the initial event, we should be good for inherits.

And I suppose install_exec_creds() already does the right thing when we may not.

#9 lore raw

Re: [PATCH 2/5] perf: Destroy event's children on task exit

Quoted 23 lines · Peter Zijlstra
Quoted 22 lines · Jiri Olsa
Quoted 20 lines · Peter Zijlstra
Quoted 11 lines · Jiri Olsa

From: Jiri Olsa <[email protected]>

When task exits we close:
1) all events that are installed in task
2) all events owned by task (via file descriptor)

But we don't close children events of 2) events. Those children events stay until the child task exits and are useless with the parent being gone, because we have no way to get to values any more.

Quoted 6 lines

Plus if the event stays installed in task even with the owner task gone, it runs the perf callback any time the task forks, for no real reason.

Closing all children events events when the owner task of the parent event is closed.

Do we need this for the other patches, or is this an unrelated change?

Quoted 7 lines
Quoted 3 lines

if we dont do it, the event stays installed without owner and perf fork callback will be called and fail on permission checking (because of owner == NULL) ... so yes, I think it's needed

Oh, right. Alternatively, we don't need permission checking for inherits at all, if we're allowed to create the initial event, we should be good for inherits.

I could adress that in follow up patch.. or you want this instead of this one? IMO we should close those events anyway..

jirka

maintainer
#10 lore raw

Re: [PATCH 2/5] perf: Destroy event's children on task exit

Quoted 10 lines · Jiri Olsa
Quoted 7 lines
Quoted 3 lines

if we dont do it, the event stays installed without owner and perf fork callback will be called and fail on permission checking (because of owner == NULL) ... so yes, I think it's needed

Oh, right. Alternatively, we don't need permission checking for inherits at all, if we're allowed to create the initial event, we should be good for inherits.

I could adress that in follow up patch.. or you want this instead of this one? IMO we should close those events anyway..

I tend to agree that closing them all is nicer. But we need to be careful while doing it so as not to make the clone/fork path block on it.

I _think_ it might be best to separate these two issues for the moment, so cure the reported problem by avoiding the permission check for inherited events -- IFF you agree with the previous argument that install_exec_creds() should be sufficient.

And then so a patch playing games with perf_event_init_context() (clone/fork) vs perf_event_exit_task() (exit).

#11 lore raw

Re: [PATCH 2/5] perf: Destroy event's children on task exit

Quoted 16 lines · Peter Zijlstra
Quoted 10 lines · Jiri Olsa
Quoted 7 lines
Quoted 3 lines

if we dont do it, the event stays installed without owner and perf fork callback will be called and fail on permission checking (because of owner == NULL) ... so yes, I think it's needed

Oh, right. Alternatively, we don't need permission checking for inherits at all, if we're allowed to create the initial event, we should be good for inherits.

I could adress that in follow up patch.. or you want this instead of this one? IMO we should close those events anyway..

I tend to agree that closing them all is nicer. But we need to be careful while doing it so as not to make the clone/fork path block on it.

Quoted 7 lines

I _think_ it might be best to separate these two issues for the moment, so cure the reported problem by avoiding the permission check for inherited events -- IFF you agree with the previous argument that install_exec_creds() should be sufficient.

And then so a patch playing games with perf_event_init_context() (clone/fork) vs perf_event_exit_task() (exit).

ook, will do

#12 lore raw

Re: [PATCH 2/5] perf: Destroy event's children on task exit

In reply to Peter Zijlstra
Quoted 12 lines · Peter Zijlstra
Quoted 10 lines · Jiri Olsa
Quoted 7 lines
Quoted 3 lines

if we dont do it, the event stays installed without owner and perf fork callback will be called and fail on permission checking (because of owner == NULL) ... so yes, I think it's needed

Oh, right. Alternatively, we don't need permission checking for inherits at all, if we're allowed to create the initial event, we should be good for inherits.

I could adress that in follow up patch.. or you want this instead of this one? IMO we should close those events anyway..

Quoted 8 lines

I tend to agree that closing them all is nicer. But we need to be careful while doing it so as not to make the clone/fork path block on it.

I _think_ it might be best to separate these two issues for the moment, so cure the reported problem by avoiding the permission check for inherited events -- IFF you agree with the previous argument that install_exec_creds() should be sufficient.

install_exec_creds remove removes current events any time suid binary is executed.. so it seems ok

/*
* Disable monitoring for regular users
* when executing setuid binaries. Must
* wait until new credentials are committed
* by commit_creds() above
*/
if (get_dumpable(current->mm) != SUID_DUMP_USER)
perf_event_exit_task(current);

jirka

#13 lore raw

Re: [PATCH 2/5] perf: Destroy event's children on task exit

In reply to Peter Zijlstra
Quoted 66 lines · Peter Zijlstra
Quoted 11 lines · Jiri Olsa

From: Jiri Olsa <[email protected]>

When task exits we close:
1) all events that are installed in task
2) all events owned by task (via file descriptor)

But we don't close children events of 2) events. Those children events stay until the child task exits and are useless with the parent being gone, because we have no way to get to values any more.

Quoted 6 lines

Plus if the event stays installed in task even with the owner task gone, it runs the perf callback any time the task forks, for no real reason.

Closing all children events events when the owner task of the parent event is closed.

Do we need this for the other patches, or is this an unrelated change?

Quoted 45 lines
---
 kernel/events/core.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
kernel/events/core.c +26 -0
index 71a56ae..37797dd 100644
@@ -7535,6 +7535,32 @@ static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
 	put_ctx(child_ctx);
 }
 
+static void perf_event_exit_children(struct perf_event *parent)
+{
+	struct perf_event *child, *tmp;
+
+	mutex_lock(&parent->child_mutex);
+	list_for_each_entry_safe(child, tmp, &parent->child_list,
+				 child_list) {
+		struct perf_event_context *child_ctx = child->ctx;
+
+		/*
+		 * Child events got removed from child_list under
+		 * child_mutex and then freed. So it's safe to access
+		 * childs context in here, because the child holds
+		 * context ref.
+		 */
+		mutex_lock(&child_ctx->mutex);
+		perf_remove_from_context(child, true);
+		mutex_unlock(&child_ctx->mutex);
+
+		list_del_init(&child->child_list);
+		put_event(parent);
+		free_event(child);
+	}
+	mutex_unlock(&parent->child_mutex);
+}
+
 /*
  * When a child task exits, feed back event values to parent events.
  */
@@ -7555,6 +7581,7 @@ void perf_event_exit_task(struct task_struct *child)
 		 */
 		smp_wmb();
Quoted 8 lines

kernel/events/core.c void perf_event_exit_task(struct task_struct *child) · line 7586

Quoted 4 lines
kernel/events/core.c +1 -0
 		event->owner = NULL;
+		perf_event_exit_children(event);
 	}
 	mutex_unlock(&child->perf_event_mutex);

I don't think this is correct, perf_event_init_context() can come in concurrently and the first place it runs into ->child_mutex is after its already allocated and created the (first) child event.

just noticed this.. I'm working on the other version we decide, but FWIW there's also mutex_lock(&child_ctx->mutex); before removing the context, that should protect it against perf_event_init_context call

jirka

maintainer
#14 lore raw

Re: [PATCH 2/5] perf: Destroy event's children on task exit

Quoted 34 lines · Jiri Olsa
Quoted 24 lines · Peter Zijlstra
Quoted 23 lines · Jiri Olsa

From: Jiri Olsa <[email protected]>

kernel/events/core.c +21 -0
+static void perf_event_exit_children(struct perf_event *parent)
+{
+	struct perf_event *child, *tmp;
+
+	mutex_lock(&parent->child_mutex);
+	list_for_each_entry_safe(child, tmp, &parent->child_list,
+				 child_list) {
+		struct perf_event_context *child_ctx = child->ctx;
+
+		/*
+		 * Child events got removed from child_list under
+		 * child_mutex and then freed. So it's safe to access
+		 * childs context in here, because the child holds
+		 * context ref.
+		 */
+		mutex_lock(&child_ctx->mutex);
+		perf_remove_from_context(child, true);
+		mutex_unlock(&child_ctx->mutex);
+
+		list_del_init(&child->child_list);
+		put_event(parent);
Quoted 8 lines

kernel/events/core.c static void perf_event_exit_task_context(struct task_struct *child, int ctxn) · line 7562

Quoted 4 lines
kernel/events/core.c +4 -0
+		free_event(child);
+	}
+	mutex_unlock(&parent->child_mutex);
+}

I don't think this is correct, perf_event_init_context() can come in concurrently and the first place it runs into ->child_mutex is after its already allocated and created the (first) child event.

Quoted 3 lines

just noticed this.. I'm working on the other version we decide, but FWIW there's also mutex_lock(&child_ctx->mutex); before removing the context, that should protect it against perf_event_init_context call

Oh, more fail :-)

You have:

perf_event::child_mutex
perf_event_context::mutex

The existing code has:

perf_event_context::mutex
perf_event::child_context

See for example:

perf_event_init_context()

    mutex_lock(&parent_ctx->mutex)
    inherit_task_group()
      inherit_group()
        inherit_event()
	  mutex_lock(&parent_event->child_mutex)

and

perf_event_for_each()

    mutex_lock(&ctx->mutex)
    perf_event_for_each_child()
      mutex_lock(&event->child_mutex)

So the patch creates an AB-BA deadlock.

#15 lore raw

Re: [PATCH 2/5] perf: Destroy event's children on task exit

Quoted 4 lines · Peter Zijlstra
Quoted 3 lines · Jiri Olsa
Quoted 2 lines · Peter Zijlstra
Quoted 1 line · Jiri Olsa

From: Jiri Olsa <[email protected]>

SNIP

Quoted 30 lines
Quoted 8 lines
Quoted 4 lines

I don't think this is correct, perf_event_init_context() can come in concurrently and the first place it runs into ->child_mutex is after its already allocated and created the (first) child event.

just noticed this.. I'm working on the other version we decide, but FWIW there's also mutex_lock(&child_ctx->mutex); before removing the context, that should protect it against perf_event_init_context call

Oh, more fail :-)

You have:

perf_event::child_mutex
perf_event_context::mutex

The existing code has:

perf_event_context::mutex
perf_event::child_context

See for example:

perf_event_init_context()

    mutex_lock(&parent_ctx->mutex)
    inherit_task_group()
      inherit_group()
        inherit_event()
	  mutex_lock(&parent_event->child_mutex)
Quoted 8 lines

and

perf_event_for_each()

    mutex_lock(&ctx->mutex)
    perf_event_for_each_child()
      mutex_lock(&event->child_mutex)

So the patch creates an AB-BA deadlock.

ouch, right.. I'll try to come with other way

thanks,
jirka

author reviewer
#16 lore raw

PATCH 3/5 perf: Initialize owner before calling event_init callback

In reply to Jiri Olsa

From: Jiri Olsa <[email protected]>

To be able to check the owner task permission in event_init PMU callback, we need to set the owner before it is called.

Cc Alexander Yarygin <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Corey Ashford <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 kernel/events/core.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
kernel/events/core.c +9 -9
index 37797dd..a36ebfe 100644
@@ -6751,6 +6751,7 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
 		 struct task_struct *task,
 		 struct perf_event *group_leader,
 		 struct perf_event *parent_event,
+		 struct task_struct *owner,
 		 perf_overflow_handler_t overflow_handler,
 		 void *context)
 {
@@ -6829,6 +6830,9 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
 
 	perf_event__state_init(event);
 
+	if (owner)
+		event->owner = owner;
+
 	pmu = NULL;
 
 	hwc = &event->hw;
@@ -7141,7 +7145,7 @@ SYSCALL_DEFINE5(perf_event_open,
 	get_online_cpus();
 
 	event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
-				 NULL, NULL);
+				 current, NULL, NULL);
 	if (IS_ERR(event)) {
 		err = PTR_ERR(event);
 		goto err_cpus;
@@ -7293,8 +7297,6 @@ SYSCALL_DEFINE5(perf_event_open,
 
 	put_online_cpus();
 
-	event->owner = current;
-
 	mutex_lock(&current->perf_event_mutex);
 	list_add_tail(&event->owner_entry, &current->perf_event_list);
 	mutex_unlock(&current->perf_event_mutex);
@@ -7353,7 +7355,7 @@ perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
 	 * Get the target context (task or percpu):
 	 */
 
-	event = perf_event_alloc(attr, cpu, task, NULL, NULL,
+	event = perf_event_alloc(attr, cpu, task, NULL, NULL, NULL,
 				 overflow_handler, context);
 	if (IS_ERR(event)) {
 		err = PTR_ERR(event);
@@ -7674,11 +7676,9 @@ inherit_event(struct perf_event *parent_event,
 	if (parent_event->parent)
 		parent_event = parent_event->parent;
 
-	child_event = perf_event_alloc(&parent_event->attr,
-					   parent_event->cpu,
-					   child,
-					   group_leader, parent_event,
-				           NULL, NULL);
+	child_event = perf_event_alloc(&parent_event->attr, parent_event->cpu,
+				       child, group_leader, parent_event,
+				       NULL, NULL, NULL);
 	if (IS_ERR(child_event))
 		return child_event;
 
Signature · 1 line
1.8.3.1
author reviewer
#17 lore raw

PATCH 4/5 perf: Move event owner retrieval into perf_event_get_owner

In reply to Jiri Olsa

Moving the code of getting safely event's owner into perf_event_get_owner function, so it could be used in following change.

Cc Alexander Yarygin <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Corey Ashford <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 include/linux/perf_event.h |  1 +
 kernel/events/core.c       | 23 +++++++++++++++--------
 2 files changed, 16 insertions(+), 8 deletions(-)
include/linux/perf_event.h +1 -0
index 707617a..ec6ef94 100644
@@ -784,6 +784,7 @@ extern void perf_event_enable(struct perf_event *event);
 extern void perf_event_disable(struct perf_event *event);
 extern int __perf_event_disable(void *info);
 extern void perf_event_task_tick(void);
+extern struct task_struct *perf_event_get_owner(struct perf_event *event);
 #else /* !CONFIG_PERF_EVENTS: */
 static inline void
 perf_event_task_sched_in(struct task_struct *prev,
kernel/events/core.c +15 -8
index a36ebfe..faba9e0 100644
@@ -3311,17 +3311,10 @@ static void free_event(struct perf_event *event)
 	_free_event(event);
 }
 
-/*
- * Called when the last reference to the file is gone.
- */
-static void put_event(struct perf_event *event)
+struct task_struct *perf_event_get_owner(struct perf_event *event)
 {
-	struct perf_event_context *ctx = event->ctx;
 	struct task_struct *owner;
 
-	if (!atomic_long_dec_and_test(&event->refcount))
-		return;
-
 	rcu_read_lock();
 	owner = ACCESS_ONCE(event->owner);
 	/*
@@ -3340,7 +3333,21 @@ static void put_event(struct perf_event *event)
 		get_task_struct(owner);
 	}
 	rcu_read_unlock();
+	return owner;
+}
+
+/*
+ * Called when the last reference to the file is gone.
+ */
+static void put_event(struct perf_event *event)
+{
+	struct perf_event_context *ctx = event->ctx;
+	struct task_struct *owner;
+
+	if (!atomic_long_dec_and_test(&event->refcount))
+		return;
 
+	owner = perf_event_get_owner(event);
 	if (owner) {
 		mutex_lock(&owner->perf_event_mutex);
 		/*
Signature · 1 line
1.8.3.1
author reviewer
#18 lore raw

PATCH 5/5 perf: Check event's owner permission in tracepoint init callback

In reply to Jiri Olsa

Checking event's owner task permissions instead of 'current' process' ones. This way we get proper check when this is called from fork callback, where 'current' process does not represent the owner.

Checking parent event owner task in case of child event.

Cc Alexander Yarygin <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Corey Ashford <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 kernel/events/core.c            |  4 ++++
 kernel/trace/trace_event_perf.c | 19 +++++++++++++++++--
 2 files changed, 21 insertions(+), 2 deletions(-)
kernel/events/core.c +4 -0
index faba9e0..c478828 100644
@@ -3315,6 +3315,10 @@ struct task_struct *perf_event_get_owner(struct perf_event *event)
 {
 	struct task_struct *owner;
 
+	/* Only the parent holds owner task. */
+	if (event->parent)
+		event = event->parent;
+
 	rcu_read_lock();
 	owner = ACCESS_ONCE(event->owner);
 	/*
kernel/trace/trace_event_perf.c +17 -2
index 5d12bb4..35b76ec 100644
@@ -21,9 +21,24 @@ typedef typeof(unsigned long [PERF_MAX_TRACE_SIZE / sizeof(unsigned long)])
 /* Count the events in use (per event id, not per instance) */
 static int	total_ref_count;
 
+static bool is_owner_admin(struct perf_event *event)
+{
+	struct task_struct *owner = perf_event_get_owner(event);
+	bool admin = false;
+
+	if (owner) {
+		admin = has_capability(owner, CAP_SYS_ADMIN);
+		put_task_struct(owner);
+	}
+
+	return admin;
+}
+
 static int perf_trace_event_perm(struct ftrace_event_call *tp_event,
 				 struct perf_event *p_event)
 {
+	bool admin = is_owner_admin(p_event);
+
 	if (tp_event->perf_perm) {
 		int ret = tp_event->perf_perm(tp_event, p_event);
 		if (ret)
@@ -32,7 +47,7 @@ static int perf_trace_event_perm(struct ftrace_event_call *tp_event,
 
 	/* The ftrace function trace is allowed only for root. */
 	if (ftrace_event_is_function(tp_event)) {
-		if (perf_paranoid_tracepoint_raw() && !capable(CAP_SYS_ADMIN))
+		if (perf_paranoid_tracepoint_raw() && !admin)
 			return -EPERM;
 
 		/*
@@ -65,7 +80,7 @@ static int perf_trace_event_perm(struct ftrace_event_call *tp_event,
 	 * ...otherwise raw tracepoint data can be a severe data leak,
 	 * only allow root to have these.
 	 */
-	if (perf_paranoid_tracepoint_raw() && !capable(CAP_SYS_ADMIN))
+	if (perf_paranoid_tracepoint_raw() && !admin)
 		return -EPERM;
 
 	return 0;
Signature · 1 line
1.8.3.1
#19 lore raw
In reply to Jiri Olsa
Quoted 4 lines · Jiri Olsa

hi,
sending fix for bug reported by Alexander Yarygin in here:
http://marc.info/?l=linux-kernel&m=140475133707722&w=2

Quoted 8 lines

The main problem was, that the event_init tracepoint callback checked permission of the 'current' task instead of the event owner task.

While this is ok for perf_event_open syscall check, it is wrong once event_init is called during fork to create child events. In this case the permission of the forked task is checked instead of the owner task of the parent event.

also available in here:
git://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
perf/core_perm

jirka

#20 lore raw

[tip:perf/core] perf: Check permission only for parent tracepoint event

In reply to Jiri Olsa
perf: Check permission only for parent tracepoint event
Commit-ID:  f4be073db878d0e79f74bc36f1642847781791a0
Gitweb:     http://git.kernel.org/tip/f4be073db878d0e79f74bc36f1642847781791a0
Author:     Jiri Olsa <[email protected]>
AuthorDate: Wed, 16 Jul 2014 14:33:29 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 28 Jul 2014 10:01:38 +0200

perf: Check permission only for parent tracepoint event

There's no need to check cloned event's permission once the parent was already checked.

Also the code is checking 'current' process permissions, which is not owner process for cloned events, thus could end up with wrong permission check result.

---
 kernel/trace/trace_event_perf.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
kernel/trace/trace_event_perf.c +12 -0
index 5d12bb4..4b9c114 100644
@@ -30,6 +30,18 @@ static int perf_trace_event_perm(struct ftrace_event_call *tp_event,
 			return ret;
 	}
 
+	/*
+	 * We checked and allowed to create parent,
+	 * allow children without checking.
+	 */
+	if (p_event->parent)
+		return 0;
+
+	/*
+	 * It's ok to check current process (owner) permissions in here,
+	 * because code below is called only via perf_event_open syscall.
+	 */
+
 	/* The ftrace function trace is allowed only for root. */
 	if (ftrace_event_is_function(tp_event)) {
 		if (perf_paranoid_tracepoint_raw() && !capable(CAP_SYS_ADMIN))