Re: [PATCH] hinic: Replace memcpy() with direct assignment

From: Nathan Chancellor
Date: Thu Jun 16 2022 - 13:19:39 EST


On Wed, Jun 15, 2022 at 10:23:12PM -0700, Kees Cook wrote:
> Under CONFIG_FORTIFY_SOURCE=y and CONFIG_UBSAN_BOUNDS=y, Clang is bugged
> here for calculating the size of the destination buffer (0x10 instead of
> 0x14). This copy is a fixed size (sizeof(struct fw_section_info_st)), with
> the source and dest being struct fw_section_info_st, so the memcpy should
> be safe, assuming the index is within bounds, which is UBSAN_BOUNDS's
> responsibility to figure out.
>
> Avoid the whole thing and just do a direct assignment. This results in
> no change to the executable code.
>
> Cc: "David S. Miller" <davem@xxxxxxxxxxxxx>
> Cc: Eric Dumazet <edumazet@xxxxxxxxxx>
> Cc: Jakub Kicinski <kuba@xxxxxxxxxx>
> Cc: Paolo Abeni <pabeni@xxxxxxxxxx>
> Cc: Nathan Chancellor <nathan@xxxxxxxxxx>
> Cc: Nick Desaulniers <ndesaulniers@xxxxxxxxxx>
> Cc: Tom Rix <trix@xxxxxxxxxx>
> Cc: Leon Romanovsky <leon@xxxxxxxxxx>
> Cc: Jiri Pirko <jiri@xxxxxxxxxx>
> Cc: Vladimir Oltean <olteanv@xxxxxxxxx>
> Cc: Simon Horman <simon.horman@xxxxxxxxxxxx>
> Cc: netdev@xxxxxxxxxxxxxxx
> Cc: llvm@xxxxxxxxxxxxxxx
> Link: https://github.com/ClangBuiltLinux/linux/issues/1592
> Signed-off-by: Kees Cook <keescook@xxxxxxxxxxxx>

Tested-by: Nathan Chancellor <nathan@xxxxxxxxxx> # build

> ---
> drivers/net/ethernet/huawei/hinic/hinic_devlink.c | 4 +---
> 1 file changed, 1 insertion(+), 3 deletions(-)
>
> diff --git a/drivers/net/ethernet/huawei/hinic/hinic_devlink.c b/drivers/net/ethernet/huawei/hinic/hinic_devlink.c
> index 60ae8bfc5f69..1749d26f4bef 100644
> --- a/drivers/net/ethernet/huawei/hinic/hinic_devlink.c
> +++ b/drivers/net/ethernet/huawei/hinic/hinic_devlink.c
> @@ -43,9 +43,7 @@ static bool check_image_valid(struct hinic_devlink_priv *priv, const u8 *buf,
>
> for (i = 0; i < fw_image->fw_info.fw_section_cnt; i++) {
> len += fw_image->fw_section_info[i].fw_section_len;
> - memcpy(&host_image->image_section_info[i],
> - &fw_image->fw_section_info[i],
> - sizeof(struct fw_section_info_st));
> + host_image->image_section_info[i] = fw_image->fw_section_info[i];
> }
>
> if (len != fw_image->fw_len ||
> --
> 2.32.0
>