[dpdk-dev] [PATCH] examples/vhost_scsi: fix buffer not terminated

Maxime Coquelin maxime.coquelin at redhat.com
Thu Oct 5 15:02:15 CEST 2017



On 10/05/2017 03:01 PM, Bruce Richardson wrote:
> On Thu, Oct 05, 2017 at 02:42:24PM +0200, Maxime Coquelin wrote:
>>
>>
>> On 10/05/2017 02:35 PM, Piasecki, JacekX wrote:
>>> -----Original Message-----
>>> From: Maxime Coquelin [mailto:maxime.coquelin at redhat.com]
>>> Sent: Monday, 2 October, 2017 17:07
>>> To: Jastrzebski, MichalX K <michalx.k.jastrzebski at intel.com>; yliu at fridaylinux.org
>>> Cc: dev at dpdk.org; Jain, Deepak K <deepak.k.jain at intel.com>; Piasecki, JacekX <jacekx.piasecki at intel.com>; Liu, Changpeng <changpeng.liu at intel.com>; stable at dpdk.org
>>> Subject: Re: [dpdk-dev] [PATCH] examples/vhost_scsi: fix buffer not terminated
>>>
>>>
>>>
>>> On 10/02/2017 03:50 PM, Jastrzebski, MichalX K wrote:
>>>>> -----Original Message-----
>>>>> From: dev [mailto:dev-bounces at dpdk.org] On Behalf Of Michal
>>>>> Jastrzebski
>>>>> Sent: Friday, September 22, 2017 3:08 PM
>>>>> To: yliu at fridaylinux.org; maxime.coquelin at redhat.com
>>>>> Cc: dev at dpdk.org; Jain, Deepak K <deepak.k.jain at intel.com>; Piasecki,
>>>>> JacekX <jacekx.piasecki at intel.com>; Liu, Changpeng
>>>>> <changpeng.liu at intel.com>; stable at dpdk.org
>>>>> Subject: [dpdk-dev] [PATCH] examples/vhost_scsi: fix buffer not
>>>>> terminated
>>>>>
>>>>> From: Jacek Piasecki <jacekx.piasecki at intel.com>
>>>>>
>>>>> Fix size of buffer in strcpy. There was possible to get not
>>>>> terminated string after copy operation.
>>>>>
>>>>> Coverity issue: 158631
>>>>> Fixes: db75c7af19bb ("examples/vhost_scsi: introduce a new sample
>>>>> app")
>>>>> Cc: changpeng.liu at intel.com
>>>>> Cc: stable at dpdk.org
>>>>>
>>>>> Signed-off-by: Jacek Piasecki <jacekx.piasecki at intel.com>
>>>>> ---
>>>>>     examples/vhost_scsi/scsi.c | 3 ++-
>>>>>     1 file changed, 2 insertions(+), 1 deletion(-)
>>>>>
>>>>> diff --git a/examples/vhost_scsi/scsi.c b/examples/vhost_scsi/scsi.c
>>>>> index 54d3104..de9639a 100644
>>>>> --- a/examples/vhost_scsi/scsi.c
>>>>> +++ b/examples/vhost_scsi/scsi.c
>>>>> @@ -307,7 +307,8 @@
>>>>>     		strncpy((char *)inqdata->t10_vendor_id, "INTEL", 8);
>>>>>
>>>>>     		/* PRODUCT IDENTIFICATION */
>>>>> -		strncpy((char *)inqdata->product_id, bdev->product_name,
>>>>> 16);
>>>>> +		strncpy((char *)inqdata->product_id, bdev->product_name,
>>>>> +				ARRAY_SIZE(inqdata->product_id) - 1);
>>>
>>> Does it assume that product_id is memzero'ed before?
>>> IIUC strncpy manpage, it wouldn't protect against non-null terminated strings if it is not the case:
>>>
>>> Yes, I assumed that this area is zeroed before strncpy. Earlier there is a casting from *task to *inqdata (*task is zmalloced).
>> Ok. I think the assumption is dangerous.
>>
>>> To be sure that the destination area is clear I propose to add:
>>> memset(inqdata->product_id, 0, ARRAY_SIZE(inqdata->product_id));
>>> before strncpy. Would that be OK?
>>
>> Or call strncpy with ARRAY_SIZE(inqdata->product_id) as max length,
>> and then do
>> inqdata->product_id[ARRAY_SIZE(inqdata->product_id) - 1] = '\0';
>>
>> Regards,
>> Maxime
>>>
> Or use snprintf instead of strncpy, as we do in many places in DPDK.
> Still only one line of code, with nice null-termination, and truncation
> semantics.

+1

> /Bruce
> 


More information about the dev mailing list