kcov: reject open when kernel not instrumented
PATCH v2 1/1 kcov: reject open when kernel not instrumented
If the toolchain does not support -fsanitize-coverage=trace-pc, we blat this option from CFLAGS_KCOV, and build the kernel without instrumentation, even if CONFIG_KCOV was selected. However, we still build the rest of the kcov infrastructure, and expose a kcov file under debugfs. This can be confusing, as the kernel will appear to support kcov, yet will never manage to sample any trace PC values. While we do note this fact at build time, this may be missed, and a user may not have access to build logs.
This patch ensures that CC_HAVE_SANCOV_TRACE_PC is defined when the toolchain supports -fsanitize-coverage=trace-pc, and is not defined otherwise. When CC_HAVE_SANCOV_TRACE_PC is not defined, the kernel will return -ENOTSUPP if userspace attempts to open the kcov debugfs file, indicating that kcov functionality is unavailable.
--- Makefile | 2 +- kernel/kcov.c | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-)
Since v1 [1]:
* Use CC_HAVE_SANCOV_TRACE_PC rather than CONFIG_KCOV_CC
[1] lkml.kernel.org/r/[email protected]
Makefile +1 -1
@@ -369,7 +369,7 @@ LDFLAGS_MODULE = CFLAGS_KERNEL = AFLAGS_KERNEL = CFLAGS_GCOV = -fprofile-arcs -ftest-coverage -fno-tree-loop-im -Wno-maybe-uninitialized -CFLAGS_KCOV = -fsanitize-coverage=trace-pc +CFLAGS_KCOV = -fsanitize-coverage=trace-pc -DCC_HAVE_SANCOV_TRACE_PC # Use USERINCLUDE when you must reference the UAPI directories only.
kernel/kcov.c +9 -0
@@ -3,6 +3,7 @@#define DISABLE_BRANCH_PROFILING #include <linux/compiler.h> #include <linux/types.h> +#include <linux/errno.h> #include <linux/file.h> #include <linux/fs.h> #include <linux/mm.h>
@@ -160,6 +161,14 @@ static int kcov_open(struct inode *inode, struct file *filep) {struct kcov *kcov; +#ifndef CC_HAVE_SANCOV_TRACE_PC +/* +* CONFIG_KCOV was selected, but the compiler does not support the +* options KCOV requires. +*/ +return -ENOTSUPP ; +#endif /* CC_HAVE_SANCOV_TRACE_PC */ + kcov =kzalloc (sizeof (*kcov),GFP_KERNEL );if (!kcov)return -ENOMEM ;
Signature · 1 line
1.9.1
Dmitry Vyukov
reviewerQuoted 63 lines · Mark Rutland
If the toolchain does not support -fsanitize-coverage=trace-pc, we blat this option from
CFLAGS_KCOV, and build the kernel without instrumentation, even ifCONFIG_KCOVwas selected. However, we still build the rest of the kcov infrastructure, and expose a kcov file under debugfs. This can be confusing, as the kernel will appear to support kcov, yet will never manage to sample any trace PC values. While we do note this fact at build time, this may be missed, and a user may not have access to build logs.This patch ensures that
CC_HAVE_SANCOV_TRACE_PCis defined when the toolchain supports -fsanitize-coverage=trace-pc, and is not defined otherwise. WhenCC_HAVE_SANCOV_TRACE_PCis not defined, the kernel will return-ENOTSUPPif userspace attempts to open the kcov debugfs file, indicating that kcov functionality is unavailable.Signed-off-by Mark Rutland <[email protected]>Cc Alexander Potapenko <[email protected]>Cc Andrew Morton <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc James Morse <[email protected]>Cc Kees Cook <[email protected]>Cc Michal Marek <[email protected]>--- Makefile | 2 +- kernel/kcov.c | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-)Since v1 [1]:
* UseCC_HAVE_SANCOV_TRACE_PCrather thanCONFIG_KCOV_CC[1] lkml.kernel.org/r/[email protected]
Makefile +1
-1@@ -369,7 +369,7 @@ LDFLAGS_MODULE = CFLAGS_KERNEL = AFLAGS_KERNEL = CFLAGS_GCOV = -fprofile-arcs -ftest-coverage -fno-tree-loop-im -Wno-maybe-uninitialized -CFLAGS_KCOV = -fsanitize-coverage=trace-pc +CFLAGS_KCOV = -fsanitize-coverage=trace-pc -DCC_HAVE_SANCOV_TRACE_PC # Use USERINCLUDE when you must reference the UAPI directories only.kernel/kcov.c +2
-0@@ -3,6 +3,7 @@#define DISABLE_BRANCH_PROFILING #include <linux/compiler.h> #include <linux/types.h> +#include <linux/errno.h> #include <linux/file.h> #include <linux/fs.h> #include <linux/mm.h> @@ -160,6 +161,14 @@ static int kcov_open(struct inode *inode, struct file *filep) {struct kcov *kcov; +#ifndef CC_HAVE_SANCOV_TRACE_PC
kernel/kcov.c static int kcov_open(struct inode *inode, struct file *filep) · line 174
Quoted 10 lines
kernel/kcov.c +7
-0+/* +* CONFIG_KCOV was selected, but the compiler does not support the +* options KCOV requires. +*/ +return -ENOTSUPP ; +#endif /* CC_HAVE_SANCOV_TRACE_PC */ + kcov =kzalloc (sizeof (*kcov),GFP_KERNEL );if (!kcov)return -ENOMEM ;
Looks good to me.
Hi Mark,
Quoted 41 lines · Mark Rutland
If the toolchain does not support -fsanitize-coverage=trace-pc, we blat this option from
CFLAGS_KCOV, and build the kernel without instrumentation, even ifCONFIG_KCOVwas selected. However, we still build the rest of the kcov infrastructure, and expose a kcov file under debugfs. This can be confusing, as the kernel will appear to support kcov, yet will never manage to sample any trace PC values. While we do note this fact at build time, this may be missed, and a user may not have access to build logs.This patch ensures that
CC_HAVE_SANCOV_TRACE_PCis defined when the toolchain supports -fsanitize-coverage=trace-pc, and is not defined otherwise. WhenCC_HAVE_SANCOV_TRACE_PCis not defined, the kernel will return-ENOTSUPPif userspace attempts to open the kcov debugfs file, indicating that kcov functionality is unavailable.Makefile +1
-1@@ -369,7 +369,7 @@ LDFLAGS_MODULE = CFLAGS_KERNEL = AFLAGS_KERNEL = CFLAGS_GCOV = -fprofile-arcs -ftest-coverage -fno-tree-loop-im -Wno-maybe-uninitialized -CFLAGS_KCOV = -fsanitize-coverage=trace-pc +CFLAGS_KCOV = -fsanitize-coverage=trace-pc -DCC_HAVE_SANCOV_TRACE_PC # Use USERINCLUDE when you must reference the UAPI directories only.kernel/kcov.c +1
-0@@ -3,6 +3,7 @@#define DISABLE_BRANCH_PROFILING #include <linux/compiler.h> #include <linux/types.h> +#include <linux/errno.h> #include <linux/file.h> #include <linux/fs.h> #include <linux/mm.h>
kernel/kcov.c static int kcov_open(struct inode *inode, struct file *filep) · line 164
Quoted 4 lines
kernel/kcov.c +1
-0@@ -160,6 +161,14 @@ static int kcov_open(struct inode *inode, struct file *filep) {struct kcov *kcov; +#ifndef CC_HAVE_SANCOV_TRACE_PC
I don't think this will work as kernel/kcov.c is listed in the Makefile as:
Quoted 2 lines
kernel/kcov.c +0
-0# Don 't self-instrument.KCOV_INSTRUMENT_kcov.o := n
This will cause the build machinery in scripts/Makefile.lib to not give kernel/kcov.c the CFLAGS_KCOV contents:
Quoted 5 lines
ifeq ($(CONFIG_KCOV),y)
_c_flags += $(if $(patsubst n%,, \ $(KCOV_INSTRUMENT_$(basetarget).o)$(KCOV_INSTRUMENT)y), \ $(CFLAGS_KCOV))endif
... so kernel/kcov.c will never see anything in CFLAGS_KCOV ...
An alternative would be to add the flag to the compiler test that generates the 'not supported' warning, but it needs to go in another CFLAGS variable. Something like:
-------------------%<-------------------
Makefile +2 -1
@@ -687,6 +687,8 @@ ifdef CONFIG_KCOV $(warning Cannot use CONFIG_KCOV: \ -fsanitize-coverage=trace-pc is not supported by compiler) CFLAGS_KCOV = + else + KBUILD_CFLAGS += -DCC_HAVE_SANCOV_TRACE_PC endif endif -------------------%<-------------------
Thanks,
James
Quoted 56 lines · James Morse
Hi Mark,
Quoted 41 lines · Mark Rutland
If the toolchain does not support -fsanitize-coverage=trace-pc, we blat this option from
CFLAGS_KCOV, and build the kernel without instrumentation, even ifCONFIG_KCOVwas selected. However, we still build the rest of the kcov infrastructure, and expose a kcov file under debugfs. This can be confusing, as the kernel will appear to support kcov, yet will never manage to sample any trace PC values. While we do note this fact at build time, this may be missed, and a user may not have access to build logs.This patch ensures that
CC_HAVE_SANCOV_TRACE_PCis defined when the toolchain supports -fsanitize-coverage=trace-pc, and is not defined otherwise. WhenCC_HAVE_SANCOV_TRACE_PCis not defined, the kernel will return-ENOTSUPPif userspace attempts to open the kcov debugfs file, indicating that kcov functionality is unavailable.Makefile +1
-1@@ -369,7 +369,7 @@ LDFLAGS_MODULE = CFLAGS_KERNEL = AFLAGS_KERNEL = CFLAGS_GCOV = -fprofile-arcs -ftest-coverage -fno-tree-loop-im -Wno-maybe-uninitialized -CFLAGS_KCOV = -fsanitize-coverage=trace-pc +CFLAGS_KCOV = -fsanitize-coverage=trace-pc -DCC_HAVE_SANCOV_TRACE_PC # Use USERINCLUDE when you must reference the UAPI directories only.kernel/kcov.c +1
-0@@ -3,6 +3,7 @@#define DISABLE_BRANCH_PROFILING #include <linux/compiler.h> #include <linux/types.h> +#include <linux/errno.h> #include <linux/file.h> #include <linux/fs.h> #include <linux/mm.h> kernel/kcov.c static int kcov_open(struct inode *inode, struct file *filep) · line 164
Quoted 4 lines
kernel/kcov.c +1
-0@@ -160,6 +161,14 @@ static int kcov_open(struct inode *inode, struct file *filep) {struct kcov *kcov; +#ifndef CC_HAVE_SANCOV_TRACE_PC I don't think this will work as
kernel/kcov.cis listed in theMakefileas:Quoted 2 lines
kernel/kcov.c +0
-0# Don 't self-instrument.KCOV_INSTRUMENT_kcov.o := n
This will cause the build machinery in
scripts/Makefile.libto not givekernel/kcov.ctheCFLAGS_KCOVcontents:
Quoted 7 lines
Quoted 5 lines
ifeq ($(CONFIG_KCOV),y)
_c_flags += $(if $(patsubst n%,, \ $(KCOV_INSTRUMENT_$(basetarget).o)$(KCOV_INSTRUMENT)y), \ $(CFLAGS_KCOV))endif
... so
kernel/kcov.cwill never see anything inCFLAGS_KCOV...
Good spot!
Evidently I was trying to be overly clever here; my bad.
Quoted 7 lines
An alternative would be to add the flag to the compiler test that generates the 'not supported' warning, but it needs to go in another CFLAGS variable. Something like:
-------------------%<-------------------
Makefile ifdef CONFIG_KCOV · line 692
Quoted 8 lines
Makefile +2
-1@@ -687,6 +687,8 @@ ifdef CONFIG_KCOV $(warning Cannot use CONFIG_KCOV: \ -fsanitize-coverage=trace-pc is not supported by compiler) CFLAGS_KCOV = + else + KBUILD_CFLAGS += -DCC_HAVE_SANCOV_TRACE_PC endif endif -------------------%<-------------------
I'll fold the above in for v3.
Thanks,
Mark.