lockdep: fix NULL pointer dereference in __lock_set_class()
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
@@ -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
Quoted 28 lines · Xiang Gao
From: Xiang Gao <[email protected]>
register_lock_class()can return NULL on failure (e.g., exceedingMAX_LOCKDEP_KEYSorlock_keys_in_useoverflow).__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 subsequenthlock_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.Signed-off-by Xiang Gao <[email protected]>--- kernel/locking/lockdep.c | 2 ++ 1 file changed, 2 insertions(+)kernel/locking/lockdep.c +0
-0
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")"
Quoted 15 lines · Xiang Gao
From: Xiang Gao <[email protected]>
register_lock_class()can return NULL on failure (e.g., exceedingMAX_LOCKDEP_KEYSorlock_keys_in_useoverflow).__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 subsequenthlock_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.Signed-off-by Xiang Gao <[email protected]>