Skip to content

Commit 3c10e0a

Browse files
musamaanjumKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
selftests/bpf: Move test_dev_cgroup to prog_tests
Move test_dev_cgroup to prog_tests to be able to run it with test_progs. Replace dev_cgroup.bpf.o with skel header file, dev_cgroup.skel.h and load program from it accourdingly. ./test_progs -t test_dev_cgroup mknod: /tmp/test_dev_cgroup_null: Operation not permitted 64+0 records in 64+0 records out 32768 bytes (33 kB, 32 KiB) copied, 0.000856684 s, 38.2 MB/s dd: failed to open '/dev/full': Operation not permitted dd: failed to open '/dev/random': Operation not permitted #365 test_dev_cgroup:OK Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Muhammad Usama Anjum <[email protected]>
1 parent c7a28df commit 3c10e0a

File tree

2 files changed

+67
-85
lines changed

2 files changed

+67
-85
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/* Copyright (c) 2017 Facebook
3+
*/
4+
5+
#include <test_progs.h>
6+
#include <time.h>
7+
#include "cgroup_helpers.h"
8+
#include "dev_cgroup.skel.h"
9+
10+
#define TEST_CGROUP "/test-bpf-based-device-cgroup/"
11+
12+
void test_test_dev_cgroup(void)
13+
{
14+
int cgroup_fd, err, duration = 0;
15+
struct dev_cgroup *skel;
16+
__u32 prog_cnt;
17+
18+
skel = dev_cgroup__open_and_load();
19+
if (CHECK(!skel, "skel_open_and_load", "failed\n"))
20+
goto cleanup;
21+
22+
cgroup_fd = cgroup_setup_and_join(TEST_CGROUP);
23+
if (CHECK(cgroup_fd < 0, "cgroup_setup_and_join", "failed: %d\n", cgroup_fd))
24+
goto cleanup;
25+
26+
err = bpf_prog_attach(bpf_program__fd(skel->progs.bpf_prog1), cgroup_fd,
27+
BPF_CGROUP_DEVICE, 0);
28+
if (CHECK(err, "bpf_attach", "failed: %d\n", err))
29+
goto cleanup;
30+
31+
err = bpf_prog_query(cgroup_fd, BPF_CGROUP_DEVICE, 0, NULL, NULL, &prog_cnt);
32+
if (CHECK(err || prog_cnt != 1, "bpf_query", "failed: %d %d\n", err, prog_cnt))
33+
goto cleanup;
34+
35+
/* All operations with /dev/zero and /dev/urandom are allowed,
36+
* everything else is forbidden.
37+
*/
38+
CHECK(system("rm -f /tmp/test_dev_cgroup_null"), "rm",
39+
"unexpected rm on _null\n");
40+
CHECK(!system("mknod /tmp/test_dev_cgroup_null c 1 3"),
41+
"mknod", "unexpected mknod on _null\n");
42+
CHECK(system("rm -f /tmp/test_dev_cgroup_null"), "rm",
43+
"unexpected rm on _null\n");
44+
45+
/* /dev/zero is whitelisted */
46+
CHECK(system("rm -f /tmp/test_dev_cgroup_zero"), "rm",
47+
"unexpected rm on _zero\n");
48+
CHECK(system("mknod /tmp/test_dev_cgroup_zero c 1 5"),
49+
"mknod", "unexpected mknod on _zero\n");
50+
CHECK(system("rm -f /tmp/test_dev_cgroup_zero"), "rm",
51+
"unexpected rm on _zero\n");
52+
53+
CHECK(system("dd if=/dev/urandom of=/dev/zero count=64"), "dd",
54+
"unexpected dd on /dev/zero\n");
55+
56+
/* src is allowed, target is forbidden */
57+
CHECK(!system("dd if=/dev/urandom of=/dev/full count=64"), "dd",
58+
"unexpected dd on /dev/full\n");
59+
60+
/* src is forbidden, target is allowed */
61+
CHECK(!system("dd if=/dev/random of=/dev/zero count=64"), "dd",
62+
"unexpected dd on /dev/zero\n");
63+
64+
cleanup:
65+
cleanup_cgroup_environment();
66+
dev_cgroup__destroy(skel);
67+
}

tools/testing/selftests/bpf/test_dev_cgroup.c

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)