Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
  3
  4#define _GNU_SOURCE
  5#include <sys/wait.h>
  6#include <test_progs.h>
  7#include <unistd.h>
  8
  9#include "task_kfunc_failure.skel.h"
 10#include "task_kfunc_success.skel.h"
 11
 
 
 
 12static struct task_kfunc_success *open_load_task_kfunc_skel(void)
 13{
 14	struct task_kfunc_success *skel;
 15	int err;
 16
 17	skel = task_kfunc_success__open();
 18	if (!ASSERT_OK_PTR(skel, "skel_open"))
 19		return NULL;
 20
 21	skel->bss->pid = getpid();
 22
 23	err = task_kfunc_success__load(skel);
 24	if (!ASSERT_OK(err, "skel_load"))
 25		goto cleanup;
 26
 27	return skel;
 28
 29cleanup:
 30	task_kfunc_success__destroy(skel);
 31	return NULL;
 32}
 33
 34static void run_success_test(const char *prog_name)
 35{
 36	struct task_kfunc_success *skel;
 37	int status;
 38	pid_t child_pid;
 39	struct bpf_program *prog;
 40	struct bpf_link *link = NULL;
 41
 42	skel = open_load_task_kfunc_skel();
 43	if (!ASSERT_OK_PTR(skel, "open_load_skel"))
 44		return;
 45
 46	if (!ASSERT_OK(skel->bss->err, "pre_spawn_err"))
 47		goto cleanup;
 48
 49	prog = bpf_object__find_program_by_name(skel->obj, prog_name);
 50	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
 51		goto cleanup;
 52
 53	link = bpf_program__attach(prog);
 54	if (!ASSERT_OK_PTR(link, "attached_link"))
 55		goto cleanup;
 56
 57	child_pid = fork();
 58	if (!ASSERT_GT(child_pid, -1, "child_pid"))
 59		goto cleanup;
 60	if (child_pid == 0)
 61		_exit(0);
 62	waitpid(child_pid, &status, 0);
 63
 64	ASSERT_OK(skel->bss->err, "post_wait_err");
 65
 66cleanup:
 67	bpf_link__destroy(link);
 68	task_kfunc_success__destroy(skel);
 69}
 70
 71static int run_vpid_test(void *prog_name)
 72{
 73	struct task_kfunc_success *skel;
 74	struct bpf_program *prog;
 75	int prog_fd, err = 0;
 76
 77	if (getpid() != 1)
 78		return 1;
 79
 80	skel = open_load_task_kfunc_skel();
 81	if (!skel)
 82		return 2;
 83
 84	if (skel->bss->err) {
 85		err = 3;
 86		goto cleanup;
 87	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 88
 89	prog = bpf_object__find_program_by_name(skel->obj, prog_name);
 90	if (!prog) {
 91		err = 4;
 92		goto cleanup;
 93	}
 94
 95	prog_fd = bpf_program__fd(prog);
 96	if (prog_fd < 0) {
 97		err = 5;
 98		goto cleanup;
 99	}
100
101	if (bpf_prog_test_run_opts(prog_fd, NULL)) {
102		err = 6;
103		goto cleanup;
104	}
105
106	if (skel->bss->err)
107		err = 7 + skel->bss->err;
108cleanup:
109	task_kfunc_success__destroy(skel);
110	return err;
111}
112
113static void run_vpid_success_test(const char *prog_name)
114{
115	const int stack_size = 1024 * 1024;
116	int child_pid, wstatus;
117	char *stack;
118
119	stack = (char *)malloc(stack_size);
120	if (!ASSERT_OK_PTR(stack, "clone_stack"))
121		return;
122
123	child_pid = clone(run_vpid_test, stack + stack_size,
124			  CLONE_NEWPID | SIGCHLD, (void *)prog_name);
125	if (!ASSERT_GT(child_pid, -1, "child_pid"))
126		goto cleanup;
127
128	if (!ASSERT_GT(waitpid(child_pid, &wstatus, 0), -1, "waitpid"))
129		goto cleanup;
 
 
130
131	if (WEXITSTATUS(wstatus) > 7)
132		ASSERT_OK(WEXITSTATUS(wstatus) - 7, "vpid_test_failure");
133	else
134		ASSERT_OK(WEXITSTATUS(wstatus), "run_vpid_test_err");
135cleanup:
136	free(stack);
137}
138
139static const char * const success_tests[] = {
140	"test_task_acquire_release_argument",
141	"test_task_acquire_release_current",
142	"test_task_acquire_leave_in_map",
143	"test_task_xchg_release",
144	"test_task_map_acquire_release",
145	"test_task_current_acquire_release",
146	"test_task_from_pid_arg",
147	"test_task_from_pid_current",
148	"test_task_from_pid_invalid",
149	"task_kfunc_acquire_trusted_walked",
150	"test_task_kfunc_flavor_relo",
151	"test_task_kfunc_flavor_relo_not_found",
152};
153
154static const char * const vpid_success_tests[] = {
155	"test_task_from_vpid_current",
156	"test_task_from_vpid_invalid",
157};
158
159void test_task_kfunc(void)
160{
161	int i;
162
163	for (i = 0; i < ARRAY_SIZE(success_tests); i++) {
164		if (!test__start_subtest(success_tests[i]))
165			continue;
166
167		run_success_test(success_tests[i]);
168	}
169
170	for (i = 0; i < ARRAY_SIZE(vpid_success_tests); i++) {
171		if (!test__start_subtest(vpid_success_tests[i]))
172			continue;
173
174		run_vpid_success_test(vpid_success_tests[i]);
175	}
176
177	RUN_TESTS(task_kfunc_failure);
178}
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
  3
  4#define _GNU_SOURCE
  5#include <sys/wait.h>
  6#include <test_progs.h>
  7#include <unistd.h>
  8
  9#include "task_kfunc_failure.skel.h"
 10#include "task_kfunc_success.skel.h"
 11
 12static size_t log_buf_sz = 1 << 20; /* 1 MB */
 13static char obj_log_buf[1048576];
 14
 15static struct task_kfunc_success *open_load_task_kfunc_skel(void)
 16{
 17	struct task_kfunc_success *skel;
 18	int err;
 19
 20	skel = task_kfunc_success__open();
 21	if (!ASSERT_OK_PTR(skel, "skel_open"))
 22		return NULL;
 23
 24	skel->bss->pid = getpid();
 25
 26	err = task_kfunc_success__load(skel);
 27	if (!ASSERT_OK(err, "skel_load"))
 28		goto cleanup;
 29
 30	return skel;
 31
 32cleanup:
 33	task_kfunc_success__destroy(skel);
 34	return NULL;
 35}
 36
 37static void run_success_test(const char *prog_name)
 38{
 39	struct task_kfunc_success *skel;
 40	int status;
 41	pid_t child_pid;
 42	struct bpf_program *prog;
 43	struct bpf_link *link = NULL;
 44
 45	skel = open_load_task_kfunc_skel();
 46	if (!ASSERT_OK_PTR(skel, "open_load_skel"))
 47		return;
 48
 49	if (!ASSERT_OK(skel->bss->err, "pre_spawn_err"))
 50		goto cleanup;
 51
 52	prog = bpf_object__find_program_by_name(skel->obj, prog_name);
 53	if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
 54		goto cleanup;
 55
 56	link = bpf_program__attach(prog);
 57	if (!ASSERT_OK_PTR(link, "attached_link"))
 58		goto cleanup;
 59
 60	child_pid = fork();
 61	if (!ASSERT_GT(child_pid, -1, "child_pid"))
 62		goto cleanup;
 63	if (child_pid == 0)
 64		_exit(0);
 65	waitpid(child_pid, &status, 0);
 66
 67	ASSERT_OK(skel->bss->err, "post_wait_err");
 68
 69cleanup:
 70	bpf_link__destroy(link);
 71	task_kfunc_success__destroy(skel);
 72}
 73
 74static const char * const success_tests[] = {
 75	"test_task_acquire_release_argument",
 76	"test_task_acquire_release_current",
 77	"test_task_acquire_leave_in_map",
 78	"test_task_xchg_release",
 79	"test_task_get_release",
 80	"test_task_current_acquire_release",
 81	"test_task_from_pid_arg",
 82	"test_task_from_pid_current",
 83	"test_task_from_pid_invalid",
 84};
 
 85
 86static struct {
 87	const char *prog_name;
 88	const char *expected_err_msg;
 89} failure_tests[] = {
 90	{"task_kfunc_acquire_untrusted", "R1 must be referenced or trusted"},
 91	{"task_kfunc_acquire_fp", "arg#0 pointer type STRUCT task_struct must point"},
 92	{"task_kfunc_acquire_unsafe_kretprobe", "reg type unsupported for arg#0 function"},
 93	{"task_kfunc_acquire_trusted_walked", "R1 must be referenced or trusted"},
 94	{"task_kfunc_acquire_null", "arg#0 pointer type STRUCT task_struct must point"},
 95	{"task_kfunc_acquire_unreleased", "Unreleased reference"},
 96	{"task_kfunc_get_non_kptr_param", "arg#0 expected pointer to map value"},
 97	{"task_kfunc_get_non_kptr_acquired", "arg#0 expected pointer to map value"},
 98	{"task_kfunc_get_null", "arg#0 expected pointer to map value"},
 99	{"task_kfunc_xchg_unreleased", "Unreleased reference"},
100	{"task_kfunc_get_unreleased", "Unreleased reference"},
101	{"task_kfunc_release_untrusted", "arg#0 is untrusted_ptr_or_null_ expected ptr_ or socket"},
102	{"task_kfunc_release_fp", "arg#0 pointer type STRUCT task_struct must point"},
103	{"task_kfunc_release_null", "arg#0 is ptr_or_null_ expected ptr_ or socket"},
104	{"task_kfunc_release_unacquired", "release kernel function bpf_task_release expects"},
105	{"task_kfunc_from_pid_no_null_check", "arg#0 is ptr_or_null_ expected ptr_ or socket"},
106	{"task_kfunc_from_lsm_task_free", "reg type unsupported for arg#0 function"},
107};
108
109static void verify_fail(const char *prog_name, const char *expected_err_msg)
110{
111	LIBBPF_OPTS(bpf_object_open_opts, opts);
112	struct task_kfunc_failure *skel;
113	int err, i;
114
115	opts.kernel_log_buf = obj_log_buf;
116	opts.kernel_log_size = log_buf_sz;
117	opts.kernel_log_level = 1;
 
 
118
119	skel = task_kfunc_failure__open_opts(&opts);
120	if (!ASSERT_OK_PTR(skel, "task_kfunc_failure__open_opts"))
121		goto cleanup;
 
122
123	for (i = 0; i < ARRAY_SIZE(failure_tests); i++) {
124		struct bpf_program *prog;
125		const char *curr_name = failure_tests[i].prog_name;
 
 
 
126
127		prog = bpf_object__find_program_by_name(skel->obj, curr_name);
128		if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
129			goto cleanup;
 
 
130
131		bpf_program__set_autoload(prog, !strcmp(curr_name, prog_name));
132	}
 
133
134	err = task_kfunc_failure__load(skel);
135	if (!ASSERT_ERR(err, "unexpected load success"))
 
136		goto cleanup;
137
138	if (!ASSERT_OK_PTR(strstr(obj_log_buf, expected_err_msg), "expected_err_msg")) {
139		fprintf(stderr, "Expected err_msg: %s\n", expected_err_msg);
140		fprintf(stderr, "Verifier output: %s\n", obj_log_buf);
141	}
142
 
 
 
 
143cleanup:
144	task_kfunc_failure__destroy(skel);
145}
146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147void test_task_kfunc(void)
148{
149	int i;
150
151	for (i = 0; i < ARRAY_SIZE(success_tests); i++) {
152		if (!test__start_subtest(success_tests[i]))
153			continue;
154
155		run_success_test(success_tests[i]);
156	}
157
158	for (i = 0; i < ARRAY_SIZE(failure_tests); i++) {
159		if (!test__start_subtest(failure_tests[i].prog_name))
160			continue;
161
162		verify_fail(failure_tests[i].prog_name, failure_tests[i].expected_err_msg);
163	}
 
 
164}