linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fs: overlayfs: Fix a possible null-pointer dereference in ovl_free_fs()
@ 2019-07-25  8:57 Jia-Ju Bai
  2019-07-25 13:03 ` Amir Goldstein
  0 siblings, 1 reply; 2+ messages in thread
From: Jia-Ju Bai @ 2019-07-25  8:57 UTC (permalink / raw)
  To: miklos; +Cc: linux-unionfs, linux-kernel, Jia-Ju Bai

In ovl_fill_super(), there is an if statement on line 1607 to check
whether ofs->upper_mnt is NULL:
    if (!ofs->upper_mnt)

When ofs->upper_mnt is NULL and d_make_root() on line 1654 fails,
ovl_free_fs() on line 1683 is executed.
In ovl_free_fs(), ofs->upper_mnt is used on line 224:
    ovl_inuse_unlock(ofs->upper_mnt->mnt_root);

Thus, a possible null-pointer dereference may occur.

To fix this bug, ofs->upper_mnt is checked before being used in
ovl_free_fs().

This bug is found by a static analysis tool STCheck written by us.

Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
---
 fs/overlayfs/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/overlayfs/super.c b/fs/overlayfs/super.c
index b368e2e102fa..1d7c3d280834 100644
--- a/fs/overlayfs/super.c
+++ b/fs/overlayfs/super.c
@@ -220,7 +220,7 @@ static void ovl_free_fs(struct ovl_fs *ofs)
 	if (ofs->workdir_locked)
 		ovl_inuse_unlock(ofs->workbasedir);
 	dput(ofs->workbasedir);
-	if (ofs->upperdir_locked)
+	if (ofs->upperdir_locked && ofs->upper_mnt)
 		ovl_inuse_unlock(ofs->upper_mnt->mnt_root);
 	mntput(ofs->upper_mnt);
 	for (i = 0; i < ofs->numlower; i++) {
-- 
2.17.0


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

* Re: [PATCH] fs: overlayfs: Fix a possible null-pointer dereference in ovl_free_fs()
  2019-07-25  8:57 [PATCH] fs: overlayfs: Fix a possible null-pointer dereference in ovl_free_fs() Jia-Ju Bai
@ 2019-07-25 13:03 ` Amir Goldstein
  0 siblings, 0 replies; 2+ messages in thread
From: Amir Goldstein @ 2019-07-25 13:03 UTC (permalink / raw)
  To: Jia-Ju Bai; +Cc: Miklos Szeredi, overlayfs, linux-kernel

On Thu, Jul 25, 2019 at 3:48 PM Jia-Ju Bai <baijiaju1990@gmail.com> wrote:
>
> In ovl_fill_super(), there is an if statement on line 1607 to check
> whether ofs->upper_mnt is NULL:
>     if (!ofs->upper_mnt)
>
> When ofs->upper_mnt is NULL and d_make_root() on line 1654 fails,
> ovl_free_fs() on line 1683 is executed.
> In ovl_free_fs(), ofs->upper_mnt is used on line 224:
>     ovl_inuse_unlock(ofs->upper_mnt->mnt_root);
>
> Thus, a possible null-pointer dereference may occur.
>
> To fix this bug, ofs->upper_mnt is checked before being used in
> ovl_free_fs().
>
> This bug is found by a static analysis tool STCheck written by us.
>
> Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
> ---
>  fs/overlayfs/super.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/overlayfs/super.c b/fs/overlayfs/super.c
> index b368e2e102fa..1d7c3d280834 100644
> --- a/fs/overlayfs/super.c
> +++ b/fs/overlayfs/super.c
> @@ -220,7 +220,7 @@ static void ovl_free_fs(struct ovl_fs *ofs)
>         if (ofs->workdir_locked)
>                 ovl_inuse_unlock(ofs->workbasedir);
>         dput(ofs->workbasedir);
> -       if (ofs->upperdir_locked)
> +       if (ofs->upperdir_locked && ofs->upper_mnt)
>                 ovl_inuse_unlock(ofs->upper_mnt->mnt_root);
>         mntput(ofs->upper_mnt);
>         for (i = 0; i < ofs->numlower; i++) {
> --

Can you teach STCheck to know that if upperdir_locked is only set this way:
        ofs->upper_mnt = upper_mnt;

        err = -EBUSY;
        if (ovl_inuse_trylock(ofs->upper_mnt->mnt_root)) {
                ofs->upperdir_locked = true;

Then upperdir_locked implies ofs->upper_mnt != NULL?

Whether or not this patch should be applied is not my call,
but the title "possible null-pointer dereference" is certainly not true.

Thanks,
Amir.

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

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

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-25  8:57 [PATCH] fs: overlayfs: Fix a possible null-pointer dereference in ovl_free_fs() Jia-Ju Bai
2019-07-25 13:03 ` Amir Goldstein

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