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

selftests/bpf: cgroup_helpers.c: Fix strncpy() fortify warning

Copy libbpf_strlcpy() from libbpf_internal.h to bpf_util.h, and rename it
to bpf_strlcpy(), then replace selftests strncpy()/libbpf_strlcpy() with
bpf_strlcpy(), fix compile warning.

The libbpf_internal.h header cannot be used directly here, because
references to cgroup_helpers.c in samples/bpf will generate compilation
errors. We also can't add libbpf_strlcpy() directly to bpf_util.h,
because the definition of libbpf_strlcpy() in libbpf_internal.h is
duplicated. In order not to modify the libbpf code, add a new function
bpf_strlcpy() to selftests bpf_util.h.

How to reproduce this compilation warning:

$ make -C samples/bpf
cgroup_helpers.c: In function ‘__enable_controllers’:
cgroup_helpers.c:80:17: warning: ‘strncpy’ specified bound 4097 equals destination size [-Wstringop-truncation]
80 | strncpy(enable, controllers, sizeof(enable));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Rong Tao <rongtao@cestc.cn>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/tencent_469D8AF32BD56816A29981BED06E96D22506@qq.com

authored by

Rong Tao and committed by
Andrii Nakryiko
b3c09fdc 1baa7e38

+25 -23
+19
tools/testing/selftests/bpf/bpf_util.h
··· 20 20 return possible_cpus; 21 21 } 22 22 23 + /* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst 24 + * is zero-terminated string no matter what (unless sz == 0, in which case 25 + * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs 26 + * in what is returned. Given this is internal helper, it's trivial to extend 27 + * this, when necessary. Use this instead of strncpy inside libbpf source code. 28 + */ 29 + static inline void bpf_strlcpy(char *dst, const char *src, size_t sz) 30 + { 31 + size_t i; 32 + 33 + if (sz == 0) 34 + return; 35 + 36 + sz--; 37 + for (i = 0; i < sz && src[i]; i++) 38 + dst[i] = src[i]; 39 + dst[i] = '\0'; 40 + } 41 + 23 42 #define __bpf_percpu_val_align __attribute__((__aligned__(8))) 24 43 25 44 #define BPF_DECLARE_PERCPU(type, name) \
+2 -1
tools/testing/selftests/bpf/cgroup_helpers.c
··· 13 13 #include <ftw.h> 14 14 15 15 #include "cgroup_helpers.h" 16 + #include "bpf_util.h" 16 17 17 18 /* 18 19 * To avoid relying on the system setup, when setup_cgroup_env is called ··· 78 77 enable[len] = 0; 79 78 close(fd); 80 79 } else { 81 - strncpy(enable, controllers, sizeof(enable)); 80 + bpf_strlcpy(enable, controllers, sizeof(enable)); 82 81 } 83 82 84 83 snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);
+4 -22
tools/testing/selftests/bpf/xsk.c
··· 33 33 #include <bpf/bpf.h> 34 34 #include <bpf/libbpf.h> 35 35 #include "xsk.h" 36 + #include "bpf_util.h" 36 37 37 38 #ifndef SOL_XDP 38 39 #define SOL_XDP 283 ··· 522 521 return 0; 523 522 } 524 523 525 - /* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst 526 - * is zero-terminated string no matter what (unless sz == 0, in which case 527 - * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs 528 - * in what is returned. Given this is internal helper, it's trivial to extend 529 - * this, when necessary. Use this instead of strncpy inside libbpf source code. 530 - */ 531 - static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz) 532 - { 533 - size_t i; 534 - 535 - if (sz == 0) 536 - return; 537 - 538 - sz--; 539 - for (i = 0; i < sz && src[i]; i++) 540 - dst[i] = src[i]; 541 - dst[i] = '\0'; 542 - } 543 - 544 524 static int xsk_get_max_queues(struct xsk_socket *xsk) 545 525 { 546 526 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS }; ··· 534 552 return -errno; 535 553 536 554 ifr.ifr_data = (void *)&channels; 537 - libbpf_strlcpy(ifr.ifr_name, ctx->ifname, IFNAMSIZ); 555 + bpf_strlcpy(ifr.ifr_name, ctx->ifname, IFNAMSIZ); 538 556 err = ioctl(fd, SIOCETHTOOL, &ifr); 539 557 if (err && errno != EOPNOTSUPP) { 540 558 ret = -errno; ··· 753 771 } 754 772 755 773 ctx->ifindex = ifindex; 756 - libbpf_strlcpy(ctx->ifname, ifname, IFNAMSIZ); 774 + bpf_strlcpy(ctx->ifname, ifname, IFNAMSIZ); 757 775 758 776 xsk->ctx = ctx; 759 777 xsk->ctx->has_bpf_link = xsk_probe_bpf_link(); ··· 940 958 ctx->refcount = 1; 941 959 ctx->umem = umem; 942 960 ctx->queue_id = queue_id; 943 - libbpf_strlcpy(ctx->ifname, ifname, IFNAMSIZ); 961 + bpf_strlcpy(ctx->ifname, ifname, IFNAMSIZ); 944 962 945 963 ctx->fill = fill; 946 964 ctx->comp = comp;