Re: [PATCH] audit: Use strscpy instead of memcpy when copying comm

From: Paul Moore
Date: Wed Jul 31 2024 - 11:51:38 EST


On Wed, Jul 31, 2024 at 3:46 AM Jinjie Ruan <ruanjinjie@xxxxxxxxxx> wrote:
>
> There may be random garbage beyond a string's null terminator, memcpy might
> use the entire comm array. so avoid that possibility by using strscpy
> instead of memcpy.
>
> Link: https://github.com/KSPP/linux/issues/90
> Signed-off-by: Jinjie Ruan <ruanjinjie@xxxxxxxxxx>
> ---
> kernel/auditsc.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)

If you look at audit_log_pid_context() you'll see that we don't record
the entire task::comm field, we only record up the NUL byte, so any
garbage present after the end of the string should not make it into
the audit record. We use memcpy(), as opposed to any of the string
based copy functions, as the task::comm field is relatively short and
having to count the length of the string in addition to copying the
string is likely more expensive than simply copying the full buffer.

--
paul-moore.com