Skip to content

Commit

Permalink
perf_counter: More paranoia settings
Browse files Browse the repository at this point in the history
Rename the perf_counter_priv knob to perf_counter_paranoia (because
priv can be read as private, as opposed to privileged) and provide
one more level:

 0 - permissive
 1 - restrict cpu counters to privilidged contexts
 2 - restrict kernel-mode code counting and profiling

Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
  • Loading branch information
Peter Zijlstra authored and Ingo Molnar committed Jun 11, 2009
1 parent 106b506 commit 0764771
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/linux/perf_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ struct perf_callchain_entry {

extern struct perf_callchain_entry *perf_callchain(struct pt_regs *regs);

extern int sysctl_perf_counter_priv;
extern int sysctl_perf_counter_paranoid;
extern int sysctl_perf_counter_mlock;
extern int sysctl_perf_counter_limit;

Expand Down
25 changes: 23 additions & 2 deletions kernel/perf_counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,23 @@ static atomic_t nr_counters __read_mostly;
static atomic_t nr_mmap_counters __read_mostly;
static atomic_t nr_comm_counters __read_mostly;

int sysctl_perf_counter_priv __read_mostly; /* do we need to be privileged */
/*
* 0 - not paranoid
* 1 - disallow cpu counters to unpriv
* 2 - disallow kernel profiling to unpriv
*/
int sysctl_perf_counter_paranoid __read_mostly; /* do we need to be privileged */

static inline bool perf_paranoid_cpu(void)
{
return sysctl_perf_counter_paranoid > 0;
}

static inline bool perf_paranoid_kernel(void)
{
return sysctl_perf_counter_paranoid > 1;
}

int sysctl_perf_counter_mlock __read_mostly = 512; /* 'free' kb per user */
int sysctl_perf_counter_limit __read_mostly = 100000; /* max NMIs per second */

Expand Down Expand Up @@ -1385,7 +1401,7 @@ static struct perf_counter_context *find_get_context(pid_t pid, int cpu)
*/
if (cpu != -1) {
/* Must be root to operate on a CPU counter: */
if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
return ERR_PTR(-EACCES);

if (cpu < 0 || cpu > num_possible_cpus())
Expand Down Expand Up @@ -3618,6 +3634,11 @@ SYSCALL_DEFINE5(perf_counter_open,
if (copy_from_user(&attr, attr_uptr, sizeof(attr)) != 0)
return -EFAULT;

if (!attr.exclude_kernel) {
if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
return -EACCES;
}

/*
* Get the target context (task or percpu):
*/
Expand Down
6 changes: 3 additions & 3 deletions kernel/sysctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -916,9 +916,9 @@ static struct ctl_table kern_table[] = {
#ifdef CONFIG_PERF_COUNTERS
{
.ctl_name = CTL_UNNUMBERED,
.procname = "perf_counter_privileged",
.data = &sysctl_perf_counter_priv,
.maxlen = sizeof(sysctl_perf_counter_priv),
.procname = "perf_counter_paranoid",
.data = &sysctl_perf_counter_paranoid,
.maxlen = sizeof(sysctl_perf_counter_paranoid),
.mode = 0644,
.proc_handler = &proc_dointvec,
},
Expand Down

0 comments on commit 0764771

Please sign in to comment.