1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 
4 void test_obj_name(void)
5 {
6 	struct {
7 		const char *name;
8 		int success;
9 		int expected_errno;
10 	} tests[] = {
11 		{ "", 1, 0 },
12 		{ "_123456789ABCDE", 1, 0 },
13 		{ "_123456789ABCDEF", 0, EINVAL },
14 		{ "_123456789ABCD\n", 0, EINVAL },
15 	};
16 	struct bpf_insn prog[] = {
17 		BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 0),
18 		BPF_EXIT_INSN(),
19 	};
20 	__u32 duration = 0;
21 	int i;
22 
23 	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
24 		size_t name_len = strlen(tests[i].name) + 1;
25 		union bpf_attr attr;
26 		size_t ncopy;
27 		int fd;
28 
29 		/* test different attr.prog_name during BPF_PROG_LOAD */
30 		ncopy = name_len < sizeof(attr.prog_name) ?
31 			name_len : sizeof(attr.prog_name);
32 		bzero(&attr, sizeof(attr));
33 		attr.prog_type = BPF_PROG_TYPE_SCHED_CLS;
34 		attr.insn_cnt = 2;
35 		attr.insns = ptr_to_u64(prog);
36 		attr.license = ptr_to_u64("");
37 		memcpy(attr.prog_name, tests[i].name, ncopy);
38 
39 		fd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
40 		CHECK((tests[i].success && fd < 0) ||
41 		      (!tests[i].success && fd >= 0) ||
42 		      (!tests[i].success && errno != tests[i].expected_errno),
43 		      "check-bpf-prog-name",
44 		      "fd %d(%d) errno %d(%d)\n",
45 		       fd, tests[i].success, errno, tests[i].expected_errno);
46 
47 		if (fd >= 0)
48 			close(fd);
49 
50 		/* test different attr.map_name during BPF_MAP_CREATE */
51 		ncopy = name_len < sizeof(attr.map_name) ?
52 			name_len : sizeof(attr.map_name);
53 		bzero(&attr, sizeof(attr));
54 		attr.map_type = BPF_MAP_TYPE_ARRAY;
55 		attr.key_size = 4;
56 		attr.value_size = 4;
57 		attr.max_entries = 1;
58 		attr.map_flags = 0;
59 		memcpy(attr.map_name, tests[i].name, ncopy);
60 		fd = syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
61 		CHECK((tests[i].success && fd < 0) ||
62 		      (!tests[i].success && fd >= 0) ||
63 		      (!tests[i].success && errno != tests[i].expected_errno),
64 		      "check-bpf-map-name",
65 		      "fd %d(%d) errno %d(%d)\n",
66 		      fd, tests[i].success, errno, tests[i].expected_errno);
67 
68 		if (fd >= 0)
69 			close(fd);
70 	}
71 }
72