From mboxrd@z Thu Jan 1 00:00:00 1970 From: walter harms Date: Wed, 30 Jan 2013 13:00:01 +0000 Subject: Re: [patch] [SCSI] libosd: check for kzalloc() failure Message-Id: <51091951.5090709@bfs.de> List-Id: References: <20130130070602.GB12396@elgon.mountain> <5108D6AF.5040205@bfs.de> <20130130082741.GT16282@mwanda> <5108E08E.70302@bfs.de> In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: Benny Halevy Cc: Dan Carpenter , Boaz Harrosh , "James E.J. Bottomley" , osd-dev@open-osd.org, linux-scsi@vger.kernel.org, kernel-janitors@vger.kernel.org Am 30.01.2013 10:51, schrieb Benny Halevy: > On Wed, Jan 30, 2013 at 10:57 AM, walter harms wrote: >> >> >> Am 30.01.2013 09:27, schrieb Dan Carpenter: >>> On Wed, Jan 30, 2013 at 09:15:43AM +0100, walter harms wrote: >>>> >>>> >>>> Am 30.01.2013 08:06, schrieb Dan Carpenter: >>>>> There wasn't any error handling for this kzalloc(). >>>>> >>>>> Signed-off-by: Dan Carpenter >>>>> >>>>> diff --git a/drivers/scsi/osd/osd_initiator.c b/drivers/scsi/osd/osd_initiator.c >>>>> index c06b8e5..d8293f2 100644 >>>>> --- a/drivers/scsi/osd/osd_initiator.c >>>>> +++ b/drivers/scsi/osd/osd_initiator.c >>>>> @@ -144,6 +144,10 @@ static int _osd_get_print_system_info(struct osd_dev *od, >>>>> odi->osdname_len = get_attrs[a].len; >>>>> /* Avoid NULL for memcmp optimization 0-length is good enough */ >>>>> odi->osdname = kzalloc(odi->osdname_len + 1, GFP_KERNEL); >>>>> + if (!odi->osdname) { >>>>> + ret = -ENOMEM; >>>>> + goto out; >>>>> + } >>>>> if (odi->osdname_len) >>>>> memcpy(odi->osdname, get_attrs[a].val_ptr, odi->osdname_len); >>>>> OSD_INFO("OSD_NAME [%s]\n", odi->osdname); >>>>> -- >>>> >>>> this looks like strdup() ? >>>> >>> >>> Maybe? It's a funny thing going on with the NUL terminator and I >>> don't understand what the comment is about. >>> >>> It appears that normally "get_attrs[a].val_ptr" is a NUL terminated >>> string but "get_attrs[a].len" does not count the terminator. >>> >>> Odd. >>> >> i have no clue what the programmer was thinking. if i read this correct >> osdname is u8 *osdname; so a simple strdup() or strndup() would be ok >> the comment seems to indicate that get_attrs[a].val_ptr could be NULL >> but where is the check ? >> Perhaps they are not using ascii here ? then a memdup(get_attrs[a].len) >> would be better. > > There are subtle differences between kstrdup or kmemdup and this implementation. > > kmemdup is problematic as get_attrs[a].val_ptr does not contain the > NUL terminator ok, i understand - but can we assume that we are talking ascii here ? > In the following case: > if get_attrs[a].len = 0 > then get_attrs[a].val_ptr = NULL > > The end result should be > odi->osdname_len = 0 > odi->osdname = kzalloc(1, GFP_KERNEL); > > while with kstrdup, odi->osdname will end up being NULL > as it's input arg "s" is NULL. > and you want the argument to be "" ? May i ask why ? kfree() can handle NULL and kprintf() (-family) also. re, wh > Benny > >> >> re, >> wh >> >> > > From mboxrd@z Thu Jan 1 00:00:00 1970 From: walter harms Subject: Re: [patch] [SCSI] libosd: check for kzalloc() failure Date: Wed, 30 Jan 2013 14:00:01 +0100 Message-ID: <51091951.5090709@bfs.de> References: <20130130070602.GB12396@elgon.mountain> <5108D6AF.5040205@bfs.de> <20130130082741.GT16282@mwanda> <5108E08E.70302@bfs.de> Reply-To: wharms@bfs.de Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Return-path: Received: from mx01.sz.bfs.de ([194.94.69.103]:61261 "EHLO mx01.sz.bfs.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753325Ab3A3NAE (ORCPT ); Wed, 30 Jan 2013 08:00:04 -0500 In-Reply-To: Sender: linux-scsi-owner@vger.kernel.org List-Id: linux-scsi@vger.kernel.org To: Benny Halevy Cc: Dan Carpenter , Boaz Harrosh , "James E.J. Bottomley" , osd-dev@open-osd.org, linux-scsi@vger.kernel.org, kernel-janitors@vger.kernel.org Am 30.01.2013 10:51, schrieb Benny Halevy: > On Wed, Jan 30, 2013 at 10:57 AM, walter harms wrote: >> >> >> Am 30.01.2013 09:27, schrieb Dan Carpenter: >>> On Wed, Jan 30, 2013 at 09:15:43AM +0100, walter harms wrote: >>>> >>>> >>>> Am 30.01.2013 08:06, schrieb Dan Carpenter: >>>>> There wasn't any error handling for this kzalloc(). >>>>> >>>>> Signed-off-by: Dan Carpenter >>>>> >>>>> diff --git a/drivers/scsi/osd/osd_initiator.c b/drivers/scsi/osd/osd_initiator.c >>>>> index c06b8e5..d8293f2 100644 >>>>> --- a/drivers/scsi/osd/osd_initiator.c >>>>> +++ b/drivers/scsi/osd/osd_initiator.c >>>>> @@ -144,6 +144,10 @@ static int _osd_get_print_system_info(struct osd_dev *od, >>>>> odi->osdname_len = get_attrs[a].len; >>>>> /* Avoid NULL for memcmp optimization 0-length is good enough */ >>>>> odi->osdname = kzalloc(odi->osdname_len + 1, GFP_KERNEL); >>>>> + if (!odi->osdname) { >>>>> + ret = -ENOMEM; >>>>> + goto out; >>>>> + } >>>>> if (odi->osdname_len) >>>>> memcpy(odi->osdname, get_attrs[a].val_ptr, odi->osdname_len); >>>>> OSD_INFO("OSD_NAME [%s]\n", odi->osdname); >>>>> -- >>>> >>>> this looks like strdup() ? >>>> >>> >>> Maybe? It's a funny thing going on with the NUL terminator and I >>> don't understand what the comment is about. >>> >>> It appears that normally "get_attrs[a].val_ptr" is a NUL terminated >>> string but "get_attrs[a].len" does not count the terminator. >>> >>> Odd. >>> >> i have no clue what the programmer was thinking. if i read this correct >> osdname is u8 *osdname; so a simple strdup() or strndup() would be ok >> the comment seems to indicate that get_attrs[a].val_ptr could be NULL >> but where is the check ? >> Perhaps they are not using ascii here ? then a memdup(get_attrs[a].len) >> would be better. > > There are subtle differences between kstrdup or kmemdup and this implementation. > > kmemdup is problematic as get_attrs[a].val_ptr does not contain the > NUL terminator ok, i understand - but can we assume that we are talking ascii here ? > In the following case: > if get_attrs[a].len == 0 > then get_attrs[a].val_ptr == NULL > > The end result should be > odi->osdname_len = 0 > odi->osdname = kzalloc(1, GFP_KERNEL); > > while with kstrdup, odi->osdname will end up being NULL > as it's input arg "s" is NULL. > and you want the argument to be "" ? May i ask why ? kfree() can handle NULL and kprintf() (-family) also. re, wh > Benny > >> >> re, >> wh >> >> > >