f2fs: extend stat_lock to avoid potential race in statfs
PATCH 1/1 f2fs: extend stat_lock to avoid potential race in statfs
The calculation for f_bfree performs a calculation involvingcurrent_reserved_blocks and total_valid_user_blocks. Both can be
modified under stat_lock. As stat_lock is not used to read both these
values in statfs, this can lead to inconsistent results. Extend the
locking to prevent this issue.
Commit c9c8ed50d94c ("f2fs: fix to avoid potential race on sbi->unusable_block_count access/update") already added the use of sbi->stat_lock in statfs in
order to make the calculation of multiple, different fields atomic so
that results are consistent. This is similar to that patch regarding the
change in statfs.
---
Note:
I am currently working on a static analyser to detect missing locks using type-based static analysis as my master's thesis in order to obtain my master's degree.
If you would like to have more details, please let me know. This was a reported case. I manually verified the report by looking at the code, so that I do not send wrong information or patches. After concluding that this seems to be a true positive, I created this patch. This was compile-tested and runtime-tested on x86_64. This issue was found on Linux v5.17.4.
fs/f2fs/super.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/f2fs/super.c +3 -1
@@ -1739,10 +1739,12 @@ static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf) buf->f_bsize = sbi->blocksize; buf->f_blocks = total_count - start_count; + +spin_lock (&sbi->stat_lock); + buf->f_bfree = user_block_count -valid_user_blocks (sbi) - sbi->current_reserved_blocks; -spin_lock (&sbi->stat_lock);if (unlikely (buf->f_bfree <= sbi->unusable_block_count)) buf->f_bfree =0 ;else
Signature · 8 lines
2.35.2 _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Jaegeuk Kim
maintainerQuoted 10 lines · Niels Dossche
The calculation for
f_bfreeperforms a calculation involvingcurrent_reserved_blocksandtotal_valid_user_blocks. Both can be
modified understat_lock. Asstat_lockis not used to read both these
values in statfs, this can lead to inconsistent results. Extend the
locking to prevent this issue.
Commitc9c8ed50d94c("f2fs: fix to avoid potential race onsbi->unusable_block_countaccess/update") already added the use ofsbi->stat_lockin statfs in
order to make the calculation of multiple, different fields atomic so
that results are consistent. This is similar to that patch regarding the
change in statfs.
Is this enough? It seems we also need to cover sbi->user_block_count and sbi->total_node_count by stat_lock.
Quoted 38 lines
Signed-off-by Niels Dossche <[email protected]>---
Note:
I am currently working on a static analyser to detect missing locks using type-based static analysis as my master's thesis in order to obtain my master's degree.
If you would like to have more details, please let me know. This was a reported case. I manually verified the report by looking at the code, so that I do not send wrong information or patches. After concluding that this seems to be a true positive, I created this patch. This was compile-tested and runtime-tested onx86_64. This issue was found on Linux v5.17.4.fs/f2fs/super.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)fs/f2fs/super.c +3
-1@@ -1739,10 +1739,12 @@ static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf) buf->f_bsize = sbi->blocksize; buf->f_blocks = total_count - start_count; + +spin_lock (&sbi->stat_lock); + buf->f_bfree = user_block_count -valid_user_blocks (sbi) - sbi->current_reserved_blocks; -spin_lock (&sbi->stat_lock);if (unlikely (buf->f_bfree <= sbi->unusable_block_count)) buf->f_bfree =0 ;else Signature · 1 line
2.35.2
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Quoted 12 lines · Jaegeuk Kim
Quoted 10 lines · Niels Dossche
The calculation for
f_bfreeperforms a calculation involvingcurrent_reserved_blocksandtotal_valid_user_blocks. Both can be
modified understat_lock. Asstat_lockis not used to read both these
values in statfs, this can lead to inconsistent results. Extend the
locking to prevent this issue.
Commitc9c8ed50d94c("f2fs: fix to avoid potential race onsbi->unusable_block_countaccess/update") already added the use ofsbi->stat_lockin statfs in
order to make the calculation of multiple, different fields atomic so
that results are consistent. This is similar to that patch regarding the
change in statfs.
Quoted 2 lines
Is this enough? It seems we also need to cover
sbi->user_block_countandsbi->total_node_countbystat_lock.
You're right, that seems to need locking for atomicity too. I'll send a v2. Thanks!
Quoted 33 lines
Quoted 33 lines
Signed-off-by Niels Dossche <[email protected]>---
Note:
I am currently working on a static analyser to detect missing locks using type-based static analysis as my master's thesis in order to obtain my master's degree.
If you would like to have more details, please let me know. This was a reported case. I manually verified the report by looking at the code, so that I do not send wrong information or patches. After concluding that this seems to be a true positive, I created this patch. This was compile-tested and runtime-tested onx86_64. This issue was found on Linux v5.17.4.fs/f2fs/super.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)fs/f2fs/super.c +3
-0@@ -1739,10 +1739,12 @@ static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf) buf->f_bsize = sbi->blocksize; buf->f_blocks = total_count - start_count; + +spin_lock (&sbi->stat_lock); + buf->f_bfree = user_block_count -valid_user_blocks (sbi) - sbi->current_reserved_blocks;
Quoted 6 lines
Quoted 6 lines
fs/f2fs/super.c +0
-1-spin_lock (&sbi->stat_lock);if (unlikely (buf->f_bfree <= sbi->unusable_block_count)) buf->f_bfree =0 ;else Signature · 1 line
2.35.2
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel