Re: [PATCH RFC] ACPICA: remove acpi_ut_safe_strncpy in favor of strscpy

From: Kees Cook
Date: Thu Aug 24 2023 - 19:22:53 EST


On Thu, Aug 24, 2023 at 10:02:02PM +0000, Justin Stitt wrote:
> I wanted to gather some thoughts on removing `acpi_ut_safe_strncpy` (and
> potentially other `acpi...safe...()` interfaces) in favor of
> pre-existing interfaces in the kernel (like strscpy).
>
> Running a git blame shows these functions were implemented 10 years ago
> and their implementations generally mirror the _newer_ and more robust
> stuff in lib/string.h -- Let's just use these, right?
>
> I appreciate any comments and whether or not I should stop at just
> `strncpy`.

ACPICA is actually a separate upstream project, so changes are best made
there[1]. However, this code base is shared with many OSes and
compilers, so there won't be a common "strscpy" available. Perhaps the
right thing to do here is to implement acpi_ut_safe_strncpy() in terms
of strnlen(), memcpy(), and memset(). That would make the upstream
project safe against "too long reads", etc, and would require no
collateral changes:

void acpi_ut_safe_strncpy(char *dest, char *source, acpi_size dest_size)
{
/* Do not over-read the source string. */
acpi_size len = 0;

if (dest_size > 0)
len = strnlen(source, dest_size - 1);
if (len)
memcpy(dest, source, len)
/* Always terminate destination string and pad to dest_size. */
memset(dest + len, '\0', dest_size - len);
}

-Kees

[1] https://github.com/acpica/acpica
e.g. https://github.com/acpica/acpica/pull/856

--
Kees Cook