Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp2498132ybp; Thu, 10 Oct 2019 08:14:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqzH8DxdUZ+6HIwBv4R23rm7rkzcEY1NYBiVkZ5yCOQZp2DLj9w42/qTNdpqoz3+/nt3pPBo X-Received: by 2002:a17:906:940a:: with SMTP id q10mr8700148ejx.250.1570720471863; Thu, 10 Oct 2019 08:14:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570720471; cv=none; d=google.com; s=arc-20160816; b=BJnJx4BlUO2RVKGrwY3RA3icWzyHRSu7XDfQUGMUeRm+PbgvDNN38ccQWgiDIHVNxz /kJyAW8RWGNCz55O+Kgkk/HefShOPJfDFCtt9ZZ8PH9Mz7m4M86GGb7UBvpXHfrceUCh lw3zbG+gKUbErdwsYib+UPEw2z7s3kDz1WTMbca3sIZrCSxTOSkQkAtBszhDGDhsc8Lx 8clI8aE5/T485DKyLD1KyW1LOSi30wg2DSmE962HhQdf632fQudnSR+AmhoKBhKC2RRf GvabypmdXar4lItAQEM6LdS/WE1ef0/B+NxTiGQI8QgPAGaslEqh8ewEwSyDqmvfCSFQ s95g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=VXeYBQAM8h1PaL67Bf61gIW65uw4eLRIRvHFtIpEQhY=; b=BThctAHza5/tHJKySmTjWaG2SdYF1/B1emhzQEcbd6Yw5RxI1RxQ9VLUZHG9YyizH3 BO9pGD5fK6XUAFLPk6NziWUL3nYUScE/dT2GWcGBOFLKBPICJ21Llh3bpCGoSOHkKUv7 8C7ygqVo3prCcKsb90D/jiX54+p6zShLylDUY2ld+VB+4Glj7Bl/tndrB3EEycTiOTj+ DK8cGTBNtGed3Dl/oJ6LyFHXQAhhfOSh3My29nL+N2s7StwtV2oA6ZMwjO4SPsxbq+7E dgblKN+wTZ3E4ZTaPVAXz2qsf0lEgfitZCfbMZ+BbpO5tM80HJuGM+V7+bSLPBAQiHkL 5UoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b="JDqRmA/d"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id bq16si3304683ejb.221.2019.10.10.08.14.04; Thu, 10 Oct 2019 08:14:31 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b="JDqRmA/d"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726320AbfJJPNi (ORCPT + 99 others); Thu, 10 Oct 2019 11:13:38 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:38235 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725901AbfJJPNi (ORCPT ); Thu, 10 Oct 2019 11:13:38 -0400 Received: by mail-pf1-f194.google.com with SMTP id h195so4103166pfe.5 for ; Thu, 10 Oct 2019 08:13:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=VXeYBQAM8h1PaL67Bf61gIW65uw4eLRIRvHFtIpEQhY=; b=JDqRmA/dj97Eh+82JlJ7S4i+CfdlJn6XhjAl+rM29OqBccgZJHcpuLQi3eydE8QPru y1wnnMHIZTrTS0K7h7GzTEBMRAYEaztShrmEZf7Z6n/KVlomGMl0k8oApxadE04OX4qz Daw6e8YITBd6KZ4Zl3hz3mM9SV7SwYMrngXQ0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=VXeYBQAM8h1PaL67Bf61gIW65uw4eLRIRvHFtIpEQhY=; b=L9ZkIKVRik31ET21IVGZgfzOaRnFvD4nwv/Lmg8Cn5jiCJrbRIQkyg6t5BotB9jBnl QAatzQZQWUmxW2sj5fUw/YwJAwKEhJVghVJ8GT6ONQ5vI4VFvLzR/2BthjpmX/ZnT0Ux 23inkEizAp99e/OByyTUmqfg9opQFxnTy8j9woAzjWiHdFq08oLiHnHz88AdKUEkw/31 ChO6vWv+wV6kOBBAwThJpU/skI0wxS1Qj9I6yk9XzINaYSyS7oOeAYNUpJCUBjMiDSgR lrQIE80UWTGE6u6MDvRGbSqjShMHGgB2szRalVha//kLFd8K9i9XPKBPhL+uQclxeVKQ dyrQ== X-Gm-Message-State: APjAAAUWCv3IILceYuDj5XiWOcn3buQU3egoQyewlDNwYQDHtzw/R26e B9x+O7CUF9ACjH6Q8ADgC4ImFw== X-Received: by 2002:a65:4208:: with SMTP id c8mr11471239pgq.230.1570720415595; Thu, 10 Oct 2019 08:13:35 -0700 (PDT) Received: from localhost ([2620:15c:6:12:9c46:e0da:efbf:69cc]) by smtp.gmail.com with ESMTPSA id y10sm5561179pfe.148.2019.10.10.08.13.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2019 08:13:34 -0700 (PDT) Date: Thu, 10 Oct 2019 11:13:33 -0400 From: Joel Fernandes To: Peter Zijlstra Cc: linux-kernel@vger.kernel.org, rostedt@goodmis.org, primiano@google.com, rsavitski@google.com, jeffv@google.com, kernel-team@android.com, Alexei Starovoitov , Arnaldo Carvalho de Melo , bpf@vger.kernel.org, Daniel Borkmann , Ingo Molnar , James Morris , Jiri Olsa , Kees Cook , linux-security-module@vger.kernel.org, Matthew Garrett , Namhyung Kim , selinux@vger.kernel.org, Song Liu , "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" , Yonghong Song Subject: Re: [PATCH RFC] perf_event: Add support for LSM and SELinux checks Message-ID: <20191010151333.GE96813@google.com> References: <20191009203657.6070-1-joel@joelfernandes.org> <20191010081251.GP2311@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20191010081251.GP2311@hirez.programming.kicks-ass.net> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 10, 2019 at 10:12:51AM +0200, Peter Zijlstra wrote: > On Wed, Oct 09, 2019 at 04:36:57PM -0400, Joel Fernandes (Google) wrote: > > In currentl mainline, the degree of access to perf_event_open(2) system > > call depends on the perf_event_paranoid sysctl. This has a number of > > limitations: > > > > 1. The sysctl is only a single value. Many types of accesses are controlled > > based on the single value thus making the control very limited and > > coarse grained. > > 2. The sysctl is global, so if the sysctl is changed, then that means > > all processes get access to perf_event_open(2) opening the door to > > security issues. > > > > This patch adds LSM and SELinux access checking which will be used in > > Android to access perf_event_open(2) for the purposes of attaching BPF > > programs to tracepoints, perf profiling and other operations from > > userspace. These operations are intended for production systems. > > > > 5 new LSM hooks are added: > > 1. perf_event_open: This controls access during the perf_event_open(2) > > syscall itself. The hook is called from all the places that the > > perf_event_paranoid sysctl is checked to keep it consistent with the > > systctl. The hook gets passed a 'type' argument which controls CPU, > > kernel and tracepoint accesses (in this context, CPU, kernel and > > tracepoint have the same semantics as the perf_event_paranoid sysctl). > > Additionally, I added an 'open' type which is similar to > > perf_event_paranoid sysctl == 3 patch carried in Android and several other > > distros but was rejected in mainline [1] in 2016. > > > > 2. perf_event_alloc: This allocates a new security object for the event > > which stores the current SID within the event. It will be useful when > > the perf event's FD is passed through IPC to another process which may > > try to read the FD. Appropriate security checks will limit access. > > > > 3. perf_event_free: Called when the event is closed. > > > > 4. perf_event_read: Called from the read(2) system call path for the event. > > + mmap() > > > > 5. perf_event_write: Called from the read(2) system call path for the event. > > - read() + ioctl() Fixed. > > fresh from the keyboard.. but maybe consoldate things a little. Looks great to me, I folded it into the patch. Thanks Peter! Just one comment on change in existing logic of the code, below: [snip] > --- a/arch/x86/events/intel/p4.c > +++ b/arch/x86/events/intel/p4.c > @@ -8,7 +8,6 @@ > */ > > #include > -#include > > #include > #include > @@ -777,10 +776,7 @@ static int p4_validate_raw_event(struct > * the user needs special permissions to be able to use it > */ > if (p4_ht_active() && p4_event_bind_map[v].shared) { > - if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) > - return -EACCES; > - > - v = security_perf_event_open(&event->attr, PERF_SECURITY_CPU); > + v = perf_allow_cpu(&event->attr); > if (v) > return v; > } > --- a/include/linux/perf_event.h > +++ b/include/linux/perf_event.h > @@ -56,6 +56,7 @@ struct perf_guest_info_callbacks { > #include > #include > #include > +#include > #include > > struct perf_callchain_entry { > @@ -1244,19 +1245,28 @@ extern int perf_cpu_time_max_percent_han > int perf_event_max_stack_handler(struct ctl_table *table, int write, > void __user *buffer, size_t *lenp, loff_t *ppos); > > -static inline bool perf_paranoid_tracepoint_raw(void) > +static inline int perf_allow_kernel(struct perf_event_attr *attr) > { > - return sysctl_perf_event_paranoid > -1; > + if (sysctl_perf_event_paranoid > 1 && !capable(CAP_SYS_ADMIN)) > + return -EACCES; > + > + return security_perf_event_open(attr, PERF_SECURITY_KERNEL); > } > > -static inline bool perf_paranoid_cpu(void) > +static inline int perf_allow_cpu(struct perf_event_attr *attr) > { > - return sysctl_perf_event_paranoid > 0; > + if (sysctl_perf_event_paranoid > 0 && !capable(CAP_SYS_ADMIN)) > + return -EACCES; > + > + return security_perf_event_open(attr, PERF_SECURITY_CPU); > } > > -static inline bool perf_paranoid_kernel(void) > +static inline int perf_allow_tracepoint(struct perf_event_attr *attr) > { > - return sysctl_perf_event_paranoid > 1; > + if (sysctl_perf_event_paranoid > -1 && !capable(CAP_SYS_ADMIN)) > + return -EPERM; > + Here the sysctl check of > -1 also is now coupled with a CAP_SYS_ADMIN check. However.. > + return security_perf_event_open(attr, PERF_SECURITY_TRACEPOINT); > } > > extern void perf_event_init(void); > --- a/kernel/events/core.c > +++ b/kernel/events/core.c > @@ -4229,10 +4229,7 @@ find_get_context(struct pmu *pmu, struct > > if (!task) { > /* Must be root to operate on a CPU event: */ > - if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) > - return ERR_PTR(-EACCES); > - > - err = security_perf_event_open(&event->attr, PERF_SECURITY_CPU); > + err = perf_allow_cpu(&event->attr); > if (err) > return ERR_PTR(err); > > @@ -5862,14 +5859,8 @@ static int perf_mmap(struct file *file, > lock_limit >>= PAGE_SHIFT; > locked = atomic64_read(&vma->vm_mm->pinned_vm) + extra; > > - if (locked > lock_limit) { > - if (perf_paranoid_tracepoint_raw() && !capable(CAP_IPC_LOCK)) { > - ret = -EPERM; > - goto unlock; > - } > - > - ret = security_perf_event_open(&event->attr, > - PERF_SECURITY_TRACEPOINT); > + if (locked > lock_limit && !capable(CAP_IPC_LOCK)) { > + ret = perf_allow_tracepoint(&event->attr); In previous code, this check did not involve a check for CAP_SYS_ADMIN. I am Ok with adding the CAP_SYS_ADMIN check as well which does make sense to me for tracepoint access. But it is still a change in the logic so I wanted to bring it up. Let me know any other thoughts and then I'll post a new patch. thanks, - Joel [snip]