1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sandbox/linux/bpf_dsl/policy_compiler.h"
8 #include <sys/syscall.h>
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "sandbox/linux/bpf_dsl/bpf_dsl.h"
15 #include "sandbox/linux/bpf_dsl/bpf_dsl_impl.h"
16 #include "sandbox/linux/bpf_dsl/codegen.h"
17 #include "sandbox/linux/bpf_dsl/dump_bpf.h"
18 #include "sandbox/linux/bpf_dsl/policy.h"
19 #include "sandbox/linux/bpf_dsl/seccomp_macros.h"
20 #include "sandbox/linux/bpf_dsl/syscall_set.h"
21 #include "sandbox/linux/bpf_dsl/verifier.h"
22 #include "sandbox/linux/seccomp-bpf/errorcode.h"
23 #include "sandbox/linux/system_headers/linux_filter.h"
24 #include "sandbox/linux/system_headers/linux_seccomp.h"
25 #include "sandbox/linux/system_headers/linux_syscalls.h"
32 #if defined(__i386__) || defined(__x86_64__)
33 const bool kIsIntel
= true;
35 const bool kIsIntel
= false;
37 #if defined(__x86_64__) && defined(__ILP32__)
38 const bool kIsX32
= true;
40 const bool kIsX32
= false;
43 const int kSyscallsRequiredForUnsafeTraps
[] = {
46 #if defined(__NR_sigprocmask)
49 #if defined(__NR_sigreturn)
54 bool HasExactlyOneBit(uint64_t x
) {
55 // Common trick; e.g., see http://stackoverflow.com/a/108329.
56 return x
!= 0 && (x
& (x
- 1)) == 0;
59 // A Trap() handler that returns an "errno" value. The value is encoded
60 // in the "aux" parameter.
61 intptr_t ReturnErrno(const struct arch_seccomp_data
&, void* aux
) {
62 // TrapFnc functions report error by following the native kernel convention
63 // of returning an exit code in the range of -1..-4096. They do not try to
64 // set errno themselves. The glibc wrapper that triggered the SIGSYS will
65 // ultimately do so for us.
66 int err
= reinterpret_cast<intptr_t>(aux
) & SECCOMP_RET_DATA
;
70 bool HasUnsafeTraps(const Policy
* policy
) {
72 for (uint32_t sysnum
: SyscallSet::ValidOnly()) {
73 if (policy
->EvaluateSyscall(sysnum
)->HasUnsafeTraps()) {
77 return policy
->InvalidSyscall()->HasUnsafeTraps();
82 struct PolicyCompiler::Range
{
87 PolicyCompiler::PolicyCompiler(const Policy
* policy
, TrapRegistry
* registry
)
93 has_unsafe_traps_(HasUnsafeTraps(policy_
)) {
97 PolicyCompiler::~PolicyCompiler() {
100 scoped_ptr
<CodeGen::Program
> PolicyCompiler::Compile(bool verify
) {
101 CHECK(policy_
->InvalidSyscall()->IsDeny())
102 << "Policies should deny invalid system calls";
104 // If our BPF program has unsafe traps, enable support for them.
105 if (has_unsafe_traps_
) {
106 CHECK_NE(0U, escapepc_
) << "UnsafeTrap() requires a valid escape PC";
108 for (int sysnum
: kSyscallsRequiredForUnsafeTraps
) {
109 CHECK(policy_
->EvaluateSyscall(sysnum
)->IsAllow())
110 << "Policies that use UnsafeTrap() must unconditionally allow all "
111 "required system calls";
114 CHECK(registry_
->EnableUnsafeTraps())
115 << "We'd rather die than enable unsafe traps";
118 // Assemble the BPF filter program.
119 scoped_ptr
<CodeGen::Program
> program(new CodeGen::Program());
120 gen_
.Compile(AssemblePolicy(), program
.get());
122 // Make sure compilation resulted in a BPF program that executes
123 // correctly. Otherwise, there is an internal error in our BPF compiler.
124 // There is really nothing the caller can do until the bug is fixed.
126 const char* err
= nullptr;
127 if (!Verifier::VerifyBPF(this, *program
, *policy_
, &err
)) {
128 DumpBPF::PrintProgram(*program
);
133 return program
.Pass();
136 void PolicyCompiler::DangerousSetEscapePC(uint64_t escapepc
) {
137 escapepc_
= escapepc
;
140 CodeGen::Node
PolicyCompiler::AssemblePolicy() {
141 // A compiled policy consists of three logical parts:
142 // 1. Check that the "arch" field matches the expected architecture.
143 // 2. If the policy involves unsafe traps, check if the syscall was
144 // invoked by Syscall::Call, and then allow it unconditionally.
145 // 3. Check the system call number and jump to the appropriate compiled
146 // system call policy number.
147 return CheckArch(MaybeAddEscapeHatch(DispatchSyscall()));
150 CodeGen::Node
PolicyCompiler::CheckArch(CodeGen::Node passed
) {
151 // If the architecture doesn't match SECCOMP_ARCH, disallow the
153 return gen_
.MakeInstruction(
154 BPF_LD
+ BPF_W
+ BPF_ABS
, SECCOMP_ARCH_IDX
,
155 gen_
.MakeInstruction(
156 BPF_JMP
+ BPF_JEQ
+ BPF_K
, SECCOMP_ARCH
, passed
,
157 CompileResult(Kill("Invalid audit architecture in BPF filter"))));
160 CodeGen::Node
PolicyCompiler::MaybeAddEscapeHatch(CodeGen::Node rest
) {
161 // If no unsafe traps, then simply return |rest|.
162 if (!has_unsafe_traps_
) {
166 // We already enabled unsafe traps in Compile, but enable them again to give
167 // the trap registry a second chance to complain before we add the backdoor.
168 CHECK(registry_
->EnableUnsafeTraps());
170 // Allow system calls, if they originate from our magic return address.
171 const uint32_t lopc
= static_cast<uint32_t>(escapepc_
);
172 const uint32_t hipc
= static_cast<uint32_t>(escapepc_
>> 32);
174 // BPF cannot do native 64-bit comparisons, so we have to compare
175 // both 32-bit halves of the instruction pointer. If they match what
176 // we expect, we return ERR_ALLOWED. If either or both don't match,
177 // we continue evalutating the rest of the sandbox policy.
179 // For simplicity, we check the full 64-bit instruction pointer even
180 // on 32-bit architectures.
181 return gen_
.MakeInstruction(
182 BPF_LD
+ BPF_W
+ BPF_ABS
, SECCOMP_IP_LSB_IDX
,
183 gen_
.MakeInstruction(
184 BPF_JMP
+ BPF_JEQ
+ BPF_K
, lopc
,
185 gen_
.MakeInstruction(
186 BPF_LD
+ BPF_W
+ BPF_ABS
, SECCOMP_IP_MSB_IDX
,
187 gen_
.MakeInstruction(BPF_JMP
+ BPF_JEQ
+ BPF_K
, hipc
,
188 CompileResult(Allow()), rest
)),
192 CodeGen::Node
PolicyCompiler::DispatchSyscall() {
193 // Evaluate all possible system calls and group their ErrorCodes into
194 // ranges of identical codes.
198 // Compile the system call ranges to an optimized BPF jumptable
199 CodeGen::Node jumptable
= AssembleJumpTable(ranges
.begin(), ranges
.end());
201 // Grab the system call number, so that we can check it and then
202 // execute the jump table.
203 return gen_
.MakeInstruction(
204 BPF_LD
+ BPF_W
+ BPF_ABS
, SECCOMP_NR_IDX
, CheckSyscallNumber(jumptable
));
207 CodeGen::Node
PolicyCompiler::CheckSyscallNumber(CodeGen::Node passed
) {
209 // On Intel architectures, verify that system call numbers are in the
210 // expected number range.
211 CodeGen::Node invalidX32
=
212 CompileResult(Kill("Illegal mixing of system call ABIs"));
214 // The newer x32 API always sets bit 30.
215 return gen_
.MakeInstruction(
216 BPF_JMP
+ BPF_JSET
+ BPF_K
, 0x40000000, passed
, invalidX32
);
218 // The older i386 and x86-64 APIs clear bit 30 on all system calls.
219 return gen_
.MakeInstruction(
220 BPF_JMP
+ BPF_JSET
+ BPF_K
, 0x40000000, invalidX32
, passed
);
224 // TODO(mdempsky): Similar validation for other architectures?
228 void PolicyCompiler::FindRanges(Ranges
* ranges
) {
229 // Please note that "struct seccomp_data" defines system calls as a signed
230 // int32_t, but BPF instructions always operate on unsigned quantities. We
231 // deal with this disparity by enumerating from MIN_SYSCALL to MAX_SYSCALL,
232 // and then verifying that the rest of the number range (both positive and
233 // negative) all return the same ErrorCode.
234 const CodeGen::Node invalid_node
= CompileResult(policy_
->InvalidSyscall());
235 uint32_t old_sysnum
= 0;
236 CodeGen::Node old_node
=
237 SyscallSet::IsValid(old_sysnum
)
238 ? CompileResult(policy_
->EvaluateSyscall(old_sysnum
))
241 for (uint32_t sysnum
: SyscallSet::All()) {
243 SyscallSet::IsValid(sysnum
)
244 ? CompileResult(policy_
->EvaluateSyscall(static_cast<int>(sysnum
)))
246 // N.B., here we rely on CodeGen folding (i.e., returning the same
247 // node value for) identical code sequences, otherwise our jump
248 // table will blow up in size.
249 if (node
!= old_node
) {
250 ranges
->push_back(Range
{old_sysnum
, old_node
});
255 ranges
->push_back(Range
{old_sysnum
, old_node
});
258 CodeGen::Node
PolicyCompiler::AssembleJumpTable(Ranges::const_iterator start
,
259 Ranges::const_iterator stop
) {
260 // We convert the list of system call ranges into jump table that performs
261 // a binary search over the ranges.
262 // As a sanity check, we need to have at least one distinct ranges for us
263 // to be able to build a jump table.
264 CHECK(start
< stop
) << "Invalid iterator range";
265 const auto n
= stop
- start
;
267 // If we have narrowed things down to a single range object, we can
268 // return from the BPF filter program.
272 // Pick the range object that is located at the mid point of our list.
273 // We compare our system call number against the lowest valid system call
274 // number in this range object. If our number is lower, it is outside of
275 // this range object. If it is greater or equal, it might be inside.
276 Ranges::const_iterator mid
= start
+ n
/ 2;
278 // Sub-divide the list of ranges and continue recursively.
279 CodeGen::Node jf
= AssembleJumpTable(start
, mid
);
280 CodeGen::Node jt
= AssembleJumpTable(mid
, stop
);
281 return gen_
.MakeInstruction(BPF_JMP
+ BPF_JGE
+ BPF_K
, mid
->from
, jt
, jf
);
284 CodeGen::Node
PolicyCompiler::CompileResult(const ResultExpr
& res
) {
285 return RetExpression(res
->Compile(this));
288 CodeGen::Node
PolicyCompiler::RetExpression(const ErrorCode
& err
) {
289 switch (err
.error_type()) {
290 case ErrorCode::ET_COND
:
291 return CondExpression(err
);
292 case ErrorCode::ET_SIMPLE
:
293 case ErrorCode::ET_TRAP
:
294 return gen_
.MakeInstruction(BPF_RET
+ BPF_K
, err
.err());
297 << "ErrorCode is not suitable for returning from a BPF program";
298 return CodeGen::kNullNode
;
302 CodeGen::Node
PolicyCompiler::CondExpression(const ErrorCode
& cond
) {
303 // Sanity check that |cond| makes sense.
304 CHECK(cond
.argno_
>= 0 && cond
.argno_
< 6) << "Invalid argument number "
306 CHECK(cond
.width_
== ErrorCode::TP_32BIT
||
307 cond
.width_
== ErrorCode::TP_64BIT
)
308 << "Invalid argument width " << cond
.width_
;
309 CHECK_NE(0U, cond
.mask_
) << "Zero mask is invalid";
310 CHECK_EQ(cond
.value_
, cond
.value_
& cond
.mask_
)
311 << "Value contains masked out bits";
312 if (sizeof(void*) == 4) {
313 CHECK_EQ(ErrorCode::TP_32BIT
, cond
.width_
)
314 << "Invalid width on 32-bit platform";
316 if (cond
.width_
== ErrorCode::TP_32BIT
) {
317 CHECK_EQ(0U, cond
.mask_
>> 32) << "Mask exceeds argument size";
318 CHECK_EQ(0U, cond
.value_
>> 32) << "Value exceeds argument size";
321 CodeGen::Node passed
= RetExpression(*cond
.passed_
);
322 CodeGen::Node failed
= RetExpression(*cond
.failed_
);
324 // We want to emit code to check "(arg & mask) == value" where arg, mask, and
325 // value are 64-bit values, but the BPF machine is only 32-bit. We implement
326 // this by independently testing the upper and lower 32-bits and continuing to
327 // |passed| if both evaluate true, or to |failed| if either evaluate false.
328 return CondExpressionHalf(cond
,
330 CondExpressionHalf(cond
, LowerHalf
, passed
, failed
),
334 CodeGen::Node
PolicyCompiler::CondExpressionHalf(const ErrorCode
& cond
,
336 CodeGen::Node passed
,
337 CodeGen::Node failed
) {
338 if (cond
.width_
== ErrorCode::TP_32BIT
&& half
== UpperHalf
) {
339 // Special logic for sanity checking the upper 32-bits of 32-bit system
342 // TODO(mdempsky): Compile Unexpected64bitArgument() just per program.
343 CodeGen::Node invalid_64bit
= RetExpression(Unexpected64bitArgument());
345 const uint32_t upper
= SECCOMP_ARG_MSB_IDX(cond
.argno_
);
346 const uint32_t lower
= SECCOMP_ARG_LSB_IDX(cond
.argno_
);
348 if (sizeof(void*) == 4) {
349 // On 32-bit platforms, the upper 32-bits should always be 0:
351 // JEQ 0, passed, invalid
352 return gen_
.MakeInstruction(
353 BPF_LD
+ BPF_W
+ BPF_ABS
,
355 gen_
.MakeInstruction(
356 BPF_JMP
+ BPF_JEQ
+ BPF_K
, 0, passed
, invalid_64bit
));
359 // On 64-bit platforms, the upper 32-bits may be 0 or ~0; but we only allow
360 // ~0 if the sign bit of the lower 32-bits is set too:
362 // JEQ 0, passed, (next)
363 // JEQ ~0, (next), invalid
365 // JSET (1<<31), passed, invalid
367 // TODO(mdempsky): The JSET instruction could perhaps jump to passed->next
368 // instead, as the first instruction of passed should be "LDW [lower]".
369 return gen_
.MakeInstruction(
370 BPF_LD
+ BPF_W
+ BPF_ABS
,
372 gen_
.MakeInstruction(
373 BPF_JMP
+ BPF_JEQ
+ BPF_K
,
376 gen_
.MakeInstruction(
377 BPF_JMP
+ BPF_JEQ
+ BPF_K
,
378 std::numeric_limits
<uint32_t>::max(),
379 gen_
.MakeInstruction(
380 BPF_LD
+ BPF_W
+ BPF_ABS
,
382 gen_
.MakeInstruction(BPF_JMP
+ BPF_JSET
+ BPF_K
,
389 const uint32_t idx
= (half
== UpperHalf
) ? SECCOMP_ARG_MSB_IDX(cond
.argno_
)
390 : SECCOMP_ARG_LSB_IDX(cond
.argno_
);
391 const uint32_t mask
= (half
== UpperHalf
) ? cond
.mask_
>> 32 : cond
.mask_
;
392 const uint32_t value
= (half
== UpperHalf
) ? cond
.value_
>> 32 : cond
.value_
;
394 // Emit a suitable instruction sequence for (arg & mask) == value.
396 // For (arg & 0) == 0, just return passed.
402 // For (arg & ~0) == value, emit:
404 // JEQ value, passed, failed
405 if (mask
== std::numeric_limits
<uint32_t>::max()) {
406 return gen_
.MakeInstruction(
407 BPF_LD
+ BPF_W
+ BPF_ABS
,
409 gen_
.MakeInstruction(BPF_JMP
+ BPF_JEQ
+ BPF_K
, value
, passed
, failed
));
412 // For (arg & mask) == 0, emit:
414 // JSET mask, failed, passed
415 // (Note: failed and passed are intentionally swapped.)
417 return gen_
.MakeInstruction(
418 BPF_LD
+ BPF_W
+ BPF_ABS
,
420 gen_
.MakeInstruction(BPF_JMP
+ BPF_JSET
+ BPF_K
, mask
, failed
, passed
));
423 // For (arg & x) == x where x is a single-bit value, emit:
425 // JSET mask, passed, failed
426 if (mask
== value
&& HasExactlyOneBit(mask
)) {
427 return gen_
.MakeInstruction(
428 BPF_LD
+ BPF_W
+ BPF_ABS
,
430 gen_
.MakeInstruction(BPF_JMP
+ BPF_JSET
+ BPF_K
, mask
, passed
, failed
));
436 // JEQ value, passed, failed
437 return gen_
.MakeInstruction(
438 BPF_LD
+ BPF_W
+ BPF_ABS
,
440 gen_
.MakeInstruction(
441 BPF_ALU
+ BPF_AND
+ BPF_K
,
443 gen_
.MakeInstruction(
444 BPF_JMP
+ BPF_JEQ
+ BPF_K
, value
, passed
, failed
)));
447 ErrorCode
PolicyCompiler::Unexpected64bitArgument() {
448 return Kill("Unexpected 64bit argument detected")->Compile(this);
451 ErrorCode
PolicyCompiler::Error(int err
) {
452 if (has_unsafe_traps_
) {
453 // When inside an UnsafeTrap() callback, we want to allow all system calls.
454 // This means, we must conditionally disable the sandbox -- and that's not
455 // something that kernel-side BPF filters can do, as they cannot inspect
456 // any state other than the syscall arguments.
457 // But if we redirect all error handlers to user-space, then we can easily
458 // make this decision.
459 // The performance penalty for this extra round-trip to user-space is not
460 // actually that bad, as we only ever pay it for denied system calls; and a
461 // typical program has very few of these.
462 return Trap(ReturnErrno
, reinterpret_cast<void*>(err
), true);
465 return ErrorCode(err
);
468 ErrorCode
PolicyCompiler::Trap(TrapRegistry::TrapFnc fnc
,
471 uint16_t trap_id
= registry_
->Add(fnc
, aux
, safe
);
472 return ErrorCode(trap_id
, fnc
, aux
, safe
);
475 bool PolicyCompiler::IsRequiredForUnsafeTrap(int sysno
) {
476 for (size_t i
= 0; i
< arraysize(kSyscallsRequiredForUnsafeTraps
); ++i
) {
477 if (sysno
== kSyscallsRequiredForUnsafeTraps
[i
]) {
484 ErrorCode
PolicyCompiler::CondMaskedEqual(int argno
,
485 ErrorCode::ArgType width
,
488 const ErrorCode
& passed
,
489 const ErrorCode
& failed
) {
490 return ErrorCode(argno
,
494 &*conds_
.insert(passed
).first
,
495 &*conds_
.insert(failed
).first
);
498 } // namespace bpf_dsl
499 } // namespace sandbox