Skip to contentSkip to conversation

workqueue: fix null-ptr-deref on __alloc_workqueue() error

PATCH Applied Sergey Senozhatsky 3 participants 16 messages Aug 15, 2024 – Aug 21, 2024 Workqueue

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 workqueue: fix null-ptr-deref on __alloc_workqueue() error

wq->lockdep_map is set only after __alloc_workqueue() successfully returns. However, on its error path __alloc_workqueue() may call destroy_workqueue() which expects wq->lockdep_map to be already set, which results in a null-ptr-deref in touch_wq_lockdep_map().

Add a simple NULL-check to touch_wq_lockdep_map().

Oops: general protection fault, probably for non-canonical address
KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007]
RIP: 0010:__lock_acquire+0x81/0x7800
[..]
Call Trace:
 <TASK>
 ? __die_body+0x66/0xb0
 ? die_addr+0xb2/0xe0
 ? exc_general_protection+0x300/0x470
 ? asm_exc_general_protection+0x22/0x30
 ? __lock_acquire+0x81/0x7800
 ? mark_lock+0x94/0x330
 ? __lock_acquire+0x12fd/0x7800
 ? __lock_acquire+0x3439/0x7800
 lock_acquire+0x14c/0x3e0
 ? __flush_workqueue+0x167/0x13a0
 ? __init_swait_queue_head+0xaf/0x150
 ? __flush_workqueue+0x167/0x13a0
 __flush_workqueue+0x17d/0x13a0
 ? __flush_workqueue+0x167/0x13a0
 ? lock_release+0x50f/0x830
 ? drain_workqueue+0x94/0x300
 drain_workqueue+0xe3/0x300
 destroy_workqueue+0xac/0xc40
 ? workqueue_sysfs_register+0x159/0x2f0
 __alloc_workqueue+0x1506/0x1760
 alloc_workqueue+0x61/0x150

...

---
 kernel/workqueue.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/workqueue.c +3 -0
index bfeeefeee332..59bd2c1e55af 100644
@@ -3870,6 +3870,9 @@ static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
 static void touch_wq_lockdep_map(struct workqueue_struct *wq)
 {
 #ifdef CONFIG_LOCKDEP
+	if (unlikely(!wq->lockdep_map))
+		return;
+
 	if (wq->flags & WQ_BH)
 		local_bh_disable();
 
Signature · 1 line
2.46.0.76.ge559c4bf1a-goog
#1 lore raw
Quoted 47 lines · Sergey Senozhatsky

wq->lockdep_map is set only after __alloc_workqueue() successfully returns. However, on its error path __alloc_workqueue() may call destroy_workqueue() which expects wq->lockdep_map to be already set, which results in a null-ptr-deref in touch_wq_lockdep_map().

Add a simple NULL-check to touch_wq_lockdep_map().

Oops: general protection fault, probably for non-canonical address
KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007]
RIP: 0010:__lock_acquire+0x81/0x7800
[..]
Call Trace:
 <TASK>
 ? __die_body+0x66/0xb0
 ? die_addr+0xb2/0xe0
 ? exc_general_protection+0x300/0x470
 ? asm_exc_general_protection+0x22/0x30
 ? __lock_acquire+0x81/0x7800
 ? mark_lock+0x94/0x330
 ? __lock_acquire+0x12fd/0x7800
 ? __lock_acquire+0x3439/0x7800
 lock_acquire+0x14c/0x3e0
 ? __flush_workqueue+0x167/0x13a0
 ? __init_swait_queue_head+0xaf/0x150
 ? __flush_workqueue+0x167/0x13a0
 __flush_workqueue+0x17d/0x13a0
 ? __flush_workqueue+0x167/0x13a0
 ? lock_release+0x50f/0x830
 ? drain_workqueue+0x94/0x300
 drain_workqueue+0xe3/0x300
 destroy_workqueue+0xac/0xc40
 ? workqueue_sysfs_register+0x159/0x2f0
 __alloc_workqueue+0x1506/0x1760
 alloc_workqueue+0x61/0x150

...

---
 kernel/workqueue.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/workqueue.c +0 -0
index bfeeefeee332..59bd2c1e55af 100644

kernel/workqueue.c static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, · line 3877

Quoted 8 lines
kernel/workqueue.c +3 -0
@@ -3870,6 +3870,9 @@ static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
 static void touch_wq_lockdep_map(struct workqueue_struct *wq)
 {
 #ifdef CONFIG_LOCKDEP
+	if (unlikely(!wq->lockdep_map))
+		return;
+
 	if (wq->flags & WQ_BH)
 		local_bh_disable();

Oh, okay, so this is related to [1]

workqueue: Split alloc_workqueue into internal function and lockdep init

Cc-ing Matthew on this.

[1] https://lore.kernel.org/all/[email protected]/

#2 lore raw
Quoted 8 lines · Sergey Senozhatsky

On (24/08/15 16:02), Sergey Senozhatsky wrote:
Oh, okay, so this is related to [1]

workqueue: Split alloc_workqueue into internal function and lockdep init

Cc-ing Matthew on this.

[1] https://lore.kernel.org/all/[email protected]/

Matthew, did you mean to do something like below instead? Otherwise it breaks boot for me.

---

kernel/workqueue.c +2 -5
index 7a2f4cbbe8b2..55e0e69ee604 100644
@@ -5615,12 +5615,11 @@ static void wq_adjust_max_active(struct workqueue_struct *wq)
 	} while (activated);
 }
 
-__printf(1, 4)
 static struct workqueue_struct *__alloc_workqueue(const char *fmt,
 						  unsigned int flags,
-						  int max_active, ...)
+						  int max_active,
+						  va_list args)
 {
-	va_list args;
 	struct workqueue_struct *wq;
 	size_t wq_size;
 	int name_len;
@@ -5652,9 +5651,7 @@ static struct workqueue_struct *__alloc_workqueue(const char *fmt,
 			goto err_free_wq;
 	}
 
-	va_start(args, max_active);
 	name_len = vsnprintf(wq->name, sizeof(wq->name), fmt, args);
-	va_end(args);
 
 	if (name_len >= WQ_NAME_LEN)
 		pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n",
#3 lore raw
Quoted 4 lines · Sergey Senozhatsky
Quoted 3 lines · Sergey Senozhatsky

On (24/08/15 16:02), Sergey Senozhatsky wrote:
Oh, okay, so this is related to [1]

Quoted 9 lines
Quoted 5 lines

workqueue: Split alloc_workqueue into internal function and lockdep init

Cc-ing Matthew on this.

[1] https://lore.kernel.org/all/[email protected]/

Matthew, did you mean to do something like below instead? Otherwise it breaks boot for me.

Yikes, my bad. My change worked locally and in Intel's CI for Xe [2]. So I did not realize this was an issue the way I had it coded.

I believe you when you say this is an issue.

With that, the way I coded alloc_ordered_workqueue_lockdep_map could also be dangerous. Thinking that also should also be changed back to a macro too.

Let's see what Tejon thinks?

Matt

[2] https://patchwork.freedesktop.org/series/136701/

Quoted 31 lines

---

kernel/workqueue.c +2 -5
index 7a2f4cbbe8b2..55e0e69ee604 100644
@@ -5615,12 +5615,11 @@ static void wq_adjust_max_active(struct workqueue_struct *wq)
 	} while (activated);
 }
 
-__printf(1, 4)
 static struct workqueue_struct *__alloc_workqueue(const char *fmt,
 						  unsigned int flags,
-						  int max_active, ...)
+						  int max_active,
+						  va_list args)
 {
-	va_list args;
 	struct workqueue_struct *wq;
 	size_t wq_size;
 	int name_len;
@@ -5652,9 +5651,7 @@ static struct workqueue_struct *__alloc_workqueue(const char *fmt,
 			goto err_free_wq;
 	}
 
-	va_start(args, max_active);
 	name_len = vsnprintf(wq->name, sizeof(wq->name), fmt, args);
-	va_end(args);
 
 	if (name_len >= WQ_NAME_LEN)
 		pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n",
#4 lore raw
Quoted 15 lines · Matthew Brost
Quoted 4 lines · Sergey Senozhatsky
Quoted 3 lines · Sergey Senozhatsky

On (24/08/15 16:02), Sergey Senozhatsky wrote:
Oh, okay, so this is related to [1]

Quoted 9 lines
Quoted 5 lines

workqueue: Split alloc_workqueue into internal function and lockdep init

Cc-ing Matthew on this.

[1] https://lore.kernel.org/all/[email protected]/

Matthew, did you mean to do something like below instead? Otherwise it breaks boot for me.

Quoted 9 lines

Yikes, my bad. My change worked locally and in Intel's CI for Xe [2]. So I did not realize this was an issue the way I had it coded.

I believe you when you say this is an issue.

With that, the way I coded alloc_ordered_workqueue_lockdep_map could also be dangerous. Thinking that also should also be changed back to a macro too.

Sergey,

Sorry double reply. Looked into this and va_start / va_end should only be called once, so I think the change you have is correct.

The change wrt to alloc_ordered_workqueue_lockdep_map should look like this I think:

include/linux/workqueue.h +3 -14
index 8ccbf510880b..5e818eae092d 100644
@@ -534,7 +534,7 @@ alloc_workqueue_lockdep_map(const char *fmt, unsigned int flags, int max_active,
  * @fmt: printf format for the name of the workqueue
  * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful)
  * @lockdep_map: user-defined lockdep_map
- * @args: args for @fmt
+ * @...: args for @fmt
  *
  * Same as alloc_ordered_workqueue but with the a user-define lockdep_map.
  * Useful for workqueues created with the same purpose and to avoid leaking a
@@ -543,20 +543,9 @@ alloc_workqueue_lockdep_map(const char *fmt, unsigned int flags, int max_active,
  * RETURNS:
  * Pointer to the allocated workqueue on success, %NULL on failure.
  */
-__printf(1, 4) static inline struct workqueue_struct *
-alloc_ordered_workqueue_lockdep_map(const char *fmt, unsigned int flags,
-                                   struct lockdep_map *lockdep_map, ...)
-{
-       struct workqueue_struct *wq;
-       va_list args;
-
-       va_start(args, lockdep_map);
-       wq = alloc_workqueue_lockdep_map(fmt, WQ_UNBOUND | __WQ_ORDERED | flags,
-                                        1, lockdep_map, args);
-       va_end(args);
+#define alloc_ordered_workqueue_lockdep_map(fmt, flags, lockdep_map, args...)  \
+       alloc_workqueue_lockdep_map(fmt, WQ_UNBOUND | __WQ_ORDERED | (flags), 1, lockdep_map, ##args)
 
-       return wq;
-}
 #endif
 

Matt

Quoted 37 lines

Let's see what Tejon thinks?

Matt

[2] https://patchwork.freedesktop.org/series/136701/

Quoted 31 lines

---

kernel/workqueue.c +2 -5
index 7a2f4cbbe8b2..55e0e69ee604 100644
@@ -5615,12 +5615,11 @@ static void wq_adjust_max_active(struct workqueue_struct *wq)
 	} while (activated);
 }
 
-__printf(1, 4)
 static struct workqueue_struct *__alloc_workqueue(const char *fmt,
 						  unsigned int flags,
-						  int max_active, ...)
+						  int max_active,
+						  va_list args)
 {
-	va_list args;
 	struct workqueue_struct *wq;
 	size_t wq_size;
 	int name_len;
@@ -5652,9 +5651,7 @@ static struct workqueue_struct *__alloc_workqueue(const char *fmt,
 			goto err_free_wq;
 	}
 
-	va_start(args, max_active);
 	name_len = vsnprintf(wq->name, sizeof(wq->name), fmt, args);
-	va_end(args);
 
 	if (name_len >= WQ_NAME_LEN)
 		pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n",
#5 lore raw

Hi Matthew,

On (24/08/15 16:24), Matthew Brost wrote:
[..]

Quoted 24 lines
include/linux/workqueue.h +1 -8
index 8ccbf510880b..5e818eae092d 100644
@@ -534,7 +534,7 @@ alloc_workqueue_lockdep_map(const char *fmt, unsigned int flags, int max_active,
  * @fmt: printf format for the name of the workqueue
  * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful)
  * @lockdep_map: user-defined lockdep_map
- * @args: args for @fmt
+ * @...: args for @fmt
  *
  * Same as alloc_ordered_workqueue but with the a user-define lockdep_map.
  * Useful for workqueues created with the same purpose and to avoid leaking a
@@ -543,20 +543,9 @@ alloc_workqueue_lockdep_map(const char *fmt, unsigned int flags, int max_active,
  * RETURNS:
  * Pointer to the allocated workqueue on success, %NULL on failure.
  */
-__printf(1, 4) static inline struct workqueue_struct *
-alloc_ordered_workqueue_lockdep_map(const char *fmt, unsigned int flags,
-                                   struct lockdep_map *lockdep_map, ...)
-{
-       struct workqueue_struct *wq;
-       va_list args;
-

include/linux/workqueue.h alloc_workqueue_lockdep_map(const char *fmt, unsigned int flags, int max_active, · line 549

Quoted 10 lines
include/linux/workqueue.h +2 -6
-       va_start(args, lockdep_map);
-       wq = alloc_workqueue_lockdep_map(fmt, WQ_UNBOUND | __WQ_ORDERED | flags,
-                                        1, lockdep_map, args);
-       va_end(args);
+#define alloc_ordered_workqueue_lockdep_map(fmt, flags, lockdep_map, args...)  \
+       alloc_workqueue_lockdep_map(fmt, WQ_UNBOUND | __WQ_ORDERED | (flags), 1, lockdep_map, ##args)
 
-       return wq;
-}
 #endif

Oh, I haven't checked the workqueue header. Yes, you are right. A macro should work.

Tejun, how do you plan to handle this? Would it be possible to drop current series from your tree so that Matthew can send an updated version (with all the fixes squashed)?

#6 lore raw
Quoted 35 lines · Sergey Senozhatsky

Hi Matthew,

On (24/08/15 16:24), Matthew Brost wrote:
[..]

Quoted 31 lines
include/linux/workqueue.h +3 -12
index 8ccbf510880b..5e818eae092d 100644
@@ -534,7 +534,7 @@ alloc_workqueue_lockdep_map(const char *fmt, unsigned int flags, int max_active,
  * @fmt: printf format for the name of the workqueue
  * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful)
  * @lockdep_map: user-defined lockdep_map
- * @args: args for @fmt
+ * @...: args for @fmt
  *
  * Same as alloc_ordered_workqueue but with the a user-define lockdep_map.
  * Useful for workqueues created with the same purpose and to avoid leaking a
@@ -543,20 +543,9 @@ alloc_workqueue_lockdep_map(const char *fmt, unsigned int flags, int max_active,
  * RETURNS:
  * Pointer to the allocated workqueue on success, %NULL on failure.
  */
-__printf(1, 4) static inline struct workqueue_struct *
-alloc_ordered_workqueue_lockdep_map(const char *fmt, unsigned int flags,
-                                   struct lockdep_map *lockdep_map, ...)
-{
-       struct workqueue_struct *wq;
-       va_list args;
-
-       va_start(args, lockdep_map);
-       wq = alloc_workqueue_lockdep_map(fmt, WQ_UNBOUND | __WQ_ORDERED | flags,
-                                        1, lockdep_map, args);
-       va_end(args);
+#define alloc_ordered_workqueue_lockdep_map(fmt, flags, lockdep_map, args...)  \
+       alloc_workqueue_lockdep_map(fmt, WQ_UNBOUND | __WQ_ORDERED | (flags), 1, lockdep_map, ##args)
 
Quoted 7 lines

include/linux/workqueue.h alloc_workqueue_lockdep_map(const char *fmt, unsigned int flags, int max_active, · line 549

Quoted 3 lines
include/linux/workqueue.h +0 -2
-       return wq;
-}
 #endif

Oh, I haven't checked the workqueue header. Yes, you are right. A macro should work.

To be clear we your change to __alloc_workqueue in workqueue.c AND my change to a macro in workqueue.h. In both cases a call chain of multiple va_start / va_end happens which from what I read up on is not allowed or undefined behavior depending on the compiler / platform target.

Quoted 4 lines

Tejun, how do you plan to handle this? Would it be possible to drop current series from your tree so that Matthew can send an updated version (with all the fixes squashed)?

Tejun, yes let me know how to move forward with this as it is highly desired for Intel Xe team to get this into 6.12.

Matt

#7 lore raw

On (24/08/16 02:45), Matthew Brost wrote:
[..]

Quoted 6 lines
Quoted 3 lines

Oh, I haven't checked the workqueue header. Yes, you are right. A macro should work.

To be clear we your change to __alloc_workqueue in workqueue.c AND my change to a macro in workqueue.h.

Oh, yes, correct, that's what I meant too - we need both.

// Plus a null-ptr-deref fix in __alloc_workqueue() error path.

maintainer
#8 lore raw
In reply to Matthew Brost

Hello,

Quoted 6 lines · Matthew Brost
Quoted 3 lines

Tejun, how do you plan to handle this? Would it be possible to drop current series from your tree so that Matthew can send an updated version (with all the fixes squashed)?

Tejun, yes let me know how to move forward with this as it is highly desired for Intel Xe team to get this into 6.12.

Can you just send a fixup patch?

Thanks.

Signature · 1 line
tejun
#9 lore raw
Quoted 12 lines · Tejun Heo

Hello,

Quoted 6 lines · Matthew Brost
Quoted 3 lines

Tejun, how do you plan to handle this? Would it be possible to drop current series from your tree so that Matthew can send an updated version (with all the fixes squashed)?

Tejun, yes let me know how to move forward with this as it is highly desired for Intel Xe team to get this into 6.12.

Can you just send a fixup patch?

Yes, will sent one out shortly.

Matt

Quoted 4 lines

Thanks.

Signature · 1 line
tejun
#10 lore raw
In reply to Tejun Heo
Quoted 11 lines · Tejun Heo

Hello,

Quoted 6 lines · Matthew Brost
Quoted 3 lines

Tejun, how do you plan to handle this? Would it be possible to drop current series from your tree so that Matthew can send an updated version (with all the fixes squashed)?

Tejun, yes let me know how to move forward with this as it is highly desired for Intel Xe team to get this into 6.12.

Can you just send a fixup patch?

Well, this will make the tree unbisectable (for a range of versions), because the errors in question break the boot.

#11 lore raw
Quoted 8 lines · Sergey Senozhatsky
Quoted 7 lines · Tejun Heo

Hello,

Quoted 4 lines · Matthew Brost
Quoted 3 lines

Tejun, how do you plan to handle this? Would it be possible to drop current series from your tree so that Matthew can send an updated version (with all the fixes squashed)?

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Tejun, yes let me know how to move forward with this as it is highly desired for Intel Xe team to get this into 6.12.

Can you just send a fixup patch?

Well, this will make the tree unbisectable (for a range of versions), because the errors in question break the boot.

Can the patches not just get squashed together in the 6.12 PR?

Matt

#12 lore raw
Quoted 12 lines · Matthew Brost
Quoted 11 lines · Sergey Senozhatsky
Quoted 10 lines · Tejun Heo

Hello,

Quoted 6 lines · Matthew Brost
Quoted 3 lines

Tejun, how do you plan to handle this? Would it be possible to drop current series from your tree so that Matthew can send an updated version (with all the fixes squashed)?

Tejun, yes let me know how to move forward with this as it is highly desired for Intel Xe team to get this into 6.12.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Can you just send a fixup patch?

Well, this will make the tree unbisectable (for a range of versions), because the errors in question break the boot.

Can the patches not just get squashed together in the 6.12 PR?

squash would be my personal preference. Usually resend works in cases like this (so that Link: in commit messages point to the relevant thread), but I don't know what Tejun's planning to do.

maintainer
#13 lore raw
In reply to Sergey Senozhatsky

On Wed, Aug 21, 2024 at 08:54:12AM +0900, Sergey Senozhatsky wrote:
...

Quoted 2 lines

Well, this will make the tree unbisectable (for a range of versions), because the errors in question break the boot.

The span is three commits with one htmldoc fix commit inbetween. If anyone is unlucky enough to hit that, git bisect skip is going to do just fine.

Thanks.

Signature · 1 line
tejun
#14 lore raw
Quoted 7 lines · Tejun Heo

On Wed, Aug 21, 2024 at 08:54:12AM +0900, Sergey Senozhatsky wrote:
...

Quoted 2 lines

Well, this will make the tree unbisectable (for a range of versions), because the errors in question break the boot.

The span is three commits with one htmldoc fix commit inbetween. If anyone is unlucky enough to hit that, git bisect skip is going to do just fine.

Well, OK. Can you also pick-up the null-ptr-deref fixup?

maintainer Applied
#15 lore raw
In reply to Sergey Senozhatsky
Quoted 35 lines · Sergey Senozhatsky

wq->lockdep_map is set only after __alloc_workqueue() successfully returns. However, on its error path __alloc_workqueue() may call destroy_workqueue() which expects wq->lockdep_map to be already set, which results in a null-ptr-deref in touch_wq_lockdep_map().

Add a simple NULL-check to touch_wq_lockdep_map().

Oops: general protection fault, probably for non-canonical address
KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007]
RIP: 0010:__lock_acquire+0x81/0x7800
[..]
Call Trace:
 <TASK>
 ? __die_body+0x66/0xb0
 ? die_addr+0xb2/0xe0
 ? exc_general_protection+0x300/0x470
 ? asm_exc_general_protection+0x22/0x30
 ? __lock_acquire+0x81/0x7800
 ? mark_lock+0x94/0x330
 ? __lock_acquire+0x12fd/0x7800
 ? __lock_acquire+0x3439/0x7800
 lock_acquire+0x14c/0x3e0
 ? __flush_workqueue+0x167/0x13a0
 ? __init_swait_queue_head+0xaf/0x150
 ? __flush_workqueue+0x167/0x13a0
 __flush_workqueue+0x17d/0x13a0
 ? __flush_workqueue+0x167/0x13a0
 ? lock_release+0x50f/0x830
 ? drain_workqueue+0x94/0x300
 drain_workqueue+0xe3/0x300
 destroy_workqueue+0xac/0xc40
 ? workqueue_sysfs_register+0x159/0x2f0
 __alloc_workqueue+0x1506/0x1760
 alloc_workqueue+0x61/0x150
Quoted 3 lines

...

Applied to wq/for-6.12.

Thanks.

Signature · 1 line
tejun