Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp689525imm; Wed, 19 Sep 2018 05:29:27 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYFnHx6JzKB29hYiecO68Dgfl8PlZBSMAEMVH3+BCrfpuPXS44gzblM5Ble0IFhsn0HI7S3 X-Received: by 2002:a17:902:7488:: with SMTP id h8-v6mr34182234pll.16.1537360167038; Wed, 19 Sep 2018 05:29:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537360167; cv=none; d=google.com; s=arc-20160816; b=D2RxFXwemWj5yF/TlTykCKtdu47fuNCB44bcZGqCVapLaDHWtmA7M19NrOIua2XSpE j55PtrobztFFUi96+KsO91CZ4wE4jEURXQDFfAxTaqnCytRmXSch+6G8+Ohu/aqJ9xUN 6uCzwEN8iwtISTjv5+cwe58ID6+DaeWr1NtGrUdxqhjcvPL1kAB1ZoIMdgv9suQZGUmt /1wcjRCs/WWvEsxpLsSjx9HFcqDDfjySDYio1jsdwNac67dQu93HazfXQGV8j04tfCKI T42GRzLJiDrPTyh3rcjfkgmx6AaJ3IbqzmzNjlzZn82G+tggj5EgRAY/1SwEFofB3+au xdHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=kqKCgu/jW3Jks7xXMY74YRJ4aD2Uozs5QdRDMkceTlg=; b=Tb1p9rxpNoqliTTK/uqpTnqZ623pkOLsWiX3dLpruAmYZaMYJRvNCGn3nqv/IkYJ2C 2lzHKENs2OC7kMn/pZRSw3zmge5iVfSeujQp/GLHpH04jyO520yyeFt8yoWPTyjJttt3 XlcBA6r9cTC+YHJAFSwgq4asFd2/8Mve32QG9x6ruobeyrRFWZXinLhYxEi7mpbGsKCx e+b9nBefe5AAxwwM9Jlf5vpiSacbZjajgLInFXXb7AX0+TAfiHsVF9QTVUm49lfhKwhU 4TddrJeN4KG1pux8AHvzmU2tyIRn/BY4SJxbi+KMV/7iFmd5eewCDjXR7Qb1xOKh67KM UKTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ursulin-net.20150623.gappssmtp.com header.s=20150623 header.b=nt8Lt0QP; 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 z188-v6si23193175pfb.26.2018.09.19.05.29.11; Wed, 19 Sep 2018 05:29:27 -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=@ursulin-net.20150623.gappssmtp.com header.s=20150623 header.b=nt8Lt0QP; 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 S1731623AbeISSFn (ORCPT + 99 others); Wed, 19 Sep 2018 14:05:43 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:34801 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731235AbeISSFm (ORCPT ); Wed, 19 Sep 2018 14:05:42 -0400 Received: by mail-wr1-f65.google.com with SMTP id g33-v6so5621183wrd.1 for ; Wed, 19 Sep 2018 05:27:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ursulin-net.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=kqKCgu/jW3Jks7xXMY74YRJ4aD2Uozs5QdRDMkceTlg=; b=nt8Lt0QPtnRP7sKN0gepGoCSru5VmtxHfBvs9D/T8TA+4rsVb055on6kOBrWeB9qxi ERA1ou+D3Cy6bLmIMwg4hvuUKzeN5A1aU36smQH+AcWb+oINzDEA+qsG62IxSfy0yzyO ql6IJSyVDiyUA34HtKGn4SM7AvPA1gRvVI0eUrUFL78bY38k8Uzpo0P+JWuyxNkRxCeT mIdV0WuepfS0me2s0s9gzjijIKcdaQhedHVwwwdjWIUaO2qJwBS9rBOWJSiUsKcXuHm/ ZKNa/wXmPw7CJc/gerqrjbFqZ8HUoCKdee/J1MXVrIXTS3QWIFUz0S6aKe68T4D0MdYV sFAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=kqKCgu/jW3Jks7xXMY74YRJ4aD2Uozs5QdRDMkceTlg=; b=kqQLwsxNPUKMN7DPxWF+0ChpWhLCBKU5kHWlMIAhKFV/FmY1uesdC+3e7kiZKHOkD/ vVr4hylvghqNsg+OTsXBzSLxwGaUgrmZkZKEBpXpjfp3Bu6lRhehR0E9dFXd1M1p47Xj Uwu+cpoOezQstpUfaR8dORHQ5BcacUhqqO/3LOoRToBmgV2eCfbc5So6vbhCVZjoRWzB cAiUjZ34VjEsoAW6imn9yywj7ubk7gut9NL7NNfA/9Kp8HiC/xWEDz1oIj7fepD5JGDF 0B6jFVePtXywwVUa3DST5P3CGq9sfXxAVGvbd/Ex0Hh3fuMZM6nrIATsNYeJFETjR49N zq0g== X-Gm-Message-State: APzg51CHHR1HUwRpmuON/gmr7Cp+uGjJIP/utjXyzNKaAwBCrMTe/9bM kZgmRnkiF6whM2boaKEAtC64au/rTGU= X-Received: by 2002:adf:e8c1:: with SMTP id k1-v6mr27430509wrn.43.1537360078593; Wed, 19 Sep 2018 05:27:58 -0700 (PDT) Received: from localhost.localdomain ([95.144.165.37]) by smtp.gmail.com with ESMTPSA id l18-v6sm19412403wru.75.2018.09.19.05.27.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 19 Sep 2018 05:27:58 -0700 (PDT) From: Tvrtko Ursulin X-Google-Original-From: Tvrtko Ursulin To: linux-kernel@vger.kernel.org Cc: tursulin@ursulin.net, tvrtko.ursulin@linux.intel.com, Tvrtko Ursulin , Thomas Gleixner , Peter Zijlstra , Ingo Molnar , "H. Peter Anvin" , Arnaldo Carvalho de Melo , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Madhavan Srinivasan , Andi Kleen , Alexey Budankov , x86@kernel.org Subject: [RFC 1/5] perf: Move some access checks later in perf_event_open Date: Wed, 19 Sep 2018 13:27:47 +0100 Message-Id: <20180919122751.12439-2-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180919122751.12439-1-tvrtko.ursulin@linux.intel.com> References: <20180919122751.12439-1-tvrtko.ursulin@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tvrtko Ursulin To enable per-PMU access controls in a following patch first move all call sites of perf_paranoid_kernel() to after the event has been created. Signed-off-by: Tvrtko Ursulin Cc: Thomas Gleixner Cc: Peter Zijlstra Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: Arnaldo Carvalho de Melo Cc: Alexander Shishkin Cc: Jiri Olsa Cc: Namhyung Kim Cc: Madhavan Srinivasan Cc: Andi Kleen Cc: Alexey Budankov Cc: linux-kernel@vger.kernel.org Cc: x86@kernel.org --- kernel/events/core.c | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index bccce538f51d..adcd9eae13fb 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -10194,10 +10194,6 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr, */ attr->branch_sample_type = mask; } - /* privileged levels capture (kernel, hv): check permissions */ - if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM) - && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) - return -EACCES; } if (attr->sample_type & PERF_SAMPLE_REGS_USER) { @@ -10414,11 +10410,6 @@ SYSCALL_DEFINE5(perf_event_open, if (err) return err; - if (!attr.exclude_kernel) { - if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) - return -EACCES; - } - if (attr.namespaces) { if (!capable(CAP_SYS_ADMIN)) return -EACCES; @@ -10432,11 +10423,6 @@ SYSCALL_DEFINE5(perf_event_open, return -EINVAL; } - /* Only privileged users can get physical addresses */ - if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR) && - perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) - return -EACCES; - /* * In cgroup mode, the pid argument is used to pass the fd * opened to the cgroup directory in cgroupfs. The cpu argument @@ -10506,6 +10492,28 @@ SYSCALL_DEFINE5(perf_event_open, goto err_cred; } + if (!attr.exclude_kernel) { + if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) { + err = -EACCES; + goto err_alloc; + } + } + + /* Only privileged users can get physical addresses */ + if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR) && + perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) { + err = -EACCES; + goto err_alloc; + } + + /* privileged levels capture (kernel, hv): check permissions */ + if ((attr.sample_type & PERF_SAMPLE_BRANCH_STACK) && + (attr.branch_sample_type & PERF_SAMPLE_BRANCH_PERM_PLM) && + perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) { + err = -EACCES; + goto err_alloc; + } + if (is_sampling_event(event)) { if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { err = -EOPNOTSUPP; -- 2.17.1