Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp3778132pxt; Tue, 10 Aug 2021 11:06:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyIqqXAUJqFozLyk2xokf72OhZJUH66DliNOAhWD332gcLt/Nt02+9p/wd80rajAB/zfJBU X-Received: by 2002:a92:8707:: with SMTP id m7mr89081ild.177.1628618773615; Tue, 10 Aug 2021 11:06:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628618773; cv=none; d=google.com; s=arc-20160816; b=qrYVhr90eYXSmXUozCvtRY4qq+DvyiV2EzCKTVJ/vBfFpn6+olL4636kzxdwx3bLZx zt5ko1sS9aKvF2OuTNy9+O5xDTifD/tuEsNz58ql9S1fZnVHYhfAa9EUWGCrlISeJHOF nTA9PyxkkujTcMxxtuJmC+39e3fikJNkA4DeR/e8WbCco9uFxbjEd8yuKeIerA81jIoU rGAi9Ps59VwFAIoOscNEisDesprAV0eVMkE6CyxT6gPVZ2AaE/rmI/N4HoFjEUMh2aRX HN2wLdu2cIg+wWOzSAtXhNiJrk1UjYE48/EAvXSJVmyiRBTt3SwSzv0FNp57wJxpmUHc XgYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=BXJ7k9n68p7pkAT99W0rvMCweTPLhXgAxcf9nbgTCvs=; b=ui5b2K9/uuClR/+Y+ym1sIx26lYqM0xvm91Qy/S5Ons6FXGnBXZZbr26Q3PR8vFkOM Se66bKqu4RhVJMo3fytI1VmUJRat8+IgTy4quP0UHdp577pfIGYUNzi+5U7IIygaA0rX zGhJKafMVXWkP9qF12wSWDum/IgbbdXY/9DmFftFwNJuPjAP5/OI6K5+PVRdvAlpW63b y6LD10qVZR6jNClnayYUSeJI8XAbFkocymQUafbyNgt97U+wARHUmYoVHpKkrfT0FK+a m+iz+SOjntyyznZXaNCqK2HYw3Ml+FhFVV4dgHyYmvFzWmpEh8lL9e6x6GkzSwYaeC65 M0gg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fCkWC3jo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n9si20990487jam.54.2021.08.10.11.06.02; Tue, 10 Aug 2021 11:06:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fCkWC3jo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239354AbhHJSFJ (ORCPT + 99 others); Tue, 10 Aug 2021 14:05:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:37208 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237657AbhHJSBa (ORCPT ); Tue, 10 Aug 2021 14:01:30 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7198B613CF; Tue, 10 Aug 2021 17:47:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1628617625; bh=G8lrQHm1yBvrEMOOKL50FarquT70ja8gKFV60lbVm8I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fCkWC3joavFQwlyCSu8citEspl71DRje8aMqaMfanc29Oz3ZWCVX9W9O79cB9GcW7 hPEZVYhspSVovzoH7rqkIVBYrHDHuDEXcY4M3MUywmNkfr6pxWtu4OCZOJZIpS+FFk 6BNHkhyeqcJjuI9dV3X4VjXkzNhwIJslpybFSBpw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dmitry Vyukov , Marco Elver , "Peter Zijlstra (Intel)" Subject: [PATCH 5.13 134/175] perf: Fix required permissions if sigtrap is requested Date: Tue, 10 Aug 2021 19:30:42 +0200 Message-Id: <20210810173005.356407159@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210810173000.928681411@linuxfoundation.org> References: <20210810173000.928681411@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Marco Elver commit 9d7a6c95f62bc335b62aaf9d50590122bd03a796 upstream. If perf_event_open() is called with another task as target and perf_event_attr::sigtrap is set, and the target task's user does not match the calling user, also require the CAP_KILL capability or PTRACE_MODE_ATTACH permissions. Otherwise, with the CAP_PERFMON capability alone it would be possible for a user to send SIGTRAP signals via perf events to another user's tasks. This could potentially result in those tasks being terminated if they cannot handle SIGTRAP signals. Note: The check complements the existing capability check, but is not supposed to supersede the ptrace_may_access() check. At a high level we now have: capable of CAP_PERFMON and (CAP_KILL if sigtrap) OR ptrace_may_access(...) // also checks for same thread-group and uid Fixes: 97ba62b27867 ("perf: Add support for SIGTRAP on perf events") Reported-by: Dmitry Vyukov Signed-off-by: Marco Elver Signed-off-by: Peter Zijlstra (Intel) Acked-by: Dmitry Vyukov Cc: # 5.13+ Link: https://lore.kernel.org/r/20210705084453.2151729-1-elver@google.com Signed-off-by: Greg Kroah-Hartman --- kernel/events/core.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -12159,10 +12159,33 @@ SYSCALL_DEFINE5(perf_event_open, } if (task) { + unsigned int ptrace_mode = PTRACE_MODE_READ_REALCREDS; + bool is_capable; + err = down_read_interruptible(&task->signal->exec_update_lock); if (err) goto err_file; + is_capable = perfmon_capable(); + if (attr.sigtrap) { + /* + * perf_event_attr::sigtrap sends signals to the other + * task. Require the current task to also have + * CAP_KILL. + */ + rcu_read_lock(); + is_capable &= ns_capable(__task_cred(task)->user_ns, CAP_KILL); + rcu_read_unlock(); + + /* + * If the required capabilities aren't available, checks + * for ptrace permissions: upgrade to ATTACH, since + * sending signals can effectively change the target + * task. + */ + ptrace_mode = PTRACE_MODE_ATTACH_REALCREDS; + } + /* * Preserve ptrace permission check for backwards compatibility. * @@ -12172,7 +12195,7 @@ SYSCALL_DEFINE5(perf_event_open, * perf_event_exit_task() that could imply). */ err = -EACCES; - if (!perfmon_capable() && !ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) + if (!is_capable && !ptrace_may_access(task, ptrace_mode)) goto err_cred; }