Skip to contentSkip to conversation

fs/overlayfs/copy_up.c:459 ovl_set_origin_fh() warn: variable dereferenced before check 'fh' (see line 458)

PATCH In review Dan Carpenter 2 participants 6 messages Sep 27, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 8/8 fs/overlayfs/copy_up.c:459 ovl_set_origin_fh() warn: variable dereferenced before check 'fh' (see line 458)

tree:   https://github.com/amir73il/linux ovl_want_write
head:   4ea9d9f55925f6631923f76831d8a2a850d3876e
commit: 4ea9d9f55925f6631923f76831d8a2a850d3876e [8/8] ovl: do not encode lower fh with upper sb_writers held
config: i386-randconfig-141-20230909 (https://download.01.org/0day-ci/archive/20230909/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce: (https://download.01.org/0day-ci/archive/20230909/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
fs/overlayfs/copy_up.c:459 ovl_set_origin_fh() warn: variable dereferenced before check 'fh' (see line 458)

vim +/fh +459 fs/overlayfs/copy_up.c

4ea9d9f55925f6 Amir Goldstein 2023-08-16  450  int ovl_set_origin_fh(struct ovl_fs *ofs, const struct ovl_fh *fh,
4ea9d9f55925f6 Amir Goldstein 2023-08-16  451  		      struct dentry *upper)
4ea9d9f55925f6 Amir Goldstein 2023-08-16  452  {
4ea9d9f55925f6 Amir Goldstein 2023-08-16  453  	int err;
4ea9d9f55925f6 Amir Goldstein 2023-08-16  454  
6266d465bde044 Miklos Szeredi 2017-05-18  455  	/*
6266d465bde044 Miklos Szeredi 2017-05-18  456  	 * Do not fail when upper doesn't support xattrs.
6266d465bde044 Miklos Szeredi 2017-05-18  457  	 */
a0c236b11706cc Amir Goldstein 2021-06-19 @458  	err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf,
                                                                                                       ^^^^^^^

Dereference

cbe7fba8edfc8c Amir Goldstein 2019-11-15 @459  				 fh ? fh->fb.len : 0, 0);
                                                                         ^^

Checked too late. This code is from 2021 though, so why is the warning getting reported now?

3a1e819b4e8021 Amir Goldstein 2017-03-30  460  
6939f977c54a39 Miklos Szeredi 2020-12-14  461  	/* Ignore -EPERM from setting "user.*" on symlink/special */
6939f977c54a39 Miklos Szeredi 2020-12-14  462  	return err == -EPERM ? 0 : err;
3a1e819b4e8021 Amir Goldstein 2017-03-30  463  }
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#1 lore raw
Quoted 29 lines · Dan Carpenter
tree:   https://github.com/amir73il/linux ovl_want_write
head:   4ea9d9f55925f6631923f76831d8a2a850d3876e
commit: 4ea9d9f55925f6631923f76831d8a2a850d3876e [8/8] ovl: do not encode lower fh with upper sb_writers held
config: i386-randconfig-141-20230909 (https://download.01.org/0day-ci/archive/20230909/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce: (https://download.01.org/0day-ci/archive/20230909/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
fs/overlayfs/copy_up.c:459 ovl_set_origin_fh() warn: variable dereferenced before check 'fh' (see line 458)

vim +/fh +459 fs/overlayfs/copy_up.c

4ea9d9f55925f6 Amir Goldstein 2023-08-16  450  int ovl_set_origin_fh(struct ovl_fs *ofs, const struct ovl_fh *fh,
4ea9d9f55925f6 Amir Goldstein 2023-08-16  451                 struct dentry *upper)
4ea9d9f55925f6 Amir Goldstein 2023-08-16  452  {
4ea9d9f55925f6 Amir Goldstein 2023-08-16  453   int err;
4ea9d9f55925f6 Amir Goldstein 2023-08-16  454
6266d465bde044 Miklos Szeredi 2017-05-18  455   /*
6266d465bde044 Miklos Szeredi 2017-05-18  456    * Do not fail when upper doesn't support xattrs.
6266d465bde044 Miklos Szeredi 2017-05-18  457    */
a0c236b11706cc Amir Goldstein 2021-06-19 @458   err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf,
                                                                                                       ^^^^^^^
Quoted 6 lines

Dereference

cbe7fba8edfc8c Amir Goldstein 2019-11-15 @459                            fh ? fh->fb.len : 0, 0);
                                                                         ^^

Checked too late. This code is from 2021 though, so why is the warning getting reported now?

That is what I was asking myself as well.
The regressing commit is actually from 2019:

cbe7fba8edfc ("ovl: make sure that real fid is 32bit aligned in memory")

I am guessing that the report above was triggered by my change of helper name (to ovl_set_origin_fh()) in overlayfs-next.

Anyway, Su Hui just reported the same smatch warning a few days ago:

https://lore.kernel.org/linux-unionfs/[email protected]/

I will apply Su Hui's fix to overlayfs-next.
It does not seem too urgent though, because setxattr with 0 length would probably ignore the invalid value of buf.

Thanks,
Amir.

#2 lore raw
Quoted 17 lines · Amir Goldstein
Quoted 8 lines

a0c236b11706cc Amir Goldstein 2021-06-19 @458 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf,

                                                                                                       ^^^^^^^

Dereference

cbe7fba8edfc8c Amir Goldstein 2019-11-15 @459                            fh ? fh->fb.len : 0, 0);
                                                                         ^^

Checked too late. This code is from 2021 though, so why is the warning getting reported now?

That is what I was asking myself as well.
The regressing commit is actually from 2019:

cbe7fba8edfc ("ovl: make sure that real fid is 32bit aligned in memory")

I am guessing that the report above was triggered by my change of helper name (to ovl_set_origin_fh()) in overlayfs-next.

Quoted 7 lines

Anyway, Su Hui just reported the same smatch warning a few days ago:

https://lore.kernel.org/linux-unionfs/[email protected]/

I will apply Su Hui's fix to overlayfs-next.
It does not seem too urgent though, because setxattr with 0 length would probably ignore the invalid value of buf.

I don't know if it's urgent or not, but that explanation doesn't work. This isn't an inline function. So the dereference would happen at the call site and not inside the ovl_check_setxattr() function.

regards,
dan carpenter

#3 lore raw
Quoted 22 lines · Dan Carpenter
Quoted 21 lines · Amir Goldstein
Quoted 8 lines

a0c236b11706cc Amir Goldstein 2021-06-19 @458 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf,

                                                                                                       ^^^^^^^

Dereference

cbe7fba8edfc8c Amir Goldstein 2019-11-15 @459                            fh ? fh->fb.len : 0, 0);
                                                                         ^^

Checked too late. This code is from 2021 though, so why is the warning getting reported now?

That is what I was asking myself as well.
The regressing commit is actually from 2019:

cbe7fba8edfc ("ovl: make sure that real fid is 32bit aligned in memory")

I am guessing that the report above was triggered by my change of helper name (to ovl_set_origin_fh()) in overlayfs-next.

Anyway, Su Hui just reported the same smatch warning a few days ago:

https://lore.kernel.org/linux-unionfs/[email protected]/

Quoted 7 lines
Quoted 3 lines

I will apply Su Hui's fix to overlayfs-next.
It does not seem too urgent though, because setxattr with 0 length would probably ignore the invalid value of buf.

I don't know if it's urgent or not, but that explanation doesn't work. This isn't an inline function. So the dereference would happen at the call site and not inside the ovl_check_setxattr() function.

Ah. Nope. I'm wrong. fb->buf[] is an array, so this is pointer
math and not a dereference. Sorry!

Perhaps we did discuss this earlier and decided it's not a bug.

regards,
dan carpenter

#4 lore raw
Quoted 28 lines · Dan Carpenter
Quoted 26 lines · Dan Carpenter
Quoted 24 lines · Amir Goldstein
Quoted 8 lines

a0c236b11706cc Amir Goldstein 2021-06-19 @458 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf,

                                                                                                       ^^^^^^^

Dereference

cbe7fba8edfc8c Amir Goldstein 2019-11-15 @459                            fh ? fh->fb.len : 0, 0);
                                                                         ^^

Checked too late. This code is from 2021 though, so why is the warning getting reported now?

That is what I was asking myself as well.
The regressing commit is actually from 2019:

cbe7fba8edfc ("ovl: make sure that real fid is 32bit aligned in memory")

I am guessing that the report above was triggered by my change of helper name (to ovl_set_origin_fh()) in overlayfs-next.

Anyway, Su Hui just reported the same smatch warning a few days ago:

https://lore.kernel.org/linux-unionfs/[email protected]/

I will apply Su Hui's fix to overlayfs-next.
It does not seem too urgent though, because setxattr with 0 length would probably ignore the invalid value of buf.

Quoted 9 lines
Quoted 3 lines

I don't know if it's urgent or not, but that explanation doesn't work. This isn't an inline function. So the dereference would happen at the call site and not inside the ovl_check_setxattr() function.

Ah. Nope. I'm wrong. fb->buf[] is an array, so this is pointer
math and not a dereference. Sorry!

Perhaps we did discuss this earlier and decided it's not a bug.

I can imagine we did :)
It's still worth fixing though, but not urgent.

Thanks,
Amir.

#5 lore raw
In reply to Dan Carpenter
Quoted 27 lines · Dan Carpenter
Quoted 26 lines · Dan Carpenter
Quoted 24 lines · Amir Goldstein
Quoted 8 lines

a0c236b11706cc Amir Goldstein 2021-06-19 @458 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf,

                                                                                                       ^^^^^^^

Dereference

cbe7fba8edfc8c Amir Goldstein 2019-11-15 @459                            fh ? fh->fb.len : 0, 0);
                                                                         ^^

Checked too late. This code is from 2021 though, so why is the warning getting reported now?

That is what I was asking myself as well.
The regressing commit is actually from 2019:

cbe7fba8edfc ("ovl: make sure that real fid is 32bit aligned in memory")

I am guessing that the report above was triggered by my change of helper name (to ovl_set_origin_fh()) in overlayfs-next.

Anyway, Su Hui just reported the same smatch warning a few days ago:

https://lore.kernel.org/linux-unionfs/[email protected]/

I will apply Su Hui's fix to overlayfs-next.
It does not seem too urgent though, because setxattr with 0 length would probably ignore the invalid value of buf.

Quoted 8 lines
Quoted 3 lines

I don't know if it's urgent or not, but that explanation doesn't work. This isn't an inline function. So the dereference would happen at the call site and not inside the ovl_check_setxattr() function.

Ah. Nope. I'm wrong. fb->buf[] is an array, so this is pointer
math and not a dereference. Sorry!

Perhaps we did discuss this earlier and decided it's not a bug.

The truth is that these are not supposed to generate warnings.

Previously Smatch handled dereferences in an awkward way and that code was duplicated in a bunch of different checks. So I would maybe silence something like this in one place but forget to update the other five places.

I've cleaned up that code now and centralized it. It's more user friendly and also just better and more accurate.

So I've written a patch today to silence this warning in Smatch because it actually was not supposed to print a warning. I'll test it out and push it later.

regards,
dan carpenter