]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
locking/lockdep: Add kasan_check_byte() check in lock_acquire()
authorWaiman Long <longman@redhat.com>
Fri, 7 Mar 2025 23:26:56 +0000 (15:26 -0800)
committerIngo Molnar <mingo@kernel.org>
Fri, 7 Mar 2025 23:55:04 +0000 (00:55 +0100)
KASAN instrumentation of lockdep has been disabled, as we don't need
KASAN to check the validity of lockdep internal data structures and
incur unnecessary performance overhead. However, the lockdep_map pointer
passed in externally may not be valid (e.g. use-after-free) and we run
the risk of using garbage data resulting in false lockdep reports.

Add kasan_check_byte() call in lock_acquire() for non kernel core data
object to catch invalid lockdep_map and print out a KASAN report before
any lockdep splat, if any.

Suggested-by: Marco Elver <elver@google.com>
Signed-off-by: Waiman Long <longman@redhat.com>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Marco Elver <elver@google.com>
Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>
Link: https://lore.kernel.org/r/20250214195242.2480920-1-longman@redhat.com
Link: https://lore.kernel.org/r/20250307232717.1759087-7-boqun.feng@gmail.com
kernel/locking/lockdep.c

index 8436f017c74d816be6b4ae9297c59b15492acd4f..b15757e6362615aeecb1629f8e60375e13a87e6d 100644 (file)
@@ -57,6 +57,7 @@
 #include <linux/lockdep.h>
 #include <linux/context_tracking.h>
 #include <linux/console.h>
+#include <linux/kasan.h>
 
 #include <asm/sections.h>
 
@@ -5830,6 +5831,14 @@ void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
        if (!debug_locks)
                return;
 
+       /*
+        * As KASAN instrumentation is disabled and lock_acquire() is usually
+        * the first lockdep call when a task tries to acquire a lock, add
+        * kasan_check_byte() here to check for use-after-free and other
+        * memory errors.
+        */
+       kasan_check_byte(lock);
+
        if (unlikely(!lockdep_enabled())) {
                /* XXX allow trylock from NMI ?!? */
                if (lockdep_nmi() && !trylock) {