brintos

brintos / linux-shallow public Read only

0
0
Text · 4.3 KiB · 25332e5 Raw
164 lines · c
1// SPDX-License-Identifier: GPL-2.02/* Copyright (C) 2023 Yafang Shao <laoar.shao@gmail.com> */3 4#include <sys/types.h>5#include <unistd.h>6#include <test_progs.h>7#include "cgroup_helpers.h"8#include "test_cgroup1_hierarchy.skel.h"9 10static void bpf_cgroup1(struct test_cgroup1_hierarchy *skel)11{12	struct bpf_link *lsm_link, *fentry_link;13	int err;14 15	/* Attach LSM prog first */16	lsm_link = bpf_program__attach_lsm(skel->progs.lsm_run);17	if (!ASSERT_OK_PTR(lsm_link, "lsm_attach"))18		return;19 20	/* LSM prog will be triggered when attaching fentry */21	fentry_link = bpf_program__attach_trace(skel->progs.fentry_run);22	ASSERT_NULL(fentry_link, "fentry_attach_fail");23 24	err = bpf_link__destroy(lsm_link);25	ASSERT_OK(err, "destroy_lsm");26}27 28static void bpf_cgroup1_sleepable(struct test_cgroup1_hierarchy *skel)29{30	struct bpf_link *lsm_link, *fentry_link;31	int err;32 33	/* Attach LSM prog first */34	lsm_link = bpf_program__attach_lsm(skel->progs.lsm_s_run);35	if (!ASSERT_OK_PTR(lsm_link, "lsm_attach"))36		return;37 38	/* LSM prog will be triggered when attaching fentry */39	fentry_link = bpf_program__attach_trace(skel->progs.fentry_run);40	ASSERT_NULL(fentry_link, "fentry_attach_fail");41 42	err = bpf_link__destroy(lsm_link);43	ASSERT_OK(err, "destroy_lsm");44}45 46static void bpf_cgroup1_invalid_id(struct test_cgroup1_hierarchy *skel)47{48	struct bpf_link *lsm_link, *fentry_link;49	int err;50 51	/* Attach LSM prog first */52	lsm_link = bpf_program__attach_lsm(skel->progs.lsm_run);53	if (!ASSERT_OK_PTR(lsm_link, "lsm_attach"))54		return;55 56	/* LSM prog will be triggered when attaching fentry */57	fentry_link = bpf_program__attach_trace(skel->progs.fentry_run);58	if (!ASSERT_OK_PTR(fentry_link, "fentry_attach_success"))59		goto cleanup;60 61	err = bpf_link__destroy(fentry_link);62	ASSERT_OK(err, "destroy_lsm");63 64cleanup:65	err = bpf_link__destroy(lsm_link);66	ASSERT_OK(err, "destroy_fentry");67}68 69void test_cgroup1_hierarchy(void)70{71	struct test_cgroup1_hierarchy *skel;72	__u64 current_cgid;73	int hid, err;74 75	skel = test_cgroup1_hierarchy__open();76	if (!ASSERT_OK_PTR(skel, "open"))77		return;78 79	skel->bss->target_pid = getpid();80 81	err = bpf_program__set_attach_target(skel->progs.fentry_run, 0, "bpf_fentry_test1");82	if (!ASSERT_OK(err, "fentry_set_target"))83		goto destroy;84 85	err = test_cgroup1_hierarchy__load(skel);86	if (!ASSERT_OK(err, "load"))87		goto destroy;88 89	/* Setup cgroup1 hierarchy */90	err = setup_cgroup_environment();91	if (!ASSERT_OK(err, "setup_cgroup_environment"))92		goto destroy;93	err = setup_classid_environment();94	if (!ASSERT_OK(err, "setup_classid_environment"))95		goto cleanup_cgroup;96 97	err = join_classid();98	if (!ASSERT_OK(err, "join_cgroup1"))99		goto cleanup;100 101	current_cgid = get_classid_cgroup_id();102	if (!ASSERT_GE(current_cgid, 0, "cgroup1 id"))103		goto cleanup;104 105	hid = get_cgroup1_hierarchy_id("net_cls");106	if (!ASSERT_GE(hid, 0, "cgroup1 id"))107		goto cleanup;108	skel->bss->target_hid = hid;109 110	if (test__start_subtest("test_cgroup1_hierarchy")) {111		skel->bss->target_ancestor_cgid = current_cgid;112		bpf_cgroup1(skel);113	}114 115	if (test__start_subtest("test_root_cgid")) {116		skel->bss->target_ancestor_cgid = 1;117		skel->bss->target_ancestor_level = 0;118		bpf_cgroup1(skel);119	}120 121	if (test__start_subtest("test_invalid_level")) {122		skel->bss->target_ancestor_cgid = 1;123		skel->bss->target_ancestor_level = 1;124		bpf_cgroup1_invalid_id(skel);125	}126 127	if (test__start_subtest("test_invalid_cgid")) {128		skel->bss->target_ancestor_cgid = 0;129		bpf_cgroup1_invalid_id(skel);130	}131 132	if (test__start_subtest("test_invalid_hid")) {133		skel->bss->target_ancestor_cgid = 1;134		skel->bss->target_ancestor_level = 0;135		skel->bss->target_hid = -1;136		bpf_cgroup1_invalid_id(skel);137	}138 139	if (test__start_subtest("test_invalid_cgrp_name")) {140		skel->bss->target_hid = get_cgroup1_hierarchy_id("net_cl");141		skel->bss->target_ancestor_cgid = current_cgid;142		bpf_cgroup1_invalid_id(skel);143	}144 145	if (test__start_subtest("test_invalid_cgrp_name2")) {146		skel->bss->target_hid = get_cgroup1_hierarchy_id("net_cls,");147		skel->bss->target_ancestor_cgid = current_cgid;148		bpf_cgroup1_invalid_id(skel);149	}150 151	if (test__start_subtest("test_sleepable_prog")) {152		skel->bss->target_hid = hid;153		skel->bss->target_ancestor_cgid = current_cgid;154		bpf_cgroup1_sleepable(skel);155	}156 157cleanup:158	cleanup_classid_environment();159cleanup_cgroup:160	cleanup_cgroup_environment();161destroy:162	test_cgroup1_hierarchy__destroy(skel);163}164