Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp755242pxb; Tue, 5 Apr 2022 21:55:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzJtHsM1NwhWQKVsKxk7DlJsk7rovwI6R1EYiTvvoYLzjb/mYtP2qFjhhLz14IAYpE/5lw6 X-Received: by 2002:a05:6a00:14cb:b0:4fb:2c72:1fdc with SMTP id w11-20020a056a0014cb00b004fb2c721fdcmr7169166pfu.55.1649220956222; Tue, 05 Apr 2022 21:55:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649220956; cv=none; d=google.com; s=arc-20160816; b=JdjpCX82Id++A3CQljtWJRX/N/4fTAwn6QPquory9vsvkP7uKfAL+LxR9vZqpAB1/w XdUREeb+s5IjGj3/kYNfb/XJaRLi1oey+xRJIXPgVF9XWRSxJpM4V84Ai0ZGF/zP8OF/ gUYzRz8rvU2Ho2s3hN+gPAAg2WoEN49OxwmROjuQZioMspYkvEXdW9DSh3HDf5QRo421 Tt58PtOt8sFx/CZNawIUyQzwIkA9XVa1IjqYJhG0eO0TgWx9NiCygv9Id/tY91RX3Y45 ybhgagZFz6f/Ctm9ev5uajppsucRkNOgANBvrBHcft8kV7IxxDTfAsZw+u+lxILFKbPG N9Tw== 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=J/sL2kjBMYMJwBUrKkzAW7GHj2+eW93+HVAxAtJbLPM=; b=VhQPhuHu/7JKr9+6+zSu36CGrbP9aYeIQ2QmYefnkdLmTQee0ZZR4j0MVcEv15lefZ bxeKW7BaQWeQXOskiswPe1xCBEMiRhRmF0xe8I2NfkpkossZipFg0bOE433d2G6Aw3LO vqbLaXL8ClDY53bCbJpO4wzPhn09jYs+Ps/fwO8oflAli1lQVVjkALEyYSGaq3lywajq DRGrgLnh6HcZyrX1yEUBxKdVmVqEnTMeA5gxgE3yCZWQ87icZs5Y0p7XQtqZ3Ld8f/WR prxODHnjgO2QER4wiOffO1WdzTK7OEbFroEPp3EyfDR9/RZzy+WypaGOFjHbKUdAdU3H S3HA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ir98MPPb; 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 c16-20020aa78e10000000b004fa3a8e0057si2325796pfr.270.2022.04.05.21.55.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Apr 2022 21:55:56 -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=ir98MPPb; 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 52D33D1CFB; Tue, 5 Apr 2022 20:15:59 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243178AbiDEK54 (ORCPT + 99 others); Tue, 5 Apr 2022 06:57:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241492AbiDEIeB (ORCPT ); Tue, 5 Apr 2022 04:34:01 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02BB165AD; Tue, 5 Apr 2022 01:32:01 -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 8CCDC60AFB; Tue, 5 Apr 2022 08:32:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9E413C385A0; Tue, 5 Apr 2022 08:31:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649147520; bh=x+MfrSWBfQ88jjgjPsoU0dTJnhaZQ+91OiSyJnCtbyQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ir98MPPbSVBAt74HAJQnfX7lCOHja21LEXzk/X6GI6K3ZKa63/6NhU88kUZAUyPPt mnhtld2e9fMXcydTEZIzHDEfdegqFeOqfNWvXPx61o5wHeAgWDPDsWGVl6o/t7/NiO iEI5ICq4XJoPG4ZmrxmR1hBQH7zJWDKBQz0e1TR0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Tetsuo Handa , Waiman Long , "Peter Zijlstra (Intel)" , Bart Van Assche , Cheng-Jui Wang Subject: [PATCH 5.16 0025/1017] locking/lockdep: Avoid potential access of invalid memory in lock_class Date: Tue, 5 Apr 2022 09:15:38 +0200 Message-Id: <20220405070354.923625558@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070354.155796697@linuxfoundation.org> References: <20220405070354.155796697@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.0 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=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 From: Waiman Long commit 61cc4534b6550997c97a03759ab46b29d44c0017 upstream. It was found that reading /proc/lockdep after a lockdep splat may potentially cause an access to freed memory if lockdep_unregister_key() is called after the splat but before access to /proc/lockdep [1]. This is due to the fact that graph_lock() call in lockdep_unregister_key() fails after the clearing of debug_locks by the splat process. After lockdep_unregister_key() is called, the lock_name may be freed but the corresponding lock_class structure still have a reference to it. That invalid memory pointer will then be accessed when /proc/lockdep is read by a user and a use-after-free (UAF) error will be reported if KASAN is enabled. To fix this problem, lockdep_unregister_key() is now modified to always search for a matching key irrespective of the debug_locks state and zap the corresponding lock class if a matching one is found. [1] https://lore.kernel.org/lkml/77f05c15-81b6-bddd-9650-80d5f23fe330@i-love.sakura.ne.jp/ Fixes: 8b39adbee805 ("locking/lockdep: Make lockdep_unregister_key() honor 'debug_locks' again") Reported-by: Tetsuo Handa Signed-off-by: Waiman Long Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Bart Van Assche Cc: Cheng-Jui Wang Link: https://lkml.kernel.org/r/20220103023558.1377055-1-longman@redhat.com Signed-off-by: Greg Kroah-Hartman --- kernel/locking/lockdep.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) --- a/kernel/locking/lockdep.c +++ b/kernel/locking/lockdep.c @@ -6288,7 +6288,13 @@ void lockdep_reset_lock(struct lockdep_m lockdep_reset_lock_reg(lock); } -/* Unregister a dynamically allocated key. */ +/* + * Unregister a dynamically allocated key. + * + * Unlike lockdep_register_key(), a search is always done to find a matching + * key irrespective of debug_locks to avoid potential invalid access to freed + * memory in lock_class entry. + */ void lockdep_unregister_key(struct lock_class_key *key) { struct hlist_head *hash_head = keyhashentry(key); @@ -6303,10 +6309,8 @@ void lockdep_unregister_key(struct lock_ return; raw_local_irq_save(flags); - if (!graph_lock()) - goto out_irq; + lockdep_lock(); - pf = get_pending_free(); hlist_for_each_entry_rcu(k, hash_head, hash_entry) { if (k == key) { hlist_del_rcu(&k->hash_entry); @@ -6314,11 +6318,13 @@ void lockdep_unregister_key(struct lock_ break; } } - WARN_ON_ONCE(!found); - __lockdep_free_key_range(pf, key, 1); - call_rcu_zapped(pf); - graph_unlock(); -out_irq: + WARN_ON_ONCE(!found && debug_locks); + if (found) { + pf = get_pending_free(); + __lockdep_free_key_range(pf, key, 1); + call_rcu_zapped(pf); + } + lockdep_unlock(); raw_local_irq_restore(flags); /* Wait until is_dynamic_key() has finished accessing k->hash_entry. */