summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/uptr_failure.c
blob: 0cfa1fd6144095bbde62f0ac2a7b17025a74bbc8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */

#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include "bpf_experimental.h"
#include "bpf_misc.h"
#include "uptr_test_common.h"

struct {
	__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
	__uint(map_flags, BPF_F_NO_PREALLOC);
	__type(key, int);
	__type(value, struct value_type);
} datamap SEC(".maps");

SEC("?syscall")
__failure __msg("store to uptr disallowed")
int uptr_write(const void *ctx)
{
	struct task_struct *task;
	struct value_type *v;

	task = bpf_get_current_task_btf();
	v = bpf_task_storage_get(&datamap, task, 0,
				 BPF_LOCAL_STORAGE_GET_F_CREATE);
	if (!v)
		return 0;

	v->udata = NULL;
	return 0;
}

SEC("?syscall")
__failure __msg("store to uptr disallowed")
int uptr_write_nested(const void *ctx)
{
	struct task_struct *task;
	struct value_type *v;

	task = bpf_get_current_task_btf();
	v = bpf_task_storage_get(&datamap, task, 0,
				 BPF_LOCAL_STORAGE_GET_F_CREATE);
	if (!v)
		return 0;

	v->nested.udata = NULL;
	return 0;
}

SEC("?syscall")
__failure __msg("R1 invalid mem access 'mem_or_null'")
int uptr_no_null_check(const void *ctx)
{
	struct task_struct *task;
	struct value_type *v;

	task = bpf_get_current_task_btf();
	v = bpf_task_storage_get(&datamap, task, 0,
				 BPF_LOCAL_STORAGE_GET_F_CREATE);
	if (!v)
		return 0;

	v->udata->result = 0;

	return 0;
}

SEC("?syscall")
__failure __msg("doesn't point to kptr")
int uptr_kptr_xchg(const void *ctx)
{
	struct task_struct *task;
	struct value_type *v;

	task = bpf_get_current_task_btf();
	v = bpf_task_storage_get(&datamap, task, 0,
				 BPF_LOCAL_STORAGE_GET_F_CREATE);
	if (!v)
		return 0;

	bpf_kptr_xchg(&v->udata, NULL);

	return 0;
}

SEC("?syscall")
__failure __msg("invalid mem access 'scalar'")
int uptr_obj_new(const void *ctx)
{
	struct value_type *v;

	v = bpf_obj_new(typeof(*v));
	if (!v)
		return 0;

	if (v->udata)
		v->udata->result = 0;

	bpf_obj_drop(v);

	return 0;
}

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