linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fs: kernfs: Fix possible null-pointer dereferences in kernfs_path_from_node_locked()
@ 2019-07-24  2:22 Jia-Ju Bai
  2019-07-29 18:51 ` Tejun Heo
  0 siblings, 1 reply; 2+ messages in thread
From: Jia-Ju Bai @ 2019-07-24  2:22 UTC (permalink / raw)
  To: gregkh, tj; +Cc: linux-kernel, Jia-Ju Bai

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 <baijiaju1990@gmail.com>
---
 fs/kernfs/dir.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index a387534c9577..9b01f6ce1d79 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -137,6 +137,9 @@ 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;
@@ -144,8 +147,7 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
 	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,
-- 
2.17.0


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

* Re: [PATCH] fs: kernfs: Fix possible null-pointer dereferences in kernfs_path_from_node_locked()
  2019-07-24  2:22 [PATCH] fs: kernfs: Fix possible null-pointer dereferences in kernfs_path_from_node_locked() Jia-Ju Bai
@ 2019-07-29 18:51 ` Tejun Heo
  0 siblings, 0 replies; 2+ messages in thread
From: Tejun Heo @ 2019-07-29 18:51 UTC (permalink / raw)
  To: Jia-Ju Bai; +Cc: gregkh, linux-kernel

On Wed, Jul 24, 2019 at 10:22:42AM +0800, Jia-Ju Bai wrote:
> 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.

Can we just get rid of the NULL testing?

Thanks.

-- 
tejun

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

end of thread, other threads:[~2019-07-29 18:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-24  2:22 [PATCH] fs: kernfs: Fix possible null-pointer dereferences in kernfs_path_from_node_locked() Jia-Ju Bai
2019-07-29 18:51 ` Tejun Heo

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).