Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp2105542ioo; Mon, 23 May 2022 10:10:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxNZQOl8GaaYBIw4HLW/bYPbBVXZugv6RlP9dzF4VW4psazWBC+wpcSf4sEx8SEkBgmQpR1 X-Received: by 2002:a63:1a63:0:b0:3f6:4513:44af with SMTP id a35-20020a631a63000000b003f6451344afmr19130662pgm.370.1653325830389; Mon, 23 May 2022 10:10:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653325830; cv=none; d=google.com; s=arc-20160816; b=LZoXIkXigWueJIYk0C4EDXg/W1CZcui0l4dA1yW2qXr1FP1UH6/+Agmz69uH+mNslt Q7L3guHp3PpYprqqTRoErSU4vpkN6YLzewlk989Z7LFbl6pqREugibYBcLf7+teBCiAt 7hzlK0vsQt61PygJvnB4Vwp5CiMAYTA5VcDG5DeZc9hPTq+EyxCUy539RWwsulPeTLHp ZXhxft6qzZLrpQDERgigodkELS4qBGsAlU8WYXceO2UQVJuyMVABNZjKjdmopjr2HVUo YMSc9lZDw+JZf2XgHvhMqYVoEQey+uByC9Nh2hYq6R4S8SQSCrc++7PSZinH1Kms/4qY bcjw== 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=pve0SgJRhk4ixrMFPE4bMoQ69dXmyogC1wH/o9rTIt0=; b=BkhSKrnFsWIPXp+olsJVKbhp09BLWbV0fWxG73MR25EQ3JTYdEO+gqhrP7bIJeIlEW Y5RDbHwACQ9/nnUUPJWi4wJLo8wT9jb3fX4X3H5FeeUNrABg1o3U6Qm1za3zb9CM75+P ldylYifVnVmPim7tWVKj5WOihagW/c1SlXSxAZvQfParRM5IgzJfSHrM2vP3DBqbB1ss fR4ng/zNGjoDKgg0ETpF7W9cHBp7Pq5oNwsR9wOIkUugcaLT7l59o/56Sclpbb7n4WpQ xDDhzt/0RVbjmI4WW5dN2muwo7xbgUcDhIX7ZN0M8AOLCsuS7/KBeV+K3nDSS3qLlyG/ LwBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BNGwyC4i; 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 q19-20020a170902bd9300b0015e89bf8982si9929507pls.261.2022.05.23.10.10.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 May 2022 10:10:30 -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=BNGwyC4i; 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 8DC686D1B4; Mon, 23 May 2022 10:10:17 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238986AbiEWRJ1 (ORCPT + 99 others); Mon, 23 May 2022 13:09:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239448AbiEWRJQ (ORCPT ); Mon, 23 May 2022 13:09:16 -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 108F26C0E6; Mon, 23 May 2022 10:08:39 -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 C80A2B81200; Mon, 23 May 2022 17:08:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 253EEC385A9; Mon, 23 May 2022 17:08:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1653325704; bh=sr/B4d37/4qkj9pTFmmpK3Nokr8z6fDzlyR7FE+yYD0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BNGwyC4iBm4+tf/TJZ4871IUxN7ij9PMYLrdtdFAOFgeeeov5VbEUXrwAKrC1s07i K5KISfy/wq4Rmx/kaLGhhKWIdj1BeoHW9DhIneMmvAhrOz43wDiHAQUCuyyzRXDqea CULxv45B/44d2gDjt4d0XoCrodSgt0mrImFksjXQ= 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.14 09/33] perf: Fix sys_perf_event_open() race against self Date: Mon, 23 May 2022 19:04:58 +0200 Message-Id: <20220523165748.703871602@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220523165746.957506211@linuxfoundation.org> References: <20220523165746.957506211@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 @@ -10228,6 +10228,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; @@ -10277,11 +10280,22 @@ SYSCALL_DEFINE5(perf_event_open, } else { perf_event_ctx_unlock(group_leader, gctx); move_group = 0; + goto not_move_group; } } } 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;