Re: [PATCH v2 16/24] locking/lockdep: Retain the class key and name while freeing a lock class

From: Waiman Long
Date: Tue Dec 04 2018 - 15:32:03 EST


On 12/04/2018 02:08 PM, Bart Van Assche wrote:
> On Tue, 2018-12-04 at 13:57 -0500, Waiman Long wrote:
>> On 12/03/2018 07:28 PM, Bart Van Assche wrote:
>>> The next patch in this series uses the class name in code that
>>> detects lock class use-after-free. Hence retain the class name for
>>> lock classes that are being freed.
>>>
>>> Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
>>> Cc: Waiman Long <longman@xxxxxxxxxx>
>>> Cc: Johannes Berg <johannes@xxxxxxxxxxxxxxxx>
>>> Signed-off-by: Bart Van Assche <bvanassche@xxxxxxx>
>>> ---
>>> kernel/locking/lockdep.c | 4 +---
>>> 1 file changed, 1 insertion(+), 3 deletions(-)
>>>
>>> diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
>>> index ecd92969674c..92bdb187987f 100644
>>> --- a/kernel/locking/lockdep.c
>>> +++ b/kernel/locking/lockdep.c
>>> @@ -4147,10 +4147,8 @@ static void zap_class(struct lock_class *class)
>>> * Unhash the class and remove it from the all_lock_classes list:
>>> */
>>> hlist_del_rcu(&class->hash_entry);
>>> + class->hash_entry.pprev = NULL;
>>> list_del(&class->lock_entry);
>>> -page
>>> - RCU_INIT_POINTER(class->key, NULL);
>>> - RCU_INIT_POINTER(class->name, NULL);
>>> }
>>>
>>> static inline int within(const void *addr, void *start, unsigned long size)
>> Using the name after module unload can be problematic if it points to
>> memory space occupied by the unloaded kernel module. The memory page may
>> be invalid with the potential of crashing the kernel. You may have to
>> duplicate the name if it is really from the module address space.
> The class name shouldn't be used by any of the code I introduced in the
> lockdep infrastructure after unloading a kernel module finished unless if
> some of the kernel module code is still running after unloading the kernel
> module finished. I think if that happens that the kernel module author is
> to blame and not lockdep :-)
>
> Bart.

You do use the name once in a WARN_ON check in check_prev_add(). You
commit log did say the class name will be used.

-Longman