f2fs: fix out-of-bounds access in sysfs attribute read/write
PATCH RESEND 1/2 f2fs: fix out-of-bounds access in sysfs attribute read/write
From: Yongpeng Yang <[email protected]>
Some f2fs sysfs attributes suffer from out-of-bounds memory access and incorrect handling of integer values whose size is not 4 bytes.
For example:
vm:~# echo 65537 > /sys/fs/f2fs/vde/gc_pin_file_thresh vm:~# cat /sys/fs/f2fs/vde/gc_pin_file_thresh 65537 vm:~# echo 4294967297 > /sys/fs/f2fs/vde/atgc_age_threshold vm:~# cat /sys/fs/f2fs/vde/atgc_age_threshold 1
gc_pin_file_thresh maps to {struct f2fs_sb_info}->gc_pin_file_threshold, which is a 16-bit integer. However, the sysfs interface allows setting it to a value larger than SHRT_MAX, resulting in an out-of-range update.
atgc_age_threshold maps to {struct atgc_management}->age_threshold, which is a 64-bit integer, but its sysfs interface cannot correctly set values larger than UINT_MAX.
The root causes are:
1. __sbi_store() treats all default values as unsigned int, which prevents updating integers larger than 4 bytes and causes out-of-bounds writes for integers smaller than 4 bytes.
2. f2fs_sbi_show() also assumes all default values are unsigned int, leading to out-of-bounds reads and incorrect access to integers larger than 4 bytes.
This patch introduces {struct f2fs_attr}->size to record the actual size of the integer associated with each sysfs attribute. With this information, sysfs read and write operations can correctly access and update values according to their real data size, avoiding memory corruption and truncation.
--- fs/f2fs/sysfs.c | 60 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 8 deletions(-)
fs/f2fs/sysfs.c +52 -8
@@ -58,6 +58,7 @@ struct f2fs_attr { const char *buf, size_t len);int struct_type;int offset; +int size;int id; };
@@ -344,11 +345,30 @@ static ssize_t main_blkaddr_show(struct f2fs_attr *a, (unsigned long long)MAIN_BLKADDR(sbi)); } +static ssize_t __sbi_show_value (struct f2fs_attr *a, +struct f2fs_sb_info *sbi,char *buf, +unsigned char *value) +{ +switch (a->size) { +case 1 : +return sysfs_emit (buf,"%u\n" , *(u8 *)value); +case 2 : +return sysfs_emit (buf,"%u\n" , *(u16 *)value); +case 4 : +return sysfs_emit (buf,"%u\n" , *(u32 *)value); +case 8 : +return sysfs_emit (buf,"%llu\n" , *(u64 *)value); +default : +f2fs_bug_on (sbi,1 ); +return sysfs_emit (buf, +"show sysfs node value with wrong type\n" ); + } +} +static ssize_t f2fs_sbi_show (struct f2fs_attr *a,struct f2fs_sb_info *sbi,char *buf) {unsigned char *ptr =NULL ; -unsigned int *ui; ptr =__struct_ptr (sbi, a->struct_type);if (!ptr)
@@ -428,9 +448,30 @@ static ssize_t f2fs_sbi_show(struct f2fs_attr *a, atomic_read(&sbi->cp_call_count[BACKGROUND]));#endif - ui = (unsigned int *)(ptr + a->offset); +return __sbi_show_value (a, sbi, buf, ptr + a->offset); +} -return sysfs_emit (buf,"%u\n" , *ui); +static void __sbi_store_value (struct f2fs_attr *a, +struct f2fs_sb_info *sbi, +unsigned char *ui,unsigned long value) +{ +switch (a->size) { +case 1 : + *(u8 *)ui = value; +break ; +case 2 : + *(u16 *)ui = value; +break ; +case 4 : + *(u32 *)ui = value; +break ; +case 8 : + *(u64 *)ui = value; +break ; +default : +f2fs_bug_on (sbi,1 ); +f2fs_err (sbi,"store sysfs node value with wrong type" ); + } } static ssize_t __sbi_store(struct f2fs_attr *a,
@@ -906,7 +947,7 @@ static ssize_t __sbi_store(struct f2fs_attr *a,return count; } - *ui = (unsigned int )t; +__sbi_store_value (a, sbi, ptr + a->offset, t);return count; }
@@ -1053,24 +1094,27 @@ static struct f2fs_attr f2fs_attr_sb_##_name = { \ .id = F2FS_FEATURE_##_feat, \ } -#define F2FS_ATTR_OFFSET (_struct_type, _name, _mode, _show, _store, _offset) \ +#define F2FS_ATTR_OFFSET (_struct_type, _name, _mode, _show, _store, _offset, _size) \ static struct f2fs_attr f2fs_attr_##_name = { \ .attr = {.name = __stringify(_name), .mode = _mode }, \ .show = _show, \ .store = _store, \ .struct_type = _struct_type, \ - .offset = _offset \ + .offset = _offset, \ + .size = _size \ }#define F2FS_RO_ATTR (struct_type, struct_name, name, elname) \ F2FS_ATTR_OFFSET(struct_type, name, 0444, \ f2fs_sbi_show, NULL, \ - offsetof(struct struct_name, elname)) + offsetof(struct struct_name, elname), \ + sizeof_field(struct struct_name, elname))#define F2FS_RW_ATTR (struct_type, struct_name, name, elname) \ F2FS_ATTR_OFFSET(struct_type, name, 0644, \ f2fs_sbi_show, f2fs_sbi_store, \ - offsetof(struct struct_name, elname)) + offsetof(struct struct_name, elname), \ + sizeof_field(struct struct_name, elname))#define F2FS_GENERAL_RO_ATTR (name) \ static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
Signature · 8 lines
2.43.0 _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
PATCH RESEND 2/2 f2fs: change seq_file_ra_mul and max_io_bytes to unsigned int
From: Yongpeng Yang <[email protected]>
{struct file_ra_state}->ra_pages and {struct bio}->bi_iter.bi_size is defined as unsigned int, so values of seq_file_ra_mul and max_io_bytes exceeding UINT_MAX are meaningless.
--- fs/f2fs/f2fs.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/f2fs/f2fs.h +2 -2
@@ -1762,7 +1762,7 @@ struct f2fs_sb_info {unsigned int total_valid_node_count;/* valid node block count */ int dir_level;/* directory level */ bool readdir_ra;/* readahead inode in readdir */ -u64 max_io_bytes;/* max io bytes to merge IOs */ +unsigned int max_io_bytes;/* max io bytes to merge IOs */ block_t user_block_count;/* # of user blocks */ block_t total_valid_block_count;/* # of valid blocks */
@@ -1910,7 +1910,7 @@ struct f2fs_sb_info {unsigned int gc_segment_mode;/* GC state for reclaimed segments */ unsigned int gc_reclaimed_segs[MAX_GC_MODE ];/* Reclaimed segs for each mode */ -unsigned long seq_file_ra_mul;/* multiplier for ra_pages of seq. files in fadvise */ +unsigned int seq_file_ra_mul;/* multiplier for ra_pages of seq. files in fadvise */ int max_fragment_chunk;/* max chunk size for block fragmentation mode */ int max_fragment_hole;/* max hole size for block fragmentation mode */
Signature · 8 lines
2.43.0 _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Re: [f2fs-dev] [PATCH RESEND 2/2] f2fs: change seq_file_ra_mul and max_io_bytes to unsigned int
Quoted 7 lines · Yongpeng Yang
From: Yongpeng Yang <[email protected]>
{struct file_ra_state}->ra_pages and {struct bio}->bi_iter.bi_size is defined as unsigned int, so values of
seq_file_ra_mulandmax_io_bytesexceedingUINT_MAXare meaningless.Signed-off-by Yongpeng Yang <[email protected]>
Thanks,
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Quoted 8 lines · Yongpeng Yang
From: Yongpeng Yang <[email protected]>
Some f2fs sysfs attributes suffer from out-of-bounds memory access and incorrect handling of integer values whose size is not 4 bytes.
For example:
vm:~# echo 65537 > /sys/fs/f2fs/vde/gc_pin_file_thresh vm:~# cat /sys/fs/f2fs/vde/gc_pin_file_thresh
It seems the root cause here is we check w/ wrong sysfs filename.
I fixed this in a separated patch below:
https://lore.kernel.org/linux-f2fs-devel/[email protected]
Quoted 22 lines
65537
vm:~# echo 4294967297 > /sys/fs/f2fs/vde/atgc_age_threshold vm:~# cat /sys/fs/f2fs/vde/atgc_age_threshold 1
gc_pin_file_threshmaps to {struct f2fs_sb_info}->gc_pin_file_threshold, which is a 16-bit integer. However, the sysfs interface allows setting it to a value larger thanSHRT_MAX, resulting in an out-of-range update.
atgc_age_thresholdmaps to {struct atgc_management}->age_threshold, which is a 64-bit integer, but its sysfs interface cannot correctly set values larger thanUINT_MAX.The root causes are:
1.__sbi_store()treats all default values as unsigned int, which prevents updating integers larger than 4 bytes and causes out-of-bounds writes for integers smaller than 4 bytes.2.
f2fs_sbi_show()also assumes all default values are unsigned int, leading to out-of-bounds reads and incorrect access to integers larger than 4 bytes.
Quoted 6 lines
This patch introduces {struct f2fs_attr}->size to record the actual size of the integer associated with each sysfs attribute. With this information, sysfs read and write operations can correctly access and update values according to their real data size, avoiding memory corruption and truncation.
Need to add Cc and Fixes tag?
Quoted 114 lines
Signed-off-by Jinbao Liu <[email protected]>Signed-off-by Yongpeng Yang <[email protected]>--- fs/f2fs/sysfs.c | 60 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 8 deletions(-)fs/f2fs/sysfs.c +50
-7@@ -58,6 +58,7 @@ struct f2fs_attr { const char *buf, size_t len);int struct_type;int offset; +int size;int id; };@@ -344,11 +345,30 @@ static ssize_t main_blkaddr_show(struct f2fs_attr *a, (unsigned long long)MAIN_BLKADDR(sbi)); } +static ssize_t __sbi_show_value (struct f2fs_attr *a, +struct f2fs_sb_info *sbi,char *buf, +unsigned char *value) +{ +switch (a->size) { +case 1 : +return sysfs_emit (buf,"%u\n" , *(u8 *)value); +case 2 : +return sysfs_emit (buf,"%u\n" , *(u16 *)value); +case 4 : +return sysfs_emit (buf,"%u\n" , *(u32 *)value); +case 8 : +return sysfs_emit (buf,"%llu\n" , *(u64 *)value); +default : +f2fs_bug_on (sbi,1 ); +return sysfs_emit (buf, +"show sysfs node value with wrong type\n" ); + } +} +static ssize_t f2fs_sbi_show (struct f2fs_attr *a,struct f2fs_sb_info *sbi,char *buf) {unsigned char *ptr =NULL ; -unsigned int *ui; ptr =__struct_ptr (sbi, a->struct_type);if (!ptr)@@ -428,9 +448,30 @@ static ssize_t f2fs_sbi_show(struct f2fs_attr *a, atomic_read(&sbi->cp_call_count[BACKGROUND]));#endif - ui = (unsigned int *)(ptr + a->offset); +return __sbi_show_value (a, sbi, buf, ptr + a->offset); +} -return sysfs_emit (buf,"%u\n" , *ui); +static void __sbi_store_value (struct f2fs_attr *a, +struct f2fs_sb_info *sbi, +unsigned char *ui,unsigned long value) +{ +switch (a->size) { +case 1 : + *(u8 *)ui = value; +break ; +case 2 : + *(u16 *)ui = value; +break ; +case 4 : + *(u32 *)ui = value; +break ; +case 8 : + *(u64 *)ui = value; +break ; +default : +f2fs_bug_on (sbi,1 ); +f2fs_err (sbi,"store sysfs node value with wrong type" ); + } } static ssize_t __sbi_store(struct f2fs_attr *a,@@ -906,7 +947,7 @@ static ssize_t __sbi_store(struct f2fs_attr *a,return count; } - *ui = (unsigned int )t; +__sbi_store_value (a, sbi, ptr + a->offset, t);return count; }@@ -1053,24 +1094,27 @@ static struct f2fs_attr f2fs_attr_sb_##_name = { \ .id = F2FS_FEATURE_##_feat, \ } -#define F2FS_ATTR_OFFSET (_struct_type, _name, _mode, _show, _store, _offset) \ +#define F2FS_ATTR_OFFSET (_struct_type, _name, _mode, _show, _store, _offset, _size) \ static struct f2fs_attr f2fs_attr_##_name = { \ .attr = {.name = __stringify(_name), .mode = _mode }, \ .show = _show, \ .store = _store, \ .struct_type = _struct_type, \ - .offset = _offset \ + .offset = _offset, \ + .size = _size \ }#define F2FS_RO_ATTR (struct_type, struct_name, name, elname) \ F2FS_ATTR_OFFSET(struct_type, name, 0444, \ f2fs_sbi_show, NULL, \ - offsetof(struct struct_name, elname)) + offsetof(struct struct_name, elname), \ + sizeof_field(struct struct_name, elname))
fs/f2fs/sysfs.c static struct f2fs_attr f2fs_attr_sb_##_name = { \ · line 1120
Quoted 10 lines
fs/f2fs/sysfs.c +2
-1#define F2FS_RW_ATTR (struct_type, struct_name, name, elname) \ F2FS_ATTR_OFFSET(struct_type, name, 0644, \ f2fs_sbi_show, f2fs_sbi_store, \ - offsetof(struct struct_name, elname)) + offsetof(struct struct_name, elname), \ + sizeof_field(struct struct_name, elname))#define F2FS_GENERAL_RO_ATTR (name) \ static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Quoted 11 lines · On 1/6/26 16:53, Chao Yu via Linux-f2fs-devel wrote:
Quoted 8 lines · Yongpeng Yang
From: Yongpeng Yang <[email protected]>
Some f2fs sysfs attributes suffer from out-of-bounds memory access and incorrect handling of integer values whose size is not 4 bytes.
For example:
vm:~# echo 65537 > /sys/fs/f2fs/vde/gc_pin_file_thresh vm:~# cat /sys/fs/f2fs/vde/gc_pin_file_threshIt seems the root cause here is we check w/ wrong sysfs filename.
Yes, my mistake. I'll modify the commit message in v2 patch.
Quoted 5 lines
I fixed this in a separated patch below:
https://lore.kernel.org/linux-f2fs-devel/20260106063117.29975-1-
[email protected]
This patch looks good to me.
Quoted 23 lines
Quoted 23 lines
65537
vm:~# echo 4294967297 > /sys/fs/f2fs/vde/atgc_age_threshold vm:~# cat /sys/fs/f2fs/vde/atgc_age_threshold 1
gc_pin_file_threshmaps to {struct f2fs_sb_info}->gc_pin_file_threshold, which is a 16-bit integer. However, the sysfs interface allows setting it to a value larger thanSHRT_MAX, resulting in an out-of-range update.
atgc_age_thresholdmaps to {struct atgc_management}->age_threshold, which is a 64-bit integer, but its sysfs interface cannot correctly set values larger thanUINT_MAX.The root causes are:
1.__sbi_store()treats all default values as unsigned int, which prevents updating integers larger than 4 bytes and causes out-of-bounds writes for integers smaller than 4 bytes.2.
f2fs_sbi_show()also assumes all default values are unsigned int, leading to out-of-bounds reads and incorrect access to integers larger than 4 bytes.
Quoted 8 lines
Quoted 6 lines
This patch introduces {struct f2fs_attr}->size to record the actual size of the integer associated with each sysfs attribute. With this information, sysfs read and write operations can correctly access and update values according to their real data size, avoiding memory corruption and truncation.
Need to add Cc and Fixes tag?
OK, I will add Fixes tag in v2 patch.
Thanks
Yongpeng,
Quoted 110 lines
Quoted 110 lines
Signed-off-by Jinbao Liu <[email protected]>Signed-off-by Yongpeng Yang <[email protected]>---
fs/f2fs/sysfs.c| 60 ++++++++++++++++++++++++++++++++++++++++++-------1 file changed, 52 insertions(+), 8 deletions(-)fs/f2fs/sysfs.c +1
-0@@ -58,6 +58,7 @@ struct f2fs_attr { const char *buf, size_t len); int struct_type; int offset; + int size; int id; };@@ -344,11 +345,30 @@ static
ssize_tmain_blkaddr_show(structf2fs_attr*a,
(unsigned long long)MAIN_BLKADDR(sbi)); }
+staticssize_t__sbi_show_value(structf2fs_attr*a, + structf2fs_sb_info*sbi, char *buf,
+ unsigned char *value)fs/f2fs/sysfs.c +17
-0+{ + switch (a->size) { + case 1: + return sysfs_emit(buf, "%u\n", *(u8 *)value); + case 2: + return sysfs_emit(buf, "%u\n", *(u16 *)value); + case 4: + return sysfs_emit(buf, "%u\n", *(u32 *)value); + case 8: + return sysfs_emit(buf, "%llu\n", *(u64 *)value); + default: + f2fs_bug_on(sbi, 1); + return sysfs_emit(buf, + "show sysfs node value with wrong type\n"); + } +} +static
ssize_tf2fs_sbi_show(struct f2fs_attr *a, struct f2fs_sb_info *sbi, char *buf){
unsigned char *ptr = NULL;
- unsigned int *ui;
ptr =__struct_ptr(sbi, a->struct_type); if (!ptr)fs/f2fs/sysfs.c +29
-2@@ -428,9 +448,30 @@ static ssize_t f2fs_sbi_show(struct f2fs_attr *a, atomic_read(&sbi->cp_call_count[BACKGROUND])); #endif - ui = (unsigned int *)(ptr + a->offset); + return __sbi_show_value(a, sbi, buf, ptr + a->offset); +} - return sysfs_emit(buf, "%u\n", *ui); +static void __sbi_store_value (struct f2fs_attr *a, + struct f2fs_sb_info *sbi, + unsigned char *ui, unsigned long value) +{ + switch (a->size) { + case 1: + *(u8 *)ui = value; + break; + case 2: + *(u16 *)ui = value; + break; + case 4: + *(u32 *)ui = value; + break; + case 8: + *(u64 *)ui = value; + break; + default: + f2fs_bug_on(sbi, 1); + f2fs_err(sbi, "store sysfs node value with wrong type"); + } } static ssize_t __sbi_store(struct f2fs_attr *a,@@ -906,7 +947,7 @@ static ssize_t __sbi_store(struct f2fs_attr *a, return count; } - *ui = (unsigned int)t; + __sbi_store_value(a, sbi, ptr + a->offset, t); return count; }@@ -1053,24 +1094,27 @@ static struct f2fs_attr f2fs_attr_sb_##_name = { \ .id = F2FS_FEATURE_##_feat, \ } -#define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \ +#define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset, _size) \ static struct f2fs_attr f2fs_attr_##_name = { \ .attr = {.name = __stringify(_name), .mode = _mode }, \ .show = _show, \ .store = _store, \ .struct_type = _struct_type, \ - .offset = _offset \ + .offset = _offset, \ + .size = _size \ } #define F2FS_RO_ATTR(struct_type, struct_name, name, elname) \ F2FS_ATTR_OFFSET(struct_type, name, 0444, \ f2fs_sbi_show, NULL, \ - offsetof(struct struct_name, elname)) + offsetof(struct struct_name, elname), \ + sizeof_field(struct struct_name, elname)) #define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
Quoted 8 lines
Quoted 8 lines
fs/f2fs/sysfs.c +2
-1F2FS_ATTR_OFFSET(struct_type, name, 0644, \ f2fs_sbi_show, f2fs_sbi_store, \ - offsetof(struct struct_name, elname)) + offsetof(struct struct_name, elname), \ + sizeof_field(struct struct_name, elname)) #define F2FS_GENERAL_RO_ATTR(name) \ static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444,name##_show, NULL)
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Hello:
This series was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <[email protected]>:
Quoted 6 lines · On Wed, 31 Dec 2025 21:54:33 +0800 you wrote:
From: Yongpeng Yang <[email protected]>
Some f2fs sysfs attributes suffer from out-of-bounds memory access and incorrect handling of integer values whose size is not 4 bytes.
For example:
Quoted 8 lines
vm:~# echo 65537 > /sys/fs/f2fs/vde/gc_pin_file_thresh vm:~# cat /sys/fs/f2fs/vde/gc_pin_file_thresh 65537 vm:~# echo 4294967297 > /sys/fs/f2fs/vde/atgc_age_threshold vm:~# cat /sys/fs/f2fs/vde/atgc_age_threshold 1[...]
Here is the summary with links:
- [f2fs-dev,RESEND,1/2] f2fs: fix out-of-bounds access in sysfs attribute read/write
https://git.kernel.org/jaegeuk/f2fs/c/98ea0039dbfd - [f2fs-dev,RESEND,2/2] f2fs: change seq_file_ra_mul and max_io_bytes to unsigned int https://git.kernel.org/jaegeuk/f2fs/c/071e50d61cf2
You are awesome, thank you!
Signature · 10 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel