All of lore.kernel.org
 help / color / mirror / Atom feed
From: Arnd Bergmann <arnd@kernel.org>
To: linux-kernel@vger.kernel.org,
	Robert Moore <robert.moore@intel.com>,
	"Rafael J. Wysocki" <rafael.j.wysocki@intel.com>,
	Alexey Starikovskiy <astarikovskiy@suse.de>,
	Lin Ming <ming.m.lin@intel.com>, Len Brown <len.brown@intel.com>
Cc: Arnd Bergmann <arnd@arndb.de>, Len Brown <lenb@kernel.org>,
	linux-acpi@vger.kernel.org, acpica-devel@lists.linux.dev
Subject: [PATCH 06/11] acpi: avoid warning for truncated string copy
Date: Thu, 28 Mar 2024 15:04:50 +0100	[thread overview]
Message-ID: <20240328140512.4148825-7-arnd@kernel.org> (raw)
In-Reply-To: <20240328140512.4148825-1-arnd@kernel.org>

From: Arnd Bergmann <arnd@arndb.de>

gcc -Wstringop-truncation warns about copying a string that results in a
missing nul termination:

drivers/acpi/acpica/tbfind.c: In function 'acpi_tb_find_table':
drivers/acpi/acpica/tbfind.c:60:9: error: 'strncpy' specified bound 6 equals destination size [-Werror=stringop-truncation]
   60 |         strncpy(header.oem_id, oem_id, ACPI_OEM_ID_SIZE);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/acpi/acpica/tbfind.c:61:9: error: 'strncpy' specified bound 8 equals destination size [-Werror=stringop-truncation]
   61 |         strncpy(header.oem_table_id, oem_table_id, ACPI_OEM_TABLE_ID_SIZE);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This one is intentional, so rewrite the code in a way that avoids the
warning. Since there is already an extra strlen() and an overflow check,
the actual size to be copied is already known here.

Fixes: 47c08729bf1c ("ACPICA: Fix for LoadTable operator, input strings")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 drivers/acpi/acpica/tbfind.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/drivers/acpi/acpica/tbfind.c b/drivers/acpi/acpica/tbfind.c
index 1c1b2e284bd9..472ce2a6624b 100644
--- a/drivers/acpi/acpica/tbfind.c
+++ b/drivers/acpi/acpica/tbfind.c
@@ -36,7 +36,7 @@ acpi_tb_find_table(char *signature,
 {
 	acpi_status status = AE_OK;
 	struct acpi_table_header header;
-	u32 i;
+	u32 len, i;
 
 	ACPI_FUNCTION_TRACE(tb_find_table);
 
@@ -46,19 +46,18 @@ acpi_tb_find_table(char *signature,
 		return_ACPI_STATUS(AE_BAD_SIGNATURE);
 	}
 
-	/* Don't allow the OEM strings to be too long */
-
-	if ((strlen(oem_id) > ACPI_OEM_ID_SIZE) ||
-	    (strlen(oem_table_id) > ACPI_OEM_TABLE_ID_SIZE)) {
-		return_ACPI_STATUS(AE_AML_STRING_LIMIT);
-	}
-
 	/* Normalize the input strings */
 
 	memset(&header, 0, sizeof(struct acpi_table_header));
 	ACPI_COPY_NAMESEG(header.signature, signature);
-	strncpy(header.oem_id, oem_id, ACPI_OEM_ID_SIZE);
-	strncpy(header.oem_table_id, oem_table_id, ACPI_OEM_TABLE_ID_SIZE);
+	len = strlen(oem_id);
+	if (len > ACPI_OEM_ID_SIZE)
+		return_ACPI_STATUS(AE_AML_STRING_LIMIT);
+	memcpy(header.oem_id, oem_id, len);
+	len = strlen(oem_table_id);
+	if (len > ACPI_OEM_TABLE_ID_SIZE)
+		return_ACPI_STATUS(AE_AML_STRING_LIMIT);
+	memcpy(header.oem_table_id, oem_table_id, len);
 
 	/* Search for the table */
 
-- 
2.39.2


  parent reply	other threads:[~2024-03-28 14:06 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-28 14:04 [PATCH 00/11] address remaining stringop-truncation warnings Arnd Bergmann
2024-03-28 14:04 ` Arnd Bergmann
2024-03-28 14:04 ` [PATCH 01/11] staging: vc04_services: changen strncpy() to strscpy_pad() Arnd Bergmann
2024-03-28 14:04   ` Arnd Bergmann
2024-03-28 14:42   ` Dan Carpenter
2024-03-28 14:42     ` Dan Carpenter
2024-03-28 16:15     ` Arnd Bergmann
2024-03-28 16:15       ` Arnd Bergmann
2024-03-28 23:10   ` Justin Stitt
2024-03-28 23:10     ` Justin Stitt
2024-03-28 14:04 ` [PATCH 02/11] scsi: devinfo: rework scsi_strcpy_devinfo() Arnd Bergmann
2024-03-28 16:46   ` Bart Van Assche
2024-03-28 23:14   ` Justin Stitt
2024-03-28 23:18     ` Arnd Bergmann
2024-03-28 14:04 ` [PATCH 03/11] staging: replace weird strncpy() with memcpy() Arnd Bergmann
2024-03-28 16:35   ` Dan Carpenter
2024-04-08 14:45     ` Arnd Bergmann
2024-04-08 15:59       ` Dan Carpenter
2024-04-08 19:20         ` Arnd Bergmann
2024-03-28 14:04 ` [PATCH 04/11] orangefs: convert strncpy() to strscpy() Arnd Bergmann
2024-03-28 23:17   ` Justin Stitt
2024-03-28 14:04 ` [PATCH 05/11] test_hexdump: avoid string truncation warning Arnd Bergmann
2024-03-28 23:54   ` Justin Stitt
2024-04-08 15:38     ` Arnd Bergmann
2024-04-08 19:53       ` Justin Stitt
2024-03-28 14:04 ` Arnd Bergmann [this message]
2024-03-28 23:20   ` [PATCH 06/11] acpi: avoid warning for truncated string copy Justin Stitt
2024-04-08 14:41   ` Rafael J. Wysocki
2024-03-28 14:04 ` [PATCH 07/11] block/partitions/ldm: convert strncpy() to strscpy() Arnd Bergmann
2024-03-28 23:24   ` Justin Stitt
2024-03-28 14:04 ` [PATCH 08/11] blktrace: convert strncpy() to strscpy_pad() Arnd Bergmann
2024-03-28 14:14   ` Steven Rostedt
2024-04-08 18:05     ` Arnd Bergmann
2024-03-28 14:04 ` [PATCH 09/11] staging: rtl8723bs: convert strncpy to strscpy Arnd Bergmann
2024-03-28 23:01   ` Justin Stitt
2024-04-08 18:15     ` Arnd Bergmann
2024-03-28 14:04 ` [PATCH 10/11] staging: greybus: change strncpy() to strscpy() Arnd Bergmann
2024-03-28 15:00   ` Dan Carpenter
2024-04-08 18:26     ` Arnd Bergmann
2024-04-09  7:09       ` Dan Carpenter
2024-03-28 23:28   ` Justin Stitt
2024-04-08 18:30     ` Arnd Bergmann
2024-03-28 14:04 ` [PATCH 11/11] kbuild: enable -Wstringop-truncation globally Arnd Bergmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240328140512.4148825-7-arnd@kernel.org \
    --to=arnd@kernel.org \
    --cc=acpica-devel@lists.linux.dev \
    --cc=arnd@arndb.de \
    --cc=astarikovskiy@suse.de \
    --cc=len.brown@intel.com \
    --cc=lenb@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ming.m.lin@intel.com \
    --cc=rafael.j.wysocki@intel.com \
    --cc=robert.moore@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.