On Mon, Mar 18, 2024 at 09:32:01PM +0000, Justin Stitt wrote: > strncpy() is deprecated for use on NUL-terminated destination strings > [1] and as such we should prefer more robust and less ambiguous string > interfaces. > > We expect db_root and db_root_stage to be NUL-terminated based on its > immediate use with pr_debug which expects a C-string argument (%s). > Moreover, it seems NUL-padding is not required. > > Considering the above, a suitable replacement is `strscpy` [2] due to > the fact that it guarantees NUL-termination on the destination buffer > without unnecessarily NUL-padding. > > Additionally, we should also change snprintf() to scnprintf(). > `read_bytes` may be improperly assigned as snprintf() does NOT return > the number of bytes written into the destination buffer, rather it > returns the number of bytes that COULD have been written to that buffer > if it had ample space. Conversely, scnprintf() returns the actual number > of bytes written into the destination buffer (except the NUL-byte). This > essentially means the ``if (!read_bytes)`` was probably never a possible > branch. > > After these changes, this code is more self-describing since it uses > string APIs that more accurately match the desired behavior. > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] > Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2] > Link: https://github.com/KSPP/linux/issues/90 > Cc: linux-hardening@xxxxxxxxxxxxxxx > Signed-off-by: Justin Stitt <justinstitt@xxxxxxxxxx> Good catch on "read_bytes"! Reviewed-by: Kees Cook <keescook@xxxxxxxxxxxx> -- Kees Cook