Skip to contentSkip to conversation

ACPICA: Replace strncpy() with strscpy_pad() in acpi_ut_safe_strncpy()

PATCH In review Kees Cook 3 participants 6 messages Mar 23 – Jun 4

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 ACPICA: Replace strncpy() with strscpy_pad() in acpi_ut_safe_strncpy()

Replace the deprecated[1] strncpy() with strscpy_pad() in acpi_ut_safe_strncpy().

The function is a "safe strncpy" wrapper that does strncpy(dest, source, dest_size) followed by manual NUL-termination at dest[dest_size - 1]. strscpy_pad() is a direct replacement: it NUL-terminates, zero-pads the remainder, and the manual termination is no longer needed.

All callers pass NUL-terminated source strings (C string literals, __FILE__ via ACPI_MODULE_NAME, or user-provided filenames that have already been validated). The destinations are fixed-size char arrays in ACPICA internal structures (allocation->module, aml_op_name, acpi_gbl_db_debug_filename), all consumed as C strings.

No behavioral change: strscpy_pad() produces identical output to strncpy() + manual NUL-termination for NUL-terminated sources that are shorter than dest_size. For sources longer than dest_size, strncpy() wrote dest_size non-NUL bytes then the manual termination overwrote the last byte with NUL; strscpy_pad() writes dest_size-1 bytes plus NUL: same result.

---
This touches the ACPICA component shared with the upstream ACPICA project (https://github.com/acpica/acpica), where the function is named AcpiUtSafeStrncpy(). The upstream codebase uses its own platform abstraction layer (acenv.h/acgcc.h) where I've mapped various kernel APIs before like ACPI_FLEX_ARRAY and similar helpers. However, acpi_ut_safe_strncpy() is an explicit function implementation rather than a macro mapping, so the approach for upstreaming this change to ACPICA is not clear. What's the best way to land this?

(This is one of the last users of strncpy in the kernel.)

---
 drivers/acpi/acpica/utnonansi.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/acpi/acpica/utnonansi.c +1 -2
index ff0802ace19b..3a7952be6545 100644
@@ -168,8 +168,7 @@ void acpi_ut_safe_strncpy(char *dest, char *source, acpi_size dest_size)
 {
 	/* Always terminate destination string */
 
-	strncpy(dest, source, dest_size);
-	dest[dest_size - 1] = 0;
+	strscpy_pad(dest, source, dest_size);
 }
 
 #endif
Signature · 1 line
2.34.1
#1 lore raw
Quoted 25 lines · Kees Cook

Replace the deprecated[1] strncpy() with strscpy_pad() in acpi_ut_safe_strncpy().

The function is a "safe strncpy" wrapper that does strncpy(dest, source, dest_size) followed by manual NUL-termination at dest[dest_size - 1]. strscpy_pad() is a direct replacement: it NUL-terminates, zero-pads the remainder, and the manual termination is no longer needed.

All callers pass NUL-terminated source strings (C string literals, __FILE__ via ACPI_MODULE_NAME, or user-provided filenames that have already been validated). The destinations are fixed-size char arrays in ACPICA internal structures (allocation->module, aml_op_name, acpi_gbl_db_debug_filename), all consumed as C strings.

No behavioral change: strscpy_pad() produces identical output to strncpy() + manual NUL-termination for NUL-terminated sources that are shorter than dest_size. For sources longer than dest_size, strncpy() wrote dest_size non-NUL bytes then the manual termination overwrote the last byte with NUL; strscpy_pad() writes dest_size-1 bytes plus NUL: same result.

Quoted 9 lines

---
This touches the ACPICA component shared with the upstream ACPICA project (https://github.com/acpica/acpica), where the function is named AcpiUtSafeStrncpy(). The upstream codebase uses its own platform abstraction layer (acenv.h/acgcc.h) where I've mapped various kernel APIs before like ACPI_FLEX_ARRAY and similar helpers. However, acpi_ut_safe_strncpy() is an explicit function implementation rather than a macro mapping, so the approach for upstreaming this change to ACPICA is not clear. What's the best way to land this?

I can apply this directly, it shouldn't be a major problem for porting patches from the upstream.

Quoted 20 lines

(This is one of the last users of strncpy in the kernel.)

---
 drivers/acpi/acpica/utnonansi.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/acpi/acpica/utnonansi.c +1 -3
index ff0802ace19b..3a7952be6545 100644
@@ -168,8 +168,7 @@ void acpi_ut_safe_strncpy(char *dest, char *source, acpi_size dest_size)
 {
        /* Always terminate destination string */
 
-       strncpy(dest, source, dest_size);
-       dest[dest_size - 1] = 0;
+       strscpy_pad(dest, source, dest_size);
 }
 
 #endif
--
author
#2 lore raw
Quoted 9 lines · Rafael J. Wysocki
Quoted 5 lines · Kees Cook

[...]
kernel APIs before like ACPI_FLEX_ARRAY and similar helpers. However, acpi_ut_safe_strncpy() is an explicit function implementation rather than a macro mapping, so the approach for upstreaming this change to ACPICA is not clear. What's the best way to land this?

I can apply this directly, it shouldn't be a major problem for porting patches from the upstream.

Okay, thanks! I wasn't sure if that was going to be a viable approach. :)

-Kees

Signature · 1 line
Kees Cook
#3 lore raw
In reply to Rafael J. Wysocki
Quoted 36 lines · Rafael J. Wysocki
Quoted 25 lines · Kees Cook

Replace the deprecated[1] strncpy() with strscpy_pad() in acpi_ut_safe_strncpy().

The function is a "safe strncpy" wrapper that does strncpy(dest, source, dest_size) followed by manual NUL-termination at dest[dest_size - 1]. strscpy_pad() is a direct replacement: it NUL-terminates, zero-pads the remainder, and the manual termination is no longer needed.

All callers pass NUL-terminated source strings (C string literals, __FILE__ via ACPI_MODULE_NAME, or user-provided filenames that have already been validated). The destinations are fixed-size char arrays in ACPICA internal structures (allocation->module, aml_op_name, acpi_gbl_db_debug_filename), all consumed as C strings.

No behavioral change: strscpy_pad() produces identical output to strncpy() + manual NUL-termination for NUL-terminated sources that are shorter than dest_size. For sources longer than dest_size, strncpy() wrote dest_size non-NUL bytes then the manual termination overwrote the last byte with NUL; strscpy_pad() writes dest_size-1 bytes plus NUL: same result.

Quoted 9 lines

---
This touches the ACPICA component shared with the upstream ACPICA project (https://github.com/acpica/acpica), where the function is named AcpiUtSafeStrncpy(). The upstream codebase uses its own platform abstraction layer (acenv.h/acgcc.h) where I've mapped various kernel APIs before like ACPI_FLEX_ARRAY and similar helpers. However, acpi_ut_safe_strncpy() is an explicit function implementation rather than a macro mapping, so the approach for upstreaming this change to ACPICA is not clear. What's the best way to land this?

Quoted 2 lines

I can apply this directly, it shouldn't be a major problem for porting patches from the upstream.

As I reported in https://github.com/acpica/acpica/issues/1158 (but got no reply), this patch breaks build of acpica against 7.1-rc*:

Quoted 1 line

../../../../../drivers/acpi/acpica/utnonansi.c:171:9: error: implicit declaration of function ‘strscpy_pad’ [-Wimplicit-function-declaration]

Is strscpy_pad() supposed to be emulated in acpica?

Quoted 13 lines
Quoted 13 lines

(This is one of the last users of strncpy in the kernel.)

---
  drivers/acpi/acpica/utnonansi.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
drivers/acpi/acpica/utnonansi.c +0 -0
index ff0802ace19b..3a7952be6545 100644
@@ -168,8 +168,7 @@ void acpi_ut_safe_strncpy(char *dest, char *source, acpi_size dest_size)
  {
         /* Always terminate destination string */
 
Quoted 8 lines

drivers/acpi/acpica/utnonansi.c void acpi_ut_safe_strncpy(char *dest, char *source, acpi_size dest_size)

Quoted 7 lines
drivers/acpi/acpica/utnonansi.c +1 -3
-       strncpy(dest, source, dest_size);
-       dest[dest_size - 1] = 0;
+       strscpy_pad(dest, source, dest_size);
  }
 
  #endif
--

thanks,

Signature · 2 lines
js
suse labs
#4 lore raw
Quoted 37 lines · Jiri Slaby
Quoted 36 lines · Rafael J. Wysocki
Quoted 34 lines · Kees Cook

Replace the deprecated[1] strncpy() with strscpy_pad() in acpi_ut_safe_strncpy().

The function is a "safe strncpy" wrapper that does strncpy(dest, source, dest_size) followed by manual NUL-termination at dest[dest_size - 1]. strscpy_pad() is a direct replacement: it NUL-terminates, zero-pads the remainder, and the manual termination is no longer needed.

All callers pass NUL-terminated source strings (C string literals, __FILE__ via ACPI_MODULE_NAME, or user-provided filenames that have already been validated). The destinations are fixed-size char arrays in ACPICA internal structures (allocation->module, aml_op_name, acpi_gbl_db_debug_filename), all consumed as C strings.

No behavioral change: strscpy_pad() produces identical output to strncpy() + manual NUL-termination for NUL-terminated sources that are shorter than dest_size. For sources longer than dest_size, strncpy() wrote dest_size non-NUL bytes then the manual termination overwrote the last byte with NUL; strscpy_pad() writes dest_size-1 bytes plus NUL: same result.

---
This touches the ACPICA component shared with the upstream ACPICA project (https://github.com/acpica/acpica), where the function is named AcpiUtSafeStrncpy(). The upstream codebase uses its own platform abstraction layer (acenv.h/acgcc.h) where I've mapped various kernel APIs before like ACPI_FLEX_ARRAY and similar helpers. However, acpi_ut_safe_strncpy() is an explicit function implementation rather than a macro mapping, so the approach for upstreaming this change to ACPICA is not clear. What's the best way to land this?

Quoted 8 lines
Quoted 2 lines

I can apply this directly, it shouldn't be a major problem for porting patches from the upstream.

As I reported in https://github.com/acpica/acpica/issues/1158 (but got no reply), this patch breaks build of acpica against 7.1-rc*:

Quoted 1 line

../../../../../drivers/acpi/acpica/utnonansi.c:171:9: error: implicit

declaration of function ‘strscpy_pad’ [-Wimplicit-function-declaration]

Is strscpy_pad() supposed to be emulated in acpica?

No, I misread the log, it's the in-kernel acpidump failing to build:

~/linux/tools/power/acpi/tools/acpidump> make

  MKDIR    include
  CP       include
  CC       tools/acpidump/apdump.o
  CC       tools/acpidump/apfiles.o
  CC       tools/acpidump/apmain.o
  CC       tools/acpidump/osunixdir.o
  CC       tools/acpidump/osunixmap.o
  CC       tools/acpidump/osunixxf.o
  CC       tools/acpidump/tbprint.o
  CC       tools/acpidump/tbxfroot.o
  CC       tools/acpidump/utascii.o
  CC       tools/acpidump/utbuffer.o
  CC       tools/acpidump/utcksum.o
  CC       tools/acpidump/utdebug.o
  CC       tools/acpidump/utexcep.o
  CC       tools/acpidump/utglobal.o
  CC       tools/acpidump/uthex.o
  CC       tools/acpidump/utmath.o
  CC       tools/acpidump/utnonansi.o
../../../../../drivers/acpi/acpica/utnonansi.c: In function ‘acpi_ut_safe_strncpy’:
../../../../../drivers/acpi/acpica/utnonansi.c:171:9: error: implicit declaration of function ‘strscpy_pad’ [-Wimplicit-function-declaration]
  171 |         strscpy_pad(dest, source, dest_size);
      |         ^~~~~~~~~~~

make: *** [../../Makefile.rules:25: /home/xslaby/linux/tools/power/acpi/tools/acpidump/utnonansi.o] Chyba 1

Quoted 24 lines
Quoted 14 lines
Quoted 14 lines

(This is one of the last users of strncpy in the kernel.)
---
  drivers/acpi/acpica/utnonansi.c | 3 +--

  1 file changed, 1 insertion(+), 2 deletions(-)
drivers/acpi/acpica/utnonansi.c +0 -0
index ff0802ace19b..3a7952be6545 100644
@@ -168,8 +168,7 @@ void acpi_ut_safe_strncpy(char *dest, char 
 *source, acpi_size dest_size)
   {
          /* Always terminate destination string */
 
Quoted 8 lines
Quoted 7 lines
drivers/acpi/acpica/utnonansi.c +1 -2
-       strncpy(dest, source, dest_size);
-       dest[dest_size - 1] = 0;
+       strscpy_pad(dest, source, dest_size);
   }
 

  #endif

thanks,

Signature · 2 lines
js
suse labs
#5 lore raw

Ping?

Quoted 99 lines · Jiri Slaby
Quoted 37 lines · Jiri Slaby
Quoted 36 lines · Rafael J. Wysocki
Quoted 34 lines · Kees Cook

Replace the deprecated[1] strncpy() with strscpy_pad() in acpi_ut_safe_strncpy().

The function is a "safe strncpy" wrapper that does strncpy(dest, source, dest_size) followed by manual NUL-termination at dest[dest_size - 1]. strscpy_pad() is a direct replacement: it NUL-terminates, zero-pads the remainder, and the manual termination is no longer needed.

All callers pass NUL-terminated source strings (C string literals, __FILE__ via ACPI_MODULE_NAME, or user-provided filenames that have already been validated). The destinations are fixed-size char arrays in ACPICA internal structures (allocation->module, aml_op_name, acpi_gbl_db_debug_filename), all consumed as C strings.

No behavioral change: strscpy_pad() produces identical output to strncpy() + manual NUL-termination for NUL-terminated sources that are shorter than dest_size. For sources longer than dest_size, strncpy() wrote dest_size non-NUL bytes then the manual termination overwrote the last byte with NUL; strscpy_pad() writes dest_size-1 bytes plus NUL: same result.

---
This touches the ACPICA component shared with the upstream ACPICA project (https://github.com/acpica/acpica), where the function is named AcpiUtSafeStrncpy(). The upstream codebase uses its own platform abstraction layer (acenv.h/acgcc.h) where I've mapped various kernel APIs before like ACPI_FLEX_ARRAY and similar helpers. However, acpi_ut_safe_strncpy() is an explicit function implementation rather than a macro mapping, so the approach for upstreaming this change to ACPICA is not clear. What's the best way to land this?

Quoted 7 lines
Quoted 2 lines

I can apply this directly, it shouldn't be a major problem for porting patches from the upstream.

As I reported in https://github.com/acpica/acpica/issues/1158 (but got no reply), this patch breaks build of acpica against 7.1-rc*:
 > ../../../../../drivers/acpi/acpica/utnonansi.c:171:9: error: implicit declaration of function ‘strscpy_pad’ [-Wimplicit-function- declaration]

Is strscpy_pad() supposed to be emulated in acpica?

No, I misread the log, it's the in-kernel acpidump failing to build:

~/linux/tools/power/acpi/tools/acpidump> make
  MKDIR    include
  CP       include
  CC       tools/acpidump/apdump.o
  CC       tools/acpidump/apfiles.o
  CC       tools/acpidump/apmain.o
  CC       tools/acpidump/osunixdir.o
  CC       tools/acpidump/osunixmap.o
  CC       tools/acpidump/osunixxf.o
  CC       tools/acpidump/tbprint.o
  CC       tools/acpidump/tbxfroot.o
  CC       tools/acpidump/utascii.o
  CC       tools/acpidump/utbuffer.o
  CC       tools/acpidump/utcksum.o
  CC       tools/acpidump/utdebug.o
  CC       tools/acpidump/utexcep.o
  CC       tools/acpidump/utglobal.o
  CC       tools/acpidump/uthex.o
  CC       tools/acpidump/utmath.o
  CC       tools/acpidump/utnonansi.o
../../../../../drivers/acpi/acpica/utnonansi.c: In function ‘acpi_ut_safe_strncpy’:
../../../../../drivers/acpi/acpica/utnonansi.c:171:9: error: implicit declaration of function ‘strscpy_pad’ [-Wimplicit-function-declaration]
  171 |         strscpy_pad(dest, source, dest_size);
      |         ^~~~~~~~~~~
make: *** [../../Makefile.rules:25: /home/xslaby/linux/tools/power/acpi/ tools/acpidump/utnonansi.o] Chyba 1

Quoted 24 lines
Quoted 14 lines
Quoted 14 lines

(This is one of the last users of strncpy in the kernel.)
---
  drivers/acpi/acpica/utnonansi.c | 3 +--

  1 file changed, 1 insertion(+), 2 deletions(-)
drivers/acpi/acpica/utnonansi.c +0 -0
index ff0802ace19b..3a7952be6545 100644
@@ -168,8 +168,7 @@ void acpi_ut_safe_strncpy(char *dest, char 
 *source, acpi_size dest_size)
   {
          /* Always terminate destination string */
 
Quoted 8 lines
Quoted 7 lines
drivers/acpi/acpica/utnonansi.c +1 -2
-       strncpy(dest, source, dest_size);
-       dest[dest_size - 1] = 0;
+       strscpy_pad(dest, source, dest_size);
   }
 

  #endif

thanks,

Signature · 2 lines
js
suse labs