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> | |
f3694e00 | 19 | #include <linux/filter.h> |
d0003ec0 AS |
20 | |
21 | /* If kernel subsystem is allowing eBPF programs to call this function, | |
22 | * inside its own verifier_ops->get_func_proto() callback it should return | |
23 | * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments | |
24 | * | |
25 | * Different map implementations will rely on rcu in map methods | |
26 | * lookup/update/delete, therefore eBPF programs must run under rcu lock | |
27 | * if program is allowed to access maps, so check rcu_read_lock_held in | |
28 | * all three functions. | |
29 | */ | |
f3694e00 | 30 | BPF_CALL_2(bpf_map_lookup_elem, struct bpf_map *, map, void *, key) |
d0003ec0 | 31 | { |
d0003ec0 | 32 | WARN_ON_ONCE(!rcu_read_lock_held()); |
f3694e00 | 33 | return (unsigned long) map->ops->map_lookup_elem(map, key); |
d0003ec0 AS |
34 | } |
35 | ||
a2c83fff | 36 | const struct bpf_func_proto bpf_map_lookup_elem_proto = { |
3324b584 DB |
37 | .func = bpf_map_lookup_elem, |
38 | .gpl_only = false, | |
39 | .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, | |
40 | .arg1_type = ARG_CONST_MAP_PTR, | |
41 | .arg2_type = ARG_PTR_TO_MAP_KEY, | |
d0003ec0 AS |
42 | }; |
43 | ||
f3694e00 DB |
44 | BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key, |
45 | void *, value, u64, flags) | |
d0003ec0 | 46 | { |
d0003ec0 | 47 | WARN_ON_ONCE(!rcu_read_lock_held()); |
f3694e00 | 48 | return map->ops->map_update_elem(map, key, value, flags); |
d0003ec0 AS |
49 | } |
50 | ||
a2c83fff | 51 | const struct bpf_func_proto bpf_map_update_elem_proto = { |
3324b584 DB |
52 | .func = bpf_map_update_elem, |
53 | .gpl_only = false, | |
54 | .ret_type = RET_INTEGER, | |
55 | .arg1_type = ARG_CONST_MAP_PTR, | |
56 | .arg2_type = ARG_PTR_TO_MAP_KEY, | |
57 | .arg3_type = ARG_PTR_TO_MAP_VALUE, | |
58 | .arg4_type = ARG_ANYTHING, | |
d0003ec0 AS |
59 | }; |
60 | ||
f3694e00 | 61 | BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key) |
d0003ec0 | 62 | { |
d0003ec0 | 63 | WARN_ON_ONCE(!rcu_read_lock_held()); |
d0003ec0 AS |
64 | return map->ops->map_delete_elem(map, key); |
65 | } | |
66 | ||
a2c83fff | 67 | const struct bpf_func_proto bpf_map_delete_elem_proto = { |
3324b584 DB |
68 | .func = bpf_map_delete_elem, |
69 | .gpl_only = false, | |
70 | .ret_type = RET_INTEGER, | |
71 | .arg1_type = ARG_CONST_MAP_PTR, | |
72 | .arg2_type = ARG_PTR_TO_MAP_KEY, | |
d0003ec0 | 73 | }; |
03e69b50 | 74 | |
03e69b50 | 75 | const struct bpf_func_proto bpf_get_prandom_u32_proto = { |
3ad00405 | 76 | .func = bpf_user_rnd_u32, |
03e69b50 DB |
77 | .gpl_only = false, |
78 | .ret_type = RET_INTEGER, | |
79 | }; | |
c04167ce | 80 | |
f3694e00 | 81 | BPF_CALL_0(bpf_get_smp_processor_id) |
c04167ce | 82 | { |
80b48c44 | 83 | return smp_processor_id(); |
c04167ce DB |
84 | } |
85 | ||
86 | const struct bpf_func_proto bpf_get_smp_processor_id_proto = { | |
87 | .func = bpf_get_smp_processor_id, | |
88 | .gpl_only = false, | |
89 | .ret_type = RET_INTEGER, | |
90 | }; | |
17ca8cbf | 91 | |
f3694e00 | 92 | BPF_CALL_0(bpf_ktime_get_ns) |
17ca8cbf DB |
93 | { |
94 | /* NMI safe access to clock monotonic */ | |
95 | return ktime_get_mono_fast_ns(); | |
96 | } | |
97 | ||
98 | const struct bpf_func_proto bpf_ktime_get_ns_proto = { | |
99 | .func = bpf_ktime_get_ns, | |
100 | .gpl_only = true, | |
101 | .ret_type = RET_INTEGER, | |
102 | }; | |
ffeedafb | 103 | |
f3694e00 | 104 | BPF_CALL_0(bpf_get_current_pid_tgid) |
ffeedafb AS |
105 | { |
106 | struct task_struct *task = current; | |
107 | ||
6088b582 | 108 | if (unlikely(!task)) |
ffeedafb AS |
109 | return -EINVAL; |
110 | ||
111 | return (u64) task->tgid << 32 | task->pid; | |
112 | } | |
113 | ||
114 | const struct bpf_func_proto bpf_get_current_pid_tgid_proto = { | |
115 | .func = bpf_get_current_pid_tgid, | |
116 | .gpl_only = false, | |
117 | .ret_type = RET_INTEGER, | |
118 | }; | |
119 | ||
f3694e00 | 120 | BPF_CALL_0(bpf_get_current_uid_gid) |
ffeedafb AS |
121 | { |
122 | struct task_struct *task = current; | |
123 | kuid_t uid; | |
124 | kgid_t gid; | |
125 | ||
6088b582 | 126 | if (unlikely(!task)) |
ffeedafb AS |
127 | return -EINVAL; |
128 | ||
129 | current_uid_gid(&uid, &gid); | |
130 | return (u64) from_kgid(&init_user_ns, gid) << 32 | | |
6088b582 | 131 | from_kuid(&init_user_ns, uid); |
ffeedafb AS |
132 | } |
133 | ||
134 | const struct bpf_func_proto bpf_get_current_uid_gid_proto = { | |
135 | .func = bpf_get_current_uid_gid, | |
136 | .gpl_only = false, | |
137 | .ret_type = RET_INTEGER, | |
138 | }; | |
139 | ||
f3694e00 | 140 | BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size) |
ffeedafb AS |
141 | { |
142 | struct task_struct *task = current; | |
ffeedafb | 143 | |
074f528e DB |
144 | if (unlikely(!task)) |
145 | goto err_clear; | |
ffeedafb | 146 | |
074f528e DB |
147 | strncpy(buf, task->comm, size); |
148 | ||
149 | /* Verifier guarantees that size > 0. For task->comm exceeding | |
150 | * size, guarantee that buf is %NUL-terminated. Unconditionally | |
151 | * done here to save the size test. | |
152 | */ | |
153 | buf[size - 1] = 0; | |
ffeedafb | 154 | return 0; |
074f528e DB |
155 | err_clear: |
156 | memset(buf, 0, size); | |
157 | return -EINVAL; | |
ffeedafb AS |
158 | } |
159 | ||
160 | const struct bpf_func_proto bpf_get_current_comm_proto = { | |
161 | .func = bpf_get_current_comm, | |
162 | .gpl_only = false, | |
163 | .ret_type = RET_INTEGER, | |
074f528e | 164 | .arg1_type = ARG_PTR_TO_RAW_STACK, |
ffeedafb AS |
165 | .arg2_type = ARG_CONST_STACK_SIZE, |
166 | }; |