[PATCH 2/2] kernfs: define name and path to "(null)" for NULL pointer kernfs nodes

From: Konstantin Khlebnikov
Date: Thu Jan 26 2017 - 04:42:02 EST


The same LTP testcase triggers NULL deref css->cgroup->kn in the same place.

Commit a5bca2152036 ("cgroup: factor out cgroup_create() out of
cgroup_mkdir()") reordered cgroup construction. Now css created and
set online twice: first in cgroup_create(), second in cgroup_mkdir().
First happens before creation of kernfs node.

It seems safer and cleaner to handle NULL pointer right in kernfs than
spreading this magic across all other code. Now all functions that
prints kernfs path and name prints "(null)" for NULL pointer.

Also it might be a good idea to reorder cgroup construction to make these
NULL pointers unreachable: online css only after complete initialization.
After that NULL checks in kernfs could be surrounded with WARN_ON().

Signed-off-by: Konstantin Khlebnikov <khlebnikov@xxxxxxxxxxxxxx>
Fixes: a5bca2152036 ("cgroup: factor out cgroup_create() out of cgroup_mkdir()")
Cc: <stable@xxxxxxxxxxxxxxx> # 4.6+
---
fs/kernfs/dir.c | 6 ++++++
include/trace/events/cgroup.h | 20 ++++++--------------
2 files changed, 12 insertions(+), 14 deletions(-)

diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index cf4c636ff4da..2ba728d134b2 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -41,6 +41,9 @@ static bool kernfs_lockdep(struct kernfs_node *kn)

static int kernfs_name_locked(struct kernfs_node *kn, char *buf, size_t buflen)
{
+ if (!kn)
+ return strlcpy(buf, "(null)", buflen);
+
return strlcpy(buf, kn->parent ? kn->name : "/", buflen);
}

@@ -123,6 +126,9 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
size_t depth_from, depth_to, len = 0;
int i, j;

+ if (!kn_to)
+ return strlcpy(buf, "(null)", buflen);
+
if (!kn_from)
kn_from = kernfs_root(kn_to)->kn;

diff --git a/include/trace/events/cgroup.h b/include/trace/events/cgroup.h
index ab68640a18d0..c226f50e88fa 100644
--- a/include/trace/events/cgroup.h
+++ b/include/trace/events/cgroup.h
@@ -61,19 +61,15 @@ DECLARE_EVENT_CLASS(cgroup,
__field( int, id )
__field( int, level )
__dynamic_array(char, path,
- cgrp->kn ? cgroup_path(cgrp, NULL, 0) + 1
- : strlen("(null)"))
+ cgroup_path(cgrp, NULL, 0) + 1)
),

TP_fast_assign(
__entry->root = cgrp->root->hierarchy_id;
__entry->id = cgrp->id;
__entry->level = cgrp->level;
- if (cgrp->kn)
- cgroup_path(cgrp, __get_dynamic_array(path),
- __get_dynamic_array_len(path));
- else
- __assign_str(path, "(null)");
+ cgroup_path(cgrp, __get_dynamic_array(path),
+ __get_dynamic_array_len(path));
),

TP_printk("root=%d id=%d level=%d path=%s",
@@ -119,8 +115,7 @@ DECLARE_EVENT_CLASS(cgroup_migrate,
__field( int, dst_id )
__field( int, dst_level )
__dynamic_array(char, dst_path,
- dst_cgrp->kn ? cgroup_path(dst_cgrp, NULL, 0) + 1
- : strlen("(null)"))
+ cgroup_path(dst_cgrp, NULL, 0) + 1)
__field( int, pid )
__string( comm, task->comm )
),
@@ -129,11 +124,8 @@ DECLARE_EVENT_CLASS(cgroup_migrate,
__entry->dst_root = dst_cgrp->root->hierarchy_id;
__entry->dst_id = dst_cgrp->id;
__entry->dst_level = dst_cgrp->level;
- if (dst_cgrp->kn)
- cgroup_path(dst_cgrp, __get_dynamic_array(dst_path),
- __get_dynamic_array_len(dst_path));
- else
- __assign_str(dst_path, "(null)");
+ cgroup_path(dst_cgrp, __get_dynamic_array(dst_path),
+ __get_dynamic_array_len(dst_path));
__entry->pid = task->pid;
__assign_str(comm, task->comm);
),