Dmitry V. Levin | ddb53dd | 2015-07-25 23:55:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org> |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 3 | * Copyright (c) 2017 Quentin Monnet <quentin.monnet@6wind.com> |
Dmitry V. Levin | ddb53dd | 2015-07-25 23:55:51 +0000 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include "defs.h" |
| 30 | |
| 31 | #ifdef HAVE_LINUX_BPF_H |
| 32 | # include <linux/bpf.h> |
| 33 | #endif |
| 34 | |
| 35 | #include "xlat/bpf_commands.h" |
| 36 | #include "xlat/bpf_map_types.h" |
| 37 | #include "xlat/bpf_prog_types.h" |
| 38 | #include "xlat/bpf_map_update_elem_flags.h" |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 39 | #include "xlat/bpf_attach_type.h" |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame^] | 40 | #include "xlat/bpf_attach_flags.h" |
Dmitry V. Levin | ddb53dd | 2015-07-25 23:55:51 +0000 | [diff] [blame] | 41 | |
| 42 | static int |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 43 | bpf_map_create(struct tcb *const tcp, const kernel_ulong_t addr, |
| 44 | unsigned int size) |
Dmitry V. Levin | ddb53dd | 2015-07-25 23:55:51 +0000 | [diff] [blame] | 45 | { |
| 46 | struct { |
| 47 | uint32_t map_type, key_size, value_size, max_entries; |
| 48 | } attr = {}; |
| 49 | |
| 50 | if (!size) { |
| 51 | printaddr(addr); |
| 52 | return RVAL_DECODED | RVAL_FD; |
| 53 | } |
| 54 | if (size > sizeof(attr)) |
| 55 | size = sizeof(attr); |
| 56 | if (umoven_or_printaddr(tcp, addr, size, &attr)) |
| 57 | return RVAL_DECODED | RVAL_FD; |
| 58 | |
| 59 | tprints("{map_type="); |
| 60 | printxval(bpf_map_types, attr.map_type, "BPF_MAP_TYPE_???"); |
| 61 | tprintf(", key_size=%u, value_size=%u, max_entries=%u}", |
| 62 | attr.key_size, attr.value_size, attr.max_entries); |
| 63 | |
| 64 | return RVAL_DECODED | RVAL_FD; |
| 65 | } |
| 66 | |
| 67 | static void |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 68 | bpf_map_update_elem(struct tcb *const tcp, const kernel_ulong_t addr, |
| 69 | unsigned int size) |
Dmitry V. Levin | ddb53dd | 2015-07-25 23:55:51 +0000 | [diff] [blame] | 70 | { |
| 71 | struct { |
| 72 | uint32_t map_fd; |
| 73 | uint64_t ATTRIBUTE_ALIGNED(8) key; |
| 74 | uint64_t ATTRIBUTE_ALIGNED(8) value; |
| 75 | uint64_t flags; |
| 76 | } attr = {}; |
| 77 | |
| 78 | if (!size) { |
| 79 | printaddr(addr); |
| 80 | return; |
| 81 | } |
| 82 | if (size > sizeof(attr)) |
| 83 | size = sizeof(attr); |
| 84 | if (umoven_or_printaddr(tcp, addr, size, &attr)) |
| 85 | return; |
| 86 | |
| 87 | tprints("{map_fd="); |
| 88 | printfd(tcp, attr.map_fd); |
| 89 | tprintf(", key=%#" PRIx64 ", value=%#" PRIx64 ", flags=", |
| 90 | attr.key, attr.value); |
Dmitry V. Levin | 18d921d | 2016-05-16 21:27:30 +0000 | [diff] [blame] | 91 | printxval64(bpf_map_update_elem_flags, attr.flags, "BPF_???"); |
Dmitry V. Levin | ddb53dd | 2015-07-25 23:55:51 +0000 | [diff] [blame] | 92 | tprints("}"); |
| 93 | } |
| 94 | |
| 95 | static void |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 96 | bpf_map_delete_elem(struct tcb *const tcp, const kernel_ulong_t addr, |
| 97 | unsigned int size) |
Dmitry V. Levin | ddb53dd | 2015-07-25 23:55:51 +0000 | [diff] [blame] | 98 | { |
| 99 | struct { |
| 100 | uint32_t map_fd; |
| 101 | uint64_t ATTRIBUTE_ALIGNED(8) key; |
| 102 | } attr = {}; |
| 103 | |
| 104 | if (!size) { |
| 105 | printaddr(addr); |
| 106 | return; |
| 107 | } |
| 108 | if (size > sizeof(attr)) |
| 109 | size = sizeof(attr); |
| 110 | if (umoven_or_printaddr(tcp, addr, size, &attr)) |
| 111 | return; |
| 112 | |
| 113 | tprints("{map_fd="); |
| 114 | printfd(tcp, attr.map_fd); |
| 115 | tprintf(", key=%#" PRIx64 "}", attr.key); |
| 116 | } |
| 117 | |
| 118 | static int |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 119 | bpf_map_io(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int size, |
| 120 | const char *const text) |
Dmitry V. Levin | ddb53dd | 2015-07-25 23:55:51 +0000 | [diff] [blame] | 121 | { |
| 122 | struct bpf_io_elem_struct { |
| 123 | uint32_t map_fd; |
| 124 | uint64_t ATTRIBUTE_ALIGNED(8) key; |
| 125 | uint64_t ATTRIBUTE_ALIGNED(8) value; |
| 126 | } attr = {}; |
| 127 | |
| 128 | if (exiting(tcp)) { |
| 129 | if (!syserror(tcp) && !umove_or_printaddr(tcp, addr, &attr)) |
| 130 | tprintf(", %s=%#" PRIx64, text, attr.value); |
| 131 | tprints("}"); |
| 132 | return RVAL_DECODED; |
| 133 | } |
| 134 | |
| 135 | if (!size) { |
| 136 | printaddr(addr); |
| 137 | return RVAL_DECODED; |
| 138 | } |
| 139 | if (size > sizeof(attr)) |
| 140 | size = sizeof(attr); |
| 141 | if (umoven_or_printaddr(tcp, addr, size, &attr)) |
| 142 | return RVAL_DECODED; |
| 143 | |
| 144 | tprints("{map_fd="); |
| 145 | printfd(tcp, attr.map_fd); |
| 146 | tprintf(", key=%#" PRIx64, attr.key); |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static int |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 152 | bpf_prog_load(struct tcb *const tcp, const kernel_ulong_t addr, |
| 153 | unsigned int size) |
Dmitry V. Levin | ddb53dd | 2015-07-25 23:55:51 +0000 | [diff] [blame] | 154 | { |
| 155 | struct { |
| 156 | uint32_t prog_type, insn_cnt; |
| 157 | uint64_t ATTRIBUTE_ALIGNED(8) insns, license; |
| 158 | uint32_t log_level, log_size; |
| 159 | uint64_t ATTRIBUTE_ALIGNED(8) log_buf; |
| 160 | uint32_t kern_version; |
| 161 | } attr = {}; |
| 162 | |
| 163 | if (!size) { |
| 164 | printaddr(addr); |
| 165 | return RVAL_DECODED | RVAL_FD; |
| 166 | } |
| 167 | if (size > sizeof(attr)) |
| 168 | size = sizeof(attr); |
| 169 | if (umoven_or_printaddr(tcp, addr, size, &attr)) |
| 170 | return RVAL_DECODED | RVAL_FD; |
| 171 | |
| 172 | tprints("{prog_type="); |
| 173 | printxval(bpf_prog_types, attr.prog_type, "BPF_PROG_TYPE_???"); |
| 174 | tprintf(", insn_cnt=%u, insns=%#" PRIx64 ", license=", |
| 175 | attr.insn_cnt, attr.insns); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 176 | printstr(tcp, attr.license); |
Dmitry V. Levin | ddb53dd | 2015-07-25 23:55:51 +0000 | [diff] [blame] | 177 | tprintf(", log_level=%u, log_size=%u, log_buf=%#" PRIx64 ", kern_version=%u}", |
| 178 | attr.log_level, attr.log_size, attr.log_buf, attr.kern_version); |
| 179 | |
| 180 | return RVAL_DECODED | RVAL_FD; |
| 181 | } |
| 182 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 183 | static int |
| 184 | bpf_obj_manage(struct tcb *const tcp, const kernel_ulong_t addr, |
| 185 | unsigned int size) |
| 186 | { |
| 187 | struct { |
| 188 | uint64_t ATTRIBUTE_ALIGNED(8) pathname; |
| 189 | uint32_t bpf_fd; |
| 190 | } attr = {}; |
| 191 | |
| 192 | if (!size) { |
| 193 | printaddr(addr); |
| 194 | return RVAL_DECODED | RVAL_FD; |
| 195 | } |
| 196 | if (size > sizeof(attr)) |
| 197 | size = sizeof(attr); |
| 198 | if (umoven_or_printaddr(tcp, addr, size, &attr)) |
| 199 | return RVAL_DECODED | RVAL_FD; |
| 200 | |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame^] | 201 | tprints("{pathname="); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 202 | printpath(tcp, attr.pathname); |
| 203 | tprints(", bpf_fd="); |
| 204 | printfd(tcp, attr.bpf_fd); |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame^] | 205 | tprints("}"); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 206 | |
| 207 | return RVAL_DECODED | RVAL_FD; |
| 208 | } |
| 209 | |
| 210 | static int |
| 211 | bpf_prog_attach_detach(struct tcb *const tcp, const kernel_ulong_t addr, |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame^] | 212 | unsigned int size, bool print_attach) |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 213 | { |
| 214 | struct { |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame^] | 215 | uint32_t target_fd, attach_bpf_fd, attach_type, attach_flags; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 216 | } attr = {}; |
| 217 | |
| 218 | if (!size) { |
| 219 | printaddr(addr); |
| 220 | return RVAL_DECODED; |
| 221 | } |
| 222 | if (size > sizeof(attr)) |
| 223 | size = sizeof(attr); |
| 224 | if (umoven_or_printaddr(tcp, addr, size, &attr)) |
| 225 | return RVAL_DECODED; |
| 226 | |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame^] | 227 | tprints("{target_fd="); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 228 | printfd(tcp, attr.target_fd); |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame^] | 229 | if (print_attach) { |
| 230 | tprints(", attach_bpf_fd="); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 231 | printfd(tcp, attr.attach_bpf_fd); |
| 232 | } |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame^] | 233 | tprints(", attach_type="); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 234 | printxval(bpf_attach_type, attr.attach_type, "BPF_???"); |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame^] | 235 | if (print_attach) { |
| 236 | tprints(", attach_flags="); |
| 237 | printflags(bpf_attach_flags, attr.attach_flags, "BPF_F_???"); |
| 238 | } |
| 239 | tprints("}"); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 240 | |
| 241 | return RVAL_DECODED; |
| 242 | } |
| 243 | |
| 244 | static int |
| 245 | bpf_prog_attach(struct tcb *const tcp, const kernel_ulong_t addr, |
| 246 | unsigned int size) |
| 247 | { |
| 248 | return bpf_prog_attach_detach(tcp, addr, size, true); |
| 249 | } |
| 250 | |
| 251 | static int |
| 252 | bpf_prog_detach(struct tcb *const tcp, const kernel_ulong_t addr, |
| 253 | unsigned int size) |
| 254 | { |
| 255 | return bpf_prog_attach_detach(tcp, addr, size, false); |
| 256 | } |
| 257 | |
Dmitry V. Levin | ddb53dd | 2015-07-25 23:55:51 +0000 | [diff] [blame] | 258 | SYS_FUNC(bpf) |
| 259 | { |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 260 | const unsigned int cmd = tcp->u_arg[0]; |
| 261 | const kernel_ulong_t addr = tcp->u_arg[1]; |
Dmitry V. Levin | ddb53dd | 2015-07-25 23:55:51 +0000 | [diff] [blame] | 262 | const unsigned int size = tcp->u_arg[2]; |
| 263 | int rc = RVAL_DECODED; |
| 264 | |
| 265 | if (entering(tcp)) { |
| 266 | printxval(bpf_commands, cmd, "BPF_???"); |
| 267 | tprints(", "); |
| 268 | } |
| 269 | |
| 270 | switch (cmd) { |
| 271 | case BPF_MAP_CREATE: |
| 272 | rc = bpf_map_create(tcp, addr, size); |
| 273 | break; |
| 274 | case BPF_MAP_LOOKUP_ELEM: |
| 275 | rc = bpf_map_io(tcp, addr, size, "value"); |
| 276 | break; |
| 277 | case BPF_MAP_UPDATE_ELEM: |
| 278 | bpf_map_update_elem(tcp, addr, size); |
| 279 | break; |
| 280 | case BPF_MAP_DELETE_ELEM: |
| 281 | bpf_map_delete_elem(tcp, addr, size); |
| 282 | break; |
| 283 | case BPF_MAP_GET_NEXT_KEY: |
| 284 | rc = bpf_map_io(tcp, addr, size, "next_key"); |
| 285 | break; |
| 286 | case BPF_PROG_LOAD: |
| 287 | rc = bpf_prog_load(tcp, addr, size); |
| 288 | break; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 289 | case BPF_OBJ_PIN: |
| 290 | rc = bpf_obj_manage(tcp, addr, size); |
| 291 | break; |
| 292 | case BPF_OBJ_GET: |
| 293 | rc = bpf_obj_manage(tcp, addr, size); |
| 294 | break; |
| 295 | case BPF_PROG_ATTACH: |
| 296 | rc = bpf_prog_attach(tcp, addr, size); |
| 297 | break; |
| 298 | case BPF_PROG_DETACH: |
| 299 | rc = bpf_prog_detach(tcp, addr, size); |
| 300 | break; |
Dmitry V. Levin | ddb53dd | 2015-07-25 23:55:51 +0000 | [diff] [blame] | 301 | default: |
| 302 | printaddr(addr); |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | if (rc & RVAL_DECODED) |
| 307 | tprintf(", %u", size); |
| 308 | |
| 309 | return rc; |
| 310 | } |