Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp1096509rdg; Wed, 11 Oct 2023 14:30:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IErf+plkHO/w64tOJmI4eQ3xlJF9TdEn+8dS2pc0JmKBUTNTDQVSU4HDvb6y9KZ3Mrs0ss4 X-Received: by 2002:a05:6870:612b:b0:1b7:27cf:9709 with SMTP id s43-20020a056870612b00b001b727cf9709mr25209347oae.43.1697059845062; Wed, 11 Oct 2023 14:30:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697059845; cv=none; d=google.com; s=arc-20160816; b=L3PFWm0Yp9dQCiUsiDHYpwyoApO/hlLC03tcl4C1Q5aMaGzU5T3RtfcmVt+VyWTk8P 15/QMWIJIp61NvjXsbJN2M5ZsY2s0kDJ4wC2v7HnTsOcGhz/D83woIEVMDC/8c4maCUd BA0pvjuAE7FBc9ckYasdUhjjFBoTIWpAP+bZlakfFNneSWtyvnJgEuosd4Vt4Ad54xo3 AY2KUeCu2uM9CCgCwStpWsQfysD00RFpVtq8bKrUnnZy3u61IT1S+P5v6mXmTYdUyU8X 9vkNDCnVDGSSo3kRxBJog/59nJn/1wwx+O3wDxVoQ7Xvekv6TnBf2wbau99fT1wDHbV9 D2eg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=m3pTpYqn2463yxqFt3mIFAjuaB6ZhS9Rkhs7QnUkljw=; fh=nsvB+05Cglr5V/ulcLOtB6IuZN2lyCvCu3UEoS/rjwU=; b=CaQbtNr2d+nOsiVqfTE90CWEOThm1ZiO7LUg01DDNfPtfeWEtRBYk+beVWOddu1xw1 LjISrtpccCilJyZWmHzV+d+QD0YQgoNsaereJj4Ntcoq5kmeHNIO1EpUNHe2uccLCVcD iVlKAuWKbm/4qEOkXrANE4mbjGUXnGL0Qt/NEl1SS6nU9lz5hF4m0ahXIO2aUocPsMWk ANFVWt6nz6H8Gj59VS29AG8HWdfkwD0poOQwWzbLVxLwrJYB6Q+7uZB/Fn3atdMouvo0 kkeVw+Dg7KOdrf/eWnHv9NXUvyRy1+3YbDHScql3LmqoTO9lOrLFaRlt/OESNL8y39yR oQ5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Nq7uSgZg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id m66-20020a632645000000b00563e283786esi672645pgm.104.2023.10.11.14.30.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 14:30:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Nq7uSgZg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 118468080E11; Wed, 11 Oct 2023 14:30:08 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233519AbjJKVaB (ORCPT + 99 others); Wed, 11 Oct 2023 17:30:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233390AbjJKVaA (ORCPT ); Wed, 11 Oct 2023 17:30:00 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3ABF8B7 for ; Wed, 11 Oct 2023 14:29:58 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a7af53bde4so4846727b3.0 for ; Wed, 11 Oct 2023 14:29:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697059797; x=1697664597; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=m3pTpYqn2463yxqFt3mIFAjuaB6ZhS9Rkhs7QnUkljw=; b=Nq7uSgZg0s1stmYMn6ZzSakIy/plW3bDKlrSQIprCvdcoMowghQ3C2GG+pqav8ZMij iFxE+b+c/pqU1LSOSMeDbVzbrJxBcQNqkxSN5+8NvmofmQ3RNTNXoFteHlXQeissIHjZ RcSPcJ02aCYEw3a7pK7a2j7NgAupJZTV/m1kBXDfPX0+Xy9HTUwjFEdkETepOyfiSsCg n3n9PsQNbZR1vm98HHuV2XJmTpFRffMDry6+L3x+M+9mLjdRB+ElplXUEGrdfMGj67Ka oOhJe2zmogwnSU4qfy8BA9ZCiYl0ayjpPt4ET2fTQIcqaF5vRf4KHb/zvgotMugxa9is qSiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697059797; x=1697664597; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=m3pTpYqn2463yxqFt3mIFAjuaB6ZhS9Rkhs7QnUkljw=; b=nNzDkYAvItoxkHgKiOMSN3h02RHZg/7SDKMsErIFPkyNmm0GdNWfeeZN/GZO7h/ZSO 27CW8paIwGsfvBlx6Z15WGhRrGXeg87DZz8TXNm2fw6i2/N/Ip5Sjq9g6kheStcN3mDa kfFCj5eykUT3Cah1piDmIRYHXjjG+LiCaqd5anVGLJNKcCuwvDuLVYOJ3A1oJ7eX7eML 0UWAKjDp228OZCTOCmlpBdF/AOYfeA3PLC6gfqHY6JuNgxDJg1zGpRmnHA1VY7IfFHDm 2nmTvy8rUrs09UhJccbMSelHMYhaSJOram6b6PschO/ov1p9OIhN4UJABlHAk3kue1fq 56cQ== X-Gm-Message-State: AOJu0Ywz6UEYiBjBZJvFkmXgyNVPBSu//Hx0dWn0aoH2ZRPc6KPqpabz UEgNRQhsjPPLtTiwQLUVt5vBRKkf2+QXSZyxgw== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a05:6902:52d:b0:d13:856b:c10a with SMTP id y13-20020a056902052d00b00d13856bc10amr387725ybs.3.1697059797461; Wed, 11 Oct 2023 14:29:57 -0700 (PDT) Date: Wed, 11 Oct 2023 21:29:57 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIANQTJ2UC/x2NQQrDIBAAvxL23AU19NB+pfQgumkWdA2rBEPI3 2t7m7nMnFBJmSo8pxOUdq5cZIi9TRBWLx9CjsPBGTdbYy3WphK2A6PyTlpRqCG1lfQHmVLyUjr m1O8YihJmz4IBXYwPPy/eOTIw4pvSwv0/fr2v6wtZvHP6iAAAAA== X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1697059796; l=2667; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=usepiRchsBWre9GwPIWtzyZBsxT9tg3v/Wn5P+lFJXo=; b=mFh0xX/ALc4XkRYsUlu0sqE+8WUIH0DgX2syBIUwocnImlvC26efKkQIraF5PVPGcpvC0ArXs uOKWtTvR0P8CTm/inIvAVzS2Y/WzNweabRpteNbBdiyh+QIkfzd5LLl X-Mailer: b4 0.12.3 Message-ID: <20231011-strncpy-drivers-net-ethernet-mellanox-mlx5-core-main-c-v1-1-90fa39998bb2@google.com> Subject: [PATCH] net/mlx5: simplify mlx5_set_driver_version string assignments From: Justin Stitt To: Saeed Mahameed , Leon Romanovsky , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: netdev@vger.kernel.org, linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Kees Cook , Justin Stitt Content-Type: text/plain; charset="utf-8" X-Spam-Status: No, score=-4.8 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Wed, 11 Oct 2023 14:30:09 -0700 (PDT) In total, just assigning this version string takes: (1) strncpy()'s (5) strlen()'s (3) strncat()'s (1) snprintf()'s (4) max_t()'s Moreover, `strncpy` is deprecated [1] and `strncat` really shouldn't be used either [2]. With this in mind, let's simply use a single `snprintf`. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] Link: https://elixir.bootlin.com/linux/v6.6-rc5/source/include/linux/fortify-string.h#L448 [2] Link: https://github.com/KSPP/linux/issues/90 Cc: linux-hardening@vger.kernel.org Cc: Kees Cook Signed-off-by: Justin Stitt --- Note: build-tested only. Found with: $ rg "strncpy\(" --- drivers/net/ethernet/mellanox/mlx5/core/main.c | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/drivers/net/ethernet/mellanox/mlx5/core/main.c b/drivers/net/ethernet/mellanox/mlx5/core/main.c index 15561965d2af..0c829b6d2b49 100644 --- a/drivers/net/ethernet/mellanox/mlx5/core/main.c +++ b/drivers/net/ethernet/mellanox/mlx5/core/main.c @@ -218,7 +218,6 @@ static void mlx5_set_driver_version(struct mlx5_core_dev *dev) int driver_ver_sz = MLX5_FLD_SZ_BYTES(set_driver_version_in, driver_version); u8 in[MLX5_ST_SZ_BYTES(set_driver_version_in)] = {}; - int remaining_size = driver_ver_sz; char *string; if (!MLX5_CAP_GEN(dev, driver_version)) @@ -226,22 +225,9 @@ static void mlx5_set_driver_version(struct mlx5_core_dev *dev) string = MLX5_ADDR_OF(set_driver_version_in, in, driver_version); - strncpy(string, "Linux", remaining_size); - - remaining_size = max_t(int, 0, driver_ver_sz - strlen(string)); - strncat(string, ",", remaining_size); - - remaining_size = max_t(int, 0, driver_ver_sz - strlen(string)); - strncat(string, KBUILD_MODNAME, remaining_size); - - remaining_size = max_t(int, 0, driver_ver_sz - strlen(string)); - strncat(string, ",", remaining_size); - - remaining_size = max_t(int, 0, driver_ver_sz - strlen(string)); - - snprintf(string + strlen(string), remaining_size, "%u.%u.%u", - LINUX_VERSION_MAJOR, LINUX_VERSION_PATCHLEVEL, - LINUX_VERSION_SUBLEVEL); + snprintf(string, driver_ver_sz, "Linux,%s,%u.%u.%u", + KBUILD_MODNAME, LINUX_VERSION_MAJOR, + LINUX_VERSION_PATCHLEVEL, LINUX_VERSION_SUBLEVEL); /*Send the command*/ MLX5_SET(set_driver_version_in, in, opcode, --- base-commit: cbf3a2cb156a2c911d8f38d8247814b4c07f49a2 change-id: 20231011-strncpy-drivers-net-ethernet-mellanox-mlx5-core-main-c-2dd9a3fa22e0 Best regards, -- Justin Stitt