Skip to contentSkip to conversation

ovl: avoid possible NULL dereference

PATCH In review Su Hui 3 participants 5 messages Sep 25, 2023 – Sep 28, 2023

Subsystems (2)
lore
Share & tools
lore

Su Hui

author
#0 lore raw

PATCH 1/1 ovl: avoid possible NULL dereference

smatch warn:
fs/overlayfs/copy_up.c:450 ovl_set_origin() warn:
variable dereferenced before check 'fh' (see line 449)

If 'fh' is NULL, passing NULL instead of 'fh->buf'.

---
 fs/overlayfs/copy_up.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/overlayfs/copy_up.c +1 -1
index d1761ec5866a..086f9176b4d4 100644
@@ -446,7 +446,7 @@ int ovl_set_origin(struct ovl_fs *ofs, struct dentry *lower,
 	/*
 	 * Do not fail when upper doesn't support xattrs.
 	 */
-	err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf,
+	err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh ? fh->buf : NULL,
 				 fh ? fh->fb.len : 0, 0);
 	kfree(fh);
 
Signature · 1 line
2.30.2
maintainer
#1 lore raw
Quoted 26 lines · Su Hui

smatch warn:
fs/overlayfs/copy_up.c:450 ovl_set_origin() warn:
variable dereferenced before check 'fh' (see line 449)

If 'fh' is NULL, passing NULL instead of 'fh->buf'.

---
 fs/overlayfs/copy_up.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/overlayfs/copy_up.c +1 -2
index d1761ec5866a..086f9176b4d4 100644
@@ -446,7 +446,7 @@ int ovl_set_origin(struct ovl_fs *ofs, struct dentry *lower,
        /*
         * Do not fail when upper doesn't support xattrs.
         */
-       err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf,
+       err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh ? fh->buf : NULL,
                                 fh ? fh->fb.len : 0, 0);
        kfree(fh);
 
--
Quoted 1 line

2.30.2

After discussing this with Dan Carpenter, this is not a kernel bug, it is a smatch bug.

The value being passed to setxattr is (void *)OVL_FH_FID_OFFSET, which is just as arbitrary as NULL when size is 0.

Thanks,
Amir.

#2 lore raw
Quoted 26 lines · Amir Goldstein
Quoted 25 lines · Su Hui

smatch warn:
fs/overlayfs/copy_up.c:450 ovl_set_origin() warn:
variable dereferenced before check 'fh' (see line 449)

If 'fh' is NULL, passing NULL instead of 'fh->buf'.

---
 fs/overlayfs/copy_up.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/overlayfs/copy_up.c +1 -1
index d1761ec5866a..086f9176b4d4 100644
@@ -446,7 +446,7 @@ int ovl_set_origin(struct ovl_fs *ofs, struct dentry *lower,
        /*
         * Do not fail when upper doesn't support xattrs.
         */
-       err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf,
+       err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh ? fh->buf : NULL,
                                 fh ? fh->fb.len : 0, 0);
        kfree(fh);
 
Quoted 5 lines
Quoted 2 lines

--
2.30.2

After discussing this with Dan Carpenter, this is not a kernel bug, it is a smatch bug.

Yeah. Sorry about that, Su Hui. The ->buf struct member is not a pointer, it's an array. So this isn't really a dereference, it's just pointer math and foo = fh->buf won't crash even if fh is NULL.

I have written a fix for this in Smatch. I'll test it a bit before I push it.

regards,
dan carpenter

author
#3 lore raw
Quoted 26 lines · Dan Carpenter
Quoted 25 lines · Amir Goldstein
Quoted 24 lines · Su Hui

smatch warn:
fs/overlayfs/copy_up.c:450 ovl_set_origin() warn:
variable dereferenced before check 'fh' (see line 449)

If 'fh' is NULL, passing NULL instead of 'fh->buf'.

---
  fs/overlayfs/copy_up.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fs/overlayfs/copy_up.c +1 -1
index d1761ec5866a..086f9176b4d4 100644
@@ -446,7 +446,7 @@ int ovl_set_origin(struct ovl_fs *ofs, struct dentry *lower,
         /*
          * Do not fail when upper doesn't support xattrs.
          */
-       err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf,
+       err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh ? fh->buf : NULL,
                                  fh ? fh->fb.len : 0, 0);
         kfree(fh);
 
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

--
2.30.2

After discussing this with Dan Carpenter, this is not a kernel bug, it is a smatch bug.

Yeah. Sorry about that, Su Hui. The ->buf struct member is not a pointer, it's an array. So this isn't really a dereference, it's just pointer math and foo = fh->buf won't crash even if fh is NULL.

Got it, I'm so careless that make this wrong patch. Really thanks for your reminder!

Su Hui

Quoted 7 lines

I have written a fix for this in Smatch. I'll test it a bit before I push it.

regards,
dan carpenter

#4 lore raw
Quoted 1 line · Su Hui

Got it, I'm so careless that make this wrong patch.

Not at all. Your patch didn't break anything and this stuff is subtle. I've done the same thing myself.

regards,
dan carpenter