diff mbox

[v2] fsck.f2fs: check nid range before use to avoid segmentation fault

Message ID 1513598025-160615-1-git-send-email-yunlong.song@huawei.com (mailing list archive)
State New, archived
Headers show

Commit Message

Yunlong Song Dec. 18, 2017, 11:53 a.m. UTC
Signed-off-by: Yunlong Song <yunlong.song@huawei.com>
---
 fsck/fsck.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Chao Yu Dec. 23, 2017, 3:14 a.m. UTC | #1
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);
>
Yunlong Song Dec. 23, 2017, 3:35 a.m. UTC | #2
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);
>>
>
> .
>
diff mbox

Patch

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