From: Hin-Tak Leung Subject: Re: [PATCH V3] hfsplus: fixes worst-case unicode to char conversion of file names and attributes Date: Thu, 10 Apr 2014 14:39:08 +0100 (BST) Message-ID: <1397137148.96163.YahooMailBasic@web172301.mail.ir2.yahoo.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: linux-fsdevel@vger.kernel.org, akpm@linux-foundation.org, aia21@cam.ac.uk, viro@zeniv.linux.org.uk, hch@infradead.org To: slava@dubeyko.com Return-path: Received: from nm35-vm8.bullet.mail.ir2.yahoo.com ([212.82.97.131]:34542 "EHLO nm35-vm8.bullet.mail.ir2.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1030546AbaDJNjM convert rfc822-to-8bit (ORCPT ); Thu, 10 Apr 2014 09:39:12 -0400 Sender: linux-fsdevel-owner@vger.kernel.org List-ID: ------------------------------ On Thu, Apr 10, 2014 8:30 AM BST Vyacheslav Dubeyko wrote: >On Wed, 2014-04-09 at 22:53 +0100, Hin-Tak Leung wrote: > >[snip] >> @@ -127,7 +128,7 @@ static int hfsplus_readdir(struct file *file, st= ruct dir_context *ctx) >>=A0 =A0=A0=A0 struct inode *inode =3D file_inode(file); >>=A0 =A0=A0=A0 struct super_block *sb =3D inode->i_sb; >>=A0 =A0=A0=A0 int len, err; >> -=A0=A0=A0 char strbuf[HFSPLUS_MAX_STRLEN + 1]; >> +=A0=A0=A0 char *strbuf; >>=A0 =A0=A0=A0 hfsplus_cat_entry entry; >>=A0 =A0=A0=A0 struct hfs_find_data fd; >>=A0 =A0=A0=A0 struct hfsplus_readdir_data *rd; >> @@ -139,6 +140,11 @@ static int hfsplus_readdir(struct file *file, s= truct dir_context *ctx) >>=A0 =A0=A0=A0 err =3D hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd); >>=A0 =A0=A0=A0 if (err) >>=A0 =A0=A0=A0 =A0=A0=A0 return err; >> +=A0=A0=A0 strbuf =3D kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_MAX_STR= LEN + 1, GFP_KERNEL); > >I think that if you use kzalloc for second case then it makes sense to >use kzalloc and here. Anyway, kzalloc can be much more safe way, from = my >viewpoint. > The difference is simply to avoid unforseeable regression - the stack b= uffer for names wasn't null terminated before the switch to dynamic allocation, therefo= re it is switch to plain kmalloc; but the xattr one is null-terminated at = initialization, so I used kzalloc when switching, just to be on the safe side. It is reall= y just about "changing like to like", avoiding unforseeable regression. I can also see in the latter case, it is being used subsequently in str= cpy, so null-terminate is probably important. Whereas the name is being pass= ed around with its length. I think Anton has a point there - it might be more efficient to do kmal= loc, and if necessary, a single assignment to the first byte only, rather than call= ing kzalloc (which we are doing, BTW, 127 x 6 ~ over 750 bytes). I am hoping kzallo= c is cleverer and does say, blocks of 4 bytes or even 64 at one go, which= won't suck too much, compared to just one byte assignment. I wish there is an intermed= iate between kmalloc and kzalloc where it zero's only the first byte. I hope this answer the question. Perhaps kzalloc is not needed, but since the code started with "strbuf[size] =3D {0};", it is safer to swi= tch to kzalloc without making the change too ugly. I mean "safer" in the sense of "without causing unforseeable regressions", *not* safer in the general philosophical sen= se of=20 "zero'ed buffers always better than non-zero'ed". Hin-Tak >[snip] > >> diff --git a/fs/hfsplus/xattr.c b/fs/hfsplus/xattr.c >> index 4e27edc..3034ce6 100644 >> --- a/fs/hfsplus/xattr.c >> +++ b/fs/hfsplus/xattr.c >> @@ -8,6 +8,7 @@ >>=A0=20 > >>=A0=20 >> +=A0=A0=A0 strbuf =3D kzalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MA= X_STRLEN + >> +=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 XATTR_MAC_OSX_PREFIX_LEN + 1, GFP_KER= NEL); >> +=A0=A0=A0 if (!strbuf) { >> +=A0=A0=A0 =A0=A0=A0 res =3D -ENOMEM; >> +=A0=A0=A0 =A0=A0=A0 goto out; >> +=A0=A0=A0 } >> + > >Thanks, >Vyacheslav Dubeyko. > > -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel= " in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html