[PATCH v2] kernel/kcov: unproxify debugfs file's fops

From: Nicolai Stange
Date: Tue May 24 2016 - 08:05:22 EST


Since commit 49d200deaa68 ("debugfs: prevent access to removed files'
private data"), a debugfs file's file_operations methods get proxied
through lifetime aware wrappers.

However, only a certain subset of the file_operations members is supported
by debugfs and ->mmap isn't among them -- it appears to be NULL from the
VFS layer's perspective.

This behaviour breaks the /sys/kernel/debug/kcov file introduced
concurrently with commit 5c9a8750a640 ("kernel: add kcov code coverage").

Since that file never gets removed, there is no file removal race and thus,
a lifetime checking proxy isn't needed.

Avoid the proxying for /sys/kernel/debug/kcov by creating it via
debugfs_create_file_unsafe() rather than debugfs_create_file().

Fixes: 49d200deaa68 ("debugfs: prevent access to removed files' private
data")
Fixes: 5c9a8750a640 ("kernel: add kcov code coverage")
Signed-off-by: Nicolai Stange <nicstange@xxxxxxxxx>
---
The v1 thread can be found at
http://lkml.kernel.org/g/1464011147-31836-1-git-send-email-nicstange@xxxxxxxxx

Changes to v1:
- Following the suggestion of Kees Cook, a comment explaining why the use
of debugfs_create_file_unsafe() is actually safe there has been added.

This issue has been debugged and reported by
Sasha Levin <sasha.levin@xxxxxxxxxx>:
http://lkml.kernel.org/g/573F4200.3080208@xxxxxxxxxx

Applicable to linux-next 20160524.
In particular, it depends on
- c64688081490 ("debugfs: add support for self-protecting attribute file
fops")
- 5c9a8750a640 ("kernel: add kcov code coverage")

kernel/kcov.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/kernel/kcov.c b/kernel/kcov.c
index a02f2dd..8d44b3f 100644
--- a/kernel/kcov.c
+++ b/kernel/kcov.c
@@ -264,7 +264,12 @@ static const struct file_operations kcov_fops = {

static int __init kcov_init(void)
{
- if (!debugfs_create_file("kcov", 0600, NULL, NULL, &kcov_fops)) {
+ /*
+ * The kcov debugfs file won't ever get removed and thus,
+ * there is no need to protect it against removal races. The
+ * use of debugfs_create_file_unsafe() is actually safe here.
+ */
+ if (!debugfs_create_file_unsafe("kcov", 0600, NULL, NULL, &kcov_fops)) {
pr_err("failed to create kcov in debugfs\n");
return -ENOMEM;
}
--
2.8.2