linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fsck.f2fs: check nid range before use to avoid segmentation fault
@ 2017-12-15  6:26 Yunlong Song
  2017-12-15 10:06 ` Sheng Yong
  2017-12-18 11:53 ` [PATCH v2] " Yunlong Song
  0 siblings, 2 replies; 5+ messages in thread
From: Yunlong Song @ 2017-12-15  6:26 UTC (permalink / raw)
  To: jaegeuk, chao, yuchao0, yunlong.song, yunlong.song
  Cc: miaoxie, bintian.wang, shengyong1, heyunlei, linux-fsdevel,
	linux-f2fs-devel, linux-kernel

Signed-off-by: Yunlong Song <yunlong.song@huawei.com>
---
 fsck/fsck.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/fsck/fsck.c b/fsck/fsck.c
index 11b8b0b..2212aa3 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -14,6 +14,15 @@
 char *tree_mark;
 uint32_t tree_mark_size = 256;
 
+static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
+{
+    if (nid < F2FS_ROOT_INO(sbi))
+        return -EINVAL;
+    if (nid >= NM_I(sbi)->max_nid)
+        return -EINVAL;
+    return 0;
+}
+
 int f2fs_set_main_bitmap(struct f2fs_sb_info *sbi, u32 blk, int type)
 {
 	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
@@ -740,7 +749,7 @@ void fsck_chk_inode_blk(struct f2fs_sb_info *sbi, u32 nid,
 	for (idx = 0; idx < 5; idx++) {
 		u32 nid = le32_to_cpu(node_blk->i.i_nid[idx]);
 
-		if (nid != 0) {
+		if (nid != 0 && !check_nid_range(sbi, nid)) {
 			struct node_info ni;
 
 			get_node_info(sbi, nid, &ni);
-- 
1.8.5.2

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] fsck.f2fs: check nid range before use to avoid segmentation fault
  2017-12-15  6:26 [PATCH] fsck.f2fs: check nid range before use to avoid segmentation fault Yunlong Song
@ 2017-12-15 10:06 ` Sheng Yong
  2017-12-18 11:53 ` [PATCH v2] " Yunlong Song
  1 sibling, 0 replies; 5+ messages in thread
From: Sheng Yong @ 2017-12-15 10:06 UTC (permalink / raw)
  To: Yunlong Song, jaegeuk, chao, yuchao0, yunlong.song
  Cc: miaoxie, bintian.wang, heyunlei, linux-fsdevel, linux-f2fs-devel,
	linux-kernel



On 2017/12/15 14:26, Yunlong Song wrote:
> Signed-off-by: Yunlong Song <yunlong.song@huawei.com>
> ---
>   fsck/fsck.c | 11 ++++++++++-
>   1 file changed, 10 insertions(+), 1 deletion(-)
> 
> diff --git a/fsck/fsck.c b/fsck/fsck.c
> index 11b8b0b..2212aa3 100644
> --- a/fsck/fsck.c
> +++ b/fsck/fsck.c
> @@ -14,6 +14,15 @@
>   char *tree_mark;
>   uint32_t tree_mark_size = 256;
>   
> +static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
> +{
> +    if (nid < F2FS_ROOT_INO(sbi))
> +        return -EINVAL;
> +    if (nid >= NM_I(sbi)->max_nid)
> +        return -EINVAL;
> +    return 0;
> +}
> +
Hi Yunlong,
I think you could use IS_VALID_NID() instead of check_nid_range. Maybe we could
add the check 'if (nid < F2FS_ROOT_INO(sbi))' to IS_VALID_NID().

thanks,
Sheng
>   int f2fs_set_main_bitmap(struct f2fs_sb_info *sbi, u32 blk, int type)
>   {
>   	struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
> @@ -740,7 +749,7 @@ void fsck_chk_inode_blk(struct f2fs_sb_info *sbi, u32 nid,
>   	for (idx = 0; idx < 5; idx++) {
>   		u32 nid = le32_to_cpu(node_blk->i.i_nid[idx]);
>   
> -		if (nid != 0) {
> +		if (nid != 0 && !check_nid_range(sbi, nid)) {
>   			struct node_info ni;
>   
>   			get_node_info(sbi, nid, &ni);
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] fsck.f2fs: check nid range before use to avoid segmentation fault
  2017-12-15  6:26 [PATCH] fsck.f2fs: check nid range before use to avoid segmentation fault Yunlong Song
  2017-12-15 10:06 ` Sheng Yong
@ 2017-12-18 11:53 ` Yunlong Song
  2017-12-23  3:14   ` Chao Yu
  1 sibling, 1 reply; 5+ messages in thread
From: Yunlong Song @ 2017-12-18 11:53 UTC (permalink / raw)
  To: jaegeuk, chao, yuchao0, yunlong.song, yunlong.song
  Cc: miaoxie, bintian.wang, shengyong1, heyunlei, linux-fsdevel,
	linux-f2fs-devel, linux-kernel

Signed-off-by: Yunlong Song <yunlong.song@huawei.com>
---
 fsck/fsck.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fsck/fsck.c b/fsck/fsck.c
index 11b8b0b..faf0663 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -740,7 +740,7 @@ void fsck_chk_inode_blk(struct f2fs_sb_info *sbi, u32 nid,
 	for (idx = 0; idx < 5; idx++) {
 		u32 nid = le32_to_cpu(node_blk->i.i_nid[idx]);
 
-		if (nid != 0) {
+		if (nid != 0 && IS_VALID_NID(sbi, nid)) {
 			struct node_info ni;
 
 			get_node_info(sbi, nid, &ni);
-- 
1.8.5.2

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] fsck.f2fs: check nid range before use to avoid segmentation fault
  2017-12-18 11:53 ` [PATCH v2] " Yunlong Song
@ 2017-12-23  3:14   ` Chao Yu
  2017-12-23  3:35     ` Yunlong Song
  0 siblings, 1 reply; 5+ messages in thread
From: Chao Yu @ 2017-12-23  3:14 UTC (permalink / raw)
  To: Yunlong Song, jaegeuk, chao, yunlong.song
  Cc: miaoxie, bintian.wang, shengyong1, heyunlei, linux-fsdevel,
	linux-f2fs-devel, linux-kernel

On 2017/12/18 19:53, Yunlong Song wrote:
> Signed-off-by: Yunlong Song <yunlong.song@huawei.com>

How about introducing IS_AVAILABLE_NID as below, and use it instead?

#define IS_AVAILABLE_NID(sbi, nid)	(IS_VALID_NID(sbi, nid) && (nid >= root_ino))

Thanks,

> ---
>  fsck/fsck.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fsck/fsck.c b/fsck/fsck.c
> index 11b8b0b..faf0663 100644
> --- a/fsck/fsck.c
> +++ b/fsck/fsck.c
> @@ -740,7 +740,7 @@ void fsck_chk_inode_blk(struct f2fs_sb_info *sbi, u32 nid,
>  	for (idx = 0; idx < 5; idx++) {
>  		u32 nid = le32_to_cpu(node_blk->i.i_nid[idx]);
>  
> -		if (nid != 0) {
> +		if (nid != 0 && IS_VALID_NID(sbi, nid)) {
>  			struct node_info ni;
>  
>  			get_node_info(sbi, nid, &ni);
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] fsck.f2fs: check nid range before use to avoid segmentation fault
  2017-12-23  3:14   ` Chao Yu
@ 2017-12-23  3:35     ` Yunlong Song
  0 siblings, 0 replies; 5+ messages in thread
From: Yunlong Song @ 2017-12-23  3:35 UTC (permalink / raw)
  To: Chao Yu, jaegeuk, chao, yunlong.song
  Cc: miaoxie, bintian.wang, shengyong1, heyunlei, linux-fsdevel,
	linux-f2fs-devel, linux-kernel

Both are OK, since nid < root_ino cannot trigger segmentation fault 
(nat_block->entries[nid%NAT_ENTRY_PER_BLOCK]).

On 2017/12/23 11:14, Chao Yu wrote:
> On 2017/12/18 19:53, Yunlong Song wrote:
>> Signed-off-by: Yunlong Song <yunlong.song@huawei.com>
> How about introducing IS_AVAILABLE_NID as below, and use it instead?
>
> #define IS_AVAILABLE_NID(sbi, nid)	(IS_VALID_NID(sbi, nid) && (nid >= root_ino))
>
> Thanks,
>
>> ---
>>   fsck/fsck.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/fsck/fsck.c b/fsck/fsck.c
>> index 11b8b0b..faf0663 100644
>> --- a/fsck/fsck.c
>> +++ b/fsck/fsck.c
>> @@ -740,7 +740,7 @@ void fsck_chk_inode_blk(struct f2fs_sb_info *sbi, u32 nid,
>>   	for (idx = 0; idx < 5; idx++) {
>>   		u32 nid = le32_to_cpu(node_blk->i.i_nid[idx]);
>>   
>> -		if (nid != 0) {
>> +		if (nid != 0 && IS_VALID_NID(sbi, nid)) {
>>   			struct node_info ni;
>>   
>>   			get_node_info(sbi, nid, &ni);
>>
>
> .
>

-- 
Thanks,
Yunlong Song

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-12-23  3:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-12-15  6:26 [PATCH] fsck.f2fs: check nid range before use to avoid segmentation fault Yunlong Song
2017-12-15 10:06 ` Sheng Yong
2017-12-18 11:53 ` [PATCH v2] " Yunlong Song
2017-12-23  3:14   ` Chao Yu
2017-12-23  3:35     ` Yunlong Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).