Skip to contentSkip to conversation

nilfs: Fix OOB in nilfs_set_de_type

PATCH In review Jeongjun Park 2 participants 2 messages Apr 15, 2024 NILFS2 filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nilfs: Fix OOB in nilfs_set_de_type

static void nilfs_set_de_type(struct nilfs_dir_entry *de, struct inode *inode)

{
	umode_t mode = inode->i_mode;

	de->file_type = nilfs_type_by_mode[(mode & S_IFMT)>>S_SHIFT]; // oob
}

The size of the nilfs_type_by_mode array in the fs/nilfs2/dir.c file is defined as "S_IFMT >> S_SHIFT", but the nilfs_set_de_type() function, which uses this array, specifies the index to read from the array in the same way as "(mode & S_IFMT) >> S_SHIFT".

However, when the index is determined this way, an out-of-bounds (OOB) error occurs by referring to an index that is 1 larger than the array size when the condition "mode & S_IFMT == S_IFMT" is satisfied. Therefore, a patch to resize the nilfs_type_by_mode array should be applied to prevent OOB errors.

Reported-and-tested-by [email protected]
Fixes 2ba466d74ed7 ("nilfs2: directory entry operations")
---
 fs/nilfs2/dir.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/nilfs2/dir.c +1 -1
index bc846b904b68..aee40db7a036 100644
@@ -240,7 +240,7 @@ nilfs_filetype_table[NILFS_FT_MAX] = {
 
 #define S_SHIFT 12
 static unsigned char
-nilfs_type_by_mode[S_IFMT >> S_SHIFT] = {
+nilfs_type_by_mode[(S_IFMT >> S_SHIFT) + 1] = {
        [S_IFREG >> S_SHIFT]    = NILFS_FT_REG_FILE,
        [S_IFDIR >> S_SHIFT]    = NILFS_FT_DIR,
        [S_IFCHR >> S_SHIFT]    = NILFS_FT_CHRDEV,
maintainer
#1 lore raw
Quoted 33 lines · Jeongjun Park

static void nilfs_set_de_type(struct nilfs_dir_entry *de, struct inode *inode)

{
        umode_t mode = inode->i_mode;

        de->file_type = nilfs_type_by_mode[(mode & S_IFMT)>>S_SHIFT]; // oob
}

The size of the nilfs_type_by_mode array in the fs/nilfs2/dir.c file is defined as "S_IFMT >> S_SHIFT", but the nilfs_set_de_type() function, which uses this array, specifies the index to read from the array in the same way as "(mode & S_IFMT) >> S_SHIFT".

However, when the index is determined this way, an out-of-bounds (OOB) error occurs by referring to an index that is 1 larger than the array size when the condition "mode & S_IFMT == S_IFMT" is satisfied. Therefore, a patch to resize the nilfs_type_by_mode array should be applied to prevent OOB errors.

Reported-and-tested-by [email protected]
Fixes 2ba466d74ed7 ("nilfs2: directory entry operations")
---
 fs/nilfs2/dir.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/nilfs2/dir.c +0 -0
index bc846b904b68..aee40db7a036 100644

fs/nilfs2/dir.c nilfs_filetype_table[NILFS_FT_MAX] = { · line 246

Quoted 8 lines
fs/nilfs2/dir.c +1 -1
@@ -240,7 +240,7 @@ nilfs_filetype_table[NILFS_FT_MAX] = {
 
 #define S_SHIFT 12
 static unsigned char
-nilfs_type_by_mode[S_IFMT >> S_SHIFT] = {
+nilfs_type_by_mode[(S_IFMT >> S_SHIFT) + 1] = {
        [S_IFREG >> S_SHIFT]    = NILFS_FT_REG_FILE,
        [S_IFDIR >> S_SHIFT]    = NILFS_FT_DIR,
        [S_IFCHR >> S_SHIFT]    = NILFS_FT_CHRDEV,

Thank you for your prompt response and posting the patch.

A little formatting and tag editing is required, but it's okay. I'll handle this patch.

Thanks,
Ryusuke Konishi