linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCHv3] kcov: reject open when kernel not instrumented
@ 2016-06-17  9:39 Mark Rutland
  2016-06-17 15:42 ` Kees Cook
  0 siblings, 1 reply; 6+ messages in thread
From: Mark Rutland @ 2016-06-17  9:39 UTC (permalink / raw)
  To: linux-kernel
  Cc: Mark Rutland, Alexander Potapenko, Andrew Morton, Dmitry Vyukov,
	James Morse, Kees Cook, Michal Marek

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.

As uninstrumented files (e.g. kernel/kcov.c) need to know when this
compiler feature is in use, wee pass the define via KBUILD_CFLAGS rather
than CFLAGS_KCOV.

Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: James Morse <james.morse@arm.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Michal Marek <mmarek@suse.com>
Cc: linux-kernel@vger.kernel.org
---
 Makefile      | 2 ++
 kernel/kcov.c | 9 +++++++++
 2 files changed, 11 insertions(+)

Since v1 [1]:
* Use CC_HAVE_SANCOV_TRACE_PC rather than CONFIG_KCOV_CC
Since v2 [2]:
* Use KBUILD_CFLAGS so kernel/kcov.c gets the flag

[1] http://lkml.kernel.org/r/1466005756-15626-1-git-send-email-mark.rutland@arm.com
[2] http://lkml.kernel.org/r/1466010285-2772-1-git-send-email-mark.rutland@arm.com

diff --git a/Makefile b/Makefile
index b409076..699d363 100644
--- a/Makefile
+++ b/Makefile
@@ -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
 
diff --git a/kernel/kcov.c b/kernel/kcov.c
index a02f2dd..0a0b164 100644
--- a/kernel/kcov.c
+++ b/kernel/kcov.c
@@ -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;
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-06-17 15:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-17  9:39 [PATCHv3] kcov: reject open when kernel not instrumented Mark Rutland
2016-06-17 15:42 ` Kees Cook
2016-06-17 15:46   ` Alexander Potapenko
2016-06-17 15:53     ` Mark Rutland
2016-06-17 15:48   ` Mark Rutland
2016-06-17 15:51     ` Dmitry Vyukov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).