Skip to content

Commit 01c8f98

Browse files
a-nogikhakpm00
authored andcommitted
kcov: don't lose track of remote references during softirqs
In kcov_remote_start()/kcov_remote_stop(), we swap the previous KCOV metadata of the current task into a per-CPU variable. However, the kcov_mode_enabled(mode) check is not sufficient in the case of remote KCOV coverage: current->kcov_mode always remains KCOV_MODE_DISABLED for remote KCOV objects. If the original task that has invoked the KCOV_REMOTE_ENABLE ioctl happens to get interrupted and kcov_remote_start() is called, it ultimately leads to kcov_remote_stop() NOT restoring the original KCOV reference. So when the task exits, all registered remote KCOV handles remain active forever. The most uncomfortable effect (at least for syzkaller) is that the bug prevents the reuse of the same /sys/kernel/debug/kcov descriptor. If we obtain it in the parent process and then e.g. drop some capabilities and continuously fork to execute individual programs, at some point current->kcov of the forked process is lost, kcov_task_exit() takes no action, and all KCOV_REMOTE_ENABLE ioctls calls from subsequent forks fail. And, yes, the efficiency is also affected if we keep on losing remote kcov objects. a) kcov_remote_map keeps on growing forever. b) (If I'm not mistaken), we're also not freeing the memory referenced by kcov->area. Fix it by introducing a special kcov_mode that is assigned to the task that owns a KCOV remote object. It makes kcov_mode_enabled() return true and yet does not trigger coverage collection in __sanitizer_cov_trace_pc() and write_comp_data(). [[email protected]: replace WRITE_ONCE() with an ordinary assignment] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Fixes: 5ff3b30 ("kcov: collect coverage from interrupts") Signed-off-by: Aleksandr Nogikh <[email protected]> Reviewed-by: Dmitry Vyukov <[email protected]> Reviewed-by: Andrey Konovalov <[email protected]> Tested-by: Andrey Konovalov <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Arnd Bergmann <[email protected]> Cc: Marco Elver <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 9094b4a commit 01c8f98

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

include/linux/kcov.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ enum kcov_mode {
2121
KCOV_MODE_TRACE_PC = 2,
2222
/* Collecting comparison operands mode. */
2323
KCOV_MODE_TRACE_CMP = 3,
24+
/* The process owns a KCOV remote reference. */
25+
KCOV_MODE_REMOTE = 4,
2426
};
2527

2628
#define KCOV_IN_CTXSW (1 << 30)

kernel/kcov.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd,
632632
return -EINVAL;
633633
kcov->mode = mode;
634634
t->kcov = kcov;
635+
t->kcov_mode = KCOV_MODE_REMOTE;
635636
kcov->t = t;
636637
kcov->remote = true;
637638
kcov->remote_size = remote_arg->area_size;

0 commit comments

Comments
 (0)