Received: by 2002:a05:7208:70d5:b0:7f:5597:fa5c with SMTP id q21csp1290812rba; Fri, 22 Mar 2024 09:28:36 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXTYLqm/4V5AQc3YN/KU0nrzbW4eryi39L6HKoOqXDweOUPohHA9bQ9ssMcz41hXFP/j+obr5qUDBmq+bjKK8sZXskxpj/Um/4hGbbdrw== X-Google-Smtp-Source: AGHT+IEpxiEAEuAY8RVjAkJXEFNIploU496c41PCHyVOfuiB2XW3xlBZab2AHnuXqDKrwt9ujmMB X-Received: by 2002:a05:6a20:bf28:b0:1a3:6397:e253 with SMTP id gc40-20020a056a20bf2800b001a36397e253mr90705pzb.28.1711124916599; Fri, 22 Mar 2024 09:28:36 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711124916; cv=pass; d=google.com; s=arc-20160816; b=vrhMMjAlfg7LQpWOaUJH98reav3gzCYEegf36+2x7oMEz5nGL1nKtebovxlx4kJKHz fy1U5v4/uLT+gNwscFeXQj//wrisCID3C+HVPzwgwBOQ7A4x3wiJoK/+XDV8wxetM5Sa 4EycKSeMV52eeknsIzIPs5YwtwbzI/6snclTa0bVikR7ryAtnkkoURMAmuT3WDmIpyb2 Ev47EAG5xQiPI462glmryiXLJRa1sWk5bvLfIGCcGuGnxEdHOct4y7p8i9NzpOUywMhk ZOTpICBZUbRbm32yqKGasDP0tFz+YqxE4UHaOzA5B8imtVatMzWG4y/HAJgBrEO2hLNy P13Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=9vYNNsWu2J7ucpvtRYDpW+Tb98CHzZkSy4Ql6GR3hhI=; fh=pVIY3pOXLsfef5zI/fKxSZPmVxUt2R3PlLgWG9+QGZo=; b=SPods49StyblgAt4xnWhj2ELeRqFq3Pj8T3Qce/4KYtvqOX+Nl3lZIyVV/RrFnvpVd VZtoxX1aZusYS5xGT9B+VQEaa8efqeWsg2szV4zvfo4Z85CDC2194Oxr5FaI5Zu17jzk yPtGk8d18ZgvblW7A26DdZs5jWKFb5hIA0suHaf50tlVepvRnjydm3fSh5gCmq+v4PK8 FTDkDO0PSLjsGuQrW4KeVndQ+5lQSisV/yOFEG3osbY0rECLIaNCctOGeZNr4EyIlCch muT1GnyHx/RUzbgVAsD3aiN30/ZB1xtqGV2wBFtruimeHsUGfmdf8Nj5aB7WIY07NBHA 9CxQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-111777-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111777-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id l2-20020a63f302000000b005d7afbb31b3si2145609pgh.352.2024.03.22.09.28.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 09:28:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-111777-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-111777-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111777-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id D0DE3B227FA for ; Fri, 22 Mar 2024 16:28:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E9A515917D; Fri, 22 Mar 2024 16:28:25 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BCB6C5674E; Fri, 22 Mar 2024 16:28:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711124905; cv=none; b=L+h1BtTih1G3mWydMe0dtg0HFDuRiDTpZPEbyIftFGRf+btUWNqYam4hfDQhKrkhBH6wU26nQKQh5Yzn5DjtrnnNUhJZrDIBIPnfoCRldbHQXiHNSYC4pZA3sKTskEYZTJHJSCFk8CZ8gtRQ+/EWXHQF4/tv6Nf9ZDg27hDghQo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711124905; c=relaxed/simple; bh=lRVx4ECF5hJdd5NdS5SDeG9HLw77wx4Jl92htBH1/iY=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=Rn7WxdXTvYhM9a3Yai+9cSfKKYRrPxt0BKWxraInIPsIPEARhxVHA4N5SHHEJmz1a0EAe+Yu9j3Lv2SrZvKve1jlE7+3KDASSkky65YuufqeAt7brAM2ub24qwS2vkMsq94XljrT83IBIf6wJc/lGNf5xvbzNO7xtvJstBaSnhw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0DC7EFEC; Fri, 22 Mar 2024 09:28:56 -0700 (PDT) Received: from PF4Q20KV.arm.com (PF4Q20KV.arm.com [10.1.26.23]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id A2D2B3F762; Fri, 22 Mar 2024 09:28:19 -0700 (PDT) From: Leo Yan To: Alexander Viro , Christian Brauner , Jan Kara , Eric Biederman , Kees Cook , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Ian Rogers Cc: Leo Yan , Al Grant , James Clark , Mark Rutland Subject: [PATCH] exec: Don't disable perf events for setuid root executables Date: Fri, 22 Mar 2024 16:27:59 +0000 Message-Id: <20240322162759.714141-1-leo.yan@arm.com> X-Mailer: git-send-email 2.39.2 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Al Grant reported that the 'perf record' command terminates abnormally after setting the setuid bit for the executable. To reproduce this issue, an additional condition is the binary file is owned by the root user but is running under a non-privileged user. The logs below provide details: $ sudo chmod u+s perf $ ls -l perf -rwsr-xr-x 1 root root 13147600 Mar 17 14:56 perf $ ./perf record -e cycles -- uname [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.003 MB perf.data (7 samples) ] Terminated Comparatively, the same command can succeed if the setuid bit is cleared for the perf executable: $ sudo chmod u-s perf $ ls -l perf -rwxr-xr-x 1 root root 13147600 Mar 17 14:56 perf $ ./perf record -e cycles -- uname Linux [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.003 MB perf.data (13 samples) ] After setting the setuid bit, the problem arises when begin_new_exec() disables the perf events upon detecting that a regular user is executing a setuid binary, which notifies the perf process. Consequently, the perf tool in user space exits from polling and sends a SIGTERM signal to kill child processes and itself. This explains why we observe the tool being 'Terminated'. With the setuid bit a non-privileged user can obtain the same permissions as the executable's owner. If the owner has the privileged permission for accessing perf events, the kernel should keep enabling perf events. For this reason, this patch adds a condition checking for perfmon_capable() to not disabling perf events when the user has privileged permission yet. Note the begin_new_exec() function only checks permission for the per-thread mode in a perf session. This is why we don't need to add any extra checking for the global knob 'perf_event_paranoid', as it always grants permission for per-thread performance monitoring for unprivileged users (see Documentation/admin-guide/perf-security.rst). Signed-off-by: Leo Yan Cc: Al Grant Cc: James Clark Cc: Mark Rutland --- fs/exec.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/exec.c b/fs/exec.c index ff6f26671cfc..5ded01190278 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1401,7 +1401,8 @@ int begin_new_exec(struct linux_binprm * bprm) * wait until new credentials are committed * by commit_creds() above */ - if (get_dumpable(me->mm) != SUID_DUMP_USER) + if ((get_dumpable(me->mm) != SUID_DUMP_USER) && + !perfmon_capable()) perf_event_exit_task(me); /* * cred_guard_mutex must be held at least to this point to prevent -- 2.39.2