Commit 9497c458 authored by Hao Luo's avatar Hao Luo Committed by Alexei Starovoitov
Browse files

bpf/selftests: Test PTR_TO_RDONLY_MEM



This test verifies that a ksym of non-struct can not be directly
updated.

Signed-off-by: default avatarHao Luo <haoluo@google.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20211217003152.48334-10-haoluo@google.com
parent 216e3cd2
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
#include "test_ksyms_btf_null_check.skel.h"
#include "test_ksyms_weak.skel.h"
#include "test_ksyms_weak.lskel.h"
#include "test_ksyms_btf_write_check.skel.h"

static int duration;

@@ -137,6 +138,16 @@ static void test_weak_syms_lskel(void)
	test_ksyms_weak_lskel__destroy(skel);
}

static void test_write_check(void)
{
	struct test_ksyms_btf_write_check *skel;

	skel = test_ksyms_btf_write_check__open_and_load();
	ASSERT_ERR_PTR(skel, "unexpected load of a prog writing to ksym memory\n");

	test_ksyms_btf_write_check__destroy(skel);
}

void test_ksyms_btf(void)
{
	int percpu_datasec;
@@ -167,4 +178,7 @@ void test_ksyms_btf(void)

	if (test__start_subtest("weak_ksyms_lskel"))
		test_weak_syms_lskel();

	if (test__start_subtest("write_check"))
		test_write_check();
}
+29 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2021 Google */

#include "vmlinux.h"

#include <bpf/bpf_helpers.h>

extern const int bpf_prog_active __ksym; /* int type global var. */

SEC("raw_tp/sys_enter")
int handler(const void *ctx)
{
	int *active;
	__u32 cpu;

	cpu = bpf_get_smp_processor_id();
	active = (int *)bpf_per_cpu_ptr(&bpf_prog_active, cpu);
	if (active) {
		/* Kernel memory obtained from bpf_{per,this}_cpu_ptr
		 * is read-only, should _not_ pass verification.
		 */
		/* WRITE_ONCE */
		*(volatile int *)active = -1;
	}

	return 0;
}

char _license[] SEC("license") = "GPL";