Skip to content

Commit eceae70

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
selftests/bpf: Fix invalid use of strncat in test_sockmap
strncat()'s third argument is how many bytes will be added *in addition* to already existing bytes in destination. Plus extra zero byte will be added after that. So existing use in test_sockmap has many opportunities to overflow the string and cause memory corruptions. And in this case, GCC complains for a good reason. Fixes: 16962b2 ("bpf: sockmap, add selftests") Fixes: 73563aa ("selftests/bpf: test_sockmap, print additional test options") Fixes: 1ade9ab ("bpf: test_sockmap, add options for msg_pop_data() helper") Fixes: 463bac5 ("bpf, selftests: Add test for ktls with skb bpf ingress policy") Fixes: e9dd904 ("bpf: add tls support for testing in test_sockmap") Fixes: 753fb2e ("bpf: sockmap, add msg_peek tests to test_sockmap") Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20201203235440.2302137-2-andrii@kernel.org
1 parent 3015b50 commit eceae70

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

tools/testing/selftests/bpf/test_sockmap.c

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,16 @@ static char *test_to_str(int test)
12731273
return "unknown";
12741274
}
12751275

1276+
static void append_str(char *dst, const char *src, size_t dst_cap)
1277+
{
1278+
size_t avail = dst_cap - strlen(dst);
1279+
1280+
if (avail <= 1) /* just zero byte could be written */
1281+
return;
1282+
1283+
strncat(dst, src, avail - 1); /* strncat() adds + 1 for zero byte */
1284+
}
1285+
12761286
#define OPTSTRING 60
12771287
static void test_options(char *options)
12781288
{
@@ -1281,42 +1291,42 @@ static void test_options(char *options)
12811291
memset(options, 0, OPTSTRING);
12821292

12831293
if (txmsg_pass)
1284-
strncat(options, "pass,", OPTSTRING);
1294+
append_str(options, "pass,", OPTSTRING);
12851295
if (txmsg_redir)
1286-
strncat(options, "redir,", OPTSTRING);
1296+
append_str(options, "redir,", OPTSTRING);
12871297
if (txmsg_drop)
1288-
strncat(options, "drop,", OPTSTRING);
1298+
append_str(options, "drop,", OPTSTRING);
12891299
if (txmsg_apply) {
12901300
snprintf(tstr, OPTSTRING, "apply %d,", txmsg_apply);
1291-
strncat(options, tstr, OPTSTRING);
1301+
append_str(options, tstr, OPTSTRING);
12921302
}
12931303
if (txmsg_cork) {
12941304
snprintf(tstr, OPTSTRING, "cork %d,", txmsg_cork);
1295-
strncat(options, tstr, OPTSTRING);
1305+
append_str(options, tstr, OPTSTRING);
12961306
}
12971307
if (txmsg_start) {
12981308
snprintf(tstr, OPTSTRING, "start %d,", txmsg_start);
1299-
strncat(options, tstr, OPTSTRING);
1309+
append_str(options, tstr, OPTSTRING);
13001310
}
13011311
if (txmsg_end) {
13021312
snprintf(tstr, OPTSTRING, "end %d,", txmsg_end);
1303-
strncat(options, tstr, OPTSTRING);
1313+
append_str(options, tstr, OPTSTRING);
13041314
}
13051315
if (txmsg_start_pop) {
13061316
snprintf(tstr, OPTSTRING, "pop (%d,%d),",
13071317
txmsg_start_pop, txmsg_start_pop + txmsg_pop);
1308-
strncat(options, tstr, OPTSTRING);
1318+
append_str(options, tstr, OPTSTRING);
13091319
}
13101320
if (txmsg_ingress)
1311-
strncat(options, "ingress,", OPTSTRING);
1321+
append_str(options, "ingress,", OPTSTRING);
13121322
if (txmsg_redir_skb)
1313-
strncat(options, "redir_skb,", OPTSTRING);
1323+
append_str(options, "redir_skb,", OPTSTRING);
13141324
if (txmsg_ktls_skb)
1315-
strncat(options, "ktls_skb,", OPTSTRING);
1325+
append_str(options, "ktls_skb,", OPTSTRING);
13161326
if (ktls)
1317-
strncat(options, "ktls,", OPTSTRING);
1327+
append_str(options, "ktls,", OPTSTRING);
13181328
if (peek_flag)
1319-
strncat(options, "peek,", OPTSTRING);
1329+
append_str(options, "peek,", OPTSTRING);
13201330
}
13211331

13221332
static int __test_exec(int cgrp, int test, struct sockmap_options *opt)

0 commit comments

Comments
 (0)