Received: by 2002:ac8:5491:0:b0:40f:fb00:664b with SMTP id h17csp642040qtq; Thu, 10 Aug 2023 11:24:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEMvXzZfnu0qpIpJ9I20dCH7m6thHDxgZf2ScN3y1Kmm2ToJYtwYVwrTEZvRw1v3eljwvPy X-Received: by 2002:a17:903:2308:b0:1bb:2093:efb1 with SMTP id d8-20020a170903230800b001bb2093efb1mr3459857plh.27.1691691844993; Thu, 10 Aug 2023 11:24:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691691844; cv=none; d=google.com; s=arc-20160816; b=hcgReN83WIU5IXlJ3aQlT0Eg3vxne/vCN0yk0dpetpgQXDLETgMvUWoSf4pu4XQ5gb gi1FJ4YZKlHI2YGAZjawFjPeVBAQoYxjXWoZcV/hLXzXX8S760/wxwj2606w37swXkKl hm+cRlwqMVE30HBetsomoCFkPLt3vAXhAcoj5nF6QoVxleXHg0cuViQmT1ENiHD1iG4I 4rgZ/mowjbByr/Gws5HFQUf+pIEP55t5BACrrnaNCtBrPmc70taarFXL+kH6dOSL6xQY oBGhcMHl4h6CqFAUAa+CnQaYwH0vkhpIiKyFxKNcCSR0Yr8p8Uk5BjmBtVYPg1piVT9s fJ2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=ft1MbYK4uKTQw07h/fxKTwcCm14Lwt/2FvLjZf6b8bo=; fh=hIAi4Kz9bkUGBSd+7xvLPo48xA4QHUaALcs7LFpBZJg=; b=rCFgTBSP1heUQP7nRddoWCXWlKfiL/k2MgUAH4zB20Wl1N5HxEr1uiNF4N/7X1IfkZ YhkVHKHzVloJzoy1DYUc/jH017oqPTtnFIswwxkk7B1n+5Pygrb1NohS1Yfbv/+sBAPP J1wMuqArHKU/Xzv+Fl4h7onG9WYTLTlY6UQbRLbFS7g5M98KYK/lCm1Q8kj/gUzhkbCI 2ZhBxJyGxRNIUNf2PqK/0tHJ5Dk4sbGLKGdlA/5zjCiyOL/RpN/5Q84WCw3sRE6HGlI2 f3089nxGzKyLpH5v1xgDBAsUBYV4XJmg6JSWuO4xop9j5G5v02/NpZA9qZO8BtqPbOFv ku2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=p8M0sFbk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e6-20020a170902cf4600b001bda57935f8si1276157plg.563.2023.08.10.11.23.53; Thu, 10 Aug 2023 11:24:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=p8M0sFbk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235229AbjHJRWx (ORCPT + 99 others); Thu, 10 Aug 2023 13:22:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235089AbjHJRWw (ORCPT ); Thu, 10 Aug 2023 13:22:52 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E0EA270A; Thu, 10 Aug 2023 10:22:50 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C97BC6649A; Thu, 10 Aug 2023 17:22:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A7251C433C7; Thu, 10 Aug 2023 17:22:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1691688169; bh=/pIjiu0uvnZYpRPBi219eWRbEc6h/Stq3OzO7FTbMhw=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=p8M0sFbkMVGd8rEicSBj1VPTL8iPWlIZ/WH/0r3GP4VxwmQhlMYA0jH9CRduGYpYn auKAoeHFMxVSPaOEedOQwKOvnAJz3AvCV6gWY96XNsqu6pE2E/3cMyGQrMT7XkE0PG G/8cz+QGRiwU14HPSqxeYbMaHZMjToq6h7Pn8TajPxLQlxanuWM2Cx2VAKS0RxeG7Y rn53ecsFphbd2oRKZaFqxcRFK7TO+rLWhUendWQ50gy+S5aymabhOMmBX27aa1sJqQ tZzuYJ/6v4jO6UVKQMkbTS9EVeVGXY9EeWGvgCb3YszFnLaAIEIw665yE1y26irbWM NzgsdUn9PIG8w== Date: Thu, 10 Aug 2023 10:22:47 -0700 From: Jakub Kicinski To: Jijie Shao , Kees Cook Cc: Leon Romanovsky , , , , , , , , , , , , Subject: Re: [PATCH net] net: hns3: fix strscpy causing content truncation issue Message-ID: <20230810102247.699ddc14@kernel.org> In-Reply-To: <7c44c161-9c86-8c60-f031-6d77d6c28c20@huawei.com> References: <20230809020902.1941471-1-shaojijie@huawei.com> <20230809070302.GR94631@unreal> <7c44c161-9c86-8c60-f031-6d77d6c28c20@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 10 Aug 2023 15:45:50 +0800 Jijie Shao wrote: > on 2023/8/9 15:03, Leon Romanovsky wrote: > > On Wed, Aug 09, 2023 at 10:09:02AM +0800, Jijie Shao wrote: =20 > >> From: Hao Chen > >> > >> hns3_dbg_fill_content()/hclge_dbg_fill_content() is aim to integrate s= ome > >> items to a string for content, and we add '\n' and '\0' in the last > >> two bytes of content. > >> > >> strscpy() will add '\0' in the last byte of destination buffer(one of > >> items), it result in finishing content print ahead of schedule and some > >> dump content truncation. > >> > >> One Error log shows as below: > >> cat mac_list/uc > >> UC MAC_LIST: > >> > >> Expected: > >> UC MAC_LIST: > >> FUNC_ID MAC_ADDR STATE > >> pf 00:2b:19:05:03:00 ACTIVE > >> > >> The destination buffer is length-bounded and not required to be > >> NUL-terminated, so just change strscpy() to memcpy() to fix it. =20 > > I think that you should change to strtomem() and not use plain memcpy(). > > > > Thanks =20 >=20 > Hi: >=20 > We tried to replace memcpy with strtomem, but errors was reported during= =20 > compilation: > /kernel/drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c: In=20 > function =E2=80=98hclge_dbg_fill_content.part.0=E2=80=99: > /kernel/include/linux/compiler_types.h:397:38: error: call to=20 > =E2=80=98__compiletime_assert_519=E2=80=99 declared with attribute error:= BUILD_BUG_ON=20 > failed: !__builtin_constant_p(_dest_len) || _dest_len =3D=3D (size_t)-1 > =C2=A0 397 |=C2=A0 _compiletime_assert(condition, msg, __compiletime_ass= ert_,=20 > __COUNTER__) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 |=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ^ > /kernel/include/linux/compiler_types.h:378:4: note: in definition of=20 > macro =E2=80=98__compiletime_assert=E2=80=99 > =C2=A0 378 |=C2=A0=C2=A0=C2=A0 prefix ## suffix();=C2=A0=C2=A0=C2=A0 \ > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 |=C2=A0=C2=A0=C2=A0 ^~~~~~ > /kernel/include/linux/compiler_types.h:397:2: note: in expansion of=20 > macro =E2=80=98_compiletime_assert=E2=80=99 > =C2=A0 397 |=C2=A0 _compiletime_assert(condition, msg, __compiletime_ass= ert_,=20 > __COUNTER__) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 |=C2=A0 ^~~~~~~~~~~~~~~~~~~ > /kernel/include/linux/build_bug.h:39:37: note: in expansion of macro=20 > =E2=80=98compiletime_assert=E2=80=99 > =C2=A0=C2=A0 39 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert= (!(cond),=20 > msg) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 |=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 ^~~~~~~~~~~~~~~~~~ > /kernel/include/linux/build_bug.h:50:2: note: in expansion of macro=20 > =E2=80=98BUILD_BUG_ON_MSG=E2=80=99 > =C2=A0=C2=A0 50 |=C2=A0 BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed= : " #condition) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 |=C2=A0 ^~~~~~~~~~~~~~~~ > /kernel/include/linux/string.h:302:2: note: in expansion of macro=20 > =E2=80=98BUILD_BUG_ON=E2=80=99 > =C2=A0 302 |=C2=A0 BUILD_BUG_ON(!__builtin_constant_p(_dest_len) ||=C2= =A0 \ > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 |=C2=A0 ^~~~~~~~~~~~ > /kernel/drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_debugfs.c:115:4:= =20 > note: in expansion of macro =E2=80=98strtomem=E2=80=99 > =C2=A0 115 |=C2=A0=C2=A0=C2=A0 strtomem(pos, result[i]); > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 |=C2=A0=C2=A0=C2=A0 ^~~~~~~~ >=20 > In the strtomem macro, __builtin_object_size is used to calculate the=20 > _dest_len. > We tried to print the _dest_len directly, and the result was -1. > How can we solve this? Let's add Kees in case he has a immediate recommendation on use of strtomem() vs memcpy() for this case..