Skip to contentSkip to conversation

lockdep: fix NULL pointer dereference in __lock_set_class()

PATCH In review Xiang Gao 3 participants 3 messages Apr 16 Locking primitives

lore
Share & tools
lore

Xiang Gao

author
#0 lore raw

PATCH 1/1 lockdep: fix NULL pointer dereference in __lock_set_class()

From: Xiang Gao <[email protected]>

register_lock_class() can return NULL on failure (e.g., exceeding MAX_LOCKDEP_KEYS or lock_keys_in_use overflow). __lock_set_class() uses the return value directly in pointer arithmetic without a NULL check:

  class = register_lock_class(lock, subclass, 0);
  hlock->class_idx = class - lock_classes;

If class is NULL, this computes a garbage negative offset that corrupts hlock->class_idx (a bitfield). Any subsequent hlock_class() call on this hlock returns a garbage pointer, leading to memory corruption or a crash.

The other call site in __lock_acquire() (line 5112) already handles this correctly with an explicit NULL check. Add the same guard here.

---
 kernel/locking/lockdep.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/locking/lockdep.c +2 -0
index 2d4c5bab5af8..e0de81114824 100644
@@ -5437,6 +5437,8 @@ __lock_set_class(struct lockdep_map *lock, const char *name,
 			      lock->wait_type_outer,
 			      lock->lock_type);
 	class = register_lock_class(lock, subclass, 0);
+	if (!class)
+		return 0;
 	hlock->class_idx = class - lock_classes;
 
 	curr->lockdep_depth = i;
Signature · 1 line
2.34.1
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 28 lines · Xiang Gao

From: Xiang Gao <[email protected]>

register_lock_class() can return NULL on failure (e.g., exceeding MAX_LOCKDEP_KEYS or lock_keys_in_use overflow). __lock_set_class() uses the return value directly in pointer arithmetic without a NULL check:

   class = register_lock_class(lock, subclass, 0);
   hlock->class_idx = class - lock_classes;

If class is NULL, this computes a garbage negative offset that corrupts hlock->class_idx (a bitfield). Any subsequent hlock_class() call on this hlock returns a garbage pointer, leading to memory corruption or a crash.

The other call site in __lock_acquire() (line 5112) already handles this correctly with an explicit NULL check. Add the same guard here.

---
  kernel/locking/lockdep.c | 2 ++
  1 file changed, 2 insertions(+)
kernel/locking/lockdep.c +0 -0
index 2d4c5bab5af8..e0de81114824 100644

kernel/locking/lockdep.c __lock_set_class(struct lockdep_map *lock, const char *name, · line 5444

Quoted 8 lines
kernel/locking/lockdep.c +2 -0
@@ -5437,6 +5437,8 @@ __lock_set_class(struct lockdep_map *lock, const char *name,
  			      lock->wait_type_outer,
  			      lock->lock_type);
  	class = register_lock_class(lock, subclass, 0);
+	if (!class)
+		return 0;
  	hlock->class_idx = class - lock_classes;
 
  	curr->lockdep_depth = i;

LKTM, maybe you can add "Fixes: 64aa348edc61 ("lockdep: lock_set_subclass - reset a held lock's subclass")"

Reviewed-by on v1 1/1
#2 lore raw
In reply to Xiang Gao
Quoted 15 lines · Xiang Gao

From: Xiang Gao <[email protected]>

register_lock_class() can return NULL on failure (e.g., exceeding MAX_LOCKDEP_KEYS or lock_keys_in_use overflow). __lock_set_class() uses the return value directly in pointer arithmetic without a NULL check:

  class = register_lock_class(lock, subclass, 0);
  hlock->class_idx = class - lock_classes;

If class is NULL, this computes a garbage negative offset that corrupts hlock->class_idx (a bitfield). Any subsequent hlock_class() call on this hlock returns a garbage pointer, leading to memory corruption or a crash.

Quoted 4 lines

The other call site in __lock_acquire() (line 5112) already handles this correctly with an explicit NULL check. Add the same guard here.