Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp272102lqz; Fri, 29 Mar 2024 16:59:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVapDPYzRSScHdxDTu0oni22EoQJLTv7fwGgEW2rxjn7gq8dqELWheXN3sYD9QIdKlk14pDWaiWY7DWikYvUrVaAdhkjKsCCdBFrfGnTw== X-Google-Smtp-Source: AGHT+IFM5+51yZoemDaHOY7TbObbDthqLatdMzN6vgU4zV/Tb8lhqeb2oZasWInFKShyF5tSSAxY X-Received: by 2002:a05:6808:120b:b0:3c3:d902:4151 with SMTP id a11-20020a056808120b00b003c3d9024151mr3741715oil.14.1711756763220; Fri, 29 Mar 2024 16:59:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711756763; cv=pass; d=google.com; s=arc-20160816; b=G7roLznfIY2hqPno+OuTV8ZSPnMbfUmFWrHQKRH5ypLtPE492BAqYf1SwZBbJL+S5J a4rRvsnNDHkC97SrxDU1nU34o2S+uiILolwko2DlaeMo4j7cyTBxaXpM0JkQ3qT8uzv4 nyfY/lob2ovW9B6Dx1I5DbYC+6bsNjcLRieEk4NQUtrJMnDG9COcC07kj+K68e8675Yo PS/K5nNSA9bw8WoJN+wKqzZSduiM8kF47jVPzx8U4Ah37bYk4EiqE3J8F0XYNnej+ZPk rDadOTwq72gvfacXnejgiDWKpPUGoQj8Mtv7OleMWMOF+U5g/+M/HROfU0Ay4XPGDwP9 oPhg== 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=BasZ6imObaSLWGBXoO3nTfE3OEy4PpiGWQ2EQ75eivU=; b=HpBi0HbZoNpvuNUbE+PhIUwI17I5w3mYFgCx/A7wCAgX0FeMP5uNjziRAOXBlMxrde VbYmrb9cEXBoj1NpxXIdxaqeuOIM68ByjOPunI1lW1ZU+WPFQW/15berM4xXjOeaB5U+ Mko9LuX9fzdZ0vlzdG9ErwGWKZhNA05jKaUNbMMvPegVG9N6YphCrktXoWfVvam/eT3K bfHFYKIqp9mGnpfPdx/uVpwvOR2XOdHSiC+UBv8JnmlsJan1Dlp9he4jmdOfdfHK3SEn /d4mXLL+FPdsJVKGDYBjxT3XUNeLgA5qyihjdB2rgs9AVnMglO2eN+PEdENP16HLWMj0 4Kvg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZNXj0OHr; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-125545-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-125545-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 g3-20020a63fa43000000b005d8b2b582dfsi4556113pgk.837.2024.03.29.16.59.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Mar 2024 16:59:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-125545-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=ZNXj0OHr; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-125545-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-125545-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 DAF12283ED2 for ; Fri, 29 Mar 2024 23:59:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9D5F013E414; Fri, 29 Mar 2024 23:58:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZNXj0OHr" 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 C54F713F01B for ; Fri, 29 Mar 2024 23:58:48 +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=1711756728; cv=none; b=lXlVF6xVvaFU8KLAuM2wvju+RGhK9Xlp485RF6x+gDdz3D5URNIzfwRhSf3/Cnrp3rBU9NWKQgjwsD6WFhnFNQ7XEKpcHyfcBTmiiUhCjYs8GG4Mis5r6K5KJZ/jFZi+Z5MZZBK1o9OqZJL149oeEFjKSFafi15gz7OuT40pAoM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711756728; c=relaxed/simple; bh=jdiXquXAPPtPajTSj3ryNiWP4Z6gOwf8+7VBRKa5ETE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bRoLAycOvLeo16XTi9CUCWvcVKoKKd05G5U6PGqS8uUabaNwnAJSsQme6pW/tpaRE6lzvE5q04nR+GbNL5DTpiIC8Q6oEmdSyxVIArGTSwlRlKO8zzqRsbYpxP9jfQGZqSp4KTkshz41UHjnu6KEokRaP/mvwIqiB0K1/GlEwv8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZNXj0OHr; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id EE78AC433B1; Fri, 29 Mar 2024 23:58:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711756728; bh=jdiXquXAPPtPajTSj3ryNiWP4Z6gOwf8+7VBRKa5ETE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZNXj0OHrqHocYTHj5KYi2Dvp4W1KeU6L284QF6RtRSMRQJnDVgK+IyAc1OQyx+7uc 9fgQDvLn1UmxLcaPxbyEpXaYyjV/3fv3Oven2GCWjiWfyn9uflPOzqTPpt9EPdG19M A70Pn5KvlNooPN1PoLiDr2UMqTxY+RAhPBoY1RHF3XoyLVtoiMN9Mg6ik1W/dnb0Ob QHrKDcejZSqvPNkes92IvfsXut5youx7rMSb4c6qKAIcf+lrIeEarhEWZn56/GkLqr /k+1DvDz+MQciUAvUuGEO2Gt4iFhchQJEzrhh24uB6dnFqT5wxpSqKSMjgFitKbUvA mTFMg45v2mr5Q== 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 Subject: [PATCH 3/4] perf: Fix event leak upon exit Date: Sat, 30 Mar 2024 00:58:11 +0100 Message-ID: <20240329235812.18917-4-frederic@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240329235812.18917-1-frederic@kernel.org> References: <20240329235812.18917-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