Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp6094092imm; Wed, 27 Jun 2018 02:02:05 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJIxAkKMG5tZk9wb9r69FyjBcgRQ7vBl5wd/sEpvLt7cwjX5h33Q4jcEvTHbXlqZCJjDszw X-Received: by 2002:a65:5c02:: with SMTP id u2-v6mr4409650pgr.304.1530090125029; Wed, 27 Jun 2018 02:02:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530090124; cv=none; d=google.com; s=arc-20160816; b=sTZx/DP9Vze3AnA7cWzhGrMdKy3MH9RT7BHtwdUvzl1VkrvMMetfnLHzoke7PDCQ1O oiJRLNT9D439E+EaeyhU2zWzS1Az+mKyCtcM2KncDV5f2ZsJkSy9f2e9fvfr5aPq33hu Jjkf9+qv+jMm4TdKFSZueg7C8mKINxeHLIIweZ6OjLGaUMsFTkjGszcjR70WBgPXrEmA 3jY+JL0FiKipVSlLXv5E2FUB2/vCpmRSWJ9MvnCFpQpfmwfMkEUkCjpxludR2Z9b/Rz1 S7sTJWGkOUesZyjl2v2yvpuAE24t7ycJeGKH129cTsQrEiCOZtYmE6ChM6pqhsev7hG8 OVjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature :arc-authentication-results; bh=pBpYWuK6rmiHygTJEzunKDodCOXKughuCiCPMfoXbZ0=; b=zTwFLCIQUtGareFjb/CpqsvkQIQ/SIiBv5CWpngAyaTLLWAnyXRXBxb+ZFd8N7Ai92 ekFdzgMxn6ogbUNha8PwI8CAuPIUU6ojyV/YXwmzoVPi+Qws6ayS0mzSoRjqWvyF+0ut hykXXqmz+5DaUBnr9eioQz7zjQMIsCrY+iUBWuub1FDOC5/kLQDfG3HABhPwl0q1Y9GN 3ucniJUiflyXfqvuSpPWhQ8LBySKBMmspEO099eR4g1ulWw779VDNRHqwGYL+r2wNYC6 jV3Gra5GqOPJJzLWcix43GAlhCGvUa040J1JxLE1p1xom4IjWOlIjVqPrGagWOCWg5N9 NWog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ursulin-net.20150623.gappssmtp.com header.s=20150623 header.b=QABzltTi; 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 c8-v6si2968594pgn.473.2018.06.27.02.01.46; Wed, 27 Jun 2018 02:02:04 -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=QABzltTi; 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 S933872AbeF0JA5 (ORCPT + 99 others); Wed, 27 Jun 2018 05:00:57 -0400 Received: from mail-wr0-f193.google.com ([209.85.128.193]:38667 "EHLO mail-wr0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933529AbeF0JAq (ORCPT ); Wed, 27 Jun 2018 05:00:46 -0400 Received: by mail-wr0-f193.google.com with SMTP id e18-v6so1195406wrs.5 for ; Wed, 27 Jun 2018 02:00:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ursulin-net.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=pBpYWuK6rmiHygTJEzunKDodCOXKughuCiCPMfoXbZ0=; b=QABzltTiFYe5GEfW2/q0ht6Za2ORvAtyeOUIUI8yEuLSW7aITPq1jCm4S3NzMaYRzz HisFnsIXluxcPljvRjxsQGrglp9TbspwnB7i179L0K21Mt7Rqvez5U/CeRX2zMFNWgwE eJIZ9PuMwMfRzWvDducVQ5iV56WLhmYpvy4dMWgJ4CErx1uZxNRom2Qz7djl+myOsJ9R t+38/TMeD0OscYcpkxGKHQ1y7Ifp5yejprB2ENg5VD0DrIJeoy1HGODh9TQwMCJzQqOU j/ijxTVJX1Qiegw4ySmzUkPjZIjDP4kV6cmg48hZMff/UkOQ3Z5Yy6JPd8Mb25clBuVx wMfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=pBpYWuK6rmiHygTJEzunKDodCOXKughuCiCPMfoXbZ0=; b=eLt/bfS0s2MTO0ztUJUV5gHiGM+kx/eu7FLp60/7RazFj//kV1eQ7PxYyClLLI5qvu rDUUGRLpGaxmm4xlICGdUOwdeVxOSRo36QH5ePFlVI67aE+P35DzpU9xlWq+uHL+Ob9d 88e84ocCzDIdZgeQGnr4ZyrEd3GY8lG2SDvH279CpuIwsBr9B5yxz1Qg8FeqpM7GGrDY uRAYaGCS6CwwgjN1E3czKjSlcYTVsu4bda9GS+YHAhTKJsUgQGHgSNWGf9mS4rW0PfBA r/mLXIrTPlsoB2PX1A5osHpBZJ9tHXbu86MmG5l3J2g7oSc0gcfP9muvOxWb5tXJBPgg 3kGg== X-Gm-Message-State: APt69E0yqegsj6NsmIp9WzIvoFG3y10zRmnZkFxeGqMLzObwoRu+M1KT x/+CsdeCSNmb6DhwlN0qF/ZZxQ== X-Received: by 2002:adf:e644:: with SMTP id b4-v6mr4604891wrn.254.1530090045215; Wed, 27 Jun 2018 02:00:45 -0700 (PDT) Received: from [192.168.0.197] ([95.146.151.144]) by smtp.googlemail.com with ESMTPSA id m3-v6sm3126559wro.27.2018.06.27.02.00.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 Jun 2018 02:00:43 -0700 (PDT) Subject: Re: [RFC 1/4] perf: Move some access checks later in perf_event_open To: Alexey Budankov , linux-kernel@vger.kernel.org Cc: 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 , x86@kernel.org References: <20180626153642.5587-1-tvrtko.ursulin@linux.intel.com> <20180626153642.5587-2-tvrtko.ursulin@linux.intel.com> <861aa316-fa18-4385-af84-0e9445f261d1@linux.intel.com> From: Tvrtko Ursulin Message-ID: Date: Wed, 27 Jun 2018 10:00:42 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: <861aa316-fa18-4385-af84-0e9445f261d1@linux.intel.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 26/06/18 18:24, Alexey Budankov wrote: > Hi, > > On 26.06.2018 18:36, Tvrtko Ursulin wrote: >> 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 f490caca9aa4..12de95b0472e 100644 >> --- a/kernel/events/core.c >> +++ b/kernel/events/core.c >> @@ -10189,10 +10189,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) { >> @@ -10409,11 +10405,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; >> @@ -10427,11 +10418,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 >> @@ -10501,6 +10487,28 @@ SYSCALL_DEFINE5(perf_event_open, >> goto err_cred; >> } >> >> + if (!attr.exclude_kernel) { >> + if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) { >> + err = -EACCES; > > I would separate this combined permissions check into a function e.g. > static bool perf_test_pmu_paranoid(const struct pmu *pmu, int *err) to avoid > code duplication. My thinking was for this to be as mechanical (code movement) as possible, but I can consider it. Regards, Tvrtko >> + 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; >> >