f2fs: fix potential .flags overflow on 32bit architecture
Chao Yu
maintainerPATCH 1/1 f2fs: fix potential .flags overflow on 32bit architecture
From: Chao Yu <[email protected]>
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.
To fix this issue, let's introduce .extra_flags to grab extra space to store those new flags.
Call Trace: __schedule+0x8d0/0x13fc ? mark_held_locks+0xac/0x100 schedule+0xcc/0x260 rwsem_down_write_slowpath+0x3ab/0x65d down_write+0xc7/0xe0 f2fs_drop_nlink+0x3d/0x600 [f2fs] f2fs_delete_inline_entry+0x300/0x440 [f2fs] f2fs_delete_entry+0x3a1/0x7f0 [f2fs] f2fs_unlink+0x500/0x790 [f2fs] vfs_unlink+0x211/0x490 do_unlinkat+0x483/0x520 sys_unlink+0x4a/0x70 do_fast_syscall_32+0x12b/0x683 entry_SYSENTER_32+0xaa/0x102
--- fs/f2fs/f2fs.h | 26 ++++++++++++++++++++------ fs/f2fs/inode.c | 1 + 2 files changed, 21 insertions(+), 6 deletions(-)
fs/f2fs/f2fs.h +20 -6
@@ -695,6 +695,7 @@ struct f2fs_inode_info {/* Use below internally in f2fs*/ unsigned long flags;/* use to pass per-file flags */ +unsigned long extra_flags;/* extra flags */ struct rw_semaphore i_sem;/* protect fi info */ atomic_t dirty_pages;/* # of dirty pages */ f2fs_hash_t chash;/* hash value of given file name */
@@ -2569,7 +2570,7 @@ enum { };static inline void __mark_inode_dirty_flag (struct inode *inode, -int flag,bool set) +unsigned long long flag,bool set) {switch (flag) {case FI_INLINE_XATTR :
@@ -2588,20 +2589,33 @@ static inline void __mark_inode_dirty_flag(struct inode *inode,static inline void set_inode_flag (struct inode *inode,int flag) { -if (!test_bit (flag, &F2FS_I (inode)->flags)) -set_bit (flag, &F2FS_I (inode)->flags); +if ((1 << flag) <=sizeof (unsigned long )) { +if (!test_bit (flag, &F2FS_I (inode)->flags)) +set_bit (flag, &F2FS_I (inode)->flags); + }else { +if (!test_bit (flag -32 , &F2FS_I (inode)->extra_flags)) +set_bit (flag -32 , &F2FS_I (inode)->extra_flags); + }__mark_inode_dirty_flag (inode, flag, true); }static inline int is_inode_flag_set (struct inode *inode,int flag) { -return test_bit (flag, &F2FS_I (inode)->flags); +if ((1 << flag) <=sizeof (unsigned long )) +return test_bit (flag, &F2FS_I (inode)->flags); +else +return test_bit (flag -32 , &F2FS_I (inode)->extra_flags); }static inline void clear_inode_flag (struct inode *inode,int flag) { -if (test_bit (flag, &F2FS_I (inode)->flags)) -clear_bit (flag, &F2FS_I (inode)->flags); +if ((1 << flag) <=sizeof (unsigned long )) { +if (test_bit (flag, &F2FS_I (inode)->flags)) +clear_bit (flag, &F2FS_I (inode)->flags); + }else { +if (test_bit (flag -32 , &F2FS_I (inode)->extra_flags)) +clear_bit (flag -32 , &F2FS_I (inode)->extra_flags); + }__mark_inode_dirty_flag (inode, flag, false); }
fs/f2fs/inode.c +1 -0
@@ -363,6 +363,7 @@ static int do_read_inode(struct inode *inode)if (S_ISREG (inode->i_mode)) fi->i_flags &= ~F2FS_PROJINHERIT_FL ; fi->flags =0 ; + fi->extra_flags =0 ; fi->i_advise = ri->i_advise; fi->i_pino =le32_to_cpu (ri->i_pino); fi->i_dir_level = ri->i_dir_level;
Signature · 1 line
2.22.0
Hello,
Quoted 56 lines · Chao Yu
From: Chao Yu <[email protected]>
f2fs_inode_info.flags is unsigned long variable, it has 32 bits in 32bit architecture, since we introduced
FI_MMAP_FILEflag when we support data compression, we may access memory cross the border of .flags field, corrupting .i_sem field, result in below deadlock.To fix this issue, let's introduce .extra_flags to grab extra space to store those new flags.
Call Trace: __schedule+0x8d0/0x13fc ? mark_held_locks+0xac/0x100 schedule+0xcc/0x260 rwsem_down_write_slowpath+0x3ab/0x65d down_write+0xc7/0xe0 f2fs_drop_nlink+0x3d/0x600 [f2fs] f2fs_delete_inline_entry+0x300/0x440 [f2fs] f2fs_delete_entry+0x3a1/0x7f0 [f2fs] f2fs_unlink+0x500/0x790 [f2fs] vfs_unlink+0x211/0x490 do_unlinkat+0x483/0x520 sys_unlink+0x4a/0x70 do_fast_syscall_32+0x12b/0x683 entry_SYSENTER_32+0xaa/0x102Fixes 4c8ff7095bef ("f2fs: support data compression")Signed-off-by Chao Yu <[email protected]>--- fs/f2fs/f2fs.h | 26 ++++++++++++++++++++------ fs/f2fs/inode.c | 1 + 2 files changed, 21 insertions(+), 6 deletions(-)fs/f2fs/f2fs.h +2
-1@@ -695,6 +695,7 @@ struct f2fs_inode_info {/* Use below internally in f2fs*/ unsigned long flags;/* use to pass per-file flags */ +unsigned long extra_flags;/* extra flags */ struct rw_semaphore i_sem;/* protect fi info */ atomic_t dirty_pages;/* # of dirty pages */ f2fs_hash_t chash;/* hash value of given file name */ @@ -2569,7 +2570,7 @@ enum { };static inline void __mark_inode_dirty_flag (struct inode *inode, -int flag,bool set) +unsigned long long flag,bool set) {switch (flag) {case FI_INLINE_XATTR :
fs/f2fs/f2fs.h static inline void __mark_inode_dirty_flag(struct inode *inode, · line 2592
Quoted 6 lines
fs/f2fs/f2fs.h +1
-2@@ -2588,20 +2589,33 @@ static inline void __mark_inode_dirty_flag(struct inode *inode,static inline void set_inode_flag (struct inode *inode,int flag) { -if (!test_bit (flag, &F2FS_I (inode)->flags)) -set_bit (flag, &F2FS_I (inode)->flags); +if ((1 << flag) <=sizeof (unsigned long )) {
^ this is wrong. Maybe you meant flag <= BITS_PER_LONG
And ditto for the same checks below. Maybe you can make flags an array of BIT_WORD(max_flag_value) + 1 and skip the branches altogether?
thank you and regards,
o.
Quoted 47 lines
fs/f2fs/f2fs.h +17
-3+if (!test_bit (flag, &F2FS_I (inode)->flags)) +set_bit (flag, &F2FS_I (inode)->flags); + }else { +if (!test_bit (flag -32 , &F2FS_I (inode)->extra_flags)) +set_bit (flag -32 , &F2FS_I (inode)->extra_flags); + }__mark_inode_dirty_flag (inode, flag, true); }static inline int is_inode_flag_set (struct inode *inode,int flag) { -return test_bit (flag, &F2FS_I (inode)->flags); +if ((1 << flag) <=sizeof (unsigned long )) +return test_bit (flag, &F2FS_I (inode)->flags); +else +return test_bit (flag -32 , &F2FS_I (inode)->extra_flags); }static inline void clear_inode_flag (struct inode *inode,int flag) { -if (test_bit (flag, &F2FS_I (inode)->flags)) -clear_bit (flag, &F2FS_I (inode)->flags); +if ((1 << flag) <=sizeof (unsigned long )) { +if (test_bit (flag, &F2FS_I (inode)->flags)) +clear_bit (flag, &F2FS_I (inode)->flags); + }else { +if (test_bit (flag -32 , &F2FS_I (inode)->extra_flags)) +clear_bit (flag -32 , &F2FS_I (inode)->extra_flags); + }__mark_inode_dirty_flag (inode, flag, false); }fs/f2fs/inode.c +1
-0@@ -363,6 +363,7 @@ static int do_read_inode(struct inode *inode)if (S_ISREG (inode->i_mode)) fi->i_flags &= ~F2FS_PROJINHERIT_FL ; fi->flags =0 ; + fi->extra_flags =0 ; fi->i_advise = ri->i_advise; fi->i_pino =le32_to_cpu (ri->i_pino); fi->i_dir_level = ri->i_dir_level;Signature · 1 line
2.22.0
Chao Yu
maintainerHi,
Quoted 60 lines · Ondřej Jirman
Hello,
Quoted 57 lines · Chao Yu
From: Chao Yu <[email protected]>
f2fs_inode_info.flags is unsigned long variable, it has 32 bits in 32bit architecture, since we introduced
FI_MMAP_FILEflag when we support data compression, we may access memory cross the border of .flags field, corrupting .i_sem field, result in below deadlock.To fix this issue, let's introduce .extra_flags to grab extra space to store those new flags.
Call Trace: __schedule+0x8d0/0x13fc ? mark_held_locks+0xac/0x100 schedule+0xcc/0x260 rwsem_down_write_slowpath+0x3ab/0x65d down_write+0xc7/0xe0 f2fs_drop_nlink+0x3d/0x600 [f2fs] f2fs_delete_inline_entry+0x300/0x440 [f2fs] f2fs_delete_entry+0x3a1/0x7f0 [f2fs] f2fs_unlink+0x500/0x790 [f2fs] vfs_unlink+0x211/0x490 do_unlinkat+0x483/0x520 sys_unlink+0x4a/0x70 do_fast_syscall_32+0x12b/0x683 entry_SYSENTER_32+0xaa/0x102Fixes 4c8ff7095bef ("f2fs: support data compression")Signed-off-by Chao Yu <[email protected]>--- fs/f2fs/f2fs.h | 26 ++++++++++++++++++++------ fs/f2fs/inode.c | 1 + 2 files changed, 21 insertions(+), 6 deletions(-)fs/f2fs/f2fs.h +2
-1@@ -695,6 +695,7 @@ struct f2fs_inode_info {/* Use below internally in f2fs*/ unsigned long flags;/* use to pass per-file flags */ +unsigned long extra_flags;/* extra flags */ struct rw_semaphore i_sem;/* protect fi info */ atomic_t dirty_pages;/* # of dirty pages */ f2fs_hash_t chash;/* hash value of given file name */ @@ -2569,7 +2570,7 @@ enum { };static inline void __mark_inode_dirty_flag (struct inode *inode, -int flag,bool set) +unsigned long long flag,bool set) {switch (flag) {case FI_INLINE_XATTR :@@ -2588,20 +2589,33 @@ static inline void __mark_inode_dirty_flag(struct inode *inode,
Quoted 7 lines
fs/f2fs/f2fs.h static inline void __mark_inode_dirty_flag(struct inode *inode, · line 2592
Quoted 5 lines
fs/f2fs/f2fs.h +1
-2static inline void set_inode_flag (struct inode *inode,int flag) { -if (!test_bit (flag, &F2FS_I (inode)->flags)) -set_bit (flag, &F2FS_I (inode)->flags); +if ((1 << flag) <=sizeof (unsigned long )) {^ this is wrong. Maybe you meant flag <=
BITS_PER_LONG
Oh, my bad, I meant that, thanks for pointing out this. :)
Quoted 3 lines
And ditto for the same checks below. Maybe you can make flags an array of
BIT_WORD(max_flag_value)+ 1 and skip the branches altogether?
That will be better, let me revise this patch.
Thanks,
Quoted 51 lines
thank you and regards,
o.Quoted 47 lines
fs/f2fs/f2fs.h +17
-3+if (!test_bit (flag, &F2FS_I (inode)->flags)) +set_bit (flag, &F2FS_I (inode)->flags); + }else { +if (!test_bit (flag -32 , &F2FS_I (inode)->extra_flags)) +set_bit (flag -32 , &F2FS_I (inode)->extra_flags); + }__mark_inode_dirty_flag (inode, flag, true); }static inline int is_inode_flag_set (struct inode *inode,int flag) { -return test_bit (flag, &F2FS_I (inode)->flags); +if ((1 << flag) <=sizeof (unsigned long )) +return test_bit (flag, &F2FS_I (inode)->flags); +else +return test_bit (flag -32 , &F2FS_I (inode)->extra_flags); }static inline void clear_inode_flag (struct inode *inode,int flag) { -if (test_bit (flag, &F2FS_I (inode)->flags)) -clear_bit (flag, &F2FS_I (inode)->flags); +if ((1 << flag) <=sizeof (unsigned long )) { +if (test_bit (flag, &F2FS_I (inode)->flags)) +clear_bit (flag, &F2FS_I (inode)->flags); + }else { +if (test_bit (flag -32 , &F2FS_I (inode)->extra_flags)) +clear_bit (flag -32 , &F2FS_I (inode)->extra_flags); + }__mark_inode_dirty_flag (inode, flag, false); }fs/f2fs/inode.c +1
-1@@ -363,6 +363,7 @@ static int do_read_inode(struct inode *inode)if (S_ISREG (inode->i_mode)) fi->i_flags &= ~F2FS_PROJINHERIT_FL ; fi->flags =0 ; + fi->extra_flags =0 ; fi->i_advise = ri->i_advise; fi->i_pino =le32_to_cpu (ri->i_pino); fi->i_dir_level = ri->i_dir_level; --2.22.0
Quoted 71 lines · Chao Yu
Hi,
Quoted 60 lines · Ondřej Jirman
Hello,
Quoted 57 lines · Chao Yu
From: Chao Yu <[email protected]>
f2fs_inode_info.flags is unsigned long variable, it has 32 bits in 32bit architecture, since we introduced
FI_MMAP_FILEflag when we support data compression, we may access memory cross the border of .flags field, corrupting .i_sem field, result in below deadlock.To fix this issue, let's introduce .extra_flags to grab extra space to store those new flags.
Call Trace: __schedule+0x8d0/0x13fc ? mark_held_locks+0xac/0x100 schedule+0xcc/0x260 rwsem_down_write_slowpath+0x3ab/0x65d down_write+0xc7/0xe0 f2fs_drop_nlink+0x3d/0x600 [f2fs] f2fs_delete_inline_entry+0x300/0x440 [f2fs] f2fs_delete_entry+0x3a1/0x7f0 [f2fs] f2fs_unlink+0x500/0x790 [f2fs] vfs_unlink+0x211/0x490 do_unlinkat+0x483/0x520 sys_unlink+0x4a/0x70 do_fast_syscall_32+0x12b/0x683 entry_SYSENTER_32+0xaa/0x102Fixes 4c8ff7095bef ("f2fs: support data compression")Signed-off-by Chao Yu <[email protected]>--- fs/f2fs/f2fs.h | 26 ++++++++++++++++++++------ fs/f2fs/inode.c | 1 + 2 files changed, 21 insertions(+), 6 deletions(-)fs/f2fs/f2fs.h +2
-1@@ -695,6 +695,7 @@ struct f2fs_inode_info {/* Use below internally in f2fs*/ unsigned long flags;/* use to pass per-file flags */ +unsigned long extra_flags;/* extra flags */ struct rw_semaphore i_sem;/* protect fi info */ atomic_t dirty_pages;/* # of dirty pages */ f2fs_hash_t chash;/* hash value of given file name */ @@ -2569,7 +2570,7 @@ enum { };static inline void __mark_inode_dirty_flag (struct inode *inode, -int flag,bool set) +unsigned long long flag,bool set) {switch (flag) {case FI_INLINE_XATTR :@@ -2588,20 +2589,33 @@ static inline void __mark_inode_dirty_flag(struct inode *inode,Quoted 7 lines
fs/f2fs/f2fs.h static inline void __mark_inode_dirty_flag(struct inode *inode, · line 2592
Quoted 5 lines
fs/f2fs/f2fs.h +1
-2static inline void set_inode_flag (struct inode *inode,int flag) { -if (!test_bit (flag, &F2FS_I (inode)->flags)) -set_bit (flag, &F2FS_I (inode)->flags); +if ((1 << flag) <=sizeof (unsigned long )) {^ this is wrong. Maybe you meant flag <=
BITS_PER_LONG
Quoted 5 lines
Oh, my bad, I meant that, thanks for pointing out this. :)
Quoted 3 lines
And ditto for the same checks below. Maybe you can make flags an array of
BIT_WORD(max_flag_value)+ 1 and skip the branches altogether?
I've found maybe a clearer macro for this:
https://elixir.bootlin.com/linux/latest/source/include/linux/bitops.h#L15
BITS_TO_LONGS(nr)
But it takes the number of bits in the bitmap, which would be "max_flag_value + 1".
regards,
o.
Quoted 55 lines
That will be better, let me revise this patch.
Thanks,
Quoted 51 lines
thank you and regards,
o.Quoted 47 lines
fs/f2fs/f2fs.h +17
-3+if (!test_bit (flag, &F2FS_I (inode)->flags)) +set_bit (flag, &F2FS_I (inode)->flags); + }else { +if (!test_bit (flag -32 , &F2FS_I (inode)->extra_flags)) +set_bit (flag -32 , &F2FS_I (inode)->extra_flags); + }__mark_inode_dirty_flag (inode, flag, true); }static inline int is_inode_flag_set (struct inode *inode,int flag) { -return test_bit (flag, &F2FS_I (inode)->flags); +if ((1 << flag) <=sizeof (unsigned long )) +return test_bit (flag, &F2FS_I (inode)->flags); +else +return test_bit (flag -32 , &F2FS_I (inode)->extra_flags); }static inline void clear_inode_flag (struct inode *inode,int flag) { -if (test_bit (flag, &F2FS_I (inode)->flags)) -clear_bit (flag, &F2FS_I (inode)->flags); +if ((1 << flag) <=sizeof (unsigned long )) { +if (test_bit (flag, &F2FS_I (inode)->flags)) +clear_bit (flag, &F2FS_I (inode)->flags); + }else { +if (test_bit (flag -32 , &F2FS_I (inode)->extra_flags)) +clear_bit (flag -32 , &F2FS_I (inode)->extra_flags); + }__mark_inode_dirty_flag (inode, flag, false); }fs/f2fs/inode.c +1
-1@@ -363,6 +363,7 @@ static int do_read_inode(struct inode *inode)if (S_ISREG (inode->i_mode)) fi->i_flags &= ~F2FS_PROJINHERIT_FL ; fi->flags =0 ; + fi->extra_flags =0 ; fi->i_advise = ri->i_advise; fi->i_pino =le32_to_cpu (ri->i_pino); fi->i_dir_level = ri->i_dir_level; --2.22.0
Quoted 75 lines · Ondřej Jirman
Quoted 74 lines · Chao Yu
Hi,
Quoted 60 lines · Ondřej Jirman
Hello,
Quoted 57 lines · Chao Yu
From: Chao Yu <[email protected]>
f2fs_inode_info.flags is unsigned long variable, it has 32 bits in 32bit architecture, since we introduced
FI_MMAP_FILEflag when we support data compression, we may access memory cross the border of .flags field, corrupting .i_sem field, result in below deadlock.To fix this issue, let's introduce .extra_flags to grab extra space to store those new flags.
Call Trace: __schedule+0x8d0/0x13fc ? mark_held_locks+0xac/0x100 schedule+0xcc/0x260 rwsem_down_write_slowpath+0x3ab/0x65d down_write+0xc7/0xe0 f2fs_drop_nlink+0x3d/0x600 [f2fs] f2fs_delete_inline_entry+0x300/0x440 [f2fs] f2fs_delete_entry+0x3a1/0x7f0 [f2fs] f2fs_unlink+0x500/0x790 [f2fs] vfs_unlink+0x211/0x490 do_unlinkat+0x483/0x520 sys_unlink+0x4a/0x70 do_fast_syscall_32+0x12b/0x683 entry_SYSENTER_32+0xaa/0x102Fixes 4c8ff7095bef ("f2fs: support data compression")Signed-off-by Chao Yu <[email protected]>--- fs/f2fs/f2fs.h | 26 ++++++++++++++++++++------ fs/f2fs/inode.c | 1 + 2 files changed, 21 insertions(+), 6 deletions(-)fs/f2fs/f2fs.h +2
-1@@ -695,6 +695,7 @@ struct f2fs_inode_info {/* Use below internally in f2fs*/ unsigned long flags;/* use to pass per-file flags */ +unsigned long extra_flags;/* extra flags */ struct rw_semaphore i_sem;/* protect fi info */ atomic_t dirty_pages;/* # of dirty pages */ f2fs_hash_t chash;/* hash value of given file name */ @@ -2569,7 +2570,7 @@ enum { };static inline void __mark_inode_dirty_flag (struct inode *inode, -int flag,bool set) +unsigned long long flag,bool set) {switch (flag) {case FI_INLINE_XATTR :@@ -2588,20 +2589,33 @@ static inline void __mark_inode_dirty_flag(struct inode *inode,Quoted 7 lines
fs/f2fs/f2fs.h static inline void __mark_inode_dirty_flag(struct inode *inode, · line 2592
Quoted 5 lines
fs/f2fs/f2fs.h +1
-2static inline void set_inode_flag (struct inode *inode,int flag) { -if (!test_bit (flag, &F2FS_I (inode)->flags)) -set_bit (flag, &F2FS_I (inode)->flags); +if ((1 << flag) <=sizeof (unsigned long )) {^ this is wrong. Maybe you meant flag <=
BITS_PER_LONGOh, my bad, I meant that, thanks for pointing out this. :)
Quoted 1 line
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
And ditto for the same checks below. Maybe you can make flags an array of
BIT_WORD(max_flag_value)+ 1 and skip the branches altogether?I've found maybe a clearer macro for this:
https://elixir.bootlin.com/linux/latest/source/include/linux/bitops.h#L15
BITS_TO_LONGS(nr)
More clean, thanks, will send v3 soon.
Thanks,
Quoted 64 lines
But it takes the number of bits in the bitmap, which would be "
max_flag_value+ 1".regards,
o.Quoted 40 lines
That will be better, let me revise this patch.
Thanks,
Quoted 36 lines
thank you and regards,
o.Quoted 32 lines
fs/f2fs/f2fs.h +17
-3+if (!test_bit (flag, &F2FS_I (inode)->flags)) +set_bit (flag, &F2FS_I (inode)->flags); + }else { +if (!test_bit (flag -32 , &F2FS_I (inode)->extra_flags)) +set_bit (flag -32 , &F2FS_I (inode)->extra_flags); + }__mark_inode_dirty_flag (inode, flag, true); }static inline int is_inode_flag_set (struct inode *inode,int flag) { -return test_bit (flag, &F2FS_I (inode)->flags); +if ((1 << flag) <=sizeof (unsigned long )) +return test_bit (flag, &F2FS_I (inode)->flags); +else +return test_bit (flag -32 , &F2FS_I (inode)->extra_flags); }static inline void clear_inode_flag (struct inode *inode,int flag) { -if (test_bit (flag, &F2FS_I (inode)->flags)) -clear_bit (flag, &F2FS_I (inode)->flags); +if ((1 << flag) <=sizeof (unsigned long )) { +if (test_bit (flag, &F2FS_I (inode)->flags)) +clear_bit (flag, &F2FS_I (inode)->flags); + }else { +if (test_bit (flag -32 , &F2FS_I (inode)->extra_flags)) +clear_bit (flag -32 , &F2FS_I (inode)->extra_flags); + }__mark_inode_dirty_flag (inode, flag, false); }Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
fs/f2fs/inode.c +0
-0@@ -363,6 +363,7 @@ static int do_read_inode(struct inode *inode)if (S_ISREG (inode->i_mode)) fi->i_flags &= ~F2FS_PROJINHERIT_FL ;Quoted 8 lines
Quoted 8 lines
fs/f2fs/inode.c +1
-1fi->flags =0 ; + fi->extra_flags =0 ; fi->i_advise = ri->i_advise; fi->i_pino =le32_to_cpu (ri->i_pino); fi->i_dir_level = ri->i_dir_level; --2.22.0
.