From: Hin-Tak Leung Subject: Re: [PATCH V3] hfsplus: fixes worst-case unicode to char conversion of file names and attributes Date: Fri, 11 Apr 2014 00:32:18 +0100 (BST) Message-ID: <1397172738.35853.YahooMailBasic@web172304.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, aia21@cam.ac.uk, viro@zeniv.linux.org.uk, hch@infradead.org, slava@dubeyko.com To: akpm@linux-foundation.org Return-path: Received: from nm36-vm5.bullet.mail.ir2.yahoo.com ([212.82.97.137]:20610 "EHLO nm36-vm5.bullet.mail.ir2.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1030615AbaDJXcU convert rfc822-to-8bit (ORCPT ); Thu, 10 Apr 2014 19:32:20 -0400 Sender: linux-fsdevel-owner@vger.kernel.org List-ID: Hi Andrew, (and others) I think I want to pull back this, and submit a V4 later. After looking at the code carefully, and continue on fixing the rest of the issues with setting/getting non-English attributes, I think ther= e is no need to do kzalloc(), and kmalloc is sufficient - the buffer is always used with its length. The original stack allocation, doing null termination on init (and othe= r similiar uses elsewhere in the extended attribute code) is too conservative. Many of the uses overwrites it with a static string, so the null termin= ation on=20 init doesn't do anything useful. It looks a bit obvious that's the case when switching to dynamic alloca= tion - and allocate it as late as possible, just before use. Hin-Tak ------------------------------ On Thu, Apr 10, 2014 2:39 PM BST Hin-Tak Leung wrote: >------------------------------ >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 t= o >>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 = buffer for names >wasn't null terminated before the switch to dynamic allocation, theref= ore >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 real= ly just about >"changing like to like", avoiding unforseeable regression. > >I can also see in the latter case, it is being used subsequently in st= rcpy, >so null-terminate is probably important. Whereas the name is being pas= sed >around with its length. > >I think Anton has a point there - it might be more efficient to do kma= lloc, and if >necessary, a single assignment to the first byte only, rather than cal= ling kzalloc >(which we are doing, BTW, 127 x 6 ~ over 750 bytes). I am hoping kzall= oc >is cleverer and does say, blocks of 4 bytes or even 64 at one go, whic= h won't suck too >much, compared to just one byte assignment. I wish there is an interme= diate >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 sw= itch 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 se= nse 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