Skip to contentSkip to conversation

workqueue: unregister wq lockdep on error path in alloc_workqueue()

PATCH In review Kefeng Wang 2 participants 3 messages Mar 8, 2019 – Mar 9, 2019 Workqueue

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 workqueue: unregister wq lockdep on error path in alloc_workqueue()

syzkaller report an issue "KASAN: use-after-free Read in alloc_workqueue",

alloc_workqueue
- kzalloc wq
- wq_init_lockdep(wq);
- lockdep_register_key(&wq->key); // add to hlist
- kfree wq

But forget to call wq_unregister_lockdep()->lockdep_unregister_key(), it will delete the entry from hlist.

Reported-by [email protected]
Fixes 669de8bda87b ("kernel/workqueue: Use dynamic lockdep keys for workqueues")
---
 kernel/workqueue.c | 1 +
 1 file changed, 1 insertion(+)
kernel/workqueue.c +1 -0
index 7abbeed13421..9209d25dfade 100644
@@ -4291,6 +4291,7 @@ struct workqueue_struct *alloc_workqueue(const char *fmt,
 	return wq;
 
 err_free_wq:
+	wq_unregister_lockdep(wq);
 	free_workqueue_attrs(wq->unbound_attrs);
 	kfree(wq);
 	return NULL;
Signature · 1 line
2.20.1
#1 lore raw
Quoted 23 lines · Kefeng Wang

syzkaller report an issue "KASAN: use-after-free Read in alloc_workqueue",

alloc_workqueue
- kzalloc wq
- wq_init_lockdep(wq);
- lockdep_register_key(&wq->key); // add to hlist
- kfree wq

But forget to call wq_unregister_lockdep()->lockdep_unregister_key(), it will delete the entry from hlist.

Reported-by [email protected]
Fixes 669de8bda87b ("kernel/workqueue: Use dynamic lockdep keys for workqueues")
---
  kernel/workqueue.c | 1 +
  1 file changed, 1 insertion(+)
kernel/workqueue.c +0 -0
index 7abbeed13421..9209d25dfade 100644

kernel/workqueue.c struct workqueue_struct *alloc_workqueue(const char *fmt, · line 4297

Quoted 7 lines
kernel/workqueue.c +1 -0
@@ -4291,6 +4291,7 @@ struct workqueue_struct *alloc_workqueue(const char *fmt,
  	return wq;
 
  err_free_wq:
+	wq_unregister_lockdep(wq);
  	free_workqueue_attrs(wq->unbound_attrs);
  	kfree(wq);
  	return NULL;

Hi Kefeng,

Five days ago I had posted a similar but slightly different patch. Can you have a look at it? See also https://lore.kernel.org/lkml/[email protected]/

Thanks,

Bart.

#2 lore raw
Quoted 33 lines · Bart Van Assche
Quoted 24 lines · Kefeng Wang

syzkaller report an issue "KASAN: use-after-free Read in alloc_workqueue",

alloc_workqueue
  - kzalloc wq
  - wq_init_lockdep(wq);
    - lockdep_register_key(&wq->key);  // add to hlist
  - kfree wq

But forget to call wq_unregister_lockdep()->lockdep_unregister_key(), it will delete the entry from hlist.

Reported-by [email protected]
Fixes 669de8bda87b ("kernel/workqueue: Use dynamic lockdep keys for workqueues")

---
  kernel/workqueue.c | 1 +

  1 file changed, 1 insertion(+)
kernel/workqueue.c +0 -0
index 7abbeed13421..9209d25dfade 100644
@@ -4291,6 +4291,7 @@ struct workqueue_struct *alloc_workqueue(const char *fmt,
       return wq;
Quoted 5 lines

    err_free_wq:
+    wq_unregister_lockdep(wq);
      free_workqueue_attrs(wq->unbound_attrs);
      kfree(wq);
      return NULL;

Hi Kefeng,

Quoted 1 line

Five days ago I had posted a similar but slightly different patch. Can you have a look at it? See also https://lore.kernel.org/lkml/[email protected]/

I don't notice your patch due to no subscribe lkml,  that is better, wq->lock_name should be freed too; )

Quoted 9 lines

Thanks,

Bart.

.