Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org> |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "defs.h" |
| 29 | |
| 30 | #ifdef HAVE_LINUX_SECCOMP_H |
| 31 | # include <linux/seccomp.h> |
| 32 | #endif |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 33 | #include "xlat/seccomp_ops.h" |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 34 | #include "xlat/seccomp_filter_flags.h" |
| 35 | |
| 36 | #ifdef HAVE_LINUX_FILTER_H |
| 37 | # include <linux/filter.h> |
| 38 | # include "xlat/bpf_class.h" |
| 39 | # include "xlat/bpf_miscop.h" |
| 40 | # include "xlat/bpf_mode.h" |
| 41 | # include "xlat/bpf_op_alu.h" |
| 42 | # include "xlat/bpf_op_jmp.h" |
| 43 | # include "xlat/bpf_rval.h" |
| 44 | # include "xlat/bpf_size.h" |
| 45 | # include "xlat/bpf_src.h" |
| 46 | |
| 47 | # ifndef SECCOMP_RET_ACTION |
| 48 | # define SECCOMP_RET_ACTION 0x7fff0000U |
| 49 | # endif |
| 50 | # include "xlat/seccomp_ret_action.h" |
| 51 | #endif |
| 52 | |
| 53 | struct bpf_filter { |
| 54 | uint16_t code; |
| 55 | uint8_t jt; |
| 56 | uint8_t jf; |
| 57 | uint32_t k; |
| 58 | }; |
| 59 | |
| 60 | #ifdef HAVE_LINUX_FILTER_H |
| 61 | |
| 62 | static void |
| 63 | decode_bpf_code(uint16_t code) |
| 64 | { |
| 65 | uint16_t i = code & ~BPF_CLASS(code); |
| 66 | |
| 67 | printxval(bpf_class, BPF_CLASS(code), "BPF_???"); |
| 68 | switch (BPF_CLASS(code)) { |
| 69 | case BPF_LD: |
| 70 | case BPF_LDX: |
Dmitry V. Levin | 3ea6a59 | 2016-04-11 20:25:01 +0000 | [diff] [blame] | 71 | tprints("|"); |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 72 | printxval(bpf_size, BPF_SIZE(code), "BPF_???"); |
Dmitry V. Levin | 3ea6a59 | 2016-04-11 20:25:01 +0000 | [diff] [blame] | 73 | tprints("|"); |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 74 | printxval(bpf_mode, BPF_MODE(code), "BPF_???"); |
| 75 | break; |
| 76 | case BPF_ST: |
| 77 | case BPF_STX: |
| 78 | if (i) |
Dmitry V. Levin | 3ea6a59 | 2016-04-11 20:25:01 +0000 | [diff] [blame] | 79 | tprintf("|%#x /* %s */", i, "BPF_???"); |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 80 | break; |
| 81 | case BPF_ALU: |
Dmitry V. Levin | 3ea6a59 | 2016-04-11 20:25:01 +0000 | [diff] [blame] | 82 | tprints("|"); |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 83 | printxval(bpf_src, BPF_SRC(code), "BPF_???"); |
Dmitry V. Levin | 3ea6a59 | 2016-04-11 20:25:01 +0000 | [diff] [blame] | 84 | tprints("|"); |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 85 | printxval(bpf_op_alu, BPF_OP(code), "BPF_???"); |
| 86 | break; |
| 87 | case BPF_JMP: |
Dmitry V. Levin | 3ea6a59 | 2016-04-11 20:25:01 +0000 | [diff] [blame] | 88 | tprints("|"); |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 89 | printxval(bpf_src, BPF_SRC(code), "BPF_???"); |
Dmitry V. Levin | 3ea6a59 | 2016-04-11 20:25:01 +0000 | [diff] [blame] | 90 | tprints("|"); |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 91 | printxval(bpf_op_jmp, BPF_OP(code), "BPF_???"); |
| 92 | break; |
| 93 | case BPF_RET: |
Dmitry V. Levin | 3ea6a59 | 2016-04-11 20:25:01 +0000 | [diff] [blame] | 94 | tprints("|"); |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 95 | printxval(bpf_rval, BPF_RVAL(code), "BPF_???"); |
| 96 | i &= ~BPF_RVAL(code); |
| 97 | if (i) |
Dmitry V. Levin | 3ea6a59 | 2016-04-11 20:25:01 +0000 | [diff] [blame] | 98 | tprintf("|%#x /* %s */", i, "BPF_???"); |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 99 | break; |
| 100 | case BPF_MISC: |
Dmitry V. Levin | 3ea6a59 | 2016-04-11 20:25:01 +0000 | [diff] [blame] | 101 | tprints("|"); |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 102 | printxval(bpf_miscop, BPF_MISCOP(code), "BPF_???"); |
| 103 | i &= ~BPF_MISCOP(code); |
| 104 | if (i) |
Dmitry V. Levin | 3ea6a59 | 2016-04-11 20:25:01 +0000 | [diff] [blame] | 105 | tprintf("|%#x /* %s */", i, "BPF_???"); |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 106 | break; |
| 107 | } |
| 108 | |
| 109 | } |
| 110 | |
Dmitry V. Levin | f82dcd9 | 2016-05-07 23:11:52 +0000 | [diff] [blame] | 111 | #endif /* HAVE_LINUX_FILTER_H */ |
| 112 | |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 113 | static void |
| 114 | decode_bpf_stmt(const struct bpf_filter *filter) |
| 115 | { |
| 116 | #ifdef HAVE_LINUX_FILTER_H |
| 117 | tprints("BPF_STMT("); |
| 118 | decode_bpf_code(filter->code); |
| 119 | tprints(", "); |
| 120 | if (BPF_CLASS(filter->code) == BPF_RET) { |
| 121 | unsigned int action = SECCOMP_RET_ACTION & filter->k; |
| 122 | unsigned int data = filter->k & ~action; |
| 123 | |
| 124 | printxval(seccomp_ret_action, action, "SECCOMP_RET_???"); |
| 125 | if (data) |
Dmitry V. Levin | 3ea6a59 | 2016-04-11 20:25:01 +0000 | [diff] [blame] | 126 | tprintf("|%#x)", data); |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 127 | else |
| 128 | tprints(")"); |
| 129 | } else { |
| 130 | tprintf("%#x)", filter->k); |
| 131 | } |
| 132 | #else |
| 133 | tprintf("BPF_STMT(%#x, %#x)", filter->code, filter->k); |
| 134 | #endif /* HAVE_LINUX_FILTER_H */ |
| 135 | } |
| 136 | |
| 137 | static void |
| 138 | decode_bpf_jump(const struct bpf_filter *filter) |
| 139 | { |
| 140 | #ifdef HAVE_LINUX_FILTER_H |
| 141 | tprints("BPF_JUMP("); |
| 142 | decode_bpf_code(filter->code); |
| 143 | tprintf(", %#x, %#x, %#x)", |
| 144 | filter->k, filter->jt, filter->jf); |
| 145 | #else |
| 146 | tprintf("BPF_JUMP(%#x, %#x, %#x, %#x)", |
| 147 | filter->code, filter->k, filter->jt, filter->jf); |
| 148 | #endif /* HAVE_LINUX_FILTER_H */ |
| 149 | } |
| 150 | |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 151 | #ifndef BPF_MAXINSNS |
| 152 | # define BPF_MAXINSNS 4096 |
| 153 | #endif |
| 154 | |
Dmitry V. Levin | f82dcd9 | 2016-05-07 23:11:52 +0000 | [diff] [blame] | 155 | static bool |
| 156 | print_bpf_filter(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data) |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 157 | { |
Dmitry V. Levin | f82dcd9 | 2016-05-07 23:11:52 +0000 | [diff] [blame] | 158 | const struct bpf_filter *filter = elem_buf; |
| 159 | unsigned int *pn = data; |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 160 | |
Dmitry V. Levin | f82dcd9 | 2016-05-07 23:11:52 +0000 | [diff] [blame] | 161 | if ((*pn)++ >= BPF_MAXINSNS) { |
| 162 | tprints("..."); |
| 163 | return false; |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 164 | } |
Dmitry V. Levin | f82dcd9 | 2016-05-07 23:11:52 +0000 | [diff] [blame] | 165 | |
| 166 | if (filter->jt || filter->jf) |
| 167 | decode_bpf_jump(filter); |
| 168 | else |
| 169 | decode_bpf_stmt(filter); |
| 170 | |
| 171 | return true; |
Dmitry V. Levin | 6ba947f | 2016-04-12 00:05:43 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Dmitry V. Levin | 50a2651 | 2016-05-12 14:23:50 +0000 | [diff] [blame] | 174 | void |
Dmitry V. Levin | f82dcd9 | 2016-05-07 23:11:52 +0000 | [diff] [blame] | 175 | print_seccomp_fprog(struct tcb *tcp, unsigned long addr, unsigned short len) |
Dmitry V. Levin | 6ba947f | 2016-04-12 00:05:43 +0000 | [diff] [blame] | 176 | { |
Dmitry V. Levin | f82dcd9 | 2016-05-07 23:11:52 +0000 | [diff] [blame] | 177 | if (abbrev(tcp)) { |
Dmitry V. Levin | 6ba947f | 2016-04-12 00:05:43 +0000 | [diff] [blame] | 178 | printaddr(addr); |
Dmitry V. Levin | f82dcd9 | 2016-05-07 23:11:52 +0000 | [diff] [blame] | 179 | } else { |
| 180 | unsigned int insns = 0; |
| 181 | struct bpf_filter filter; |
| 182 | |
| 183 | print_array(tcp, addr, len, &filter, sizeof(filter), |
| 184 | umoven_or_printaddr, print_bpf_filter, &insns); |
| 185 | } |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Dmitry V. Levin | 7a12974 | 2015-09-19 21:28:23 +0000 | [diff] [blame] | 188 | #include "seccomp_fprog.h" |
| 189 | |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 190 | void |
| 191 | print_seccomp_filter(struct tcb *tcp, unsigned long addr) |
| 192 | { |
Dmitry V. Levin | 7a12974 | 2015-09-19 21:28:23 +0000 | [diff] [blame] | 193 | struct seccomp_fprog fprog; |
Dmitry V. Levin | 931ebbf | 2015-07-17 01:10:45 +0000 | [diff] [blame] | 194 | |
Dmitry V. Levin | 50a2651 | 2016-05-12 14:23:50 +0000 | [diff] [blame] | 195 | if (fetch_seccomp_fprog(tcp, addr, &fprog)) { |
| 196 | tprintf("{len=%hu, filter=", fprog.len); |
Dmitry V. Levin | f82dcd9 | 2016-05-07 23:11:52 +0000 | [diff] [blame] | 197 | print_seccomp_fprog(tcp, fprog.filter, fprog.len); |
Dmitry V. Levin | 50a2651 | 2016-05-12 14:23:50 +0000 | [diff] [blame] | 198 | tprints("}"); |
| 199 | } |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static void |
| 203 | decode_seccomp_set_mode_strict(unsigned int flags, unsigned long addr) |
| 204 | { |
| 205 | tprintf("%u, ", flags); |
Dmitry V. Levin | 931ebbf | 2015-07-17 01:10:45 +0000 | [diff] [blame] | 206 | printaddr(addr); |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Dmitry V. Levin | a0bd374 | 2015-04-07 01:36:50 +0000 | [diff] [blame] | 209 | SYS_FUNC(seccomp) |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 210 | { |
Dmitry V. Levin | 1252d26 | 2015-07-17 01:14:51 +0000 | [diff] [blame] | 211 | unsigned int op = tcp->u_arg[0]; |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 212 | |
Dmitry V. Levin | 1252d26 | 2015-07-17 01:14:51 +0000 | [diff] [blame] | 213 | printxval(seccomp_ops, op, "SECCOMP_SET_MODE_???"); |
| 214 | tprints(", "); |
| 215 | |
| 216 | if (op == SECCOMP_SET_MODE_FILTER) { |
| 217 | printflags(seccomp_filter_flags, tcp->u_arg[1], |
| 218 | "SECCOMP_FILTER_FLAG_???"); |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 219 | tprints(", "); |
Dmitry V. Levin | 1252d26 | 2015-07-17 01:14:51 +0000 | [diff] [blame] | 220 | print_seccomp_filter(tcp, tcp->u_arg[2]); |
| 221 | } else { |
| 222 | decode_seccomp_set_mode_strict(tcp->u_arg[1], |
| 223 | tcp->u_arg[2]); |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 224 | } |
Dmitry V. Levin | 1252d26 | 2015-07-17 01:14:51 +0000 | [diff] [blame] | 225 | |
| 226 | return RVAL_DECODED; |
Dmitry V. Levin | 2af6903 | 2015-02-04 23:50:50 +0000 | [diff] [blame] | 227 | } |