Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp2110250ioo; Mon, 23 May 2022 10:15:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwa2ONKkJUdbbRNNZQe6cv9zs7LNEGpxtiD8yZwUhd6fuD72YS7OfY1haWGebZPNot0gUja X-Received: by 2002:a05:6a00:2444:b0:4fd:db81:cbdd with SMTP id d4-20020a056a00244400b004fddb81cbddmr24706449pfj.32.1653326149366; Mon, 23 May 2022 10:15:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653326149; cv=none; d=google.com; s=arc-20160816; b=N78Vvc+SNCw4seYebv+zM2UZhloi9qEggX3YHvPzxxl7cLy8lVqaAe27FJGbRIQJYU JNFLV6xGO4e4b0PZVAsSQ/AZcfcE6RZqjOZy/n6cyLBkJ678J8nMMLm5O+PLukWjO+L4 GkkFDjTjxP1QRhOvvIN3Viv1h4Ek8kfAN1M/lz5SKMteZIPhQqm3x5o++Y83v8ZJZXqI EQORu8YhBt7glY+FfuB+TpnpdbWSaJlDPkH7Td8UCXgHq4/P9AuHTfT+1rPd0MxuGRFN KtiLd2H9wZp1Q1jQaIr1fFgQ3KLYMSWt3mCSbypLUlEa2+XVlsv2DSQrq+/KLacIr2MQ mN0Q== 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=IRep3+l3g8hK7R4sGOUdbjGuu4Kmw7VS5ci6fbrZuOg=; b=bKTXhu4p7KKMNq8GRafsdPz5opTXGkFkXeFesG0vY1pXVLFoR8h0yH2wU6yTh2yfuC T1RGbzxLxYPb6knfk1TICB57sEpusjohG0nGuzkjbsUJYbnZ1ojyXwyBcwhbKOEqyBEl krOn6FqT1lCqBAJGinHiMKc8VtOU7meCemTuFgC4ipP2+jgaZynceZoQykaSgjNsvI1z fzMzTbZfOFUlegc46z0pBDcPf+YNroDJrDQ6u3Vd7bbOgOyDHqvfwQ+da+BeiDBiSoKI QEKrA+DSvuj26ciR+BkjXgAfi6HSFzHquNIVKMQcQ5CdyJGMGBsm/oCtF47E2DNfzp+0 4ckA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GHHHkUPK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id w4-20020a1709027b8400b0015cbb6e943fsi10860453pll.299.2022.05.23.10.15.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 May 2022 10:15:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GHHHkUPK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 2B476719E1; Mon, 23 May 2022 10:15:31 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240013AbiEWROp (ORCPT + 99 others); Mon, 23 May 2022 13:14:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240532AbiEWRMb (ORCPT ); Mon, 23 May 2022 13:12:31 -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 0A00B62CE5; Mon, 23 May 2022 10:11:42 -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 78A636149F; Mon, 23 May 2022 17:11:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 60CFEC385A9; Mon, 23 May 2022 17:11:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1653325863; bh=us182Po8y/92L2m2KNkWy/1esHIVSixRF1EcaXFXeLU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GHHHkUPKtYKTgs9cG/wGqqaKHUBf9oCWrgoBe6mSoDv0YtnZe6IJ7ipQWBbPwFZLc LXy8gEfTYV+ct+ICUT1cWAu9dv8Zm/IYjRCBM2sZM56i5iqKOl6tTkV9fjtFIseaVF fYdX21JLcAnQvRrS7kVTBKAoJKmFAIqMs22wIQJE= 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 4.19 12/44] perf: Fix sys_perf_event_open() race against self Date: Mon, 23 May 2022 19:04:56 +0200 Message-Id: <20220523165755.544731604@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220523165752.797318097@linuxfoundation.org> References: <20220523165752.797318097@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 @@ -10758,6 +10758,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; @@ -10807,6 +10810,7 @@ SYSCALL_DEFINE5(perf_event_open, } else { perf_event_ctx_unlock(group_leader, gctx); move_group = 0; + goto not_move_group; } } @@ -10823,7 +10827,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;