1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Facebook */
3 
4 #include <test_progs.h>
5 #include "dynptr_fail.skel.h"
6 #include "dynptr_success.skel.h"
7 
8 static size_t log_buf_sz = 1048576; /* 1 MB */
9 static char obj_log_buf[1048576];
10 
11 static struct {
12 	const char *prog_name;
13 	const char *expected_err_msg;
14 } dynptr_tests[] = {
15 	/* failure cases */
16 	{"ringbuf_missing_release1", "Unreleased reference id=1"},
17 	{"ringbuf_missing_release2", "Unreleased reference id=2"},
18 	{"ringbuf_missing_release_callback", "Unreleased reference id"},
19 	{"use_after_invalid", "Expected an initialized dynptr as arg #3"},
20 	{"ringbuf_invalid_api", "type=mem expected=alloc_mem"},
21 	{"add_dynptr_to_map1", "invalid indirect read from stack"},
22 	{"add_dynptr_to_map2", "invalid indirect read from stack"},
23 	{"data_slice_out_of_bounds_ringbuf", "value is outside of the allowed memory range"},
24 	{"data_slice_out_of_bounds_map_value", "value is outside of the allowed memory range"},
25 	{"data_slice_use_after_release1", "invalid mem access 'scalar'"},
26 	{"data_slice_use_after_release2", "invalid mem access 'scalar'"},
27 	{"data_slice_missing_null_check1", "invalid mem access 'mem_or_null'"},
28 	{"data_slice_missing_null_check2", "invalid mem access 'mem_or_null'"},
29 	{"invalid_helper1", "invalid indirect read from stack"},
30 	{"invalid_helper2", "Expected an initialized dynptr as arg #3"},
31 	{"invalid_write1", "Expected an initialized dynptr as arg #1"},
32 	{"invalid_write2", "Expected an initialized dynptr as arg #3"},
33 	{"invalid_write3", "Expected an initialized dynptr as arg #1"},
34 	{"invalid_write4", "arg 1 is an unacquired reference"},
35 	{"invalid_read1", "invalid read from stack"},
36 	{"invalid_read2", "cannot pass in dynptr at an offset"},
37 	{"invalid_read3", "invalid read from stack"},
38 	{"invalid_read4", "invalid read from stack"},
39 	{"invalid_offset", "invalid write to stack"},
40 	{"global", "type=map_value expected=fp"},
41 	{"release_twice", "arg 1 is an unacquired reference"},
42 	{"release_twice_callback", "arg 1 is an unacquired reference"},
43 	{"dynptr_from_mem_invalid_api",
44 		"Unsupported reg type fp for bpf_dynptr_from_mem data"},
45 
46 	/* success cases */
47 	{"test_read_write", NULL},
48 	{"test_data_slice", NULL},
49 	{"test_ringbuf", NULL},
50 };
51 
52 static void verify_fail(const char *prog_name, const char *expected_err_msg)
53 {
54 	LIBBPF_OPTS(bpf_object_open_opts, opts);
55 	struct bpf_program *prog;
56 	struct dynptr_fail *skel;
57 	int err;
58 
59 	opts.kernel_log_buf = obj_log_buf;
60 	opts.kernel_log_size = log_buf_sz;
61 	opts.kernel_log_level = 1;
62 
63 	skel = dynptr_fail__open_opts(&opts);
64 	if (!ASSERT_OK_PTR(skel, "dynptr_fail__open_opts"))
65 		goto cleanup;
66 
67 	prog = bpf_object__find_program_by_name(skel->obj, prog_name);
68 	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
69 		goto cleanup;
70 
71 	bpf_program__set_autoload(prog, true);
72 
73 	bpf_map__set_max_entries(skel->maps.ringbuf, getpagesize());
74 
75 	err = dynptr_fail__load(skel);
76 	if (!ASSERT_ERR(err, "unexpected load success"))
77 		goto cleanup;
78 
79 	if (!ASSERT_OK_PTR(strstr(obj_log_buf, expected_err_msg), "expected_err_msg")) {
80 		fprintf(stderr, "Expected err_msg: %s\n", expected_err_msg);
81 		fprintf(stderr, "Verifier output: %s\n", obj_log_buf);
82 	}
83 
84 cleanup:
85 	dynptr_fail__destroy(skel);
86 }
87 
88 static void verify_success(const char *prog_name)
89 {
90 	struct dynptr_success *skel;
91 	struct bpf_program *prog;
92 	struct bpf_link *link;
93 
94 	skel = dynptr_success__open();
95 	if (!ASSERT_OK_PTR(skel, "dynptr_success__open"))
96 		return;
97 
98 	skel->bss->pid = getpid();
99 
100 	bpf_map__set_max_entries(skel->maps.ringbuf, getpagesize());
101 
102 	dynptr_success__load(skel);
103 	if (!ASSERT_OK_PTR(skel, "dynptr_success__load"))
104 		goto cleanup;
105 
106 	prog = bpf_object__find_program_by_name(skel->obj, prog_name);
107 	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
108 		goto cleanup;
109 
110 	link = bpf_program__attach(prog);
111 	if (!ASSERT_OK_PTR(link, "bpf_program__attach"))
112 		goto cleanup;
113 
114 	usleep(1);
115 
116 	ASSERT_EQ(skel->bss->err, 0, "err");
117 
118 	bpf_link__destroy(link);
119 
120 cleanup:
121 	dynptr_success__destroy(skel);
122 }
123 
124 void test_dynptr(void)
125 {
126 	int i;
127 
128 	for (i = 0; i < ARRAY_SIZE(dynptr_tests); i++) {
129 		if (!test__start_subtest(dynptr_tests[i].prog_name))
130 			continue;
131 
132 		if (dynptr_tests[i].expected_err_msg)
133 			verify_fail(dynptr_tests[i].prog_name,
134 				    dynptr_tests[i].expected_err_msg);
135 		else
136 			verify_success(dynptr_tests[i].prog_name);
137 	}
138 }
139