From mboxrd@z Thu Jan 1 00:00:00 1970 From: Hin-Tak Leung Subject: Re: [PATCH] hfs: add error checking for hfs_find_init() Date: Sat, 30 Mar 2013 14:01:03 +0000 (GMT) Message-ID: <1364652063.24328.YahooMailClassic@web172301.mail.ir2.yahoo.com> References: <0D203BE4-D965-42FE-ADA7-831070860222@dubeyko.com> Reply-To: htl10@users.sourceforge.net Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: Al Viro , Artem Bityutskiy , Christoph Hellwig , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, ldv-project@linuxtesting.org To: Alexey Khoroshilov , Vyacheslav Dubeyko Return-path: In-Reply-To: <0D203BE4-D965-42FE-ADA7-831070860222@dubeyko.com> Sender: linux-kernel-owner@vger.kernel.org List-Id: linux-fsdevel.vger.kernel.org --- On Sat, 30/3/13, Vyacheslav Dubeyko wrote: > From: Vyacheslav Dubeyko > Subject: Re: [PATCH] hfs: add error checking for hfs_find_init() > To: "Alexey Khoroshilov" > Cc: "Al Viro" , "Artem Bityutskiy" , "Christoph Hellwig" , linux-fsde= vel@vger.kernel.org, linux-kernel@vger.kernel.org, ldv-project@linuxtes= ting.org, "Hin-Tak Leung" > Date: Saturday, 30 March, 2013, 11:35 > Hi Alexey, >=20 > On Mar 30, 2013, at 12:44 AM, Alexey Khoroshilov wrote: >=20 > > hfs_find_init() may fail with ENOMEM, but there are > places, > > where the returned value is not checked. The > consequences can be > > very unpleasant, e.g. kfree uninitialized pointer and > > inappropriate mutex unlocking. > >=20 > > The patch adds checks for errors in hfs_find_init(). > >=20 >=20 > Thank you for your efforts. I have several remarks. Please, > see below. Argh, interesting. I wonder if that is related to how I can get the hfs= plus driver all confused just running 'du' on one large directory in on= e of my disks repeatedly. I'll be interested to trying the hfsplus vers= ion out. Perhaps I would suggest/add a few dprint/printk's so that ther= e is a sign in dmesg when the error condition is triggered. Hin-Tak =20 > > Found by Linux Driver Verification project > (linuxtesting.org). > >=20 > > Signed-off-by: Alexey Khoroshilov > > --- > > fs/hfs/catalog.c |=A0=A0=A012 +++++++++--- > > fs/hfs/dir.c=A0 =A0=A0=A0|=A0 =A0 8 > ++++++-- > > fs/hfs/extent.c=A0 |=A0=A0=A048 > +++++++++++++++++++++++++++++++++--------------- > > fs/hfs/hfs_fs.h=A0 |=A0 =A0 2 +- > > fs/hfs/inode.c=A0=A0=A0|=A0=A0=A011 > +++++++++-- > > fs/hfs/super.c=A0=A0=A0|=A0 =A0 4 +++- > > 6 files changed, 61 insertions(+), 24 deletions(-) > >=20 > > diff --git a/fs/hfs/catalog.c b/fs/hfs/catalog.c > > index 424b033..9569b39 100644 > > --- a/fs/hfs/catalog.c > > +++ b/fs/hfs/catalog.c > > @@ -92,7 +92,9 @@ int hfs_cat_create(u32 cnid, struct > inode *dir, struct qstr *str, struct inode * > > =A0=A0=A0 =A0=A0=A0 return -ENOSPC; > >=20 > > =A0=A0=A0 sb =3D dir->i_sb; > > -=A0=A0=A0 > hfs_find_init(HFS_SB(sb)->cat_tree, &fd); > > +=A0=A0=A0 err =3D > hfs_find_init(HFS_SB(sb)->cat_tree, &fd); > > +=A0=A0=A0 if (err) > > +=A0=A0=A0 =A0=A0=A0 return err; > >=20 > > =A0=A0=A0 hfs_cat_build_key(sb, fd.search_key, > cnid, NULL); > > =A0=A0=A0 entry_size =3D > hfs_cat_build_thread(sb, &entry, > S_ISDIR(inode->i_mode) ? > > @@ -214,7 +216,9 @@ int hfs_cat_delete(u32 cnid, struct > inode *dir, struct qstr *str) > >=20 > > =A0=A0=A0 dprint(DBG_CAT_MOD, "delete_cat: > %s,%u\n", str ? str->name : NULL, cnid); > > =A0=A0=A0 sb =3D dir->i_sb; > > -=A0=A0=A0 > hfs_find_init(HFS_SB(sb)->cat_tree, &fd); > > +=A0=A0=A0 res =3D > hfs_find_init(HFS_SB(sb)->cat_tree, &fd); > > +=A0=A0=A0 if (res) > > +=A0=A0=A0 =A0=A0=A0 return res; > >=20 > > =A0=A0=A0 hfs_cat_build_key(sb, fd.search_key, > dir->i_ino, str); > > =A0=A0=A0 res =3D hfs_brec_find(&fd); > > @@ -281,7 +285,9 @@ int hfs_cat_move(u32 cnid, struct > inode *src_dir, struct qstr *src_name, > > =A0=A0=A0 dprint(DBG_CAT_MOD, "rename_cat: %u > - %lu,%s - %lu,%s\n", cnid, src_dir->i_ino, > src_name->name, > > =A0=A0=A0 =A0=A0=A0 > dst_dir->i_ino, dst_name->name); > > =A0=A0=A0 sb =3D src_dir->i_sb; > > -=A0=A0=A0 > hfs_find_init(HFS_SB(sb)->cat_tree, &src_fd); > > +=A0=A0=A0 err =3D > hfs_find_init(HFS_SB(sb)->cat_tree, &src_fd); > > +=A0=A0=A0 if (err) > > +=A0=A0=A0 =A0=A0=A0 return err; > > =A0=A0=A0 dst_fd =3D src_fd; > >=20 > > =A0=A0=A0 /* find the old dir entry and read > the data */ > > diff --git a/fs/hfs/dir.c b/fs/hfs/dir.c > > index 5f7f1ab..e1c8048 100644 > > --- a/fs/hfs/dir.c > > +++ b/fs/hfs/dir.c > > @@ -25,7 +25,9 @@ static struct dentry > *hfs_lookup(struct inode *dir, struct dentry *dentry, > > =A0=A0=A0 struct inode *inode =3D NULL; > > =A0=A0=A0 int res; > >=20 > > -=A0=A0=A0 > hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd); > > +=A0=A0=A0 res =3D > hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd); > > +=A0=A0=A0 if (res) > > +=A0=A0=A0 =A0=A0=A0 return > ERR_PTR(res); > > =A0=A0=A0 hfs_cat_build_key(dir->i_sb, > fd.search_key, dir->i_ino, &dentry->d_name); > > =A0=A0=A0 res =3D hfs_brec_read(&fd, > &rec, sizeof(rec)); > > =A0=A0=A0 if (res) { > > @@ -63,7 +65,9 @@ static int hfs_readdir(struct file > *filp, void *dirent, filldir_t filldir) > > =A0=A0=A0 if (filp->f_pos >=3D > inode->i_size) > > =A0=A0=A0 =A0=A0=A0 return 0; > >=20 > > -=A0=A0=A0 > hfs_find_init(HFS_SB(sb)->cat_tree, &fd); > > +=A0=A0=A0 err =3D > hfs_find_init(HFS_SB(sb)->cat_tree, &fd); > > +=A0=A0=A0 if (err) > > +=A0=A0=A0 =A0=A0=A0 return err; > > =A0=A0=A0 hfs_cat_build_key(sb, fd.search_key, > inode->i_ino, NULL); > > =A0=A0=A0 err =3D hfs_brec_find(&fd); > > =A0=A0=A0 if (err) > > diff --git a/fs/hfs/extent.c b/fs/hfs/extent.c > > index a67955a..813447b 100644 > > --- a/fs/hfs/extent.c > > +++ b/fs/hfs/extent.c > > @@ -107,7 +107,7 @@ static u16 hfs_ext_lastblock(struct > hfs_extent *ext) > > =A0=A0=A0 return be16_to_cpu(ext->block) + > be16_to_cpu(ext->count); > > } > >=20 > > -static void __hfs_ext_write_extent(struct inode > *inode, struct hfs_find_data *fd) > > +static int __hfs_ext_write_extent(struct inode *inode, > struct hfs_find_data *fd) > > { > > =A0=A0=A0 int res; > >=20 > > @@ -116,26 +116,31 @@ static void > __hfs_ext_write_extent(struct inode *inode, struct > hfs_find_data *fd > > =A0=A0=A0 res =3D hfs_brec_find(fd); > > =A0=A0=A0 if (HFS_I(inode)->flags & > HFS_FLG_EXT_NEW) { > > =A0=A0=A0 =A0=A0=A0 if (res !=3D > -ENOENT) > > -=A0=A0=A0 =A0=A0=A0 > =A0=A0=A0 return; > > +=A0=A0=A0 =A0=A0=A0 > =A0=A0=A0 return res; > > =A0=A0=A0 =A0=A0=A0 > hfs_brec_insert(fd, HFS_I(inode)->cached_extents, > sizeof(hfs_extent_rec)); > > =A0=A0=A0 =A0=A0=A0 > HFS_I(inode)->flags &=3D > ~(HFS_FLG_EXT_DIRTY|HFS_FLG_EXT_NEW); > > =A0=A0=A0 } else { > > =A0=A0=A0 =A0=A0=A0 if (res) > > -=A0=A0=A0 =A0=A0=A0 > =A0=A0=A0 return; > > +=A0=A0=A0 =A0=A0=A0 > =A0=A0=A0 return res; > > =A0=A0=A0 =A0=A0=A0 > hfs_bnode_write(fd->bnode, > HFS_I(inode)->cached_extents, fd->entryoffset, > fd->entrylength); > > =A0=A0=A0 =A0=A0=A0 > HFS_I(inode)->flags &=3D ~HFS_FLG_EXT_DIRTY; > > =A0=A0=A0 } > > +=A0=A0=A0 return 0; > > } > >=20 >=20 > As I see, this fix makes sense and for hfsplus also. Please, > make it and for hfsplus. >=20 > > -void hfs_ext_write_extent(struct inode *inode) > > +int hfs_ext_write_extent(struct inode *inode) > > { > > =A0=A0=A0 struct hfs_find_data fd; > > +=A0=A0=A0 int res =3D 0; > >=20 > > =A0=A0=A0 if (HFS_I(inode)->flags & > HFS_FLG_EXT_DIRTY) { > > -=A0=A0=A0 =A0=A0=A0 > hfs_find_init(HFS_SB(inode->i_sb)->ext_tree, > &fd); > > -=A0=A0=A0 =A0=A0=A0 > __hfs_ext_write_extent(inode, &fd); > > +=A0=A0=A0 =A0=A0=A0 res =3D > hfs_find_init(HFS_SB(inode->i_sb)->ext_tree, > &fd); > > +=A0=A0=A0 =A0=A0=A0 if (res) > > +=A0=A0=A0 =A0=A0=A0 > =A0=A0=A0 return res; > > +=A0=A0=A0 =A0=A0=A0 res =3D > __hfs_ext_write_extent(inode, &fd); > > =A0=A0=A0 =A0=A0=A0 > hfs_find_exit(&fd); > > =A0=A0=A0 } > > +=A0=A0=A0 return res; > > } > >=20 > > static inline int __hfs_ext_read_extent(struct > hfs_find_data *fd, struct hfs_extent *extent, > > @@ -161,8 +166,11 @@ static inline int > __hfs_ext_cache_extent(struct hfs_find_data *fd, struct > inode > > { > > =A0=A0=A0 int res; > >=20 > > -=A0=A0=A0 if (HFS_I(inode)->flags & > HFS_FLG_EXT_DIRTY) > > -=A0=A0=A0 =A0=A0=A0 > __hfs_ext_write_extent(inode, fd); > > +=A0=A0=A0 if (HFS_I(inode)->flags & > HFS_FLG_EXT_DIRTY) { > > +=A0=A0=A0 =A0=A0=A0 res =3D > __hfs_ext_write_extent(inode, fd); > > +=A0=A0=A0 =A0=A0=A0 if (res) > > +=A0=A0=A0 =A0=A0=A0 > =A0=A0=A0 return res; > > +=A0=A0=A0 } > >=20 >=20 > Ditto for hfsplus. >=20 > Thanks, > Vyacheslav Dubeyko. >=20 > > =A0=A0=A0 res =3D __hfs_ext_read_extent(fd, > HFS_I(inode)->cached_extents, inode->i_ino, > > =A0=A0=A0 =A0=A0=A0 > =A0=A0=A0 =A0=A0=A0 =A0 =A0 block, > HFS_IS_RSRC(inode) ? HFS_FK_RSRC : HFS_FK_DATA); > > @@ -185,9 +193,11 @@ static int > hfs_ext_read_extent(struct inode *inode, u16 block) > > =A0=A0=A0 =A0 =A0 block < > HFS_I(inode)->cached_start + > HFS_I(inode)->cached_blocks) > > =A0=A0=A0 =A0=A0=A0 return 0; > >=20 > > -=A0=A0=A0 > hfs_find_init(HFS_SB(inode->i_sb)->ext_tree, > &fd); > > -=A0=A0=A0 res =3D > __hfs_ext_cache_extent(&fd, inode, block); > > -=A0=A0=A0 hfs_find_exit(&fd); > > +=A0=A0=A0 res =3D > hfs_find_init(HFS_SB(inode->i_sb)->ext_tree, > &fd); > > +=A0=A0=A0 if (!res) { > > +=A0=A0=A0 =A0=A0=A0 res =3D > __hfs_ext_cache_extent(&fd, inode, block); > > +=A0=A0=A0 =A0=A0=A0 > hfs_find_exit(&fd); > > +=A0=A0=A0 } > > =A0=A0=A0 return res; > > } > >=20 > > @@ -298,7 +308,9 @@ int hfs_free_fork(struct > super_block *sb, struct hfs_cat_file *file, int type) > > =A0=A0=A0 if (total_blocks =3D=3D blocks) > > =A0=A0=A0 =A0=A0=A0 return 0; > >=20 > > -=A0=A0=A0 > hfs_find_init(HFS_SB(sb)->ext_tree, &fd); > > +=A0=A0=A0 res =3D > hfs_find_init(HFS_SB(sb)->ext_tree, &fd); > > +=A0=A0=A0 if (res) > > +=A0=A0=A0 =A0=A0=A0 return res; > > =A0=A0=A0 do { > > =A0=A0=A0 =A0=A0=A0 res =3D > __hfs_ext_read_extent(&fd, extent, cnid, total_blocks, > type); > > =A0=A0=A0 =A0=A0=A0 if (res) > > @@ -438,7 +450,9 @@ out: > >=20 > > insert_extent: > > =A0=A0=A0 dprint(DBG_EXTENT, "insert new > extent\n"); > > -=A0=A0=A0 hfs_ext_write_extent(inode); > > +=A0=A0=A0 res =3D hfs_ext_write_extent(inode); > > +=A0=A0=A0 if (res) > > +=A0=A0=A0 =A0=A0=A0 goto out; > >=20 > > =A0=A0=A0 > memset(HFS_I(inode)->cached_extents, 0, > sizeof(hfs_extent_rec)); > > =A0=A0=A0 > HFS_I(inode)->cached_extents[0].block =3D > cpu_to_be16(start); > > @@ -466,7 +480,6 @@ void hfs_file_truncate(struct inode > *inode) > > =A0=A0=A0 =A0=A0=A0 struct > address_space *mapping =3D inode->i_mapping; > > =A0=A0=A0 =A0=A0=A0 void *fsdata; > > =A0=A0=A0 =A0=A0=A0 struct page > *page; > > -=A0=A0=A0 =A0=A0=A0 int res; > >=20 > > =A0=A0=A0 =A0=A0=A0 /* XXX: Can use > generic_cont_expand? */ > > =A0=A0=A0 =A0=A0=A0 size =3D > inode->i_size - 1; > > @@ -488,7 +501,12 @@ void hfs_file_truncate(struct > inode *inode) > > =A0=A0=A0 =A0=A0=A0 goto out; > >=20 > > =A0=A0=A0 > mutex_lock(&HFS_I(inode)->extents_lock); > > -=A0=A0=A0 > hfs_find_init(HFS_SB(sb)->ext_tree, &fd); > > +=A0=A0=A0 res =3D > hfs_find_init(HFS_SB(sb)->ext_tree, &fd); > > +=A0=A0=A0 if (res) { > > +=A0=A0=A0 =A0=A0=A0 > mutex_unlock(&HFS_I(inode)->extents_lock); > > +=A0=A0=A0 =A0=A0=A0 /* XXX: We lack > error handling of hfs_file_truncate() */ > > +=A0=A0=A0 =A0=A0=A0 return; > > +=A0=A0=A0 } > > =A0=A0=A0 while (1) { > > =A0=A0=A0 =A0=A0=A0 if (alloc_cnt =3D=3D > HFS_I(inode)->first_blocks) { > > =A0=A0=A0 =A0=A0=A0 > =A0=A0=A0 hfs_free_extents(sb, > HFS_I(inode)->first_extents, > > diff --git a/fs/hfs/hfs_fs.h b/fs/hfs/hfs_fs.h > > index 693df9f..67817af 100644 > > --- a/fs/hfs/hfs_fs.h > > +++ b/fs/hfs/hfs_fs.h > > @@ -174,7 +174,7 @@ extern const struct > inode_operations hfs_dir_inode_operations; > > /* extent.c */ > > extern int hfs_ext_keycmp(const btree_key *, const > btree_key *); > > extern int hfs_free_fork(struct super_block *, struct > hfs_cat_file *, int); > > -extern void hfs_ext_write_extent(struct inode *); > > +extern int hfs_ext_write_extent(struct inode *); > > extern int hfs_extend_file(struct inode *); > > extern void hfs_file_truncate(struct inode *); > >=20 > > diff --git a/fs/hfs/inode.c b/fs/hfs/inode.c > > index 3031dfd..8ff4b5a 100644 > > --- a/fs/hfs/inode.c > > +++ b/fs/hfs/inode.c > > @@ -416,9 +416,12 @@ int hfs_write_inode(struct inode > *inode, struct writeback_control *wbc) > > =A0=A0=A0 struct inode *main_inode =3D inode; > > =A0=A0=A0 struct hfs_find_data fd; > > =A0=A0=A0 hfs_cat_rec rec; > > +=A0=A0=A0 int res; > >=20 > > =A0=A0=A0 dprint(DBG_INODE, "hfs_write_inode: > %lu\n", inode->i_ino); > > -=A0=A0=A0 hfs_ext_write_extent(inode); > > +=A0=A0=A0 res =3D hfs_ext_write_extent(inode); > > +=A0=A0=A0 if (res) > > +=A0=A0=A0 =A0=A0=A0 return res; > >=20 > > =A0=A0=A0 if (inode->i_ino < > HFS_FIRSTUSER_CNID) { > > =A0=A0=A0 =A0=A0=A0 switch > (inode->i_ino) { > > @@ -515,7 +518,11 @@ static struct dentry > *hfs_file_lookup(struct inode *dir, struct dentry *dentry, > > =A0=A0=A0 if (!inode) > > =A0=A0=A0 =A0=A0=A0 return > ERR_PTR(-ENOMEM); > >=20 > > -=A0=A0=A0 > hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd); > > +=A0=A0=A0 res =3D > hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd); > > +=A0=A0=A0 if (res) { > > +=A0=A0=A0 =A0=A0=A0 iput(inode); > > +=A0=A0=A0 =A0=A0=A0 return > ERR_PTR(res); > > +=A0=A0=A0 } > > =A0=A0=A0 fd.search_key->cat =3D > HFS_I(dir)->cat_key; > > =A0=A0=A0 res =3D hfs_brec_read(&fd, > &rec, sizeof(rec)); > > =A0=A0=A0 if (!res) { > > diff --git a/fs/hfs/super.c b/fs/hfs/super.c > > index bbaaa8a..719760b 100644 > > --- a/fs/hfs/super.c > > +++ b/fs/hfs/super.c > > @@ -418,7 +418,9 @@ static int hfs_fill_super(struct > super_block *sb, void *data, int silent) > > =A0=A0=A0 } > >=20 > > =A0=A0=A0 /* try to get the root inode */ > > -=A0=A0=A0 > hfs_find_init(HFS_SB(sb)->cat_tree, &fd); > > +=A0=A0=A0 res =3D > hfs_find_init(HFS_SB(sb)->cat_tree, &fd); > > +=A0=A0=A0 if (res) > > +=A0=A0=A0 =A0=A0=A0 goto > bail_no_root; > > =A0=A0=A0 res =3D hfs_cat_find_brec(sb, > HFS_ROOT_CNID, &fd); > > =A0=A0=A0 if (!res) { > > =A0=A0=A0 =A0=A0=A0 if > (fd.entrylength > sizeof(rec) || fd.entrylength < 0) > { > > --=20 > > 1.7.9.5 > >=20 > > -- > > To unsubscribe from this list: send the line > "unsubscribe linux-kernel" in > > the body of a message to majordomo@vger.kernel.org > > More majordomo info at=A0 http://vger.kernel.org/majordomo-info.htm= l > > Please read the FAQ at=A0 http://www.tux.org/lkml/ >=20 >=20