cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

dynptr.c (4280B)


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