Skip to content

Commit 6a087ac

Browse files
theihorAlexei Starovoitov
authored andcommitted
selftests/bpf: Replace strcpy() calls with strscpy()
strcpy() does not perform bounds checking and is considered deprecated [1]. Replace strcpy() calls with strscpy() defined in bpf_util.h. [1] https://docs.kernel.org/process/deprecated.html#strcpy Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev> Link: https://lore.kernel.org/r/20260223190736.649171-3-ihor.solodrai@linux.dev Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 63c49ef commit 6a087ac

6 files changed

Lines changed: 8 additions & 7 deletions

File tree

tools/testing/selftests/bpf/network_helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ int make_sockaddr(int family, const char *addr_str, __u16 port,
432432
memset(addr, 0, sizeof(*sun));
433433
sun->sun_family = family;
434434
sun->sun_path[0] = 0;
435-
strcpy(sun->sun_path + 1, addr_str);
435+
strscpy(sun->sun_path + 1, addr_str, sizeof(sun->sun_path) - 1);
436436
if (len)
437437
*len = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(addr_str);
438438
return 0;

tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ static void test_dctcp_fallback(void)
281281
dctcp_skel = bpf_dctcp__open();
282282
if (!ASSERT_OK_PTR(dctcp_skel, "dctcp_skel"))
283283
return;
284-
strcpy(dctcp_skel->rodata->fallback_cc, "cubic");
284+
strscpy(dctcp_skel->rodata->fallback_cc, "cubic");
285285
if (!ASSERT_OK(bpf_dctcp__load(dctcp_skel), "bpf_dctcp__load"))
286286
goto done;
287287

tools/testing/selftests/bpf/prog_tests/setget_sockopt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ void test_setget_sockopt(void)
212212
if (!ASSERT_OK_PTR(skel, "open skel"))
213213
goto done;
214214

215-
strcpy(skel->rodata->veth, "binddevtest1");
215+
strscpy(skel->rodata->veth, "binddevtest1");
216216
skel->rodata->veth_ifindex = if_nametoindex("binddevtest1");
217217
if (!ASSERT_GT(skel->rodata->veth_ifindex, 0, "if_nametoindex"))
218218
goto done;

tools/testing/selftests/bpf/prog_tests/sockopt_sk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ static int getsetsockopt(void)
142142

143143
/* TCP_CONGESTION can extend the string */
144144

145-
strcpy(buf.cc, "nv");
145+
strscpy(buf.cc, "nv");
146146
err = setsockopt(fd, SOL_TCP, TCP_CONGESTION, &buf, strlen("nv"));
147147
if (err) {
148148
log_err("Failed to call setsockopt(TCP_CONGESTION)");

tools/testing/selftests/bpf/prog_tests/test_veristat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ static struct fixture *init_fixture(void)
2424

2525
/* for no_alu32 and cpuv4 veristat is in parent folder */
2626
if (access("./veristat", F_OK) == 0)
27-
strcpy(fix->veristat, "./veristat");
27+
strscpy(fix->veristat, "./veristat");
2828
else if (access("../veristat", F_OK) == 0)
29-
strcpy(fix->veristat, "../veristat");
29+
strscpy(fix->veristat, "../veristat");
3030
else
3131
PRINT_FAIL("Can't find veristat binary");
3232

tools/testing/selftests/bpf/xdp_features.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <network_helpers.h>
1818

19+
#include "bpf_util.h"
1920
#include "xdp_features.skel.h"
2021
#include "xdp_features.h"
2122

@@ -212,7 +213,7 @@ static void set_env_default(void)
212213
env.feature.drv_feature = NETDEV_XDP_ACT_NDO_XMIT;
213214
env.feature.action = -EINVAL;
214215
env.ifindex = -ENODEV;
215-
strcpy(env.ifname, "unknown");
216+
strscpy(env.ifname, "unknown");
216217
make_sockaddr(AF_INET6, "::ffff:127.0.0.1", DUT_CTRL_PORT,
217218
&env.dut_ctrl_addr, NULL);
218219
make_sockaddr(AF_INET6, "::ffff:127.0.0.1", DUT_ECHO_PORT,

0 commit comments

Comments
 (0)