| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sandbox/linux/bpf_dsl/policy_compiler.h" | 5 #include "sandbox/linux/bpf_dsl/policy_compiler.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <linux/filter.h> | 8 #include <linux/filter.h> |
| 9 #include <sys/syscall.h> | 9 #include <sys/syscall.h> |
| 10 | 10 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 // ultimately do so for us. | 69 // ultimately do so for us. |
| 70 int err = reinterpret_cast<intptr_t>(aux) & SECCOMP_RET_DATA; | 70 int err = reinterpret_cast<intptr_t>(aux) & SECCOMP_RET_DATA; |
| 71 return -err; | 71 return -err; |
| 72 } | 72 } |
| 73 | 73 |
| 74 intptr_t BPFFailure(const struct arch_seccomp_data&, void* aux) { | 74 intptr_t BPFFailure(const struct arch_seccomp_data&, void* aux) { |
| 75 SANDBOX_DIE(static_cast<char*>(aux)); | 75 SANDBOX_DIE(static_cast<char*>(aux)); |
| 76 } | 76 } |
| 77 | 77 |
| 78 bool HasUnsafeTraps(const SandboxBPFDSLPolicy* policy) { | 78 bool HasUnsafeTraps(const SandboxBPFDSLPolicy* policy) { |
| 79 for (SyscallIterator iter(false); !iter.Done();) { | 79 for (uint32_t sysnum : SyscallSet::All()) { |
| 80 uint32_t sysnum = iter.Next(); | 80 if (SyscallSet::IsValid(sysnum) && |
| 81 if (SyscallIterator::IsValid(sysnum) && | |
| 82 policy->EvaluateSyscall(sysnum)->HasUnsafeTraps()) { | 81 policy->EvaluateSyscall(sysnum)->HasUnsafeTraps()) { |
| 83 return true; | 82 return true; |
| 84 } | 83 } |
| 85 } | 84 } |
| 86 return policy->InvalidSyscall()->HasUnsafeTraps(); | 85 return policy->InvalidSyscall()->HasUnsafeTraps(); |
| 87 } | 86 } |
| 88 | 87 |
| 89 } // namespace | 88 } // namespace |
| 90 | 89 |
| 91 struct PolicyCompiler::Range { | 90 struct PolicyCompiler::Range { |
| 92 Range(uint32_t f, uint32_t t, const ErrorCode& e) : from(f), to(t), err(e) {} | 91 Range(uint32_t f, const ErrorCode& e) : from(f), err(e) {} |
| 93 uint32_t from, to; | 92 uint32_t from; |
| 94 ErrorCode err; | 93 ErrorCode err; |
| 95 }; | 94 }; |
| 96 | 95 |
| 97 PolicyCompiler::PolicyCompiler(const SandboxBPFDSLPolicy* policy, | 96 PolicyCompiler::PolicyCompiler(const SandboxBPFDSLPolicy* policy, |
| 98 TrapRegistry* registry) | 97 TrapRegistry* registry) |
| 99 : policy_(policy), | 98 : policy_(policy), |
| 100 registry_(registry), | 99 registry_(registry), |
| 101 conds_(), | 100 conds_(), |
| 102 gen_(), | 101 gen_(), |
| 103 has_unsafe_traps_(HasUnsafeTraps(policy_)) { | 102 has_unsafe_traps_(HasUnsafeTraps(policy_)) { |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 } | 244 } |
| 246 | 245 |
| 247 void PolicyCompiler::FindRanges(Ranges* ranges) { | 246 void PolicyCompiler::FindRanges(Ranges* ranges) { |
| 248 // Please note that "struct seccomp_data" defines system calls as a signed | 247 // Please note that "struct seccomp_data" defines system calls as a signed |
| 249 // int32_t, but BPF instructions always operate on unsigned quantities. We | 248 // int32_t, but BPF instructions always operate on unsigned quantities. We |
| 250 // deal with this disparity by enumerating from MIN_SYSCALL to MAX_SYSCALL, | 249 // deal with this disparity by enumerating from MIN_SYSCALL to MAX_SYSCALL, |
| 251 // and then verifying that the rest of the number range (both positive and | 250 // and then verifying that the rest of the number range (both positive and |
| 252 // negative) all return the same ErrorCode. | 251 // negative) all return the same ErrorCode. |
| 253 const ErrorCode invalid_err = policy_->InvalidSyscall()->Compile(this); | 252 const ErrorCode invalid_err = policy_->InvalidSyscall()->Compile(this); |
| 254 uint32_t old_sysnum = 0; | 253 uint32_t old_sysnum = 0; |
| 255 ErrorCode old_err = SyscallIterator::IsValid(old_sysnum) | 254 ErrorCode old_err = SyscallSet::IsValid(old_sysnum) |
| 256 ? policy_->EvaluateSyscall(old_sysnum)->Compile(this) | 255 ? policy_->EvaluateSyscall(old_sysnum)->Compile(this) |
| 257 : invalid_err; | 256 : invalid_err; |
| 258 | 257 |
| 259 for (SyscallIterator iter(false); !iter.Done();) { | 258 for (uint32_t sysnum : SyscallSet::All()) { |
| 260 uint32_t sysnum = iter.Next(); | |
| 261 ErrorCode err = | 259 ErrorCode err = |
| 262 SyscallIterator::IsValid(sysnum) | 260 SyscallSet::IsValid(sysnum) |
| 263 ? policy_->EvaluateSyscall(static_cast<int>(sysnum))->Compile(this) | 261 ? policy_->EvaluateSyscall(static_cast<int>(sysnum))->Compile(this) |
| 264 : invalid_err; | 262 : invalid_err; |
| 265 if (!err.Equals(old_err) || iter.Done()) { | 263 if (!err.Equals(old_err)) { |
| 266 ranges->push_back(Range(old_sysnum, sysnum - 1, old_err)); | 264 ranges->push_back(Range(old_sysnum, old_err)); |
| 267 old_sysnum = sysnum; | 265 old_sysnum = sysnum; |
| 268 old_err = err; | 266 old_err = err; |
| 269 } | 267 } |
| 270 } | 268 } |
| 269 ranges->push_back(Range(old_sysnum, old_err)); |
| 271 } | 270 } |
| 272 | 271 |
| 273 Instruction* PolicyCompiler::AssembleJumpTable(Ranges::const_iterator start, | 272 Instruction* PolicyCompiler::AssembleJumpTable(Ranges::const_iterator start, |
| 274 Ranges::const_iterator stop) { | 273 Ranges::const_iterator stop) { |
| 275 // We convert the list of system call ranges into jump table that performs | 274 // We convert the list of system call ranges into jump table that performs |
| 276 // a binary search over the ranges. | 275 // a binary search over the ranges. |
| 277 // As a sanity check, we need to have at least one distinct ranges for us | 276 // As a sanity check, we need to have at least one distinct ranges for us |
| 278 // to be able to build a jump table. | 277 // to be able to build a jump table. |
| 279 if (stop - start <= 0) { | 278 if (stop - start <= 0) { |
| 280 SANDBOX_DIE("Invalid set of system call ranges"); | 279 SANDBOX_DIE("Invalid set of system call ranges"); |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 &*conds_.insert(passed).first, | 514 &*conds_.insert(passed).first, |
| 516 &*conds_.insert(failed).first); | 515 &*conds_.insert(failed).first); |
| 517 } | 516 } |
| 518 | 517 |
| 519 ErrorCode PolicyCompiler::Kill(const char* msg) { | 518 ErrorCode PolicyCompiler::Kill(const char* msg) { |
| 520 return Trap(BPFFailure, const_cast<char*>(msg)); | 519 return Trap(BPFFailure, const_cast<char*>(msg)); |
| 521 } | 520 } |
| 522 | 521 |
| 523 } // namespace bpf_dsl | 522 } // namespace bpf_dsl |
| 524 } // namespace sandbox | 523 } // namespace sandbox |
| OLD | NEW |