From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.0 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AB91DC433DF for ; Sun, 9 Aug 2020 03:40:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 86E6A206B6 for ; Sun, 9 Aug 2020 03:40:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726360AbgHIDfz (ORCPT ); Sat, 8 Aug 2020 23:35:55 -0400 Received: from szxga08-in.huawei.com ([45.249.212.255]:34524 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726050AbgHIDfz (ORCPT ); Sat, 8 Aug 2020 23:35:55 -0400 Received: from dggeme758-chm.china.huawei.com (unknown [172.30.72.53]) by Forcepoint Email with ESMTP id DB4FF6E2EFDF04FC2EF2; Sun, 9 Aug 2020 11:35:51 +0800 (CST) Received: from [10.174.61.242] (10.174.61.242) by dggeme758-chm.china.huawei.com (10.3.19.104) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1913.5; Sun, 9 Aug 2020 11:35:51 +0800 Subject: Re: [PATCH net-next v1] hinic: fix strncpy output truncated compile warnings To: David Laight , "davem@davemloft.net" CC: "linux-kernel@vger.kernel.org" , "netdev@vger.kernel.org" , "luoxianjun@huawei.com" , "yin.yinshi@huawei.com" , "cloud.wangxiaoyun@huawei.com" , "chiqijun@huawei.com" References: <20200807020914.3123-1-luobin9@huawei.com> <89ee89b8c4a54a848aa57a31c5f5c81a@AcuMS.aculab.com> From: "luobin (L)" Message-ID: <20195cff-6a5c-5e75-d337-64cadd714f07@huawei.com> Date: Sun, 9 Aug 2020 11:35:50 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.1.1 MIME-Version: 1.0 In-Reply-To: <89ee89b8c4a54a848aa57a31c5f5c81a@AcuMS.aculab.com> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.61.242] X-ClientProxiedBy: dggeme701-chm.china.huawei.com (10.1.199.97) To dggeme758-chm.china.huawei.com (10.3.19.104) X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020/8/8 20:50, David Laight wrote: > From: luobin (L) >> Sent: 08 August 2020 04:37 >> >> On 2020/8/7 17:32, David Laight wrote: >>> From: Luo bin >>>> Sent: 07 August 2020 03:09 >>>> >>>> fix the compile warnings of 'strncpy' output truncated before >>>> terminating nul copying N bytes from a string of the same length >>>> >>>> Signed-off-by: Luo bin >>>> Reported-by: kernel test robot >>>> --- >>>> V0~V1: >>>> - use the strlen()+1 pattern consistently >>>> >>>> drivers/net/ethernet/huawei/hinic/hinic_devlink.c | 8 ++++---- >>>> 1 file changed, 4 insertions(+), 4 deletions(-) >>>> >>>> diff --git a/drivers/net/ethernet/huawei/hinic/hinic_devlink.c >>>> b/drivers/net/ethernet/huawei/hinic/hinic_devlink.c >>>> index c6adc776f3c8..1ec88ebf81d6 100644 >>>> --- a/drivers/net/ethernet/huawei/hinic/hinic_devlink.c >>>> +++ b/drivers/net/ethernet/huawei/hinic/hinic_devlink.c >>>> @@ -342,9 +342,9 @@ static int chip_fault_show(struct devlink_fmsg *fmsg, >>>> >>>> level = event->event.chip.err_level; >>>> if (level < FAULT_LEVEL_MAX) >>>> - strncpy(level_str, fault_level[level], strlen(fault_level[level])); >>>> + strncpy(level_str, fault_level[level], strlen(fault_level[level]) + 1); >>> >>> Have you even considered what that code is actually doing? >>> >>> David >> >> I'm sorry that I haven't got what you mean and I haven't found any defects in that code. Can you >> explain more to me? > > If you can't see it you probably shouldn't be submitting patches.... > > Consider what happens when the string is long. > > David > > - > Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK > Registration No: 1397386 (Wales) > Thanks for your explanation and review. The fault_level[level] is a fixed and NUL-terminated character string in that code and its length is smaller than the dest buffer size so I think using strlen(fault_level[level]) + 1 will not overflow the destination buffer. But using strncpy() on NUL-terminated strings is dangerous indeed and there is totally no need to use it in that code as Kees points out.