Received: by 2002:ab2:7a09:0:b0:1f8:46dc:890e with SMTP id k9csp59853lqo; Wed, 15 May 2024 07:44:15 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUX03NVcI0DUQKU59gj8W1eCc/+XkiykxcN+kADx9vbWyy+FY+aQqqeKdEky9E1clBNtTgXtoYyRE2n47WkL+wEFelb/ZScyb2bo+aasA== X-Google-Smtp-Source: AGHT+IE7bFmQ/fXxSnIuOfk8xsR5V5npxpoREuhRg1QZD6gDoE4jeeSkaxWn0ClsfDLyEv6E25fL X-Received: by 2002:a17:90a:ce18:b0:2a5:f85b:8b2c with SMTP id 98e67ed59e1d1-2b6cc44f97cmr13965162a91.14.1715784255600; Wed, 15 May 2024 07:44:15 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715784255; cv=pass; d=google.com; s=arc-20160816; b=0nW9nE8bn+RXWWtRvLQpvDs0oM8b74ywARbSQ59vGWIKCkfA1V1zZM/vR/F5P6FVkp NnWccdoFx7N+vjUNBPGmOZ6Bjl0uvCjoUQnps4EkfpqnRfPOKpO6m0y02kvoSjAWSseM SAI5AYeIyhBr3HlrpPcfnApDJpyBB1kp19si5oLqIkVp0e7wHP1wXxVOwvTQe866VVVx WzrfXO5K7BHv9molPQgfbmiEJK0e1guVMwSuQe9tLGmZ4UGz0VE4vPxK95iFWCl6ThT1 AhcTd4cnfbT2+dMwkukdjfA+idlvx90DZTvyCv8Iz4JzigoVWaZh5qGkV7BGyLVmlPHk jREw== 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:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=ysNYJ3hsLJxgtcHVmjX4FbsIZ06jYst/wxmJg5bXCC0=; fh=2zwCh0TY81QL+m8lKeshJdiobUNV+OjhPy1yNmYchVY=; b=KJvxzZtvMeL0sTTS9Gj68VS6jUz8dMPyyPa+8SHPD49z0gtmit5hZw2XqVvb9iWJDe i3yBqY/9w9cuIK04IdgZ9JIrWiW7l5243zwC33zGvT+okXO5Ni9Bkb2/8i4GsvkQMPBC NrlBoGpco1TzlXMjJOi/oL2SQXGaZpv5I+IVIFri8VVOw0K45Ql6CYw5bfFkYn5rTH0n xDKT6naaYjiWpshpxenHEkqcIh9JB8R3QtLCxCDpvdw3FNTEckvP/+9HLAHT+EVK5OGL z5E/9c+WDE3D3DWvBL2QkNN9ebMsAjbIMqv14P0SroOcbXRqIuFs9fx1g+rvd6PEYnzo jIlw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=EbtTKF6Z; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-180023-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-180023-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2b628a5d5c3si15550429a91.69.2024.05.15.07.44.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 07:44:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-180023-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=EbtTKF6Z; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-180023-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-180023-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 3A6592826C6 for ; Wed, 15 May 2024 14:44:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 010D5156668; Wed, 15 May 2024 14:43:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EbtTKF6Z" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2AA2A156641 for ; Wed, 15 May 2024 14:43:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715784207; cv=none; b=BVi6bXA6ouVcuXh6rglH0jGDZiu518IXT0cXGuUGf4LzrNcXuM6SY6qi6B5FCtWX5cLjVK6iGeBenfEo+M8q7FcikA0VegrGcOjhyyFcEEUC3rkG67HzUn3CYMnjZ6I+CTU6+CnehmHDvPX3jcAjXuLJzesuysF4Vy6bypv81Q8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715784207; c=relaxed/simple; bh=jdiXquXAPPtPajTSj3ryNiWP4Z6gOwf8+7VBRKa5ETE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=r7SfzGMwWfoFcvyUx90mfqzEqdyGY/Gai5AboUoYMDmx7RGsUX3XAc3XzBNcDctLqkbhTqLt238I8hIrBUT6nb2v2AIbVoG73Emf2lo0mj6vxWjx9049tYjsUHxx3zbCaQUNMlOFGzUll3rbcwqJ46xoqWuuLi8pMRygob2GzC8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EbtTKF6Z; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B8BB4C4AF08; Wed, 15 May 2024 14:43:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715784207; bh=jdiXquXAPPtPajTSj3ryNiWP4Z6gOwf8+7VBRKa5ETE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EbtTKF6ZV0X7RbH+v9/aKJzpPhBbxhP8uPYX1PxxhdU3i46mHXH7VJVO9rRO1D+BX /OsB+zeGrtsd3TlqXWSYjlSzHC+QEVViuRZL9XYTdndfZfIdGzm9TbbgHyiCnboYuq wiykjpgVd3RzXudnv9IlaqMNT+oDwN7i9f9S0Vx9sgKC2fQBgpVFXZzHH/abtrxWkq AAvAhwGDj6qYPfDw65Lhr1tCRR5TCPXk5S2l62XlM+fAO59hBdiU0kbsnfmuSKRSuP F06kVmLsM+x8/iliNtOZECtsLwRbHEoYHI1NbVRxYB2WFjryM+LZd2WWPQghvoCB/G 5244qLLb76msg== From: Frederic Weisbecker To: LKML Cc: Frederic Weisbecker , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Sebastian Andrzej Siewior Subject: [PATCH 3/4] perf: Fix event leak upon exit Date: Wed, 15 May 2024 16:43:10 +0200 Message-ID: <20240515144311.16038-4-frederic@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240515144311.16038-1-frederic@kernel.org> References: <20240515144311.16038-1-frederic@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit When a task is scheduled out, pending sigtrap deliveries are deferred to the target task upon resume to userspace via task_work. However failures while adding en event's callback to the task_work engine are ignored. And since the last call for events exit happen after task work is eventually closed, there is a small window during which pending sigtrap can be queued though ignored, leaking the event refcount addition such as in the following scenario: TASK A ----- do_exit() exit_task_work(tsk); perf_event_overflow() event->pending_sigtrap = pending_id; irq_work_queue(&event->pending_irq); =========> PREEMPTION: TASK A -> TASK B event_sched_out() event->pending_sigtrap = 0; atomic_long_inc_not_zero(&event->refcount) // FAILS: task work has exited task_work_add(&event->pending_task) [...] perf_pending_irq() // early return: event->oncpu = -1 [...] =========> TASK B -> TASK A perf_event_exit_task(tsk) perf_event_exit_event() free_event() WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1) // leak event due to unexpected refcount == 2 As a result the event is never released while the task exits. Fix this with appropriate task_work_add()'s error handling. Fixes: 517e6a301f34 ("perf: Fix perf_pending_task() UaF") Signed-off-by: Frederic Weisbecker --- kernel/events/core.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 724e6d7e128f..c1632e69c69d 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -2289,10 +2289,11 @@ event_sched_out(struct perf_event *event, struct perf_event_context *ctx) event->pending_sigtrap = 0; if (state != PERF_EVENT_STATE_OFF && !event->pending_work) { - event->pending_work = 1; - dec = false; - WARN_ON_ONCE(!atomic_long_inc_not_zero(&event->refcount)); - task_work_add(current, &event->pending_task, TWA_RESUME); + if (task_work_add(current, &event->pending_task, TWA_RESUME) >= 0) { + WARN_ON_ONCE(!atomic_long_inc_not_zero(&event->refcount)); + dec = false; + event->pending_work = 1; + } } if (dec) local_dec(&event->ctx->nr_pending); -- 2.44.0