Alexei Starovoitov | d0003ec | 2014-11-13 17:36:49 -0800 | [diff] [blame] | 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> |
Daniel Borkmann | 03e69b5 | 2015-03-14 02:27:16 +0100 | [diff] [blame] | 14 | #include <linux/random.h> |
Daniel Borkmann | c04167c | 2015-03-14 02:27:17 +0100 | [diff] [blame] | 15 | #include <linux/smp.h> |
Daniel Borkmann | 17ca8cb | 2015-05-29 23:23:06 +0200 | [diff] [blame] | 16 | #include <linux/ktime.h> |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 17 | #include <linux/sched.h> |
| 18 | #include <linux/uidgid.h> |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 19 | #include <linux/filter.h> |
Alexei Starovoitov | d0003ec | 2014-11-13 17:36:49 -0800 | [diff] [blame] | 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 | */ |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 30 | BPF_CALL_2(bpf_map_lookup_elem, struct bpf_map *, map, void *, key) |
Alexei Starovoitov | d0003ec | 2014-11-13 17:36:49 -0800 | [diff] [blame] | 31 | { |
Alexei Starovoitov | d0003ec | 2014-11-13 17:36:49 -0800 | [diff] [blame] | 32 | WARN_ON_ONCE(!rcu_read_lock_held()); |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 33 | return (unsigned long) map->ops->map_lookup_elem(map, key); |
Alexei Starovoitov | d0003ec | 2014-11-13 17:36:49 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Daniel Borkmann | a2c83ff | 2015-03-01 12:31:42 +0100 | [diff] [blame] | 36 | const struct bpf_func_proto bpf_map_lookup_elem_proto = { |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 37 | .func = bpf_map_lookup_elem, |
| 38 | .gpl_only = false, |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 39 | .pkt_access = true, |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 40 | .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 41 | .arg1_type = ARG_CONST_MAP_PTR, |
| 42 | .arg2_type = ARG_PTR_TO_MAP_KEY, |
Alexei Starovoitov | d0003ec | 2014-11-13 17:36:49 -0800 | [diff] [blame] | 43 | }; |
| 44 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 45 | BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key, |
| 46 | void *, value, u64, flags) |
Alexei Starovoitov | d0003ec | 2014-11-13 17:36:49 -0800 | [diff] [blame] | 47 | { |
Alexei Starovoitov | d0003ec | 2014-11-13 17:36:49 -0800 | [diff] [blame] | 48 | WARN_ON_ONCE(!rcu_read_lock_held()); |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 49 | return map->ops->map_update_elem(map, key, value, flags); |
Alexei Starovoitov | d0003ec | 2014-11-13 17:36:49 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Daniel Borkmann | a2c83ff | 2015-03-01 12:31:42 +0100 | [diff] [blame] | 52 | const struct bpf_func_proto bpf_map_update_elem_proto = { |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 53 | .func = bpf_map_update_elem, |
| 54 | .gpl_only = false, |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 55 | .pkt_access = true, |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 56 | .ret_type = RET_INTEGER, |
| 57 | .arg1_type = ARG_CONST_MAP_PTR, |
| 58 | .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 59 | .arg3_type = ARG_PTR_TO_MAP_VALUE, |
| 60 | .arg4_type = ARG_ANYTHING, |
Alexei Starovoitov | d0003ec | 2014-11-13 17:36:49 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 63 | BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key) |
Alexei Starovoitov | d0003ec | 2014-11-13 17:36:49 -0800 | [diff] [blame] | 64 | { |
Alexei Starovoitov | d0003ec | 2014-11-13 17:36:49 -0800 | [diff] [blame] | 65 | WARN_ON_ONCE(!rcu_read_lock_held()); |
Alexei Starovoitov | d0003ec | 2014-11-13 17:36:49 -0800 | [diff] [blame] | 66 | return map->ops->map_delete_elem(map, key); |
| 67 | } |
| 68 | |
Daniel Borkmann | a2c83ff | 2015-03-01 12:31:42 +0100 | [diff] [blame] | 69 | const struct bpf_func_proto bpf_map_delete_elem_proto = { |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 70 | .func = bpf_map_delete_elem, |
| 71 | .gpl_only = false, |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 72 | .pkt_access = true, |
Daniel Borkmann | 3324b58 | 2015-05-29 23:23:07 +0200 | [diff] [blame] | 73 | .ret_type = RET_INTEGER, |
| 74 | .arg1_type = ARG_CONST_MAP_PTR, |
| 75 | .arg2_type = ARG_PTR_TO_MAP_KEY, |
Alexei Starovoitov | d0003ec | 2014-11-13 17:36:49 -0800 | [diff] [blame] | 76 | }; |
Daniel Borkmann | 03e69b5 | 2015-03-14 02:27:16 +0100 | [diff] [blame] | 77 | |
Daniel Borkmann | 03e69b5 | 2015-03-14 02:27:16 +0100 | [diff] [blame] | 78 | const struct bpf_func_proto bpf_get_prandom_u32_proto = { |
Daniel Borkmann | 3ad0040 | 2015-10-08 01:20:39 +0200 | [diff] [blame] | 79 | .func = bpf_user_rnd_u32, |
Daniel Borkmann | 03e69b5 | 2015-03-14 02:27:16 +0100 | [diff] [blame] | 80 | .gpl_only = false, |
| 81 | .ret_type = RET_INTEGER, |
| 82 | }; |
Daniel Borkmann | c04167c | 2015-03-14 02:27:17 +0100 | [diff] [blame] | 83 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 84 | BPF_CALL_0(bpf_get_smp_processor_id) |
Daniel Borkmann | c04167c | 2015-03-14 02:27:17 +0100 | [diff] [blame] | 85 | { |
Daniel Borkmann | 80b48c4 | 2016-06-28 12:18:26 +0200 | [diff] [blame] | 86 | return smp_processor_id(); |
Daniel Borkmann | c04167c | 2015-03-14 02:27:17 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | const struct bpf_func_proto bpf_get_smp_processor_id_proto = { |
| 90 | .func = bpf_get_smp_processor_id, |
| 91 | .gpl_only = false, |
| 92 | .ret_type = RET_INTEGER, |
| 93 | }; |
Daniel Borkmann | 17ca8cb | 2015-05-29 23:23:06 +0200 | [diff] [blame] | 94 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 95 | BPF_CALL_0(bpf_ktime_get_ns) |
Daniel Borkmann | 17ca8cb | 2015-05-29 23:23:06 +0200 | [diff] [blame] | 96 | { |
| 97 | /* NMI safe access to clock monotonic */ |
| 98 | return ktime_get_mono_fast_ns(); |
| 99 | } |
| 100 | |
| 101 | const struct bpf_func_proto bpf_ktime_get_ns_proto = { |
| 102 | .func = bpf_ktime_get_ns, |
| 103 | .gpl_only = true, |
| 104 | .ret_type = RET_INTEGER, |
| 105 | }; |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 106 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 107 | BPF_CALL_0(bpf_get_current_pid_tgid) |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 108 | { |
| 109 | struct task_struct *task = current; |
| 110 | |
Daniel Borkmann | 6088b58 | 2016-09-09 02:45:28 +0200 | [diff] [blame] | 111 | if (unlikely(!task)) |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 112 | return -EINVAL; |
| 113 | |
| 114 | return (u64) task->tgid << 32 | task->pid; |
| 115 | } |
| 116 | |
| 117 | const struct bpf_func_proto bpf_get_current_pid_tgid_proto = { |
| 118 | .func = bpf_get_current_pid_tgid, |
| 119 | .gpl_only = false, |
| 120 | .ret_type = RET_INTEGER, |
| 121 | }; |
| 122 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 123 | BPF_CALL_0(bpf_get_current_uid_gid) |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 124 | { |
| 125 | struct task_struct *task = current; |
| 126 | kuid_t uid; |
| 127 | kgid_t gid; |
| 128 | |
Daniel Borkmann | 6088b58 | 2016-09-09 02:45:28 +0200 | [diff] [blame] | 129 | if (unlikely(!task)) |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 130 | return -EINVAL; |
| 131 | |
| 132 | current_uid_gid(&uid, &gid); |
| 133 | return (u64) from_kgid(&init_user_ns, gid) << 32 | |
Daniel Borkmann | 6088b58 | 2016-09-09 02:45:28 +0200 | [diff] [blame] | 134 | from_kuid(&init_user_ns, uid); |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | const struct bpf_func_proto bpf_get_current_uid_gid_proto = { |
| 138 | .func = bpf_get_current_uid_gid, |
| 139 | .gpl_only = false, |
| 140 | .ret_type = RET_INTEGER, |
| 141 | }; |
| 142 | |
Daniel Borkmann | f3694e0 | 2016-09-09 02:45:31 +0200 | [diff] [blame] | 143 | BPF_CALL_2(bpf_get_current_comm, char *, buf, u32, size) |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 144 | { |
| 145 | struct task_struct *task = current; |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 146 | |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 147 | if (unlikely(!task)) |
| 148 | goto err_clear; |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 149 | |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 150 | strncpy(buf, task->comm, size); |
| 151 | |
| 152 | /* Verifier guarantees that size > 0. For task->comm exceeding |
| 153 | * size, guarantee that buf is %NUL-terminated. Unconditionally |
| 154 | * done here to save the size test. |
| 155 | */ |
| 156 | buf[size - 1] = 0; |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 157 | return 0; |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 158 | err_clear: |
| 159 | memset(buf, 0, size); |
| 160 | return -EINVAL; |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | const struct bpf_func_proto bpf_get_current_comm_proto = { |
| 164 | .func = bpf_get_current_comm, |
| 165 | .gpl_only = false, |
| 166 | .ret_type = RET_INTEGER, |
Daniel Borkmann | 074f528 | 2016-04-13 00:10:52 +0200 | [diff] [blame] | 167 | .arg1_type = ARG_PTR_TO_RAW_STACK, |
Alexei Starovoitov | ffeedaf | 2015-06-12 19:39:12 -0700 | [diff] [blame] | 168 | .arg2_type = ARG_CONST_STACK_SIZE, |
| 169 | }; |