From: Peter Zijlstra <a.p.zijlstra@chello.nl>
To: Ingo Molnar <mingo@elte.hu>, Paul Mackerras <paulus@samba.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>,
Frederic Weisbecker <fweisbec@gmail.com>,
Mike Galbraith <efault@gmx.de>,
linux-kernel@vger.kernel.org, Jens Axboe <jens.axboe@oracle.com>,
Peter Zijlstra <a.p.zijlstra@chello.nl>
Subject: [PATCH 1/4] perf_counter: Default to higher paranoia level
Date: Wed, 19 Aug 2009 11:18:24 +0200 [thread overview]
Message-ID: <20090819092023.728070630@chello.nl> (raw)
In-Reply-To: 20090819091823.916851355@chello.nl
[-- Attachment #1: perf-sekure.patch --]
[-- Type: text/plain, Size: 3901 bytes --]
Change the default permissions on perf counters.
The new default will disallow regular users to create cpu-wide
counters, and will anonymize kernel IPs for task samples.
This will allow a user to profile his own applications and still know
the proportion of the kernel events, but does not expose kernel IPs.
Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Jens Axboe <jens.axboe@oracle.com>
---
arch/x86/kernel/cpu/perf_counter.c | 6 ++++++
include/linux/perf_counter.h | 1 +
kernel/perf_counter.c | 27 ++++++++++++++++++++-------
3 files changed, 27 insertions(+), 7 deletions(-)
Index: linux-2.6/arch/x86/kernel/cpu/perf_counter.c
===================================================================
--- linux-2.6.orig/arch/x86/kernel/cpu/perf_counter.c
+++ linux-2.6/arch/x86/kernel/cpu/perf_counter.c
@@ -2153,7 +2153,13 @@ static const struct stacktrace_ops backt
static void
perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
{
+ u64 anon_ip = perf_paranoid_anon_ip();
+
callchain_store(entry, PERF_CONTEXT_KERNEL);
+ if (anon_ip) {
+ callchain_store(entry, anon_ip);
+ return;
+ }
callchain_store(entry, regs->ip);
dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
Index: linux-2.6/include/linux/perf_counter.h
===================================================================
--- linux-2.6.orig/include/linux/perf_counter.h
+++ linux-2.6/include/linux/perf_counter.h
@@ -754,6 +754,7 @@ static inline void perf_counter_mmap(str
extern void perf_counter_comm(struct task_struct *tsk);
extern void perf_counter_fork(struct task_struct *tsk);
+extern unsigned long perf_paranoid_anon_ip(void);
extern struct perf_callchain_entry *perf_callchain(struct pt_regs *regs);
extern int sysctl_perf_counter_paranoid;
Index: linux-2.6/kernel/perf_counter.c
===================================================================
--- linux-2.6.orig/kernel/perf_counter.c
+++ linux-2.6/kernel/perf_counter.c
@@ -48,18 +48,29 @@ static atomic_t nr_task_counters __read_
* perf counter paranoia level:
* 0 - not paranoid
* 1 - disallow cpu counters to unpriv
- * 2 - disallow kernel profiling to unpriv
+ * 2 - anonymize kernel RIPs to unpriv
+ * 3 - disallow kernel profiling to unpriv
*/
-int sysctl_perf_counter_paranoid __read_mostly;
+int sysctl_perf_counter_paranoid __read_mostly = 2;
static inline bool perf_paranoid_cpu(void)
{
- return sysctl_perf_counter_paranoid > 0;
+ return !capable(CAP_SYS_ADMIN) && sysctl_perf_counter_paranoid > 0;
+}
+
+static inline bool perf_paranoid_anon(void)
+{
+ return !capable(CAP_SYS_ADMIN) && sysctl_perf_counter_paranoid > 1;
}
static inline bool perf_paranoid_kernel(void)
{
- return sysctl_perf_counter_paranoid > 1;
+ return !capable(CAP_SYS_ADMIN) && sysctl_perf_counter_paranoid > 2;
+}
+
+unsigned long perf_paranoid_anon_ip(void)
+{
+ return perf_paranoid_anon() ? _THIS_IP_ : 0;
}
int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
@@ -1571,7 +1582,7 @@ static struct perf_counter_context *find
*/
if (cpu != -1) {
/* Must be root to operate on a CPU counter: */
- if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
+ if (perf_paranoid_cpu())
return ERR_PTR(-EACCES);
if (cpu < 0 || cpu > num_possible_cpus())
@@ -2841,7 +2852,9 @@ void perf_counter_output(struct perf_cou
header.misc |= perf_misc_flags(data->regs);
if (sample_type & PERF_SAMPLE_IP) {
- ip = perf_instruction_pointer(data->regs);
+ ip = perf_paranoid_anon_ip();
+ if (!ip || user_mode(data->regs))
+ ip = perf_instruction_pointer(data->regs);
header.size += sizeof(ip);
}
@@ -4227,7 +4240,7 @@ SYSCALL_DEFINE5(perf_counter_open,
return ret;
if (!attr.exclude_kernel) {
- if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
+ if (perf_paranoid_kernel())
return -EACCES;
}
--
next prev parent reply other threads:[~2009-08-19 9:23 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-08-19 9:18 [PATCH 0/4] perf counter bits Peter Zijlstra
2009-08-19 9:18 ` Peter Zijlstra [this message]
2009-08-19 14:07 ` [PATCH 1/4] perf_counter: Default to higher paranoia level Peter Zijlstra
2009-08-19 16:04 ` Frederic Weisbecker
2009-08-20 12:00 ` Peter Zijlstra
2009-08-21 14:21 ` Ingo Molnar
2009-08-24 7:29 ` Peter Zijlstra
2009-08-24 7:37 ` Ingo Molnar
2009-08-19 9:18 ` [PATCH 2/4] perf_counter: powerpc: Support the anonymized kernel callchain bits Peter Zijlstra
2009-08-19 13:30 ` [tip:perfcounters/core] perf_counter: powerpc: Support the anonimized " tip-bot for Peter Zijlstra
2009-08-19 9:18 ` [PATCH 3/4] perf tools: Check perf.data owner Peter Zijlstra
2009-08-19 13:32 ` [tip:perfcounters/core] " tip-bot for Peter Zijlstra
2009-08-19 9:18 ` [PATCH 4/4][RFC] perf_counter: Allow sharing of output channels Peter Zijlstra
2009-08-19 10:58 ` Ingo Molnar
2009-08-19 11:07 ` Peter Zijlstra
2009-08-19 12:41 ` Paul Mackerras
2009-08-19 12:36 ` Paul Mackerras
2009-08-19 12:56 ` Ingo Molnar
2009-08-19 12:56 ` Peter Zijlstra
2009-08-19 13:00 ` Ingo Molnar
2009-08-20 10:13 ` stephane eranian
2009-08-20 10:24 ` Peter Zijlstra
2009-08-20 10:28 ` Ingo Molnar
2009-08-19 16:19 ` Frederic Weisbecker
2009-08-19 16:24 ` Peter Zijlstra
2009-08-19 16:27 ` Frederic Weisbecker
2009-08-25 7:39 ` [tip:perfcounters/core] " tip-bot for Peter Zijlstra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20090819092023.728070630@chello.nl \
--to=a.p.zijlstra@chello.nl \
--cc=acme@redhat.com \
--cc=efault@gmx.de \
--cc=fweisbec@gmail.com \
--cc=jens.axboe@oracle.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@elte.hu \
--cc=paulus@samba.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.