Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp314762lqo; Thu, 16 May 2024 07:11:24 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUFHVKcyDjO+eSmbbN5+V7BtHPrcraAulgRR2UIYzCd9DqZsJp5SCKtl/iM+lQRXXIlqatE5Tgfp5ZtIQRSv8Uy9z7WoKj0qbWRTA139A== X-Google-Smtp-Source: AGHT+IGBY6Jsi7vmckgdAkcBgIBJmpF7F6XBVPZL3vBqCnIdoQKzfke38b+yvFA33IBxTaJ0ydTg X-Received: by 2002:a17:903:2381:b0:1e2:6b5d:5b18 with SMTP id d9443c01a7336-1ef43f4cf98mr229064405ad.51.1715868683639; Thu, 16 May 2024 07:11:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715868683; cv=pass; d=google.com; s=arc-20160816; b=WQrE/KcxgVliwiufyIjnUs+mVQkQWOMwMLyawDBfCIhu7dXBSpu1ccXk+nIC680bq2 mQcJdhs5b2mJkcmhx9PJGGs3MNqGDf2/4CGexVwT6eQmwu/983aHpCWF4BgM9iogtsp3 KfDOQKh4RuuzosQzZ+IeBi34q2y+Jq72yABDIjlyjnnZujkPl4VDl/Sa9okIrDVU+s+E ohSixMzy98+E+B0eT+MtZw8SXYA9tmZHDGx/K2oy82TZYotr/JckWEkCJWrMPkZnyYDp 9FABy5sSOHaxmiscAbZ2QFK0oIE849vvlFhsKQ+juVsFdrnea1zoUDjT9r/haZqE+07M vp7Q== 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=Y+v8S98bxg8Gv3IBcdaqpihAbYM+TdBQ3ICP5J+agK8=; fh=2zwCh0TY81QL+m8lKeshJdiobUNV+OjhPy1yNmYchVY=; b=ZGt4nNzW4EzUM4Qidr8C0ahcjamtPMpiXJJPsKjS4WqXAM6gPnKsNH56MwTrW+U+PQ nP6/475kinkQ3enxOj08EjW2TmSVFvXRtIlFb4ZFDVvrFGRgNWPc54MrwcOingtQzyhy vhn4vwqPe7TIWXyHWFqMBzvpsYa8Jfa8mcYNDmtfScj2et4Fl1wKez6vG10ipqAXsBq7 iS/FQM19ny2pBedMLqeyw9HuBJdMiqWAJqARYiqRtuTVBJsP3NibLgoQxRgZFoH7dhQD PX1xRyBU/6f5E1hOw2JtaDk2i6AL0F75cgETg/phTSZiJh261WDrZOjxH4wT4cGd+yp8 A72g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="i/S2yQ2T"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-181153-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-181153-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. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d9443c01a7336-1f0cb21422asi16126305ad.511.2024.05.16.07.11.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 07:11:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-181153-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="i/S2yQ2T"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-181153-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-181153-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 C288C2840F0 for ; Thu, 16 May 2024 14:10:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D94DB14D42C; Thu, 16 May 2024 14:09:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="i/S2yQ2T" 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 101D614D2A6 for ; Thu, 16 May 2024 14:09:50 +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=1715868590; cv=none; b=TaRskOWVHukCOJ73XE/FGuw3+egJDEJn7c62ktf3iO5NLbF9kTKKdzMfxy42e43/mOAM+VMY+87FjRA4yJ49aMesXGlo1BHsbTeM/kDaA4vw3DmYocNydIrvCliLBxcBZs12lkFf7PSD89+lRSuvDRIGMBPURL9gVppNO40Ro7k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715868590; c=relaxed/simple; bh=h3hTYa+QVLo1ar17Qiq7dfJ3JmqF/A7sdI3WXMpbxes=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HH9HfctUyyM7T/FRHbFvTYbOVhIuUfz/eLuwmCWNZycI7MwbZEwVq0DzxzmLwrddqMkXJbAp+uUObyGvanf9Cc/s5KNAUgKgOMv2GL5QaUusZthpRK3mOLXNlonPdxC6Xi3ORpjAmTji8WOj0pxheX/6EKmb3BGwjBuCy16xg2Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=i/S2yQ2T; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id A1D73C4AF07; Thu, 16 May 2024 14:09:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715868589; bh=h3hTYa+QVLo1ar17Qiq7dfJ3JmqF/A7sdI3WXMpbxes=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=i/S2yQ2TQwsMe1LqJ5T4I+ryqon6kdFShMOPc9B2wiZhNTm/sikbCrRtQ4HO2r1W5 46hjavwQwclVUy8HH1bKQq/a44ILMp2dvt0QZHTKf+TZnt5k+6UQLQEg0jzK8QjVBL we1xSfYTc4bCSiyo1QprKI7PkqikQc82zwEwt4PIlFjHDuRR7jnJxvjxXWu5Zs6jio JXn96gQUUsNQjdd3vu0zTszYLB/uXrLq+ukNiVvLGmc/u/tl6S1pALtxxfRYRnYFn2 8h3TZcNv1/LXwDWuaMi3Gfz9WQzBeY8MC06SSauRbLGTWNE+F+YfegK8RMACeugxhW X8gsBKhWdKWSA== 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: Thu, 16 May 2024 16:09:35 +0200 Message-Id: <20240516140936.13694-4-frederic@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240516140936.13694-1-frederic@kernel.org> References: <20240516140936.13694-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 | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 724e6d7e128f..f2a366e736a4 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -2284,18 +2284,15 @@ event_sched_out(struct perf_event *event, struct perf_event_context *ctx) } if (event->pending_sigtrap) { - bool dec = true; - event->pending_sigtrap = 0; if (state != PERF_EVENT_STATE_OFF && - !event->pending_work) { - event->pending_work = 1; - dec = false; + !event->pending_work && + !task_work_add(current, &event->pending_task, TWA_RESUME)) { WARN_ON_ONCE(!atomic_long_inc_not_zero(&event->refcount)); - task_work_add(current, &event->pending_task, TWA_RESUME); - } - if (dec) + event->pending_work = 1; + } else { local_dec(&event->ctx->nr_pending); + } } perf_event_set_state(event, state); -- 2.34.1