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 <linux/filter.h>
9 #include <sys/syscall.h>
13 #include "base/logging.h"
14 #include "base/macros.h"
15 #include "sandbox/linux/bpf_dsl/bpf_dsl.h"
16 #include "sandbox/linux/bpf_dsl/bpf_dsl_impl.h"
17 #include "sandbox/linux/bpf_dsl/policy.h"
18 #include "sandbox/linux/seccomp-bpf/codegen.h"
19 #include "sandbox/linux/seccomp-bpf/die.h"
20 #include "sandbox/linux/seccomp-bpf/errorcode.h"
21 #include "sandbox/linux/seccomp-bpf/linux_seccomp.h"
22 #include "sandbox/linux/seccomp-bpf/syscall.h"
23 #include "sandbox/linux/seccomp-bpf/syscall_iterator.h"
30 #if defined(__i386__) || defined(__x86_64__)
31 const bool kIsIntel
= true;
33 const bool kIsIntel
= false;
35 #if defined(__x86_64__) && defined(__ILP32__)
36 const bool kIsX32
= true;
38 const bool kIsX32
= false;
41 const int kSyscallsRequiredForUnsafeTraps
[] = {
44 #if defined(__NR_sigprocmask)
47 #if defined(__NR_sigreturn)
52 bool HasExactlyOneBit(uint64_t x
) {
53 // Common trick; e.g., see http://stackoverflow.com/a/108329.
54 return x
!= 0 && (x
& (x
- 1)) == 0;
57 bool IsDenied(const ErrorCode
& code
) {
58 return (code
.err() & SECCOMP_RET_ACTION
) == SECCOMP_RET_TRAP
||
59 (code
.err() >= (SECCOMP_RET_ERRNO
+ ErrorCode::ERR_MIN_ERRNO
) &&
60 code
.err() <= (SECCOMP_RET_ERRNO
+ ErrorCode::ERR_MAX_ERRNO
));
63 // A Trap() handler that returns an "errno" value. The value is encoded
64 // in the "aux" parameter.
65 intptr_t ReturnErrno(const struct arch_seccomp_data
&, void* aux
) {
66 // TrapFnc functions report error by following the native kernel convention
67 // of returning an exit code in the range of -1..-4096. They do not try to
68 // set errno themselves. The glibc wrapper that triggered the SIGSYS will
69 // ultimately do so for us.
70 int err
= reinterpret_cast<intptr_t>(aux
) & SECCOMP_RET_DATA
;
74 intptr_t BPFFailure(const struct arch_seccomp_data
&, void* aux
) {
75 SANDBOX_DIE(static_cast<char*>(aux
));
78 bool HasUnsafeTraps(const Policy
* policy
) {
79 for (uint32_t sysnum
: SyscallSet::ValidOnly()) {
80 if (policy
->EvaluateSyscall(sysnum
)->HasUnsafeTraps()) {
84 return policy
->InvalidSyscall()->HasUnsafeTraps();
89 struct PolicyCompiler::Range
{
90 Range(uint32_t f
, const ErrorCode
& e
) : from(f
), err(e
) {}
95 PolicyCompiler::PolicyCompiler(const Policy
* policy
, TrapRegistry
* registry
)
100 has_unsafe_traps_(HasUnsafeTraps(policy_
)) {
103 PolicyCompiler::~PolicyCompiler() {
106 scoped_ptr
<CodeGen::Program
> PolicyCompiler::Compile() {
107 if (!IsDenied(policy_
->InvalidSyscall()->Compile(this))) {
108 SANDBOX_DIE("Policies should deny invalid system calls.");
111 // If our BPF program has unsafe traps, enable support for them.
112 if (has_unsafe_traps_
) {
113 // As support for unsafe jumps essentially defeats all the security
114 // measures that the sandbox provides, we print a big warning message --
115 // and of course, we make sure to only ever enable this feature if it
116 // is actually requested by the sandbox policy.
117 if (Syscall::Call(-1) == -1 && errno
== ENOSYS
) {
119 "Support for UnsafeTrap() has not yet been ported to this "
123 for (int sysnum
: kSyscallsRequiredForUnsafeTraps
) {
124 if (!policy_
->EvaluateSyscall(sysnum
)->Compile(this)
125 .Equals(ErrorCode(ErrorCode::ERR_ALLOWED
))) {
127 "Policies that use UnsafeTrap() must unconditionally allow all "
128 "required system calls");
132 if (!registry_
->EnableUnsafeTraps()) {
133 // We should never be able to get here, as UnsafeTrap() should never
134 // actually return a valid ErrorCode object unless the user set the
135 // CHROME_SANDBOX_DEBUGGING environment variable; and therefore,
136 // "has_unsafe_traps" would always be false. But better double-check
137 // than enabling dangerous code.
138 SANDBOX_DIE("We'd rather die than enable unsafe traps");
142 // Assemble the BPF filter program.
143 scoped_ptr
<CodeGen::Program
> program(new CodeGen::Program());
144 gen_
.Compile(AssemblePolicy(), program
.get());
145 return program
.Pass();
148 CodeGen::Node
PolicyCompiler::AssemblePolicy() {
149 // A compiled policy consists of three logical parts:
150 // 1. Check that the "arch" field matches the expected architecture.
151 // 2. If the policy involves unsafe traps, check if the syscall was
152 // invoked by Syscall::Call, and then allow it unconditionally.
153 // 3. Check the system call number and jump to the appropriate compiled
154 // system call policy number.
155 return CheckArch(MaybeAddEscapeHatch(DispatchSyscall()));
158 CodeGen::Node
PolicyCompiler::CheckArch(CodeGen::Node passed
) {
159 // If the architecture doesn't match SECCOMP_ARCH, disallow the
161 return gen_
.MakeInstruction(
162 BPF_LD
+ BPF_W
+ BPF_ABS
,
164 gen_
.MakeInstruction(
165 BPF_JMP
+ BPF_JEQ
+ BPF_K
,
168 RetExpression(Kill("Invalid audit architecture in BPF filter"))));
171 CodeGen::Node
PolicyCompiler::MaybeAddEscapeHatch(CodeGen::Node rest
) {
172 // If no unsafe traps, then simply return |rest|.
173 if (!has_unsafe_traps_
) {
177 // Allow system calls, if they originate from our magic return address
178 // (which we can query by calling Syscall::Call(-1)).
179 uint64_t syscall_entry_point
=
180 static_cast<uint64_t>(static_cast<uintptr_t>(Syscall::Call(-1)));
181 uint32_t low
= static_cast<uint32_t>(syscall_entry_point
);
182 uint32_t hi
= static_cast<uint32_t>(syscall_entry_point
>> 32);
184 // BPF cannot do native 64-bit comparisons, so we have to compare
185 // both 32-bit halves of the instruction pointer. If they match what
186 // we expect, we return ERR_ALLOWED. If either or both don't match,
187 // we continue evalutating the rest of the sandbox policy.
189 // For simplicity, we check the full 64-bit instruction pointer even
190 // on 32-bit architectures.
191 return gen_
.MakeInstruction(
192 BPF_LD
+ BPF_W
+ BPF_ABS
,
194 gen_
.MakeInstruction(
195 BPF_JMP
+ BPF_JEQ
+ BPF_K
,
197 gen_
.MakeInstruction(
198 BPF_LD
+ BPF_W
+ BPF_ABS
,
200 gen_
.MakeInstruction(
201 BPF_JMP
+ BPF_JEQ
+ BPF_K
,
203 RetExpression(ErrorCode(ErrorCode::ERR_ALLOWED
)),
208 CodeGen::Node
PolicyCompiler::DispatchSyscall() {
209 // Evaluate all possible system calls and group their ErrorCodes into
210 // ranges of identical codes.
214 // Compile the system call ranges to an optimized BPF jumptable
215 CodeGen::Node jumptable
= AssembleJumpTable(ranges
.begin(), ranges
.end());
217 // Grab the system call number, so that we can check it and then
218 // execute the jump table.
219 return gen_
.MakeInstruction(
220 BPF_LD
+ BPF_W
+ BPF_ABS
, SECCOMP_NR_IDX
, CheckSyscallNumber(jumptable
));
223 CodeGen::Node
PolicyCompiler::CheckSyscallNumber(CodeGen::Node passed
) {
225 // On Intel architectures, verify that system call numbers are in the
226 // expected number range.
227 CodeGen::Node invalidX32
=
228 RetExpression(Kill("Illegal mixing of system call ABIs"));
230 // The newer x32 API always sets bit 30.
231 return gen_
.MakeInstruction(
232 BPF_JMP
+ BPF_JSET
+ BPF_K
, 0x40000000, passed
, invalidX32
);
234 // The older i386 and x86-64 APIs clear bit 30 on all system calls.
235 return gen_
.MakeInstruction(
236 BPF_JMP
+ BPF_JSET
+ BPF_K
, 0x40000000, invalidX32
, passed
);
240 // TODO(mdempsky): Similar validation for other architectures?
244 void PolicyCompiler::FindRanges(Ranges
* ranges
) {
245 // Please note that "struct seccomp_data" defines system calls as a signed
246 // int32_t, but BPF instructions always operate on unsigned quantities. We
247 // deal with this disparity by enumerating from MIN_SYSCALL to MAX_SYSCALL,
248 // and then verifying that the rest of the number range (both positive and
249 // negative) all return the same ErrorCode.
250 const ErrorCode invalid_err
= policy_
->InvalidSyscall()->Compile(this);
251 uint32_t old_sysnum
= 0;
252 ErrorCode old_err
= SyscallSet::IsValid(old_sysnum
)
253 ? policy_
->EvaluateSyscall(old_sysnum
)->Compile(this)
256 for (uint32_t sysnum
: SyscallSet::All()) {
258 SyscallSet::IsValid(sysnum
)
259 ? policy_
->EvaluateSyscall(static_cast<int>(sysnum
))->Compile(this)
261 if (!err
.Equals(old_err
)) {
262 ranges
->push_back(Range(old_sysnum
, old_err
));
267 ranges
->push_back(Range(old_sysnum
, old_err
));
270 CodeGen::Node
PolicyCompiler::AssembleJumpTable(Ranges::const_iterator start
,
271 Ranges::const_iterator stop
) {
272 // We convert the list of system call ranges into jump table that performs
273 // a binary search over the ranges.
274 // As a sanity check, we need to have at least one distinct ranges for us
275 // to be able to build a jump table.
276 if (stop
- start
<= 0) {
277 SANDBOX_DIE("Invalid set of system call ranges");
278 } else if (stop
- start
== 1) {
279 // If we have narrowed things down to a single range object, we can
280 // return from the BPF filter program.
281 return RetExpression(start
->err
);
284 // Pick the range object that is located at the mid point of our list.
285 // We compare our system call number against the lowest valid system call
286 // number in this range object. If our number is lower, it is outside of
287 // this range object. If it is greater or equal, it might be inside.
288 Ranges::const_iterator mid
= start
+ (stop
- start
) / 2;
290 // Sub-divide the list of ranges and continue recursively.
291 CodeGen::Node jf
= AssembleJumpTable(start
, mid
);
292 CodeGen::Node jt
= AssembleJumpTable(mid
, stop
);
293 return gen_
.MakeInstruction(BPF_JMP
+ BPF_JGE
+ BPF_K
, mid
->from
, jt
, jf
);
296 CodeGen::Node
PolicyCompiler::RetExpression(const ErrorCode
& err
) {
297 switch (err
.error_type()) {
298 case ErrorCode::ET_COND
:
299 return CondExpression(err
);
300 case ErrorCode::ET_SIMPLE
:
301 case ErrorCode::ET_TRAP
:
302 return gen_
.MakeInstruction(BPF_RET
+ BPF_K
, err
.err());
304 SANDBOX_DIE("ErrorCode is not suitable for returning from a BPF program");
308 CodeGen::Node
PolicyCompiler::CondExpression(const ErrorCode
& cond
) {
309 // Sanity check that |cond| makes sense.
310 if (cond
.argno_
< 0 || cond
.argno_
>= 6) {
311 SANDBOX_DIE("sandbox_bpf: invalid argument number");
313 if (cond
.width_
!= ErrorCode::TP_32BIT
&&
314 cond
.width_
!= ErrorCode::TP_64BIT
) {
315 SANDBOX_DIE("sandbox_bpf: invalid argument width");
317 if (cond
.mask_
== 0) {
318 SANDBOX_DIE("sandbox_bpf: zero mask is invalid");
320 if ((cond
.value_
& cond
.mask_
) != cond
.value_
) {
321 SANDBOX_DIE("sandbox_bpf: value contains masked out bits");
323 if (cond
.width_
== ErrorCode::TP_32BIT
&&
324 ((cond
.mask_
>> 32) != 0 || (cond
.value_
>> 32) != 0)) {
325 SANDBOX_DIE("sandbox_bpf: test exceeds argument size");
327 // TODO(mdempsky): Reject TP_64BIT on 32-bit platforms. For now we allow it
328 // because some SandboxBPF unit tests exercise it.
330 CodeGen::Node passed
= RetExpression(*cond
.passed_
);
331 CodeGen::Node failed
= RetExpression(*cond
.failed_
);
333 // We want to emit code to check "(arg & mask) == value" where arg, mask, and
334 // value are 64-bit values, but the BPF machine is only 32-bit. We implement
335 // this by independently testing the upper and lower 32-bits and continuing to
336 // |passed| if both evaluate true, or to |failed| if either evaluate false.
337 return CondExpressionHalf(cond
,
339 CondExpressionHalf(cond
, LowerHalf
, passed
, failed
),
343 CodeGen::Node
PolicyCompiler::CondExpressionHalf(const ErrorCode
& cond
,
345 CodeGen::Node passed
,
346 CodeGen::Node failed
) {
347 if (cond
.width_
== ErrorCode::TP_32BIT
&& half
== UpperHalf
) {
348 // Special logic for sanity checking the upper 32-bits of 32-bit system
351 // TODO(mdempsky): Compile Unexpected64bitArgument() just per program.
352 CodeGen::Node invalid_64bit
= RetExpression(Unexpected64bitArgument());
354 const uint32_t upper
= SECCOMP_ARG_MSB_IDX(cond
.argno_
);
355 const uint32_t lower
= SECCOMP_ARG_LSB_IDX(cond
.argno_
);
357 if (sizeof(void*) == 4) {
358 // On 32-bit platforms, the upper 32-bits should always be 0:
360 // JEQ 0, passed, invalid
361 return gen_
.MakeInstruction(
362 BPF_LD
+ BPF_W
+ BPF_ABS
,
364 gen_
.MakeInstruction(
365 BPF_JMP
+ BPF_JEQ
+ BPF_K
, 0, passed
, invalid_64bit
));
368 // On 64-bit platforms, the upper 32-bits may be 0 or ~0; but we only allow
369 // ~0 if the sign bit of the lower 32-bits is set too:
371 // JEQ 0, passed, (next)
372 // JEQ ~0, (next), invalid
374 // JSET (1<<31), passed, invalid
376 // TODO(mdempsky): The JSET instruction could perhaps jump to passed->next
377 // instead, as the first instruction of passed should be "LDW [lower]".
378 return gen_
.MakeInstruction(
379 BPF_LD
+ BPF_W
+ BPF_ABS
,
381 gen_
.MakeInstruction(
382 BPF_JMP
+ BPF_JEQ
+ BPF_K
,
385 gen_
.MakeInstruction(
386 BPF_JMP
+ BPF_JEQ
+ BPF_K
,
387 std::numeric_limits
<uint32_t>::max(),
388 gen_
.MakeInstruction(
389 BPF_LD
+ BPF_W
+ BPF_ABS
,
391 gen_
.MakeInstruction(BPF_JMP
+ BPF_JSET
+ BPF_K
,
398 const uint32_t idx
= (half
== UpperHalf
) ? SECCOMP_ARG_MSB_IDX(cond
.argno_
)
399 : SECCOMP_ARG_LSB_IDX(cond
.argno_
);
400 const uint32_t mask
= (half
== UpperHalf
) ? cond
.mask_
>> 32 : cond
.mask_
;
401 const uint32_t value
= (half
== UpperHalf
) ? cond
.value_
>> 32 : cond
.value_
;
403 // Emit a suitable instruction sequence for (arg & mask) == value.
405 // For (arg & 0) == 0, just return passed.
411 // For (arg & ~0) == value, emit:
413 // JEQ value, passed, failed
414 if (mask
== std::numeric_limits
<uint32_t>::max()) {
415 return gen_
.MakeInstruction(
416 BPF_LD
+ BPF_W
+ BPF_ABS
,
418 gen_
.MakeInstruction(BPF_JMP
+ BPF_JEQ
+ BPF_K
, value
, passed
, failed
));
421 // For (arg & mask) == 0, emit:
423 // JSET mask, failed, passed
424 // (Note: failed and passed are intentionally swapped.)
426 return gen_
.MakeInstruction(
427 BPF_LD
+ BPF_W
+ BPF_ABS
,
429 gen_
.MakeInstruction(BPF_JMP
+ BPF_JSET
+ BPF_K
, mask
, failed
, passed
));
432 // For (arg & x) == x where x is a single-bit value, emit:
434 // JSET mask, passed, failed
435 if (mask
== value
&& HasExactlyOneBit(mask
)) {
436 return gen_
.MakeInstruction(
437 BPF_LD
+ BPF_W
+ BPF_ABS
,
439 gen_
.MakeInstruction(BPF_JMP
+ BPF_JSET
+ BPF_K
, mask
, passed
, failed
));
445 // JEQ value, passed, failed
446 return gen_
.MakeInstruction(
447 BPF_LD
+ BPF_W
+ BPF_ABS
,
449 gen_
.MakeInstruction(
450 BPF_ALU
+ BPF_AND
+ BPF_K
,
452 gen_
.MakeInstruction(
453 BPF_JMP
+ BPF_JEQ
+ BPF_K
, value
, passed
, failed
)));
456 ErrorCode
PolicyCompiler::Unexpected64bitArgument() {
457 return Kill("Unexpected 64bit argument detected");
460 ErrorCode
PolicyCompiler::Error(int err
) {
461 if (has_unsafe_traps_
) {
462 // When inside an UnsafeTrap() callback, we want to allow all system calls.
463 // This means, we must conditionally disable the sandbox -- and that's not
464 // something that kernel-side BPF filters can do, as they cannot inspect
465 // any state other than the syscall arguments.
466 // But if we redirect all error handlers to user-space, then we can easily
467 // make this decision.
468 // The performance penalty for this extra round-trip to user-space is not
469 // actually that bad, as we only ever pay it for denied system calls; and a
470 // typical program has very few of these.
471 return Trap(ReturnErrno
, reinterpret_cast<void*>(err
));
474 return ErrorCode(err
);
477 ErrorCode
PolicyCompiler::MakeTrap(TrapRegistry::TrapFnc fnc
,
480 uint16_t trap_id
= registry_
->Add(fnc
, aux
, safe
);
481 return ErrorCode(trap_id
, fnc
, aux
, safe
);
484 ErrorCode
PolicyCompiler::Trap(TrapRegistry::TrapFnc fnc
, const void* aux
) {
485 return MakeTrap(fnc
, aux
, true /* Safe Trap */);
488 ErrorCode
PolicyCompiler::UnsafeTrap(TrapRegistry::TrapFnc fnc
,
490 return MakeTrap(fnc
, aux
, false /* Unsafe Trap */);
493 bool PolicyCompiler::IsRequiredForUnsafeTrap(int sysno
) {
494 for (size_t i
= 0; i
< arraysize(kSyscallsRequiredForUnsafeTraps
); ++i
) {
495 if (sysno
== kSyscallsRequiredForUnsafeTraps
[i
]) {
502 ErrorCode
PolicyCompiler::CondMaskedEqual(int argno
,
503 ErrorCode::ArgType width
,
506 const ErrorCode
& passed
,
507 const ErrorCode
& failed
) {
508 return ErrorCode(argno
,
512 &*conds_
.insert(passed
).first
,
513 &*conds_
.insert(failed
).first
);
516 ErrorCode
PolicyCompiler::Kill(const char* msg
) {
517 return Trap(BPFFailure
, const_cast<char*>(msg
));
520 } // namespace bpf_dsl
521 } // namespace sandbox