Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpftool: ensure task comm is always NUL-terminated

Let's explicitly ensure the destination string is NUL-terminated. This
way, it won't be affected by changes to the source string.

Link: https://lkml.kernel.org/r/20241007144911.27693-5-laoar.shao@gmail.com
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Reviewed-by: Quentin Monnet <qmo@kernel.org>
Cc: Alejandro Colomar <alx@kernel.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
Cc: David Airlie <airlied@gmail.com>
Cc: Eric Biederman <ebiederm@xmission.com>
Cc: Eric Paris <eparis@redhat.com>
Cc: James Morris <jmorris@namei.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Justin Stitt <justinstitt@google.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Matus Jokay <matus.jokay@stuba.sk>
Cc: Maxime Ripard <mripard@kernel.org>
Cc: Ondrej Mosnacek <omosnace@redhat.com>
Cc: Paul Moore <paul@paul-moore.com>
Cc: "Serge E. Hallyn" <serge@hallyn.com>
Cc: Simon Horman <horms@kernel.org>
Cc: Stephen Smalley <stephen.smalley.work@gmail.com>
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Cc: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Cc: Thomas Zimmermann <tzimmermann@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Yafang Shao and committed by
Andrew Morton
d967757d d4ee4ac3

+2
+2
tools/bpf/bpftool/pids.c
··· 54 54 ref = &refs->refs[refs->ref_cnt]; 55 55 ref->pid = e->pid; 56 56 memcpy(ref->comm, e->comm, sizeof(ref->comm)); 57 + ref->comm[sizeof(ref->comm) - 1] = '\0'; 57 58 refs->ref_cnt++; 58 59 59 60 return; ··· 78 77 ref = &refs->refs[0]; 79 78 ref->pid = e->pid; 80 79 memcpy(ref->comm, e->comm, sizeof(ref->comm)); 80 + ref->comm[sizeof(ref->comm) - 1] = '\0'; 81 81 refs->ref_cnt = 1; 82 82 refs->has_bpf_cookie = e->has_bpf_cookie; 83 83 refs->bpf_cookie = e->bpf_cookie;