Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp1567592lqe; Mon, 8 Apr 2024 12:49:04 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW3kY8Wgoq6JCAfiAvEiJcPungtFOrSJHInSCejX31sciD1nxu38Ii05B6X10dO5vwSTYkqDaMVxJGup5TUhI99LTp9en4BHPuo5+6WEg== X-Google-Smtp-Source: AGHT+IGBRQVtSbbg2IwkLTnCW0e2L2XDq+EfI7OTUMETbVjaVgWP59yfaWN5qA+ac9PW70MTwFUh X-Received: by 2002:a17:906:5610:b0:a4e:7831:59b7 with SMTP id f16-20020a170906561000b00a4e783159b7mr6279824ejq.61.1712605744029; Mon, 08 Apr 2024 12:49:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712605744; cv=pass; d=google.com; s=arc-20160816; b=MUS3EvEfOcWBs4lYCPmkkk5Urm/oDcDFGBWnLeYtpDTT7v36fZkzvHYiaqxukpUTMU ofh8fnYDbQHNQ5efwUKAYKmTz1YRkL+XTCi+zEY1ROBl8JoFhPRf/OqIb7h1iG0xbVYh aVZxl77QLqPaQfaEVw6ITTMgZ3BeYmqnrbGjzdyGu6pcsvVAVJQRByKLQZlhzN3A0o9J 5foQqh2gZ+1dTmasxmjWL1Gzsiz6cIcIsohS+tsNLnh7m1QnXbZN0CiHxx7/ayZc+oXO p7z0YwS6H+mBUwQ+ICgXllPCoyOByKoxC7UL5vaPXE8nJZRLxQsBLRrUmo6HCBq27/AP k1zg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=+uuXnmr8ixM1wR50btNJpQP0h35NNJ5PCSWZ/+ykObw=; fh=Kqcr9aUlpvyR8TNEEj2DvVbv8Qmyvq2wmVId76l5fW4=; b=x+I6MkVSC5eXmzdKtiHMhLS2glMMqbQi6dlw4NYNb0og+QjnIbqvvBe3Tdj3wSxG14 a5pjJ9b/0WZFTpjudqpdFk/O8/73ojO5BFtEY121CQdsFB554yTZiU2OGGXh7YF8vLtH Mq1r1sv6chHshdJHkhrKHUVFlxNkZhNc1aRQrhza95HZrKnUfRHZyzA5jl2H7sGNUYNK 0z3TSZTVvGJw+JkY3M+qkVl0lCjpdpL2vnO2DQ+3KojOzRgXZ6TkALwuMERTiXXhqXTU R8GG69rSJBS94smaWC2rmEhhS0BV0fzgH7xzTZdrPXvqHGGdjNuvnJs7fZdKnwVgAvW+ EPJg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=cuqouAGK; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-135887-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135887-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id w20-20020a170906481400b00a51e1596e2csi521944ejq.1023.2024.04.08.12.49.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 12:49:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-135887-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=cuqouAGK; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-135887-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135887-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 4A2C21F22D02 for ; Mon, 8 Apr 2024 19:49:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B4A18146A74; Mon, 8 Apr 2024 19:48:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="cuqouAGK" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D209814600D; Mon, 8 Apr 2024 19:48:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712605725; cv=none; b=gcCp7Vm0OWQa3etvD4cCeHRHzYLP5ntsuPI6BpaWzs8h1CTaIYWrArYPFq6k2VUVklR6CRPIZZ2eQxmmtmXdjQS5PS7w9TDMDrvfTiCqfVezpBj/RDYlIfrwMFgUv6d+Or3aq9pu2qo3J7HTYTe3ZTnmeRjtA8SpF163M79uyhw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712605725; c=relaxed/simple; bh=e7FNZkCRwzJc9ynpMaKnxLTKxonZ60uDaRzM13K/KA0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=e6upDAZGp0Xz4UjylFcn/92qyz3r0LJaYyYokreWTRZX7mwoN/gP+LHfyIyG12jTY+aAmK8WJUZg9TIWap5p6fL2BkoTKJrnBVrr648nR0RYSQbt05JexP+LHWrQ7m6gM+qFbcW+GmonVadwARegnER2QRAcGcvy8WxvbXCnYZY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=cuqouAGK; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4B385C433F1; Mon, 8 Apr 2024 19:48:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712605725; bh=e7FNZkCRwzJc9ynpMaKnxLTKxonZ60uDaRzM13K/KA0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cuqouAGKUeGkDyrdxrdsGxBnYNDVPKyNbwFOycplmtj6O/rv1anr9TxPGFxEcMcMC ArIkGnAgahQITvMSgImOBgzugMIx839snRTqMxVoNXijpVxAMagKHwpQ1GFs8Mh8pV NH8ut2aEMSjdU5X5kKNX5Hi5hQZoJmaimt0AL+YUJfKeYSU+jQ1RFc9RNjh5zuzloH VZenf+GTwlURY1qORe0cvX7TLaL+rIPY0q/608jQd2TNesQhmKg3Dob8K7dVsPOB5c PR0WeKhKpGe1NQsSveZuPsoDqGRajNfHdpRo9hbL1iYt8LgrHMTheDcLveF9/cDjqO JUo3UMOu2Ae7g== From: Arnd Bergmann To: Greg Kroah-Hartman , Viresh Kumar , Johan Hovold , Alex Elder Cc: Justin Stitt , Dan Carpenter , Arnd Bergmann , Christophe JAILLET , greybus-dev@lists.linaro.org, linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 3/3] [v2] staging: greybus: change strncpy() to strscpy_pad() Date: Mon, 8 Apr 2024 21:48:11 +0200 Message-Id: <20240408194821.3183462-3-arnd@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240408194821.3183462-1-arnd@kernel.org> References: <20240408194821.3183462-1-arnd@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Arnd Bergmann gcc-10 warns about a strncpy() that does not enforce zero-termination: In file included from include/linux/string.h:369, from drivers/staging/greybus/fw-management.c:9: In function 'strncpy', inlined from 'fw_mgmt_backend_fw_update_operation' at drivers/staging/greybus/fw-management.c:306:2: include/linux/fortify-string.h:108:30: error: '__builtin_strncpy' specified bound 10 equals destination size [-Werror=stringop-truncation] 108 | #define __underlying_strncpy __builtin_strncpy | ^ include/linux/fortify-string.h:187:9: note: in expansion of macro '__underlying_strncpy' 187 | return __underlying_strncpy(p, q, size); | ^~~~~~~~~~~~~~~~~~~~ For some reason, I cannot reproduce this with gcc-9 or gcc-11, and I only get a warning for one of the four related strncpy()s, so I'm not sure what's going on. Change all four to strscpy_pad(), which is the safest replacement here, as it avoids ending up with uninitialized stack data in the tag name. Signed-off-by: Arnd Bergmann --- v2 - use strscpy_pad() - use two-argument form - change all four instances, not just the one that produced the warning --- drivers/staging/greybus/fw-management.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/drivers/staging/greybus/fw-management.c b/drivers/staging/greybus/fw-management.c index 3054f084d777..a47385175582 100644 --- a/drivers/staging/greybus/fw-management.c +++ b/drivers/staging/greybus/fw-management.c @@ -123,8 +123,7 @@ static int fw_mgmt_interface_fw_version_operation(struct fw_mgmt *fw_mgmt, fw_info->major = le16_to_cpu(response.major); fw_info->minor = le16_to_cpu(response.minor); - strncpy(fw_info->firmware_tag, response.firmware_tag, - GB_FIRMWARE_TAG_MAX_SIZE); + strscpy_pad(fw_info->firmware_tag, response.firmware_tag); /* * The firmware-tag should be NULL terminated, otherwise throw error but @@ -153,7 +152,7 @@ static int fw_mgmt_load_and_validate_operation(struct fw_mgmt *fw_mgmt, } request.load_method = load_method; - strncpy(request.firmware_tag, tag, GB_FIRMWARE_TAG_MAX_SIZE); + strscpy_pad(request.firmware_tag, tag); /* * The firmware-tag should be NULL terminated, otherwise throw error and @@ -249,8 +248,7 @@ static int fw_mgmt_backend_fw_version_operation(struct fw_mgmt *fw_mgmt, struct gb_fw_mgmt_backend_fw_version_response response; int ret; - strncpy(request.firmware_tag, fw_info->firmware_tag, - GB_FIRMWARE_TAG_MAX_SIZE); + strscpy_pad(request.firmware_tag, fw_info->firmware_tag); /* * The firmware-tag should be NULL terminated, otherwise throw error and @@ -303,13 +301,13 @@ static int fw_mgmt_backend_fw_update_operation(struct fw_mgmt *fw_mgmt, struct gb_fw_mgmt_backend_fw_update_request request; int ret; - strncpy(request.firmware_tag, tag, GB_FIRMWARE_TAG_MAX_SIZE); + ret = strscpy_pad(request.firmware_tag, tag); /* * The firmware-tag should be NULL terminated, otherwise throw error and * fail. */ - if (request.firmware_tag[GB_FIRMWARE_TAG_MAX_SIZE - 1] != '\0') { + if (ret == -E2BIG) { dev_err(fw_mgmt->parent, "backend-update: firmware-tag is not NULL terminated\n"); return -EINVAL; } -- 2.39.2