]> git.baikalelectronics.ru Git - kernel.git/commit
libbpf: Add sane strncpy alternative and use it internally
authorAndrii Nakryiko <andrii@kernel.org>
Sat, 11 Dec 2021 00:40:43 +0000 (16:40 -0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Tue, 14 Dec 2021 14:47:56 +0000 (15:47 +0100)
commit7937bce132c977e075b87676d26802fc37638a94
tree9964b204c6a1721168b7041e060df59b5123ce7a
parent08b1ab13e1f7dd4f400f0d0ad568c79dee137eba
libbpf: Add sane strncpy alternative and use it internally

strncpy() has a notoriously error-prone semantics which makes GCC
complain about it a lot (and quite often completely completely falsely
at that). Instead of pleasing GCC all the time (-Wno-stringop-truncation
is unfortunately only supported by GCC, so it's a bit too messy to just
enable it in Makefile), add libbpf-internal libbpf_strlcpy() helper
which follows what FreeBSD's strlcpy() does and what most people would
expect from strncpy(): copies up to N-1 first bytes from source string
into destination string and ensures zero-termination afterwards.

Replace all the relevant uses of strncpy/strncat/memcpy in libbpf with
libbpf_strlcpy().

This also fixes the issue reported by Emmanuel Deloget in xsk.c where
memcpy() could access source string beyond its end.

Fixes: d31988fd4fb85 (libbpf: Support shared umems between queues and devices)
Reported-by: Emmanuel Deloget <emmanuel.deloget@eho.link>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20211211004043.2374068-1-andrii@kernel.org
tools/lib/bpf/bpf.c
tools/lib/bpf/btf_dump.c
tools/lib/bpf/gen_loader.c
tools/lib/bpf/libbpf.c
tools/lib/bpf/libbpf_internal.h
tools/lib/bpf/xsk.c