Skip to content

Commit

Permalink
fs: kernfs: Fix possible null-pointer dereferences in kernfs_path_fro…
Browse files Browse the repository at this point in the history
…m_node_locked()

In kernfs_path_from_node_locked(), there is an if statement on line 147
to check whether buf is NULL:
    if (buf)

When buf is NULL, it is used on line 151:
    len += strlcpy(buf + len, parent_str, ...)
and line 158:
    len += strlcpy(buf + len, "/", ...)
and line 160:
    len += strlcpy(buf + len, kn->name, ...)

Thus, possible null-pointer dereferences may occur.

To fix these possible bugs, buf is checked before being used.
If it is NULL, -EINVAL is returned.

These bugs are found by a static analysis tool STCheck written by us.

Signed-off-by: Jia-Ju Bai <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
XidianGeneral authored and gregkh committed Jul 25, 2019
1 parent 2fd60da commit bbe70e4
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions fs/kernfs/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,17 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
if (kn_from == kn_to)
return strlcpy(buf, "/", buflen);

if (!buf)
return -EINVAL;

common = kernfs_common_ancestor(kn_from, kn_to);
if (WARN_ON(!common))
return -EINVAL;

depth_to = kernfs_depth(common, kn_to);
depth_from = kernfs_depth(common, kn_from);

if (buf)
buf[0] = '\0';
buf[0] = '\0';

for (i = 0; i < depth_from; i++)
len += strlcpy(buf + len, parent_str,
Expand Down

0 comments on commit bbe70e4

Please sign in to comment.