deepin-kernel/fs/overlayfs
Kees Cook 44b3575ae0 ovl: Check for NULL d_inode() in ovl_dentry_upper()
[ Upstream commit 8a39f1c870e9d6fbac5638f3a42a6a6363829c49 ]

In ovl_path_type() and ovl_is_metacopy_dentry() GCC notices that it is
possible for OVL_E() to return NULL (which implies that d_inode(dentry)
may be NULL). This would result in out of bounds reads via container_of(),
seen with GCC 15's -Warray-bounds -fdiagnostics-details. For example:

In file included from arch/x86/include/generated/asm/rwonce.h:1,
                 from include/linux/compiler.h:339,
                 from include/linux/export.h:5,
                 from include/linux/linkage.h:7,
                 from include/linux/fs.h:5,
                 from fs/overlayfs/util.c:7:
In function 'ovl_upperdentry_dereference',
    inlined from 'ovl_dentry_upper' at ../fs/overlayfs/util.c:305:9,
    inlined from 'ovl_path_type' at ../fs/overlayfs/util.c:216:6:
include/asm-generic/rwonce.h:44:26: error: array subscript 0 is outside array bounds of 'struct inode[7486503276667837]' [-Werror=array-bounds=]
   44 | #define __READ_ONCE(x)  (*(const volatile __unqual_scalar_typeof(x) *)&(x))
      |                         ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/asm-generic/rwonce.h:50:9: note: in expansion of macro '__READ_ONCE'
   50 |         __READ_ONCE(x);                                                 \
      |         ^~~~~~~~~~~
fs/overlayfs/ovl_entry.h:195:16: note: in expansion of macro 'READ_ONCE'
  195 |         return READ_ONCE(oi->__upperdentry);
      |                ^~~~~~~~~
  'ovl_path_type': event 1
  185 |         return inode ? OVL_I(inode)->oe : NULL;
  'ovl_path_type': event 2

Avoid this by allowing ovl_dentry_upper() to return NULL if d_inode() is
NULL, as that means the problematic dereferencing can never be reached.
Note that this fixes the over-eager compiler warning in an effort to
being able to enable -Warray-bounds globally. There is no known
behavioral bug here.

Suggested-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Kees Cook <kees@kernel.org>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
(cherry picked from commit 2cbeb47ea983cb79ca6464c5fd1abcb1372a7532)
2025-07-09 09:49:57 +08:00
..
Kconfig ovl: Kconfig: introduce CONFIG_OVERLAY_FS_DEBUG 2023-08-12 19:02:53 +03:00
Makefile ovl: modify layer parameter parsing 2023-06-20 14:10:40 +03:00
copy_up.c ovl: fix UAF in ovl_dentry_update_reval by moving dput() in ovl_link_up 2025-03-17 20:08:50 +08:00
dir.c HAOC: Add support for x86 CRED Protection (CREDP). 2025-05-12 15:02:47 +08:00
export.c ovl: support encoding fid from inode with no alias 2025-01-24 10:13:49 +08:00
file.c ovl: fix file reference leak when submitting aio 2023-10-02 13:08:31 +03:00
inode.c ovl: properly handle large files in ovl_security_fileattr 2024-12-10 20:45:15 +08:00
namei.c ovl: pass realinode to ovl_encode_real_fh() instead of realdentry 2025-01-24 10:13:49 +08:00
overlayfs.h ovl: remove unused forward declaration 2025-05-09 18:47:31 +08:00
ovl_entry.h ovl: make use of ->layers safe in rcu pathwalk 2023-10-02 17:45:02 +03:00
params.c ovl: fail if trusted xattrs are needed but caller lacks permission 2024-12-10 20:42:28 +08:00
params.h ovl: store and show the user provided lowerdir mount option 2024-03-26 18:19:18 -04:00
readdir.c vfs: get rid of old '->iterate' directory operation 2023-08-06 15:08:35 +02:00
super.c HAOC: Add support for x86 CRED Protection (CREDP). 2025-05-12 15:02:47 +08:00
util.c ovl: Check for NULL d_inode() in ovl_dentry_upper() 2025-07-09 09:49:57 +08:00