Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp675989pxb; Tue, 5 Apr 2022 18:19:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzflpO0IKyj6MUePyx89rxUnDagVqw0ta3Cf0xiGkg3tj3Qm8LdMTAv2a/UPnslR0lVqGUq X-Received: by 2002:a17:907:168b:b0:6e7:f2a5:bb0f with SMTP id hc11-20020a170907168b00b006e7f2a5bb0fmr6042281ejc.162.1649207952091; Tue, 05 Apr 2022 18:19:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649207952; cv=none; d=google.com; s=arc-20160816; b=NXrd9MWQxAv1Jy++5cErviumdv7HzRG/NHVh5dWwriD28E0evackBRym3vPbSQBcEW RiLfsq3Apf8fqQE6iCYy7YGlObxHp3T7/Ou8rxm5ZGGrfKnVFaj5p64/hL3AkrTf9B9j K5hI2AAAqKKfRy9mTloWDIdhWLpN+krl5RaQZeNh61W6kL3vJPpfy6kul8wxaWwCc/jY YQ3WKarzzAVqp6eR9LIxbh1OSDVK4jLgyZ9zO/JLoo50EvKnjmsvlv23XWvoa203iord YxSITM6vP11fySbKlOWbKvt8bQ03ROJolTtqVnALehgY+5gpbxjJzwyL265VrrUYFFRq nJyQ== 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=3NfpQN6Ph1ZwYdUtQONJy+QHJr++D+/f/1bSUTdWcSk=; b=EUF/wAyrvSmdT3YnB3c6DQQaTl6qtpYmqNqMY0zRi52l6f/qI6LejSz9z2ECuxvE87 UxBZFbqH+i3tJ28hSy2nxlU2hvlh8tuuJwvjwQiitVYX5xas7hs2pHDJZAzt9aAja6n8 0PGgK1QYnylKaxxjVg3JItURA0gEG/d8cNq8Z2rf27CMa9M8Q+TsuudO51p8Mr/LAfdV JY7ieq9A4uoMFZfoSzxUgraAoyDqfPjKOwtjhRnecR+pAWt/3mmfrbmsgigzWx2BxIh7 ohKqa2sM+6swKgfg0bPGfXKqmHn0gvLWnD8X58m0lmizhakByvvbu7grXmP74PP3i5Yv yLlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=eAbZfG6B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s10-20020a170906284a00b006df76385c39si10183887ejc.217.2022.04.05.18.18.36; Tue, 05 Apr 2022 18:19:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=eAbZfG6B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231751AbiDEHlq (ORCPT + 99 others); Tue, 5 Apr 2022 03:41:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231836AbiDEHlX (ORCPT ); Tue, 5 Apr 2022 03:41:23 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7DBA291366; Tue, 5 Apr 2022 00:39:22 -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 sin.source.kernel.org (Postfix) with ESMTPS id 51F8FCE1BF0; Tue, 5 Apr 2022 07:39:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 431DFC340EE; Tue, 5 Apr 2022 07:39:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649144358; bh=7uj4GxtLx2Q9hPDEJouo8h2fEFB6TdTXpiQ25YfsT5U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eAbZfG6BomAJfmiu3laQbxipeKbD9LnSc9DE0XKtx3Xf0ns9ZAk9Q5AKqDR0iieb3 N8rI+I1TwK7W0M1DXAzcRZ1PL6Rn5cW/GAguCQlNpewB3sqQQlyMKlNqgzd1A47wpb LcPH4KiKn8vWyImROrSgS3ZLzYhXzxOhtvjkpDXI= 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.17 0007/1126] locking/lockdep: Avoid potential access of invalid memory in lock_class Date: Tue, 5 Apr 2022 09:12:34 +0200 Message-Id: <20220405070407.750985604@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070407.513532867@linuxfoundation.org> References: <20220405070407.513532867@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=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 @@ -6290,7 +6290,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); @@ -6305,10 +6311,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); @@ -6316,11 +6320,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. */