Commit | Line | Data |
---|---|---|
d0003ec0 AS |
1 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
2 | * | |
3 | * This program is free software; you can redistribute it and/or | |
4 | * modify it under the terms of version 2 of the GNU General Public | |
5 | * License as published by the Free Software Foundation. | |
6 | * | |
7 | * This program is distributed in the hope that it will be useful, but | |
8 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
10 | * General Public License for more details. | |
11 | */ | |
12 | #include <linux/bpf.h> | |
13 | #include <linux/rcupdate.h> | |
03e69b50 | 14 | #include <linux/random.h> |
c04167ce | 15 | #include <linux/smp.h> |
17ca8cbf | 16 | #include <linux/ktime.h> |
ffeedafb AS |
17 | #include <linux/sched.h> |
18 | #include <linux/uidgid.h> | |
d0003ec0 AS |
19 | |
20 | /* If kernel subsystem is allowing eBPF programs to call this function, | |
21 | * inside its own verifier_ops->get_func_proto() callback it should return | |
22 | * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments | |
23 | * | |
24 | * Different map implementations will rely on rcu in map methods | |
25 | * lookup/update/delete, therefore eBPF programs must run under rcu lock | |
26 | * if program is allowed to access maps, so check rcu_read_lock_held in | |
27 | * all three functions. | |
28 | */ | |
29 | static u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
30 | { | |
31 | /* verifier checked that R1 contains a valid pointer to bpf_map | |
32 | * and R2 points to a program stack and map->key_size bytes were | |
33 | * initialized | |
34 | */ | |
35 | struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
36 | void *key = (void *) (unsigned long) r2; | |
37 | void *value; | |
38 | ||
39 | WARN_ON_ONCE(!rcu_read_lock_held()); | |
40 | ||
41 | value = map->ops->map_lookup_elem(map, key); | |
42 | ||
43 | /* lookup() returns either pointer to element value or NULL | |
44 | * which is the meaning of PTR_TO_MAP_VALUE_OR_NULL type | |
45 | */ | |
46 | return (unsigned long) value; | |
47 | } | |
48 | ||
a2c83fff | 49 | const struct bpf_func_proto bpf_map_lookup_elem_proto = { |
3324b584 DB |
50 | .func = bpf_map_lookup_elem, |
51 | .gpl_only = false, | |
52 | .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, | |
53 | .arg1_type = ARG_CONST_MAP_PTR, | |
54 | .arg2_type = ARG_PTR_TO_MAP_KEY, | |
d0003ec0 AS |
55 | }; |
56 | ||
57 | static u64 bpf_map_update_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
58 | { | |
59 | struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
60 | void *key = (void *) (unsigned long) r2; | |
61 | void *value = (void *) (unsigned long) r3; | |
62 | ||
63 | WARN_ON_ONCE(!rcu_read_lock_held()); | |
64 | ||
65 | return map->ops->map_update_elem(map, key, value, r4); | |
66 | } | |
67 | ||
a2c83fff | 68 | const struct bpf_func_proto bpf_map_update_elem_proto = { |
3324b584 DB |
69 | .func = bpf_map_update_elem, |
70 | .gpl_only = false, | |
71 | .ret_type = RET_INTEGER, | |
72 | .arg1_type = ARG_CONST_MAP_PTR, | |
73 | .arg2_type = ARG_PTR_TO_MAP_KEY, | |
74 | .arg3_type = ARG_PTR_TO_MAP_VALUE, | |
75 | .arg4_type = ARG_ANYTHING, | |
d0003ec0 AS |
76 | }; |
77 | ||
78 | static u64 bpf_map_delete_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
79 | { | |
80 | struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; | |
81 | void *key = (void *) (unsigned long) r2; | |
82 | ||
83 | WARN_ON_ONCE(!rcu_read_lock_held()); | |
84 | ||
85 | return map->ops->map_delete_elem(map, key); | |
86 | } | |
87 | ||
a2c83fff | 88 | const struct bpf_func_proto bpf_map_delete_elem_proto = { |
3324b584 DB |
89 | .func = bpf_map_delete_elem, |
90 | .gpl_only = false, | |
91 | .ret_type = RET_INTEGER, | |
92 | .arg1_type = ARG_CONST_MAP_PTR, | |
93 | .arg2_type = ARG_PTR_TO_MAP_KEY, | |
d0003ec0 | 94 | }; |
03e69b50 DB |
95 | |
96 | static u64 bpf_get_prandom_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
97 | { | |
98 | return prandom_u32(); | |
99 | } | |
100 | ||
101 | const struct bpf_func_proto bpf_get_prandom_u32_proto = { | |
102 | .func = bpf_get_prandom_u32, | |
103 | .gpl_only = false, | |
104 | .ret_type = RET_INTEGER, | |
105 | }; | |
c04167ce DB |
106 | |
107 | static u64 bpf_get_smp_processor_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
108 | { | |
109 | return raw_smp_processor_id(); | |
110 | } | |
111 | ||
112 | const struct bpf_func_proto bpf_get_smp_processor_id_proto = { | |
113 | .func = bpf_get_smp_processor_id, | |
114 | .gpl_only = false, | |
115 | .ret_type = RET_INTEGER, | |
116 | }; | |
17ca8cbf DB |
117 | |
118 | static u64 bpf_ktime_get_ns(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
119 | { | |
120 | /* NMI safe access to clock monotonic */ | |
121 | return ktime_get_mono_fast_ns(); | |
122 | } | |
123 | ||
124 | const struct bpf_func_proto bpf_ktime_get_ns_proto = { | |
125 | .func = bpf_ktime_get_ns, | |
126 | .gpl_only = true, | |
127 | .ret_type = RET_INTEGER, | |
128 | }; | |
ffeedafb AS |
129 | |
130 | static u64 bpf_get_current_pid_tgid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
131 | { | |
132 | struct task_struct *task = current; | |
133 | ||
134 | if (!task) | |
135 | return -EINVAL; | |
136 | ||
137 | return (u64) task->tgid << 32 | task->pid; | |
138 | } | |
139 | ||
140 | const struct bpf_func_proto bpf_get_current_pid_tgid_proto = { | |
141 | .func = bpf_get_current_pid_tgid, | |
142 | .gpl_only = false, | |
143 | .ret_type = RET_INTEGER, | |
144 | }; | |
145 | ||
146 | static u64 bpf_get_current_uid_gid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
147 | { | |
148 | struct task_struct *task = current; | |
149 | kuid_t uid; | |
150 | kgid_t gid; | |
151 | ||
152 | if (!task) | |
153 | return -EINVAL; | |
154 | ||
155 | current_uid_gid(&uid, &gid); | |
156 | return (u64) from_kgid(&init_user_ns, gid) << 32 | | |
157 | from_kuid(&init_user_ns, uid); | |
158 | } | |
159 | ||
160 | const struct bpf_func_proto bpf_get_current_uid_gid_proto = { | |
161 | .func = bpf_get_current_uid_gid, | |
162 | .gpl_only = false, | |
163 | .ret_type = RET_INTEGER, | |
164 | }; | |
165 | ||
166 | static u64 bpf_get_current_comm(u64 r1, u64 size, u64 r3, u64 r4, u64 r5) | |
167 | { | |
168 | struct task_struct *task = current; | |
169 | char *buf = (char *) (long) r1; | |
170 | ||
171 | if (!task) | |
172 | return -EINVAL; | |
173 | ||
174 | memcpy(buf, task->comm, min_t(size_t, size, sizeof(task->comm))); | |
175 | return 0; | |
176 | } | |
177 | ||
178 | const struct bpf_func_proto bpf_get_current_comm_proto = { | |
179 | .func = bpf_get_current_comm, | |
180 | .gpl_only = false, | |
181 | .ret_type = RET_INTEGER, | |
182 | .arg1_type = ARG_PTR_TO_STACK, | |
183 | .arg2_type = ARG_CONST_STACK_SIZE, | |
184 | }; |