Received: by 2002:ac2:464d:0:0:0:0:0 with SMTP id s13csp3662313lfo; Mon, 23 May 2022 10:40:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyl86bj3Te9Tlr36dSCzusE80y6BAS1RIBAwHh6pCfK9kxYBGIriAv3nfg1Q97txPlQlSRm X-Received: by 2002:a05:6a00:1487:b0:518:b952:8894 with SMTP id v7-20020a056a00148700b00518b9528894mr2370599pfu.73.1653327640176; Mon, 23 May 2022 10:40:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653327640; cv=none; d=google.com; s=arc-20160816; b=FxxK6YZmi9Lrf3Yh3+6A2a71FFvjIqt9WS6yTqweIAcgOB1mGQY6HUohRuDK6Mt5Lt o/WY9fo4xZTSzYIa83WAVlLfU1xH6BLB3kHoCLE1y9uLgjmgkJEDUMAZz8q/bQYHxKnr PfGrw7+JsDpGU54WMHak6ZNTtyVVyDPrnp8zlWznBUgxAk7bfxS1zJ8SxA732NUJhxtC J6+350q2RXeBIoam6LyQqxBMGtodwjwrLt+vzknL0Oe6TCylQMzcMz1524ZzAjxb7tJe 9CuzahrfRP44P6F5CDFzxM0uPgEdRs4YR5tnIvEsk0xp1Ta+/ArL+SMGi/gdUYrSEta7 4CcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=M55VmutNoMIvljWF284bEpDTAjVjYCGH1le4AG0E59A=; b=YnmXokiqGr2qCvBy0ZF7ewarXQRnyygtneRxgJSFuXW+Gw2RpsLvkEgGT+WTBZ4U55 2ACDTCu/myXGI8qajqEQSIgtXdP2ILVkbcJKdhsf0FTDqNWQ8WZaXGfI6TJOjzUR27cN U5upwO4BZajxajp/bpS1KVHBFY5JpaiQ0Q4j96KzpRGSs50X5fYHV4lKmrkZzsfry10p QafW/iHY26Sa8/jtbn8L1UEyRZuhypt9HQkpJqyJ0++4KVfxbfBOpwzGLAsI4ci34wNA Uf2sMXRwdHfbUTFKv7WE2NoInrkEXCnqtPoNMKdSBp4ldyTySMXamk0pLZAtqf/+rcZQ xnuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=AprjX2N3; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id j3-20020a170902da8300b00161a0786fc3si10819755plx.60.2022.05.23.10.40.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 May 2022 10:40:40 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=AprjX2N3; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A4E0C95DED; Mon, 23 May 2022 10:40:00 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242413AbiEWRhi (ORCPT + 99 others); Mon, 23 May 2022 13:37:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241499AbiEWR0u (ORCPT ); Mon, 23 May 2022 13:26:50 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A16D087A1A; Mon, 23 May 2022 10:21:57 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id AD77F60BD3; Mon, 23 May 2022 17:20:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A4F03C385A9; Mon, 23 May 2022 17:20:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1653326436; bh=jdK76Fn/2nNzyCK/vvVOQAVF0PxEDe4Z3BZqfFWJzDE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AprjX2N3xLAH4x5RI+hhAHGB/Un2/+eBtw84cdWkbddEW9V6oA3bINbK3TDJLp+/x 9nB6TczrCuh9Cs4yP7TsOJV9HWumksADN0emJswwfguQx26G6GNwqzeivL0pn0vSD/ laG1u0iTgkgHarq5zEOK+4LqFRv7TFp8CY4m1gP8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Norbert Slusarek , "Peter Zijlstra (Intel)" , Linus Torvalds Subject: [PATCH 5.15 047/132] perf: Fix sys_perf_event_open() race against self Date: Mon, 23 May 2022 19:04:16 +0200 Message-Id: <20220523165831.045423740@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220523165823.492309987@linuxfoundation.org> References: <20220523165823.492309987@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 From: Peter Zijlstra commit 3ac6487e584a1eb54071dbe1212e05b884136704 upstream. Norbert reported that it's possible to race sys_perf_event_open() such that the looser ends up in another context from the group leader, triggering many WARNs. The move_group case checks for races against itself, but the !move_group case doesn't, seemingly relying on the previous group_leader->ctx == ctx check. However, that check is racy due to not holding any locks at that time. Therefore, re-check the result after acquiring locks and bailing if they no longer match. Additionally, clarify the not_move_group case from the move_group-vs-move_group race. Fixes: f63a8daa5812 ("perf: Fix event->ctx locking") Reported-by: Norbert Slusarek Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- kernel/events/core.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -12283,6 +12283,9 @@ SYSCALL_DEFINE5(perf_event_open, * Do not allow to attach to a group in a different task * or CPU context. If we're moving SW events, we'll fix * this up later, so allow that. + * + * Racy, not holding group_leader->ctx->mutex, see comment with + * perf_event_ctx_lock(). */ if (!move_group && group_leader->ctx != ctx) goto err_context; @@ -12348,6 +12351,7 @@ SYSCALL_DEFINE5(perf_event_open, } else { perf_event_ctx_unlock(group_leader, gctx); move_group = 0; + goto not_move_group; } } @@ -12364,7 +12368,17 @@ SYSCALL_DEFINE5(perf_event_open, } } else { mutex_lock(&ctx->mutex); + + /* + * Now that we hold ctx->lock, (re)validate group_leader->ctx == ctx, + * see the group_leader && !move_group test earlier. + */ + if (group_leader && group_leader->ctx != ctx) { + err = -EINVAL; + goto err_locked; + } } +not_move_group: if (ctx->task == TASK_TOMBSTONE) { err = -ESRCH;