exfat: add dir-entry set checksum validation
Subsystems (2)
PATCH 1/2 exfat: add dir-entry set checksum validation
Add checksum validation for dir-entry set when getting it. exfat_calc_dir_chksum_with_entry_set() also validates entry-type.
** This patch depends on:
'[PATCH v3] exfat: integrates dir-entry getting and validation'
--- fs/exfat/dir.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-)
fs/exfat/dir.c +22 -12
@@ -563,18 +563,27 @@ int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,return 0 ; } -void exfat_update_dir_chksum_with_entry_set (struct exfat_entry_set_cache *es) +static int exfat_calc_dir_chksum_with_entry_set (struct exfat_entry_set_cache *es,u16 *chksum) { -int chksum_type =CS_DIR_ENTRY , i; -unsigned short chksum =0 ;struct exfat_dentry *ep; +int i; -for (i =0 ; i < es->num_entries; i++) { - ep =exfat_get_validated_dentry (es, i,TYPE_ALL ); - chksum =exfat_calc_chksum16 (ep,DENTRY_SIZE , chksum, - chksum_type); - chksum_type =CS_DEFAULT ; + ep = container_of(es->de_file, struct exfat_dentry, dentry.file); + *chksum =exfat_calc_chksum16 (ep,DENTRY_SIZE ,0 ,CS_DIR_ENTRY ); +for (i =0 ; i < es->de_file->num_ext; i++) { + ep =exfat_get_validated_dentry (es,1 + i,TYPE_SECONDARY ); +if (!ep) +return -EIO ; + *chksum =exfat_calc_chksum16 (ep,DENTRY_SIZE , *chksum,CS_DEFAULT ); } +return 0 ; +} + +void exfat_update_dir_chksum_with_entry_set (struct exfat_entry_set_cache *es) +{ +u16 chksum; + +exfat_calc_dir_chksum_with_entry_set (es, &chksum); es->de_file->checksum =cpu_to_le16 (chksum); es->modified = true; }
@@ -775,6 +784,7 @@ struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,struct exfat_entry_set_cache *es;struct exfat_dentry *ep;struct buffer_head *bh; +u16 chksum;if (p_dir->dir ==DIR_DELETED ) {exfat_err (sb,"access to deleted dentry" );
@@ -839,10 +849,10 @@ struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb, goto free_es; es->de_stream = &ep->dentry.stream; -for (i =2 ; i < es->num_entries; i++) { -if (!exfat_get_validated_dentry (es, i,TYPE_SECONDARY )) - goto free_es; - } +if (max_entries ==ES_ALL_ENTRIES && + ((exfat_calc_dir_chksum_with_entry_set (es, &chksum) || + chksum !=le16_to_cpu (es->de_file->checksum)))) + goto free_es;return es;
Signature · 1 line
2.25.1
PATCH 2/2 exfat: write only modified part of dir-entry set
Currently exfat_free_dentry_set() writes all of dir-entry set. Change it to write only the modified part of dir-entry set. And, Integrate exfat_free_dentry_set() and exfat_update_dir_chksum_with_entry_set() as exfat_put_dentry_set().
** This patch depends on:
'[PATCH v3] exfat: integrates dir-entry getting and validation' '[PATCH] exfat: add NameLength check when extracting name' '[PATCH] exfat: unify name extraction'
--- fs/exfat/dir.c | 33 ++++++++++++++++----------------- fs/exfat/exfat_fs.h | 4 +--- fs/exfat/file.c | 3 +-- fs/exfat/inode.c | 6 ++---- fs/exfat/namei.c | 4 ++-- 5 files changed, 22 insertions(+), 28 deletions(-)
fs/exfat/dir.c +16 -17
@@ -133,7 +133,7 @@ static int exfat_readdir(struct inode *inode, struct exfat_dir_entry *dir_entry) dir_entry->namebuf.lfn, dir_entry->namebuf.lfnbuf_len); -exfat_free_dentry_set (es, false); +exfat_put_dentry_set (es,0 , false); ei->hint_bmap.off = dentry >> dentries_per_clu_bits; ei->hint_bmap.clu = clu.dir;
@@ -579,21 +579,21 @@ static int exfat_calc_dir_chksum_with_entry_set(struct exfat_entry_set_cache *esreturn 0 ; } -void exfat_update_dir_chksum_with_entry_set (struct exfat_entry_set_cache *es) -{ -u16 chksum; - -exfat_calc_dir_chksum_with_entry_set (es, &chksum); - es->de_file->checksum =cpu_to_le16 (chksum); - es->modified = true; -} - -int exfat_free_dentry_set (struct exfat_entry_set_cache *es,int sync) +int exfat_put_dentry_set (struct exfat_entry_set_cache *es,int modified,int sync) {int i, err =0 ; -if (es->modified) - err =exfat_update_bhs (es->bh, es->num_bh, sync); +if (modified) { +int off = es->start_off + (modified -1 ) *DENTRY_SIZE ; +int modified_bh =min (EXFAT_B_TO_BLK (off, es->sb) +1 , es->num_bh); +u16 chksum; + + err =exfat_calc_dir_chksum_with_entry_set (es, &chksum); +if (!err) { + es->de_file->checksum =cpu_to_le16 (chksum); + err =exfat_update_bhs (es->bh, modified_bh, sync); + } + }for (i =0 ; i < es->num_bh; i++)if (err)
@@ -800,7 +800,6 @@ struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,if (!es)return NULL ; es->sb = sb; - es->modified = false; es->num_entries =1 ;/* byte offset in cluster */
@@ -857,7 +856,7 @@ struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,return es; free_es: -exfat_free_dentry_set (es, false); +exfat_put_dentry_set (es,0 , false);return NULL ; }
@@ -962,7 +961,7 @@ int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei, num_ext = es->de_file->num_ext; name_hash =le16_to_cpu (es->de_stream->name_hash); name_len = es->de_stream->name_len; -exfat_free_dentry_set (es, false); +exfat_put_dentry_set (es,0 , false);if (p_uniname->name_hash != name_hash || p_uniname->name_len != name_len)
@@ -973,7 +972,7 @@ int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,continue ;exfat_get_uniname_from_name_entries (es, &uni_name); -exfat_free_dentry_set (es, false); +exfat_put_dentry_set (es,0 , false); if (!exfat_uniname_ncmp(sb, p_uniname->name,
fs/exfat/exfat_fs.h +1 -3
@@ -170,7 +170,6 @@ struct exfat_hint {struct exfat_entry_set_cache {struct super_block *sb; -bool modified;unsigned int start_off;int num_bh;struct buffer_head *bh[DIR_CACHE_SIZE ];
@@ -452,7 +451,6 @@ int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir, int entry, int order, int num_entries);int exfat_update_dir_chksum (struct inode *inode,struct exfat_chain *p_dir,int entry); -void exfat_update_dir_chksum_with_entry_set (struct exfat_entry_set_cache *es);int exfat_calc_num_entries (struct exfat_uni_name *p_uniname); int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei, struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
@@ -467,7 +465,7 @@ struct exfat_dentry *exfat_get_validated_dentry(struct exfat_entry_set_cache *es int num, unsigned int type);struct exfat_entry_set_cache *exfat_get_dentry_set (struct super_block *sb,struct exfat_chain *p_dir,int entry,int max_entries); -int exfat_free_dentry_set (struct exfat_entry_set_cache *es,int sync); +int exfat_put_dentry_set (struct exfat_entry_set_cache *es,int modified,int sync);int exfat_count_dir_entries (struct super_block *sb,struct exfat_chain *p_dir);/* inode.c */
fs/exfat/file.c +1 -2
@@ -185,8 +185,7 @@ int __exfat_truncate(struct inode *inode, loff_t new_size) es->de_stream->start_clu =EXFAT_FREE_CLUSTER ; } -exfat_update_dir_chksum_with_entry_set (es); - err =exfat_free_dentry_set (es,inode_needs_sync (inode)); + err =exfat_put_dentry_set (es,2 ,inode_needs_sync (inode));if (err)return err; }
fs/exfat/inode.c +2 -4
@@ -73,8 +73,7 @@ static int __exfat_write_inode(struct inode *inode, int sync) es->de_stream->valid_size =cpu_to_le64 (on_disk_size); es->de_stream->size = es->de_stream->valid_size; -exfat_update_dir_chksum_with_entry_set (es); -return exfat_free_dentry_set (es, sync); +return exfat_put_dentry_set (es,2 , sync); }int exfat_write_inode (struct inode *inode,struct writeback_control *wbc)
@@ -230,8 +229,7 @@ static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset, es->de_stream->valid_size =cpu_to_le64 (i_size_read (inode)); es->de_stream->size = es->de_stream->valid_size; -exfat_update_dir_chksum_with_entry_set (es); - err =exfat_free_dentry_set (es,inode_needs_sync (inode)); + err =exfat_put_dentry_set (es,2 ,inode_needs_sync (inode));if (err)return err; }/* end of if != DIR_DELETED */
fs/exfat/namei.c +2 -2
@@ -677,7 +677,7 @@ static int exfat_find(struct inode *dir, struct qstr *qname,exfat_fs_error (sb,"non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)" ,i_size_read (dir), ei->dir.dir, ei->entry); -exfat_free_dentry_set (es, false); +exfat_put_dentry_set (es,0 , false);return -EIO ; }
@@ -696,7 +696,7 @@ static int exfat_find(struct inode *dir, struct qstr *qname, es->de_file->access_time, es->de_file->access_date, 0); -exfat_free_dentry_set (es, false); +exfat_put_dentry_set (es,0 , false);if (info->type ==TYPE_DIR ) { exfat_chain_set(&cdir, info->start_clu,
Signature · 1 line
2.25.1
Quoted 31 lines
Add checksum validation for dir-entry set when getting it.
exfat_calc_dir_chksum_with_entry_set()also validates entry-type.** This patch depends on:
'[PATCH v3] exfat: integrates dir-entry getting and validation'Signed-off-by Tetsuhiro Kohada <[email protected]>--- fs/exfat/dir.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-)fs/exfat/dir.c +3
-7@@ -563,18 +563,27 @@ int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,return 0 ; } -void exfat_update_dir_chksum_with_entry_set (struct exfat_entry_set_cache *es) +static int exfat_calc_dir_chksum_with_entry_set (struct +exfat_entry_set_cache *es,u16 *chksum) { -int chksum_type =CS_DIR_ENTRY , i; -unsigned short chksum =0 ;struct exfat_dentry *ep; +int i; -for (i =0 ; i < es->num_entries; i++) { - ep =exfat_get_validated_dentry (es, i,TYPE_ALL ); - chksum =exfat_calc_chksum16 (ep,DENTRY_SIZE , chksum, - chksum_type);
fs/exfat/dir.c int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir, · line 580
Quoted 10 lines
fs/exfat/dir.c +8
-1- chksum_type =CS_DEFAULT ; + ep = container_of(es->de_file, struct exfat_dentry, dentry.file); + *chksum =exfat_calc_chksum16 (ep,DENTRY_SIZE ,0 ,CS_DIR_ENTRY ); +for (i =0 ; i < es->de_file->num_ext; i++) { + ep =exfat_get_validated_dentry (es,1 + i,TYPE_SECONDARY ); +if (!ep) +return -EIO ; + *chksum =exfat_calc_chksum16 (ep,DENTRY_SIZE , *chksum,CS_DEFAULT ); } +return 0 ;
We can return checksum after removing u16 *chksum argument.
Quoted 19 lines
fs/exfat/dir.c +8
-0+} + +void exfat_update_dir_chksum_with_entry_set (struct +exfat_entry_set_cache *es) { +u16 chksum; + +exfat_calc_dir_chksum_with_entry_set (es, &chksum); es->de_file->checksum =cpu_to_le16 (chksum); es->modified = true; }@@ -775,6 +784,7 @@ struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,struct exfat_entry_set_cache *es;struct exfat_dentry *ep;struct buffer_head *bh; +u16 chksum;if (p_dir->dir ==DIR_DELETED ) { exfat_err(sb, "access to deleted dentry"); @@ -839,10 +849,10 @@ struct
exfat_entry_set_cache*exfat_get_dentry_set(structsuper_block*sb,
fs/exfat/dir.c struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb, · line 854
Quoted 10 lines
fs/exfat/dir.c +3
-4goto free_es; es->de_stream = &ep->dentry.stream; -for (i =2 ; i < es->num_entries; i++) { -if (!exfat_get_validated_dentry (es, i,TYPE_SECONDARY )) - goto free_es; - } +if (max_entries ==ES_ALL_ENTRIES && + ((exfat_calc_dir_chksum_with_entry_set (es, &chksum) || + chksum !=le16_to_cpu (es->de_file->checksum))))
Please add error print log if checksum mismatch error happen.
Quoted 6 lines
fs/exfat/dir.c +1
-1+ goto free_es;return es; --2.25.1
Thnak you for your reply.
Quoted 9 lines
Quoted 9 lines
fs/exfat/dir.c +3
-3-void exfat_update_dir_chksum_with_entry_set (struct exfat_entry_set_cache *es) +static int exfat_calc_dir_chksum_with_entry_set (struct +exfat_entry_set_cache *es,u16 *chksum) { -int chksum_type =CS_DIR_ENTRY , i; -unsigned short chksum =0 ;struct exfat_dentry *ep; +int i;
Quoted 15 lines
Quoted 4 lines
fs/exfat/dir.c +0
-4-for (i =0 ; i < es->num_entries; i++) { - ep =exfat_get_validated_dentry (es, i,TYPE_ALL ); - chksum =exfat_calc_chksum16 (ep,DENTRY_SIZE , chksum, - chksum_type);fs/exfat/dir.c int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir, · line 579
Quoted 10 lines
fs/exfat/dir.c +8
-1- chksum_type =CS_DEFAULT ; + ep = container_of(es->de_file, struct exfat_dentry, dentry.file); + *chksum =exfat_calc_chksum16 (ep,DENTRY_SIZE ,0 ,CS_DIR_ENTRY ); +for (i =0 ; i < es->de_file->num_ext; i++) { + ep =exfat_get_validated_dentry (es,1 + i,TYPE_SECONDARY ); +if (!ep) +return -EIO ; + *chksum =exfat_calc_chksum16 (ep,DENTRY_SIZE , *chksum,CS_DEFAULT ); } +return 0 ;We can return checksum after removing u16 *chksum argument.
I want to do that too!
How should I return the error?
Right now, I found that the type of chksum is not 'u16'. I'll fix in v2.
Quoted 22 lines
Quoted 22 lines
fs/exfat/dir.c +8
-0+} + +void exfat_update_dir_chksum_with_entry_set (struct +exfat_entry_set_cache *es) { +u16 chksum; + +exfat_calc_dir_chksum_with_entry_set (es, &chksum); es->de_file->checksum =cpu_to_le16 (chksum); es->modified = true; }@@ -775,6 +784,7 @@ struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,struct exfat_entry_set_cache *es;struct exfat_dentry *ep;struct buffer_head *bh; +u16 chksum;if (p_dir->dir ==DIR_DELETED ) { exfat_err(sb, "access to deleted dentry"); @@ -839,10 +849,10 @@ struct
exfat_entry_set_cache*exfat_get_dentry_set(structsuper_block*sb,
gotofree_es;
es->de_stream=&ep->dentry.stream;
Quoted 8 lines
fs/exfat/dir.c struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb, · line 854
Quoted 7 lines
fs/exfat/dir.c +3
-4-for (i =2 ; i < es->num_entries; i++) { -if (!exfat_get_validated_dentry (es, i,TYPE_SECONDARY )) - goto free_es; - } +if (max_entries ==ES_ALL_ENTRIES && + ((exfat_calc_dir_chksum_with_entry_set (es, &chksum) || + chksum !=le16_to_cpu (es->de_file->checksum))))Please add error print log if checksum mismatch error happen.
OK.
I'll add in v2.
BR
---
Tetsuhiro Kohada <[email protected]>