Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1831179pxb; Sat, 27 Feb 2021 01:24:25 -0800 (PST) X-Google-Smtp-Source: ABdhPJyy6B/lZ10Q6rDArFvCY1QSL6nlVKUULuHHybnaQ+saHLRaPI72mZtsJauZohGObEt3CSeo X-Received: by 2002:a17:906:c1cf:: with SMTP id bw15mr7551470ejb.82.1614417865596; Sat, 27 Feb 2021 01:24:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614417865; cv=none; d=google.com; s=arc-20160816; b=CTvl7fOIfoWpbjBmjPeCQnio2eDr0nRBEecsuB/GgtW2qNCajFoyvl57+/KTOVJXo4 4TzUO83keinAEiOtaIf4pcmgnOzryTIrLnrAY4syuA/wHFSyvCsWHQJYoUwzmIuRPrRZ QXYH8srhfpIAjEWWui1KR33UkFugUftRYCc6FvXjm3IBIdvsmixSnWfleFlcYmAC+Xxb dir0O05KV5FAp4r2WydBDS7J9ka3n697n0ojC4SwqX7+FKTkWS3e9NP336qioH1ETgUI 6BTOQs+qHIeld7z1HdgP+5HT5RpdinyP+3t5GEklMl6OOTX4aZwsNAZvchODcR4msSNp X5IQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=rdsugCeqAskEx0jkSOK5RZlvq0xfx1TzakA+Tg2/xCQ=; b=MDerQzh4FfZb8n7Mp/otcJi150ZnYHzXButB6MA2glcMlyGCR8L3UxzjqU+3rx0bnk lpp9nMrntx4lEpJHxrql02O3fCK33CkLdAXkoEXVGi88jhzql5NaCy+2bPdStSLjtsWv ZG1j/cuhxW7uV/RZ8EhGFRvMqK3ldmVMAXQXZ/uTIgrfGL16K+/W4RHVIRTkmoEi7Ps3 ei6/TNy5uoochh5bkLX04pukzgEih16Ds4ze944WyKIkOgiTQjRvOa9a8aMgXwK2k9Ai YZZDHG62lHe+OxhUAekULu5ffoXXjDu8FpA2uvpR/NHh/3rhlVDhpLLCy2jHX6VTY1bP 53BA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c17si7161835edv.580.2021.02.27.01.24.03; Sat, 27 Feb 2021 01:24:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229990AbhB0JVT (ORCPT + 99 others); Sat, 27 Feb 2021 04:21:19 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:12587 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229864AbhB0JVT (ORCPT ); Sat, 27 Feb 2021 04:21:19 -0500 Received: from DGGEMS406-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4DngtY1HNVzMf1V; Sat, 27 Feb 2021 17:18:29 +0800 (CST) Received: from [127.0.0.1] (10.40.192.131) by DGGEMS406-HUB.china.huawei.com (10.3.19.206) with Microsoft SMTP Server id 14.3.498.0; Sat, 27 Feb 2021 17:20:24 +0800 Subject: Re: [Linuxarm] [PATCH v1] drm/nouveau/device: append a NUL-terminated character for the string which filled by strncpy() To: "Song Bao Hua (Barry Song)" , "nouveau@lists.freedesktop.org" , "dri-devel@lists.freedesktop.org" , "bskeggs@redhat.com" CC: "linux-kernel@vger.kernel.org" , "linuxarm@openeuler.org" References: <1614253132-21793-1-git-send-email-luojiaxing@huawei.com> <1b841f487ad742ee941282b534bdcb4d@hisilicon.com> From: luojiaxing Message-ID: <27a28507-f394-43af-998e-6d03a971b85d@huawei.com> Date: Sat, 27 Feb 2021 17:20:24 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: <1b841f487ad742ee941282b534bdcb4d@hisilicon.com> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Originating-IP: [10.40.192.131] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021/2/26 9:01, Song Bao Hua (Barry Song) wrote: > >> -----Original Message----- >> From: Luo Jiaxing [mailto:luojiaxing@huawei.com] >> Sent: Friday, February 26, 2021 12:39 AM >> To: nouveau@lists.freedesktop.org; dri-devel@lists.freedesktop.org; >> bskeggs@redhat.com >> Cc: linux-kernel@vger.kernel.org; linuxarm@openeuler.org; luojiaxing >> >> Subject: [Linuxarm] [PATCH v1] drm/nouveau/device: append a NUL-terminated >> character for the string which filled by strncpy() >> >> Following warning is found when using W=1 to build kernel: >> >> In function ‘nvkm_udevice_info’, >> inlined from ‘nvkm_udevice_mthd’ at >> drivers/gpu/drm/nouveau/nvkm/engine/device/user.c:195:10: >> drivers/gpu/drm/nouveau/nvkm/engine/device/user.c:164:2: warning: ‘strncpy’ >> specified bound 16 equals destination size [-Wstringop-truncation] >> 164 | strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip)); >> drivers/gpu/drm/nouveau/nvkm/engine/device/user.c:165:2: warning: ‘strncpy’ >> specified bound 64 equals destination size [-Wstringop-truncation] >> 165 | strncpy(args->v0.name, device->name, sizeof(args->v0.name)); >> >> The reason of this warning is strncpy() does not guarantee that the >> destination buffer will be NUL terminated. If the length of source string >> is bigger than number we set by third input parameter, only first [number] >> of characters is copied to the destination, and no NUL-terminated is >> automatically added. There are some potential risks. >> >> Signed-off-by: Luo Jiaxing >> --- >> drivers/gpu/drm/nouveau/nvkm/engine/device/user.c | 6 ++++-- >> 1 file changed, 4 insertions(+), 2 deletions(-) >> >> diff --git a/drivers/gpu/drm/nouveau/nvkm/engine/device/user.c >> b/drivers/gpu/drm/nouveau/nvkm/engine/device/user.c >> index fea9d8f..2a32fe0 100644 >> --- a/drivers/gpu/drm/nouveau/nvkm/engine/device/user.c >> +++ b/drivers/gpu/drm/nouveau/nvkm/engine/device/user.c >> @@ -161,8 +161,10 @@ nvkm_udevice_info(struct nvkm_udevice *udev, void *data, >> u32 size) >> if (imem && args->v0.ram_size > 0) >> args->v0.ram_user = args->v0.ram_user - imem->reserved; >> >> - strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip)); >> - strncpy(args->v0.name, device->name, sizeof(args->v0.name)); >> + strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip) - 1); >> + args->v0.chip[sizeof(args->v0.chip) - 1] = '\0'; >> + strncpy(args->v0.name, device->name, sizeof(args->v0.name) - 1); >> + args->v0.name[sizeof(args->v0.name) - 1] = '\0'; > > Isn't it better to use snprintf()? yes, you are right,  snprintf() is better. Most of drivers use snprintf() to format a string, but still some examples in kernel that use it for copy. I modify to code to the follow and I think it's the same with strncpy but more safety diff --git a/drivers/gpu/drm/nouveau/nvkm/engine/device/user.c b/drivers/gpu/drm/nouveau/nvkm/engine/device/user.c index fea9d8f..4bf65bb 100644 --- a/drivers/gpu/drm/nouveau/nvkm/engine/device/user.c +++ b/drivers/gpu/drm/nouveau/nvkm/engine/device/user.c @@ -161,8 +161,8 @@ nvkm_udevice_info(struct nvkm_udevice *udev, void *data, u32 size)         if (imem && args->v0.ram_size > 0)                 args->v0.ram_user = args->v0.ram_user - imem->reserved; -       strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip)); -       strncpy(args->v0.name, device->name, sizeof(args->v0.name)); +       snprintf(args->v0.chip, sizeof(args->v0.chip), "%s", device->chip->name); +       snprintf(args->v0.name, sizeof(args->v0.name), "%s", device->name); Thanks Jiaxing > >> return 0; >> } >> > Thanks > Barry >