linux-f2fs-devel.lists.sourceforge.net archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] fsck.f2fs: check validity of nat journal
@ 2019-01-14  1:33 Chao Yu
  2019-01-22 23:59 ` Jaegeuk Kim
  0 siblings, 1 reply; 3+ messages in thread
From: Chao Yu @ 2019-01-14  1:33 UTC (permalink / raw)
  To: linux-f2fs-devel, jaegeuk

As reported by Aravind:

I built f2fs tools from source (at tag v1.12.0) and was able to get this backtrace in gdb:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7f8eb54 in f2fs_set_bit (nr=1041170432,
    addr=0x7fffff621010 <error: Cannot access memory at address 0x7fffff621010>) at libf2fs.c:312
312        mask = 1 << (7 - (nr & 0x07));
(gdb) where
    addr=0x7fffff621010 <error: Cannot access memory at address 0x7fffff621010>) at libf2fs.c:312

> [ 5338.040024] nats:8781, sits:6
> [ 5338.040027] F2FS-fs (sda2): Failed to initialize F2FS segment manager
> [ 5338.128893] nats:8781, sits:6
> [ 5338.128895] F2FS-fs (sda2): Failed to initialize F2FS segment manager

nat_count/nid/blkaddr recorded in journal may be corrupted, let's do
sanity check on them, skip loading invalid ones during build_node_manager().

Reported-by: Aravind R S <aravindet@gmail.com>
Signed-off-by: Chao Yu <yuchao0@huawei.com>
---
v3:
- truncate nat journal table once any journalled nat entry is corrupted
 fsck/mount.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/fsck/mount.c b/fsck/mount.c
index 3966525104a7..51d0a09695eb 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -1066,11 +1066,29 @@ static int f2fs_init_nid_bitmap(struct f2fs_sb_info *sbi)
 			f2fs_set_bit(nid, nm_i->nid_bitmap);
 	}
 
+	if (nats_in_cursum(journal) > NAT_JOURNAL_ENTRIES) {
+		MSG(0, "\tError: f2fs_init_nid_bitmap truncate n_nats(%u) to "
+			"NAT_JOURNAL_ENTRIES(%lu)\n",
+			nats_in_cursum(journal), NAT_JOURNAL_ENTRIES);
+		journal->n_nats = cpu_to_le16(NAT_JOURNAL_ENTRIES);
+	}
+
 	for (i = 0; i < nats_in_cursum(journal); i++) {
 		block_t addr;
 
 		addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
+		if (!IS_VALID_BLK_ADDR(sbi, addr)) {
+			MSG(0, "\tError: f2fs_init_nid_bitmap: addr(%u) is invalid!!!\n", addr);
+			journal->n_nats = cpu_to_le16(i);
+			continue;
+		}
+
 		nid = le32_to_cpu(nid_in_journal(journal, i));
+		if (!IS_VALID_NID(sbi, nid)) {
+			MSG(0, "\tError: f2fs_init_nid_bitmap: nid(%u) is invalid!!!\n", nid);
+			journal->n_nats = cpu_to_le16(i);
+			continue;
+		}
 		if (addr != NULL_ADDR)
 			f2fs_set_bit(nid, nm_i->nid_bitmap);
 	}
-- 
2.18.0.rc1

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

* Re: [PATCH v3] fsck.f2fs: check validity of nat journal
  2019-01-14  1:33 [PATCH v3] fsck.f2fs: check validity of nat journal Chao Yu
@ 2019-01-22 23:59 ` Jaegeuk Kim
  2019-01-23  2:02   ` Chao Yu
  0 siblings, 1 reply; 3+ messages in thread
From: Jaegeuk Kim @ 2019-01-22 23:59 UTC (permalink / raw)
  To: Chao Yu; +Cc: linux-f2fs-devel

On 01/14, Chao Yu wrote:
> As reported by Aravind:
> 
> I built f2fs tools from source (at tag v1.12.0) and was able to get this backtrace in gdb:
> 
> Program received signal SIGSEGV, Segmentation fault.
> 0x00007ffff7f8eb54 in f2fs_set_bit (nr=1041170432,
>     addr=0x7fffff621010 <error: Cannot access memory at address 0x7fffff621010>) at libf2fs.c:312
> 312        mask = 1 << (7 - (nr & 0x07));
> (gdb) where
>     addr=0x7fffff621010 <error: Cannot access memory at address 0x7fffff621010>) at libf2fs.c:312
> 
> > [ 5338.040024] nats:8781, sits:6
> > [ 5338.040027] F2FS-fs (sda2): Failed to initialize F2FS segment manager
> > [ 5338.128893] nats:8781, sits:6
> > [ 5338.128895] F2FS-fs (sda2): Failed to initialize F2FS segment manager
> 
> nat_count/nid/blkaddr recorded in journal may be corrupted, let's do
> sanity check on them, skip loading invalid ones during build_node_manager().

This requires the below diff.

--- a/fsck/f2fs.h
+++ b/fsck/f2fs.h
@@ -376,6 +376,9 @@ static inline bool IS_VALID_NID(struct f2fs_sb_info *sbi, u32 nid)

 static inline bool IS_VALID_BLK_ADDR(struct f2fs_sb_info *sbi, u32 addr)
 {
+       if (addr == NULL_ADDR || addr == NEW_ADDR)
+               return 1;
+
        if (addr >= le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count) ||
                                addr < SM_I(sbi)->main_blkaddr) {
                DBG(1, "block addr [0x%x]\n", addr);

> 
> Reported-by: Aravind R S <aravindet@gmail.com>
> Signed-off-by: Chao Yu <yuchao0@huawei.com>
> ---
> v3:
> - truncate nat journal table once any journalled nat entry is corrupted
>  fsck/mount.c | 18 ++++++++++++++++++
>  1 file changed, 18 insertions(+)
> 
> diff --git a/fsck/mount.c b/fsck/mount.c
> index 3966525104a7..51d0a09695eb 100644
> --- a/fsck/mount.c
> +++ b/fsck/mount.c
> @@ -1066,11 +1066,29 @@ static int f2fs_init_nid_bitmap(struct f2fs_sb_info *sbi)
>  			f2fs_set_bit(nid, nm_i->nid_bitmap);
>  	}
>  
> +	if (nats_in_cursum(journal) > NAT_JOURNAL_ENTRIES) {
> +		MSG(0, "\tError: f2fs_init_nid_bitmap truncate n_nats(%u) to "
> +			"NAT_JOURNAL_ENTRIES(%lu)\n",
> +			nats_in_cursum(journal), NAT_JOURNAL_ENTRIES);
> +		journal->n_nats = cpu_to_le16(NAT_JOURNAL_ENTRIES);
> +	}
> +
>  	for (i = 0; i < nats_in_cursum(journal); i++) {
>  		block_t addr;
>  
>  		addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
> +		if (!IS_VALID_BLK_ADDR(sbi, addr)) {
> +			MSG(0, "\tError: f2fs_init_nid_bitmap: addr(%u) is invalid!!!\n", addr);
> +			journal->n_nats = cpu_to_le16(i);
> +			continue;
> +		}
> +
>  		nid = le32_to_cpu(nid_in_journal(journal, i));
> +		if (!IS_VALID_NID(sbi, nid)) {
> +			MSG(0, "\tError: f2fs_init_nid_bitmap: nid(%u) is invalid!!!\n", nid);
> +			journal->n_nats = cpu_to_le16(i);
> +			continue;
> +		}
>  		if (addr != NULL_ADDR)
>  			f2fs_set_bit(nid, nm_i->nid_bitmap);
>  	}
> -- 
> 2.18.0.rc1

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

* Re: [PATCH v3] fsck.f2fs: check validity of nat journal
  2019-01-22 23:59 ` Jaegeuk Kim
@ 2019-01-23  2:02   ` Chao Yu
  0 siblings, 0 replies; 3+ messages in thread
From: Chao Yu @ 2019-01-23  2:02 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: linux-f2fs-devel

On 2019/1/23 7:59, Jaegeuk Kim wrote:
> On 01/14, Chao Yu wrote:
>> As reported by Aravind:
>>
>> I built f2fs tools from source (at tag v1.12.0) and was able to get this backtrace in gdb:
>>
>> Program received signal SIGSEGV, Segmentation fault.
>> 0x00007ffff7f8eb54 in f2fs_set_bit (nr=1041170432,
>>     addr=0x7fffff621010 <error: Cannot access memory at address 0x7fffff621010>) at libf2fs.c:312
>> 312        mask = 1 << (7 - (nr & 0x07));
>> (gdb) where
>>     addr=0x7fffff621010 <error: Cannot access memory at address 0x7fffff621010>) at libf2fs.c:312
>>
>>> [ 5338.040024] nats:8781, sits:6
>>> [ 5338.040027] F2FS-fs (sda2): Failed to initialize F2FS segment manager
>>> [ 5338.128893] nats:8781, sits:6
>>> [ 5338.128895] F2FS-fs (sda2): Failed to initialize F2FS segment manager
>>
>> nat_count/nid/blkaddr recorded in journal may be corrupted, let's do
>> sanity check on them, skip loading invalid ones during build_node_manager().
> 
> This requires the below diff.
> 
> --- a/fsck/f2fs.h
> +++ b/fsck/f2fs.h
> @@ -376,6 +376,9 @@ static inline bool IS_VALID_NID(struct f2fs_sb_info *sbi, u32 nid)
> 
>  static inline bool IS_VALID_BLK_ADDR(struct f2fs_sb_info *sbi, u32 addr)
>  {
> +       if (addr == NULL_ADDR || addr == NEW_ADDR)
> +               return 1;

Actually, IS_VALID_BLK_ADDR() is used in many places, after the check, we
may load data from position pointed with @addr, so, we may not return true
if addr is equal to NULL_ADDR/NEW_ADDR.

Thanks,

> +
>         if (addr >= le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count) ||
>                                 addr < SM_I(sbi)->main_blkaddr) {
>                 DBG(1, "block addr [0x%x]\n", addr);
> 
>>
>> Reported-by: Aravind R S <aravindet@gmail.com>
>> Signed-off-by: Chao Yu <yuchao0@huawei.com>
>> ---
>> v3:
>> - truncate nat journal table once any journalled nat entry is corrupted
>>  fsck/mount.c | 18 ++++++++++++++++++
>>  1 file changed, 18 insertions(+)
>>
>> diff --git a/fsck/mount.c b/fsck/mount.c
>> index 3966525104a7..51d0a09695eb 100644
>> --- a/fsck/mount.c
>> +++ b/fsck/mount.c
>> @@ -1066,11 +1066,29 @@ static int f2fs_init_nid_bitmap(struct f2fs_sb_info *sbi)
>>  			f2fs_set_bit(nid, nm_i->nid_bitmap);
>>  	}
>>  
>> +	if (nats_in_cursum(journal) > NAT_JOURNAL_ENTRIES) {
>> +		MSG(0, "\tError: f2fs_init_nid_bitmap truncate n_nats(%u) to "
>> +			"NAT_JOURNAL_ENTRIES(%lu)\n",
>> +			nats_in_cursum(journal), NAT_JOURNAL_ENTRIES);
>> +		journal->n_nats = cpu_to_le16(NAT_JOURNAL_ENTRIES);
>> +	}
>> +
>>  	for (i = 0; i < nats_in_cursum(journal); i++) {
>>  		block_t addr;
>>  
>>  		addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
>> +		if (!IS_VALID_BLK_ADDR(sbi, addr)) {
>> +			MSG(0, "\tError: f2fs_init_nid_bitmap: addr(%u) is invalid!!!\n", addr);
>> +			journal->n_nats = cpu_to_le16(i);
>> +			continue;
>> +		}
>> +
>>  		nid = le32_to_cpu(nid_in_journal(journal, i));
>> +		if (!IS_VALID_NID(sbi, nid)) {
>> +			MSG(0, "\tError: f2fs_init_nid_bitmap: nid(%u) is invalid!!!\n", nid);
>> +			journal->n_nats = cpu_to_le16(i);
>> +			continue;
>> +		}
>>  		if (addr != NULL_ADDR)
>>  			f2fs_set_bit(nid, nm_i->nid_bitmap);
>>  	}
>> -- 
>> 2.18.0.rc1
> 
> .
> 

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

end of thread, other threads:[~2019-01-23  2:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-01-14  1:33 [PATCH v3] fsck.f2fs: check validity of nat journal Chao Yu
2019-01-22 23:59 ` Jaegeuk Kim
2019-01-23  2:02   ` Chao Yu

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).