From patchwork Wed Nov 30 10:02:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 27709 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp838622wrr; Wed, 30 Nov 2022 02:19:23 -0800 (PST) X-Google-Smtp-Source: AA0mqf5TLWZs0HN4IQYp+mQi4vsRHj5aZJ7yE0zzuc83n2jP/as3nWkSwIUM27n/mpHkQqk2775a X-Received: by 2002:a17:906:2ac3:b0:7ad:f2f9:2b49 with SMTP id m3-20020a1709062ac300b007adf2f92b49mr38328460eje.94.1669803563102; Wed, 30 Nov 2022 02:19:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669803563; cv=none; d=google.com; s=arc-20160816; b=tB+p9v5Mi8dJxKv6VlbdEaEAYr3afZFn9ZXZ9IFfylXRXE7ZDE8MqzdWAxLC9u4SJu j/kJTJSFkcu3eN2TpzAPZSlSfYZSoJpaMU7XzOQGkbltWVDcSIlGiRkXszM5fekAjWII BWA8WkZmx6DPRMRpVa4lDIoCQ4p2Vgjiz8XhDKeI2udXZ5MT1t3aseF6X3BaLJXr76E4 fPwSyH5EHrc9iBIsVOpVTT6crFA+3aiFCGvL+ko5lkizgvHD9DYJ9uH4o9ICudkgWHSK k2ngFUd8wvxOdT0hhN4J0ZqI/kr6gJ8p5JkJ4fhQEx5ZpPhx5LFWDxxQ774XWgicfOh7 2TuA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:cc:subject:to:reply-to:sender:from :dkim-signature:dkim-signature:date; bh=VHa/C1sCb9t0gkcdPzHNek3kN6cA/XbDouIGUpO2Jh0=; b=jBdbxyBj8suL2uDhu4fkH+FjnjI9s5yarDUvIzj15kxlYaG/WKXgxO6JddhBp/nxxh T3G2pDKz+Rec5+vhxL+BWKuifDnkgQswoLF9gVSpd1LFcwOWNFO5xN3eE5BZAkdAtb3J t5cck21EGNgGbOiaNmARgbrmDPc3z6a2zZ80HcnkIO3ctO1loEN4bNhzqAYEJCWd3H2r ij7hKsncIzvDPut3gSh2WSSN7fj/ZRES4NsoDji41OdErOxf5JFeXPkdjZVCufM3Uufy +k8+QIlA9luyP6wRtAWEY2yH+AhNcgaUoajf5INADsdFWo5vDFnMJ1XtXJusMDvhDZ5B m9wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=wDBcog33; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=1Z8dlZoj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b18-20020a056402279200b0045ad02bb8fbsi1089337ede.290.2022.11.30.02.18.59; Wed, 30 Nov 2022 02:19:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=wDBcog33; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=1Z8dlZoj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235295AbiK3KCx (ORCPT + 99 others); Wed, 30 Nov 2022 05:02:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235099AbiK3KCv (ORCPT ); Wed, 30 Nov 2022 05:02:51 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CED5F1009; Wed, 30 Nov 2022 02:02:46 -0800 (PST) Date: Wed, 30 Nov 2022 10:02:43 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1669802565; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=VHa/C1sCb9t0gkcdPzHNek3kN6cA/XbDouIGUpO2Jh0=; b=wDBcog33gzS4nuVQ2SXAoVBzqKGSpln78xCKHSNL7QXSzDPb3++aQRK5SuXG5DWSVhDH5T g4WdDZRf5byf4dpyDUp9BAP0vqB2H7J+BMhpGsQhSmGyEI/w3rwV0w0WDmzttH2AdpA2wX wbxufUOZah+OO+1Sw3BQ/6A/7twavcJV4vnF8kJh4dtTqoFmy64jXgDzo6lbK+Jj7rNvl1 q4UZRjH2qv2CaeRMJ4lx1xSUKQV3vuiPDJPpTNswfCexZKhcVUGsdC4H3u8TbpxN5plwVD H0cauGLq5nuWVHgREqg4ekWUoTWI7QiGDGybPDGNz1ju5IXjAcSeUcFxGaeNHA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1669802565; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=VHa/C1sCb9t0gkcdPzHNek3kN6cA/XbDouIGUpO2Jh0=; b=1Z8dlZojWtiexjhvYkR/+tCeG5WyFxk7nTbUJObfWkBkQRjVtyUESyonCGNNhufUlN2BxG Adt3FSPK7UI42ICw== From: "tip-bot2 for Peter Zijlstra" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: perf/urgent] perf: Fix perf_pending_task() UaF Cc: syzbot+9228d6098455bb209ec8@syzkaller.appspotmail.com, "Peter Zijlstra (Intel)" , Marco Elver , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <166980256366.4906.11174941629243947427.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750915941247616173?= X-GMAIL-MSGID: =?utf-8?q?1750915941247616173?= The following commit has been merged into the perf/urgent branch of tip: Commit-ID: 517e6a301f34613bff24a8e35b5455884f2d83d8 Gitweb: https://git.kernel.org/tip/517e6a301f34613bff24a8e35b5455884f2d83d8 Author: Peter Zijlstra AuthorDate: Thu, 24 Nov 2022 12:49:12 +01:00 Committer: Peter Zijlstra CommitterDate: Tue, 29 Nov 2022 17:42:49 +01:00 perf: Fix perf_pending_task() UaF Per syzbot it is possible for perf_pending_task() to run after the event is free()'d. There are two related but distinct cases: - the task_work was already queued before destroying the event; - destroying the event itself queues the task_work. The first cannot be solved using task_work_cancel() since perf_release() itself might be called from a task_work (____fput), which means the current->task_works list is already empty and task_work_cancel() won't be able to find the perf_pending_task() entry. The simplest alternative is extending the perf_event lifetime to cover the task_work. The second is just silly, queueing a task_work while you know the event is going away makes no sense and is easily avoided by re-arranging how the event is marked STATE_DEAD and ensuring it goes through STATE_OFF on the way down. Reported-by: syzbot+9228d6098455bb209ec8@syzkaller.appspotmail.com Signed-off-by: Peter Zijlstra (Intel) Tested-by: Marco Elver --- kernel/events/core.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 9d15d2d..ad82479 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -2291,6 +2291,7 @@ event_sched_out(struct perf_event *event, !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 (dec) @@ -2336,6 +2337,7 @@ group_sched_out(struct perf_event *group_event, #define DETACH_GROUP 0x01UL #define DETACH_CHILD 0x02UL +#define DETACH_DEAD 0x04UL /* * Cross CPU call to remove a performance event @@ -2356,12 +2358,20 @@ __perf_remove_from_context(struct perf_event *event, update_cgrp_time_from_cpuctx(cpuctx, false); } + /* + * Ensure event_sched_out() switches to OFF, at the very least + * this avoids raising perf_pending_task() at this time. + */ + if (flags & DETACH_DEAD) + event->pending_disable = 1; event_sched_out(event, cpuctx, ctx); if (flags & DETACH_GROUP) perf_group_detach(event); if (flags & DETACH_CHILD) perf_child_detach(event); list_del_event(event, ctx); + if (flags & DETACH_DEAD) + event->state = PERF_EVENT_STATE_DEAD; if (!ctx->nr_events && ctx->is_active) { if (ctx == &cpuctx->ctx) @@ -5121,9 +5131,7 @@ int perf_event_release_kernel(struct perf_event *event) ctx = perf_event_ctx_lock(event); WARN_ON_ONCE(ctx->parent_ctx); - perf_remove_from_context(event, DETACH_GROUP); - raw_spin_lock_irq(&ctx->lock); /* * Mark this event as STATE_DEAD, there is no external reference to it * anymore. @@ -5135,8 +5143,7 @@ int perf_event_release_kernel(struct perf_event *event) * Thus this guarantees that we will in fact observe and kill _ALL_ * child events. */ - event->state = PERF_EVENT_STATE_DEAD; - raw_spin_unlock_irq(&ctx->lock); + perf_remove_from_context(event, DETACH_GROUP|DETACH_DEAD); perf_event_ctx_unlock(event, ctx); @@ -6577,6 +6584,8 @@ static void perf_pending_task(struct callback_head *head) if (rctx >= 0) perf_swevent_put_recursion_context(rctx); preempt_enable_notrace(); + + put_event(event); } #ifdef CONFIG_GUEST_PERF_EVENTS