blob: e62f99a0c711429486060306dace58ff6a924e77 [file] [log] [blame]
Dmitry V. Levin2af69032015-02-04 23:50:50 +00001/*
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. Levin2af69032015-02-04 23:50:50 +000033#include "xlat/seccomp_ops.h"
Dmitry V. Levin2af69032015-02-04 23:50:50 +000034#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
53struct 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
62static void
63decode_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. Levin3ea6a592016-04-11 20:25:01 +000071 tprints("|");
Dmitry V. Levin2af69032015-02-04 23:50:50 +000072 printxval(bpf_size, BPF_SIZE(code), "BPF_???");
Dmitry V. Levin3ea6a592016-04-11 20:25:01 +000073 tprints("|");
Dmitry V. Levin2af69032015-02-04 23:50:50 +000074 printxval(bpf_mode, BPF_MODE(code), "BPF_???");
75 break;
76 case BPF_ST:
77 case BPF_STX:
78 if (i)
Dmitry V. Levin3ea6a592016-04-11 20:25:01 +000079 tprintf("|%#x /* %s */", i, "BPF_???");
Dmitry V. Levin2af69032015-02-04 23:50:50 +000080 break;
81 case BPF_ALU:
Dmitry V. Levin3ea6a592016-04-11 20:25:01 +000082 tprints("|");
Dmitry V. Levin2af69032015-02-04 23:50:50 +000083 printxval(bpf_src, BPF_SRC(code), "BPF_???");
Dmitry V. Levin3ea6a592016-04-11 20:25:01 +000084 tprints("|");
Dmitry V. Levin2af69032015-02-04 23:50:50 +000085 printxval(bpf_op_alu, BPF_OP(code), "BPF_???");
86 break;
87 case BPF_JMP:
Dmitry V. Levin3ea6a592016-04-11 20:25:01 +000088 tprints("|");
Dmitry V. Levin2af69032015-02-04 23:50:50 +000089 printxval(bpf_src, BPF_SRC(code), "BPF_???");
Dmitry V. Levin3ea6a592016-04-11 20:25:01 +000090 tprints("|");
Dmitry V. Levin2af69032015-02-04 23:50:50 +000091 printxval(bpf_op_jmp, BPF_OP(code), "BPF_???");
92 break;
93 case BPF_RET:
Dmitry V. Levin3ea6a592016-04-11 20:25:01 +000094 tprints("|");
Dmitry V. Levin2af69032015-02-04 23:50:50 +000095 printxval(bpf_rval, BPF_RVAL(code), "BPF_???");
96 i &= ~BPF_RVAL(code);
97 if (i)
Dmitry V. Levin3ea6a592016-04-11 20:25:01 +000098 tprintf("|%#x /* %s */", i, "BPF_???");
Dmitry V. Levin2af69032015-02-04 23:50:50 +000099 break;
100 case BPF_MISC:
Dmitry V. Levin3ea6a592016-04-11 20:25:01 +0000101 tprints("|");
Dmitry V. Levin2af69032015-02-04 23:50:50 +0000102 printxval(bpf_miscop, BPF_MISCOP(code), "BPF_???");
103 i &= ~BPF_MISCOP(code);
104 if (i)
Dmitry V. Levin3ea6a592016-04-11 20:25:01 +0000105 tprintf("|%#x /* %s */", i, "BPF_???");
Dmitry V. Levin2af69032015-02-04 23:50:50 +0000106 break;
107 }
108
109}
110
111static void
112decode_bpf_stmt(const struct bpf_filter *filter)
113{
114#ifdef HAVE_LINUX_FILTER_H
115 tprints("BPF_STMT(");
116 decode_bpf_code(filter->code);
117 tprints(", ");
118 if (BPF_CLASS(filter->code) == BPF_RET) {
119 unsigned int action = SECCOMP_RET_ACTION & filter->k;
120 unsigned int data = filter->k & ~action;
121
122 printxval(seccomp_ret_action, action, "SECCOMP_RET_???");
123 if (data)
Dmitry V. Levin3ea6a592016-04-11 20:25:01 +0000124 tprintf("|%#x)", data);
Dmitry V. Levin2af69032015-02-04 23:50:50 +0000125 else
126 tprints(")");
127 } else {
128 tprintf("%#x)", filter->k);
129 }
130#else
131 tprintf("BPF_STMT(%#x, %#x)", filter->code, filter->k);
132#endif /* HAVE_LINUX_FILTER_H */
133}
134
135static void
136decode_bpf_jump(const struct bpf_filter *filter)
137{
138#ifdef HAVE_LINUX_FILTER_H
139 tprints("BPF_JUMP(");
140 decode_bpf_code(filter->code);
141 tprintf(", %#x, %#x, %#x)",
142 filter->k, filter->jt, filter->jf);
143#else
144 tprintf("BPF_JUMP(%#x, %#x, %#x, %#x)",
145 filter->code, filter->k, filter->jt, filter->jf);
146#endif /* HAVE_LINUX_FILTER_H */
147}
148
149static void
150decode_filter(const struct bpf_filter *filter)
151{
152 if (filter->jt || filter->jf)
153 decode_bpf_jump(filter);
154 else
155 decode_bpf_stmt(filter);
156}
157
158#endif /* HAVE_LINUX_FILTER_H */
159
160#ifndef BPF_MAXINSNS
161# define BPF_MAXINSNS 4096
162#endif
163
164static void
Dmitry V. Levin6ba947f2016-04-12 00:05:43 +0000165decode_seccomp_fprog(struct tcb *tcp, unsigned short len, unsigned long addr)
Dmitry V. Levin2af69032015-02-04 23:50:50 +0000166{
Dmitry V. Levin6ba947f2016-04-12 00:05:43 +0000167 struct bpf_filter filter;
168 const unsigned long start_addr = addr;
169 unsigned int i = 0;
Dmitry V. Levin2af69032015-02-04 23:50:50 +0000170
Dmitry V. Levin6ba947f2016-04-12 00:05:43 +0000171 for (; addr >= start_addr && i < len; ++i, addr += sizeof(filter)) {
172 if (i) {
173 tprints(", ");
174 if (i >= BPF_MAXINSNS) {
175 tprints("...");
Dmitry V. Levin2af69032015-02-04 23:50:50 +0000176 break;
Dmitry V. Levin6ba947f2016-04-12 00:05:43 +0000177 }
Dmitry V. Levin2af69032015-02-04 23:50:50 +0000178 }
Dmitry V. Levin6ba947f2016-04-12 00:05:43 +0000179 if (umove_or_printaddr(tcp, addr, &filter))
180 break;
181 if (!i)
182 tprints("[");
183 decode_filter(&filter);
Dmitry V. Levin2af69032015-02-04 23:50:50 +0000184 }
Dmitry V. Levin6ba947f2016-04-12 00:05:43 +0000185 if (i)
186 tprints("]");
187}
188
189static void
190print_seccomp_fprog(struct tcb *tcp, unsigned short len, unsigned long addr)
191{
192 tprintf("{len=%u, filter=", len);
193 if (abbrev(tcp) || !len)
194 printaddr(addr);
195 else
196 decode_seccomp_fprog(tcp, len, addr);
197 tprints("}");
Dmitry V. Levin2af69032015-02-04 23:50:50 +0000198}
199
Dmitry V. Levin7a129742015-09-19 21:28:23 +0000200#include "seccomp_fprog.h"
201
Dmitry V. Levin2af69032015-02-04 23:50:50 +0000202void
203print_seccomp_filter(struct tcb *tcp, unsigned long addr)
204{
Dmitry V. Levin7a129742015-09-19 21:28:23 +0000205 struct seccomp_fprog fprog;
Dmitry V. Levin931ebbf2015-07-17 01:10:45 +0000206
Dmitry V. Levin7a129742015-09-19 21:28:23 +0000207 if (fetch_seccomp_fprog(tcp, addr, &fprog))
Dmitry V. Levin6ba947f2016-04-12 00:05:43 +0000208 print_seccomp_fprog(tcp, fprog.len, fprog.filter);
Dmitry V. Levin2af69032015-02-04 23:50:50 +0000209}
210
211static void
212decode_seccomp_set_mode_strict(unsigned int flags, unsigned long addr)
213{
214 tprintf("%u, ", flags);
Dmitry V. Levin931ebbf2015-07-17 01:10:45 +0000215 printaddr(addr);
Dmitry V. Levin2af69032015-02-04 23:50:50 +0000216}
217
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000218SYS_FUNC(seccomp)
Dmitry V. Levin2af69032015-02-04 23:50:50 +0000219{
Dmitry V. Levin1252d262015-07-17 01:14:51 +0000220 unsigned int op = tcp->u_arg[0];
Dmitry V. Levin2af69032015-02-04 23:50:50 +0000221
Dmitry V. Levin1252d262015-07-17 01:14:51 +0000222 printxval(seccomp_ops, op, "SECCOMP_SET_MODE_???");
223 tprints(", ");
224
225 if (op == SECCOMP_SET_MODE_FILTER) {
226 printflags(seccomp_filter_flags, tcp->u_arg[1],
227 "SECCOMP_FILTER_FLAG_???");
Dmitry V. Levin2af69032015-02-04 23:50:50 +0000228 tprints(", ");
Dmitry V. Levin1252d262015-07-17 01:14:51 +0000229 print_seccomp_filter(tcp, tcp->u_arg[2]);
230 } else {
231 decode_seccomp_set_mode_strict(tcp->u_arg[1],
232 tcp->u_arg[2]);
Dmitry V. Levin2af69032015-02-04 23:50:50 +0000233 }
Dmitry V. Levin1252d262015-07-17 01:14:51 +0000234
235 return RVAL_DECODED;
Dmitry V. Levin2af69032015-02-04 23:50:50 +0000236}