Received: by 2002:ac2:464d:0:0:0:0:0 with SMTP id s13csp3670141lfo; Mon, 23 May 2022 10:55:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwdLeR4Jd/u3DUZO7YEPqHuzI/Uh1b6t8tgKHZrbkkira3Fr623yUi39qasSWQqsm9E5xWy X-Received: by 2002:a17:902:d64d:b0:162:3595:220c with SMTP id y13-20020a170902d64d00b001623595220cmr2277264plh.10.1653328547177; Mon, 23 May 2022 10:55:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653328547; cv=none; d=google.com; s=arc-20160816; b=oVoPXMpyxE7fyOwZqqV8LH5TUHlXo0PLBxk54bHbGQmDJ6rgJMUj0JksinytihNPm0 Pch/3xrvqoMN+dWkp4XbMAkdakulR/2L+jRDDCbMq57J3veVZxFUazfMAn6DDp7j032L TaPura4Mg/lR5HQsa+omAWk4t/ZqPnmJIjIjrHdaW1FwauUslHiVgB5z02fS7LcsmP1G SsJU43Ksr5scW1lC7YKz2IUj9PgCYPj/Gv0Ya4Fw22mgkcCB+0Xv910htSXwSthOwnUx dL4psCOSax9v2jWB/jL2tld5bmzWe0T/LBHuQ88dTU8uW1I8CgdW3O8EsGOwkNcKAtFs Q0fA== 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=X6mMiIIBKUu6LIQZR9toBo9d9CIAe4qJYlnNTwWQBC0=; b=JarzwCoyrIGwfEUlWKOaFJjvZymDA5lCrdK6HrOOrP4kkfP+iedWR705juYWOObi4O O65hoG/Y4RTNL++paiN1TI0P+6IyGUctkzRVW5TINUL9nx5zE2KtOcJhkiaY7ysgj+Wj v+F+k9MVmW+h5cTNZQeaviVJcX//f+fh2BCMJb/4gaqxff3+M8YfYPdqCmjtGx6nXBLI xWn1HfwLjOxThsC+1ksB+crHY9Nk3hQbrOWtPSFCax76y4WJVqLZcGOzwRyqRlkf5pZ1 4jsETifG++Z8mhiIiKGPF/5ave8SQ5GiKutol6vr76lUTLu9L7/pjX4AQxaiMwrducMP 0nXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="G9xm/fkt"; 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 g16-20020a63e610000000b003f6ba0c3a6csi12017018pgh.80.2022.05.23.10.55.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 May 2022 10:55:47 -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="G9xm/fkt"; 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 4D15E11CA02; Mon, 23 May 2022 10:55:27 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243430AbiEWRvg (ORCPT + 99 others); Mon, 23 May 2022 13:51:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240874AbiEWR3L (ORCPT ); Mon, 23 May 2022 13:29:11 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 216531E3EF; Mon, 23 May 2022 10:26:26 -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 ams.source.kernel.org (Postfix) with ESMTPS id 5A526B81216; Mon, 23 May 2022 17:14:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B47D4C385AA; Mon, 23 May 2022 17:14:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1653326060; bh=jydNrHBro0hUXzM5ccI1XyLjAw4x1Am2+1BWEGm+Jds=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=G9xm/fktKhkj3qagwEY74WnZjYHsnuiko4XPU5A9jN4304ejU/fs3FefBbnX6KboI KiyFs4ytVtsz3MgobISuoZec0JHiLi8AgfQMg7/XwLFwQTz8/fcLmpcHIuSqSPGcdw 7pzEMnvgT3b25v8CQEZXQob48nF/+MInrElGHpLY= 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.4 27/68] perf: Fix sys_perf_event_open() race against self Date: Mon, 23 May 2022 19:04:54 +0200 Message-Id: <20220523165807.096871574@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220523165802.500642349@linuxfoundation.org> References: <20220523165802.500642349@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 @@ -11114,6 +11114,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; @@ -11181,6 +11184,7 @@ SYSCALL_DEFINE5(perf_event_open, } else { perf_event_ctx_unlock(group_leader, gctx); move_group = 0; + goto not_move_group; } } @@ -11197,7 +11201,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;