Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp960530pxb; Wed, 6 Apr 2022 05:22:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1MQDXH9Kg7bbrw1CF9T5sFa0TIfNkoYrNDS5kzyJjiIJf/LrWpmVChZbJ6LcbceMn7ZI9 X-Received: by 2002:a17:90a:6949:b0:1ca:b37b:ba73 with SMTP id j9-20020a17090a694900b001cab37bba73mr9375089pjm.217.1649247770623; Wed, 06 Apr 2022 05:22:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649247770; cv=none; d=google.com; s=arc-20160816; b=lL8dWX0X6huEfjrzRvwB66xOPakLidTS1thnENwL5311kIraFLgMTK1d6BGISa7xwU armzJIcl5qLGgDItxGlqa+Cwe25/N16tAR99/niPfgCR9fQDQJjUEg+QIn06gJ/C2bxT 5Q9Q6QpgUG+ETaccI3l0OFc9E2PJSvQOQVwHNayP95hc4q13KY921IVVANcDTvbi4cv2 yI9sT1olmqJnLGfPlyYVBzcTiv0sscqDIfSWdRW4rxUkYTLcIqFjX3fAvWh2IlnFUJB9 glA4LkUtMKYKV2au7DrSCdF4bSPFNtYLqo79aN3PzHU7HMxkgImSSBFOvgmZGKKAiBUf OE4A== 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=QwQJpM4UfLmnWRDvIFdrjZMOJcv5W9q2XidE/JxuZvM=; b=cfdDNGCo9WSaISzKDeXYu2VIiSYrrc41zA5PogIdYDBfxAKy9sNrCOvZnLAaV90uVx DHS2zVCrpyqDaoUcoX2tzGaNq1T9zx1XrCva033bWTuS5s1mu/6WIY2n29P2UmSFPucv 3V3pnSvVH2MoNCb314JOQKX2/f5Ylz+DEDD6s6GSwVPpq65/hD05ZH9O5xH9glZwkbFs 5G/AFNX95Ic8Q97mBM4quE/gV5M79FUXDtFgCqqOWV4etyzuSjGUUeahpkRwupp9cmF8 E2xbqJD9SEeMzQjFHBxR5IFc6BZiCSn38bb01mgUcep14xDcjRlIT+CfMwmzxl8PZyAN A8+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ajdHqvhK; 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 q12-20020a170902bd8c00b001564f2a0782si13750044pls.345.2022.04.06.05.22.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 05:22:50 -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=ajdHqvhK; 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 2CF3B6DA4CD; Wed, 6 Apr 2022 04:20:23 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1842345AbiDFBbA (ORCPT + 99 others); Tue, 5 Apr 2022 21:31:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354574AbiDEKOj (ORCPT ); Tue, 5 Apr 2022 06:14:39 -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 500E848E55; Tue, 5 Apr 2022 03:01:20 -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 14036B81B96; Tue, 5 Apr 2022 10:01:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74372C385A1; Tue, 5 Apr 2022 10:01:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649152877; bh=zTkIlxHMHoKKV9SZz13H6R9GwmKrGgfVELCZXZEtKxU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ajdHqvhKzRh0kbYzIvjKDtoXl7XwunMyB6IEjbLnIm3tiVexgcLb2i/9y9Wgqm0oX 0eqehz8h1/oZscDSEqcoI1XsbfKiX7ufGcNHk9hcjUt0A3aoPBFwrgEykimxM9DUHB KbpzdenUyIGiSfloRUmY2s3XAXtzzH6sdNyfJvvA= 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.10 021/599] locking/lockdep: Avoid potential access of invalid memory in lock_class Date: Tue, 5 Apr 2022 09:25:15 +0200 Message-Id: <20220405070259.447667012@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070258.802373272@linuxfoundation.org> References: <20220405070258.802373272@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 @@ -6209,7 +6209,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); @@ -6224,10 +6230,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); @@ -6235,11 +6239,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. */