|
| 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 | +} |
0 commit comments