Re: [PATCH v4] f2fs: fix potential .flags overflow on 32bit architecture

From: Joe Perches
Date: Sun Mar 22 2020 - 22:59:01 EST


On Mon, 2020-03-23 at 10:41 +0800, Chao Yu wrote:
> f2fs_inode_info.flags is unsigned long variable, it has 32 bits
> in 32bit architecture, since we introduced FI_MMAP_FILE flag
> when we support data compression, we may access memory cross
> the border of .flags field, corrupting .i_sem field, result in
> below deadlock.
[]
> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
[]
> @@ -682,6 +682,47 @@ enum {
[]
> +/* used for f2fs_inode_info->flags */
> +enum {
[]
> + FI_MAX, /* max flag, never be used */
> +};
> +
> +/* f2fs_inode_info.flags array size */
> +#define FI_ARRAY_SIZE (BITS_TO_LONGS(FI_MAX))

Perhaps FI_ARRAY_SIZE isn't necessary.

> +
> struct f2fs_inode_info {
> struct inode vfs_inode; /* serve a vfs inode */
> unsigned long i_flags; /* keep an inode flags for ioctl */
> @@ -694,7 +735,7 @@ struct f2fs_inode_info {
> umode_t i_acl_mode; /* keep file acl mode temporarily */
>
> /* Use below internally in f2fs*/
> - unsigned long flags; /* use to pass per-file flags */
> + unsigned long flags[FI_ARRAY_SIZE]; /* use to pass per-file flags */

and BITS_TO_LONGS should be used here.

> diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c
[]
> @@ -362,7 +363,8 @@ static int do_read_inode(struct inode *inode)
> fi->i_flags = le32_to_cpu(ri->i_flags);
> if (S_ISREG(inode->i_mode))
> fi->i_flags &= ~F2FS_PROJINHERIT_FL;
> - fi->flags = 0;
> + for (i = 0; i < FI_ARRAY_SIZE; i++)
> + fi->flags[i] = 0;

And this could become

bitmap_zero(fi->flags, BITS_TO_LONG(FI_MAX));

Is FI_ARRAY_SIZE used anywhere else?