hpfs: add checks for ea addresses
PATCH 1/1 hpfs: add checks for ea addresses
The addresses of the extended attributes are computed using the fnode_ea() and next_ea() functions which refer to the fields residing in a given fnode. There are no sanity checks for the returned values, so in the case of corrupted data in the fnode, the ea addresses are invalid.
Fix the bug by adding ea_valid_addr() function which checks if a given extended attribute resides within the range of the ea array of a given fnode.
#syz test
--- fs/hpfs/anode.c | 2 +- fs/hpfs/ea.c | 6 +++--- fs/hpfs/hpfs_fn.h | 5 +++++ fs/hpfs/map.c | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-)
fs/hpfs/anode.c +1 -1
@@ -488,7 +488,7 @@ void hpfs_remove_fnode(struct super_block *s, fnode_secno fno)if (!fnode_is_dir (fnode))hpfs_remove_btree (s, &fnode->btree);else hpfs_remove_dtree (s,le32_to_cpu (fnode->u.external[0 ].disk_secno)); ea_end =fnode_end_ea (fnode); -for (ea =fnode_ea (fnode); ea < ea_end; ea =next_ea (ea)) +for (ea =fnode_ea (fnode); ea < ea_end &&ea_valid_addr (fnode, ea); ea =next_ea (ea))if (ea_indirect (ea))hpfs_ea_remove (s,ea_sec (ea),ea_in_anode (ea),ea_len (ea));hpfs_ea_ext_remove (s,le32_to_cpu (fnode->ea_secno),fnode_in_anode (fnode),le32_to_cpu (fnode->ea_size_l));
fs/hpfs/ea.c +3 -3
@@ -80,7 +80,7 @@ int hpfs_read_ea(struct super_block *s, struct fnode *fnode, char *key,char ex[4 +255 +1 +8 ];struct extended_attribute *ea;struct extended_attribute *ea_end =fnode_end_ea (fnode); -for (ea =fnode_ea (fnode); ea < ea_end; ea =next_ea (ea)) +for (ea =fnode_ea (fnode); ea < ea_end &&ea_valid_addr (fnode, ea); ea =next_ea (ea))if (!strcmp (ea->name, key)) {if (ea_indirect (ea)) goto indirect;
@@ -135,7 +135,7 @@ char *hpfs_get_ea(struct super_block *s, struct fnode *fnode, char *key, int *sisecno a;struct extended_attribute *ea;struct extended_attribute *ea_end =fnode_end_ea (fnode); -for (ea =fnode_ea (fnode); ea < ea_end; ea =next_ea (ea)) +for (ea =fnode_ea (fnode); ea < ea_end &&ea_valid_addr (fnode, ea); ea =next_ea (ea))if (!strcmp (ea->name, key)) {if (ea_indirect (ea))return get_indirect_ea (s,ea_in_anode (ea),ea_sec (ea), *size =ea_len (ea));
@@ -198,7 +198,7 @@ void hpfs_set_ea(struct inode *inode, struct fnode *fnode, const char *key,unsigned char h[4 ];struct extended_attribute *ea;struct extended_attribute *ea_end =fnode_end_ea (fnode); -for (ea =fnode_ea (fnode); ea < ea_end; ea =next_ea (ea)) +for (ea =fnode_ea (fnode); ea < ea_end &&ea_valid_addr (fnode, ea); ea =next_ea (ea))if (!strcmp (ea->name, key)) {if (ea_indirect (ea)) {if (ea_len (ea) == size)
fs/hpfs/hpfs_fn.h +5 -0
@@ -152,6 +152,11 @@ static inline struct extended_attribute *next_ea(struct extended_attribute *ea)return (struct extended_attribute *)((char *)ea +5 + ea->namelen +ea_valuelen (ea)); } +static inline bool ea_valid_addr (struct fnode *fnode,struct extended_attribute *ea) +{ +return ((char *)ea >= (char *)&fnode->ea) && ((char *)ea < (char *)&fnode->ea +sizeof (fnode->ea)); +} +static inline secno ea_sec (struct extended_attribute *ea) {return le32_to_cpu (get_unaligned ((__le32 *)((char *)ea +9 + ea->namelen)));
fs/hpfs/map.c +1 -1
@@ -202,7 +202,7 @@ struct fnode *hpfs_map_fnode(struct super_block *s, ino_t ino, struct buffer_hea } ea =fnode_ea (fnode); ea_end =fnode_end_ea (fnode); -while (ea != ea_end) { +while (ea != ea_end &&ea_valid_addr (fnode, ea)) {if (ea > ea_end) {hpfs_error (s,"bad EA in fnode %08lx" , (unsigned long )ino);
Signature · 1 line
2.25.1
Re: [syzbot] [fs?] KASAN: use-after-free Read in hpfs_get_ea
syzbot has tested the proposed patch and the reproducer did not trigger any issue:
Hello,
syzbot has tested the proposed patch and the reproducer did not trigger any issue:
Tested on:
commit: f4a40a42 Merge tag 'efi-fixes-for-v6.16-2' of git://gi.. git tree: upstream console output: https://syzkaller.appspot.com/x/log.txt?x=12314b82580000 kernel config: https://syzkaller.appspot.com/x/.config?x=f09d04131ef56b22 dashboard link: https://syzkaller.appspot.com/bug?extid=fa88eb476e42878f2844 compiler: Debian clang version 20.1.7 (++20250616065708+6146a88f6049-1~exp1~20250616065826.132), Debian LLD 20.1.7 patch: https://syzkaller.appspot.com/x/patch.diff?x=17c0538c580000
Note: testing is done by a robot and is best-effort only.
fs/hpfs/anode.c void hpfs_remove_fnode(struct super_block *s, fnode_secno fno) · line 491
Quoted 3 lines · Antoni Pokusinski
fs/hpfs/anode.c +1
-1ea_end =fnode_end_ea (fnode); -for (ea =fnode_ea (fnode); ea < ea_end; ea =next_ea (ea)) +for (ea =fnode_ea (fnode); ea < ea_end &&ea_valid_addr (fnode, ea); ea =next_ea (ea))
That looks really fishy. If nothing else, your ea_valid_addr() is a range check; are there any situations where we would want to allow fnode_ea() and fnode_end_ea() be out of that range?