Skip to contentSkip to conversation

coredump: Do not lock during 'comm' reporting

PATCH In review Kees Cook 3 participants 6 messages Sep 28, 2024 – Sep 29, 2024

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 coredump: Do not lock during 'comm' reporting

The 'comm' member will always be NUL terminated, and this is not fast-path, so we can just perform a direct memcpy during a coredump instead of potentially deadlocking while holding the task struct lock.

Fixes c114e9948c2b ("coredump: Standartize and fix logging")

---
Cc: "Eric W. Biederman" <[email protected]>
Cc: Allen Pais <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Roman Kisel <[email protected]>
Cc: Xiaoming Ni <[email protected]>

Vegard, can you validate that this fixes the problem for you? I have been wrecked by covid, so very slow to respond here. There's a related thread about this locked, but we can just totally bypass it here.

---
 include/linux/coredump.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/coredump.h +2 -2
index edeb8532ce0f..a99079115a38 100644
@@ -52,8 +52,8 @@ extern int do_coredump(const kernel_siginfo_t *siginfo);
 #define __COREDUMP_PRINTK(Level, Format, ...) \
 	do {	\
 		char comm[TASK_COMM_LEN];	\
-	\
-		get_task_comm(comm, current);	\
+		/* This will always be NUL terminated. */ \
+		memcpy(comm, current->comm, sizeof(comm)); \
 		printk_ratelimited(Level "coredump: %d(%*pE): " Format "\n",	\
 			task_tgid_vnr(current), (int)strlen(comm), comm, ##__VA_ARGS__);	\
 	} while (0)	\
Signature · 1 line
2.34.1
#1 lore raw
Quoted 1 line · On Sat, 28 Sep 2024 14:08:31 -0700 Kees Cook <[email protected]> wrote:

The 'comm' member will always be NUL terminated,

Why is this? I thought this is only true if the caller holds task_lock()?

Quoted 4 lines

and this is not
fast-path, so we can just perform a direct memcpy during a coredump instead of potentially deadlocking while holding the task struct lock.

author
#2 lore raw
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Sat, 28 Sep 2024 14:08:31 -0700 Kees Cook <[email protected]> wrote:

The 'comm' member will always be NUL terminated,

Why is this? I thought this is only true if the caller holds task_lock()?

Because it's always written with strscpy_pad(). The final byte will always be NUL. (And this has been true for a very long time.)

Signature · 1 line
Kees Cook
#3 lore raw
Quoted 9 lines · On Sat, 28 Sep 2024 14:39:45 -0700 Kees Cook <[email protected]> wrote:
Quoted 5 lines · Andrew Morton
Quoted 1 line · On Sat, 28 Sep 2024 14:08:31 -0700 Kees Cook <[email protected]> wrote:

The 'comm' member will always be NUL terminated,

Why is this? I thought this is only true if the caller holds task_lock()?

Because it's always written with strscpy_pad(). The final byte will always be NUL. (And this has been true for a very long time.)

So why does __get_task_comm() need to take task_lock()?

author
#4 lore raw
Quoted 5 lines · Andrew Morton
Quoted 3 lines · On Sat, 28 Sep 2024 14:39:45 -0700 Kees Cook <[email protected]> wrote:
Quoted 2 lines · Andrew Morton

On Sat, 28 Sep 2024 14:08:31 -0700 Kees Cook <[email protected]> wrote:

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

The 'comm' member will always be NUL terminated,

Why is this? I thought this is only true if the caller holds task_lock()?

Because it's always written with strscpy_pad(). The final byte will always be NUL. (And this has been true for a very long time.)

So why does __get_task_comm() need to take task_lock()?

That was to make sure we didn't end up with garbled results, but discussions have determined that we don't care:
https://lore.kernel.org/all/[email protected]/

But just for safety's sake, I'll change this memcpy to:

    memcpy_and_pad(comm, sizeof(comm), current->comm, sizeof(comm) - 1, 0);
Signature · 1 line
Kees Cook
Tested-by
#5 lore raw
Quoted 5 lines · Kees Cook
Quoted 3 lines · Andrew Morton
Quoted 1 line · On Sat, 28 Sep 2024 14:39:45 -0700 Kees Cook <[email protected]> wrote:

On Sat, Sep 28, 2024 at 02:35:32PM -0700, Andrew Morton wrote:

So why does __get_task_comm() need to take task_lock()?

Quoted 8 lines

That was to make sure we didn't end up with garbled results, but discussions have determined that we don't care:
https://lore.kernel.org/all/[email protected]/

But just for safety's sake, I'll change this memcpy to:

     memcpy_and_pad(comm, sizeof(comm), current->comm, sizeof(comm) - 1, 0);

Reverting Linus's revert, applying the patch in this thread, and then changing it to that memcpy_and_pad above, everything seems to work well.

(However, I have not looked at the _safety_ of omitting task_lock()...)

I'm also wondering how I could successfully cat /proc/$pid/status before on one of these hung processes given that it does __get_task_comm(), which does task_lock(), which should have presumably hung as well?

Finally, I'll add that the comm/pid format is different from some other messages:

kernel: coredump: 31447(entry-fuzz.1): coredump has not been created, error -13
kernel: entry-fuzz.1[43396] bad frame in rt_sigreturn frame:00000000e4ec200e ip:40a6712e sp:40c25f20 orax:f

Vegard