Received: by 2002:ac2:464d:0:0:0:0:0 with SMTP id s13csp3298701lfo; Mon, 23 May 2022 01:03:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxYqw1Gx0OVY27mBdCK7WUFxup6rZ7Ww8XCNHFR0CIqUR1PD/IiXC3QBLbB5z12oOdJAMEE X-Received: by 2002:aa7:888a:0:b0:518:a0b8:f5c1 with SMTP id z10-20020aa7888a000000b00518a0b8f5c1mr3861527pfe.46.1653292996833; Mon, 23 May 2022 01:03:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653292996; cv=none; d=google.com; s=arc-20160816; b=jQvnTSUaCWdZGiMwZ7ZM5bcLBKSPEMb0SWLyy2tuTTcIQYOd5y7lzuaKBX4vdP/iri 5yuZGBnURtazboUVQ0SOrcbYe0f5V/fGUmKkMCITJ8l/ATSywEL34+qUabwu8zOf3lRX mf+iD7snbRvehLCX1VRbOhAPGMriWvv4GZEl2w8w7SvVr1WwlKs924kWlc0EDf63LsgD noIDEMslxAlNFcJFKTWzgzMs3Xmtcz1cBfFo14m2iL/F2WAB954kgQ0zhjFP0PlimIKw nUKUDGACxyp1NV3CSnB4PRql/7KFn9xvIpO0fpx689yGearbA+6YgkpzJBvkqeldaftH 0tcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=qqixKEXzoDshMrun7TBlJ9cCMvDNubk0hG9BbTmw7h0=; b=QLGROc7bG7v+3EzlAB4hGzx7Tx8pI+qvFor8yXfMMttrw8M18YJUteuUMCUYS2JLRO wV0W4vBQw8RLNy1dWre8CDgzOLjLN8Obo4xo+V0UuOn7lwEkiF/H+abaiVIm+gZrlcBx NpcUsec57lG85qjcpDlq++0vGuB/myAszXcNVqc8Mg0aAzsoPD09euUqmBdqP7hEvcpA YqEpNwrkapRGU1/oeJ/u+/K2EYnl45F9C/B4yq3YRyaB5K4DgT/Fxm9KzZN8LyACpWSs LDcCWFH3oeWblHwm8IuYSCeL/9hnRrS33muQCkPa8nQLsh9tXW6nRyW7pTQ9eRTUYxPc N1og== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=rJ4hYWhC; 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 Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id v2-20020a1709029a0200b00162188be343si5431711plp.264.2022.05.23.01.03.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 May 2022 01:03:16 -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=@infradead.org header.s=desiato.20200630 header.b=rJ4hYWhC; 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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D6C7D11CA27; Mon, 23 May 2022 00:02:20 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353034AbiETSik (ORCPT + 99 others); Fri, 20 May 2022 14:38:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352967AbiETSiZ (ORCPT ); Fri, 20 May 2022 14:38:25 -0400 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA19B197F45 for ; Fri, 20 May 2022 11:38:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:MIME-Version:Message-ID: Subject:Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:In-Reply-To:References; bh=qqixKEXzoDshMrun7TBlJ9cCMvDNubk0hG9BbTmw7h0=; b=rJ4hYWhCYHM2CE97cDIXIPnZ/e 5Y7xnVpdeTPV9jfXO7YefiFFtDs6jTGeeooXCRw2js215wj/C65Irdot+3ZkAjhHe9n0Efyv4r/wh PN5LvBQd+Ay3AY8MRz24EiBmbMne53LmpQfe34uf/asusPmy0UtN8i2BLQRP+PxvALFtVcmGwBxnW r8G/cIR6XZxSOrAAG42xBFiIfF8IwT2UCFhBQ3EnkwKINDj2hLwWZarG+zNkNZF+M386QVWeQt9IY 1bs/c+ENsvQm50f2MoZVWWTV1sp3qtLmfln5myez9o1TwgH5pAHb8aiZmldK117T2HuFwDTABVRbz EtxH6N5Q==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=worktop.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1ns7Vo-0001Jz-SB; Fri, 20 May 2022 18:38:09 +0000 Received: by worktop.programming.kicks-ass.net (Postfix, from userid 1000) id 3BBA8981243; Fri, 20 May 2022 20:38:06 +0200 (CEST) Date: Fri, 20 May 2022 20:38:06 +0200 From: Peter Zijlstra To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: nslusarek@gmx.net, Linus Torvalds Subject: [PATCH] perf: Fix sys_perf_event_open() race against self Message-ID: <20220520183806.GV2578@worktop.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,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=no 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 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) --- kernel/events/core.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -12217,6 +12217,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; @@ -12282,6 +12285,7 @@ SYSCALL_DEFINE5(perf_event_open, } else { perf_event_ctx_unlock(group_leader, gctx); move_group = 0; + goto not_move_group; } } @@ -12298,7 +12302,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;