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/seccomp-bpf/syscall_iterator.h" | 5 #include "sandbox/linux/seccomp-bpf/syscall_iterator.h" |
6 | 6 |
| 7 #include "base/logging.h" |
7 #include "base/macros.h" | 8 #include "base/macros.h" |
8 #include "sandbox/linux/seccomp-bpf/linux_seccomp.h" | 9 #include "sandbox/linux/seccomp-bpf/linux_seccomp.h" |
9 | 10 |
10 namespace sandbox { | 11 namespace sandbox { |
11 | 12 |
12 namespace { | 13 namespace { |
13 | 14 |
14 #if defined(__mips__) && (_MIPS_SIM == _MIPS_SIM_ABI32) | 15 #if defined(__mips__) && (_MIPS_SIM == _MIPS_SIM_ABI32) |
15 // This is true for Mips O32 ABI. | 16 // This is true for Mips O32 ABI. |
16 COMPILE_ASSERT(MIN_SYSCALL == __NR_Linux, min_syscall_should_be_4000); | 17 COMPILE_ASSERT(MIN_SYSCALL == __NR_Linux, min_syscall_should_be_4000); |
(...skipping 14 matching lines...) Expand all Loading... |
31 {MIN_SYSCALL, MAX_PUBLIC_SYSCALL}, | 32 {MIN_SYSCALL, MAX_PUBLIC_SYSCALL}, |
32 #if defined(__arm__) | 33 #if defined(__arm__) |
33 // ARM EABI includes "ARM private" system calls starting at | 34 // ARM EABI includes "ARM private" system calls starting at |
34 // MIN_PRIVATE_SYSCALL, and a "ghost syscall private to the kernel" at | 35 // MIN_PRIVATE_SYSCALL, and a "ghost syscall private to the kernel" at |
35 // MIN_GHOST_SYSCALL. | 36 // MIN_GHOST_SYSCALL. |
36 {MIN_PRIVATE_SYSCALL, MAX_PRIVATE_SYSCALL}, | 37 {MIN_PRIVATE_SYSCALL, MAX_PRIVATE_SYSCALL}, |
37 {MIN_GHOST_SYSCALL, MAX_SYSCALL}, | 38 {MIN_GHOST_SYSCALL, MAX_SYSCALL}, |
38 #endif | 39 #endif |
39 }; | 40 }; |
40 | 41 |
| 42 // NextSyscall returns the next system call in the specified system |
| 43 // call set after |cur|, or 0 if no such system call exists. |
41 uint32_t NextSyscall(uint32_t cur, bool invalid_only) { | 44 uint32_t NextSyscall(uint32_t cur, bool invalid_only) { |
42 for (const SyscallRange& range : kValidSyscallRanges) { | 45 for (const SyscallRange& range : kValidSyscallRanges) { |
43 if (range.first > 0 && cur < range.first - 1) { | 46 if (range.first > 0 && cur < range.first - 1) { |
44 return range.first - 1; | 47 return range.first - 1; |
45 } | 48 } |
46 if (cur <= range.last) { | 49 if (cur <= range.last) { |
47 if (invalid_only && cur < range.last) { | 50 if (invalid_only) { |
48 return range.last; | 51 return range.last + 1; |
49 } | 52 } |
50 return cur + 1; | 53 return cur + 1; |
51 } | 54 } |
52 } | 55 } |
53 | 56 |
54 // BPF programs only ever operate on unsigned quantities. So, that's how | 57 // BPF programs only ever operate on unsigned quantities. So, that's how |
55 // we iterate; we return values from 0..0xFFFFFFFFu. But there are places, | 58 // we iterate; we return values from 0..0xFFFFFFFFu. But there are places, |
56 // where the kernel might interpret system call numbers as signed | 59 // where the kernel might interpret system call numbers as signed |
57 // quantities, so the boundaries between signed and unsigned values are | 60 // quantities, so the boundaries between signed and unsigned values are |
58 // potential problem cases. We want to explicitly return these values from | 61 // potential problem cases. We want to explicitly return these values from |
59 // our iterator. | 62 // our iterator. |
60 if (cur < 0x7FFFFFFFu) | 63 if (cur < 0x7FFFFFFFu) |
61 return 0x7FFFFFFFu; | 64 return 0x7FFFFFFFu; |
62 if (cur < 0x80000000u) | 65 if (cur < 0x80000000u) |
63 return 0x80000000u; | 66 return 0x80000000u; |
64 | 67 |
65 return 0xFFFFFFFFu; | 68 if (cur < 0xFFFFFFFFu) |
| 69 return 0xFFFFFFFFu; |
| 70 return 0; |
66 } | 71 } |
67 | 72 |
68 } // namespace | 73 } // namespace |
69 | 74 |
70 uint32_t SyscallIterator::Next() { | 75 SyscallSet::Iterator SyscallSet::begin() const { |
71 if (done_) { | 76 return Iterator(set_, false); |
72 return num_; | |
73 } | |
74 | |
75 uint32_t val; | |
76 do { | |
77 val = num_; | |
78 num_ = NextSyscall(num_, invalid_only_); | |
79 } while (invalid_only_ && IsValid(val)); | |
80 | |
81 done_ |= val == 0xFFFFFFFFu; | |
82 return val; | |
83 } | 77 } |
84 | 78 |
85 bool SyscallIterator::IsValid(uint32_t num) { | 79 SyscallSet::Iterator SyscallSet::end() const { |
| 80 return Iterator(set_, true); |
| 81 } |
| 82 |
| 83 bool SyscallSet::IsValid(uint32_t num) { |
86 for (const SyscallRange& range : kValidSyscallRanges) { | 84 for (const SyscallRange& range : kValidSyscallRanges) { |
87 if (num >= range.first && num <= range.last) { | 85 if (num >= range.first && num <= range.last) { |
88 return true; | 86 return true; |
89 } | 87 } |
90 } | 88 } |
91 return false; | 89 return false; |
92 } | 90 } |
93 | 91 |
| 92 bool operator==(const SyscallSet& lhs, const SyscallSet& rhs) { |
| 93 return (lhs.set_ == rhs.set_); |
| 94 } |
| 95 |
| 96 SyscallSet::Iterator::Iterator(Set set, bool done) |
| 97 : set_(set), done_(done), num_(0) { |
| 98 if (set_ == Set::INVALID_ONLY && !done_ && IsValid(num_)) { |
| 99 ++*this; |
| 100 } |
| 101 } |
| 102 |
| 103 uint32_t SyscallSet::Iterator::operator*() const { |
| 104 DCHECK(!done_); |
| 105 return num_; |
| 106 } |
| 107 |
| 108 SyscallSet::Iterator& SyscallSet::Iterator::operator++() { |
| 109 DCHECK(!done_); |
| 110 |
| 111 num_ = NextSyscall(num_, set_ == Set::INVALID_ONLY); |
| 112 if (num_ == 0) { |
| 113 done_ = true; |
| 114 } |
| 115 |
| 116 return *this; |
| 117 } |
| 118 |
| 119 bool operator==(const SyscallSet::Iterator& lhs, |
| 120 const SyscallSet::Iterator& rhs) { |
| 121 DCHECK(lhs.set_ == rhs.set_); |
| 122 return (lhs.done_ == rhs.done_) && (lhs.num_ == rhs.num_); |
| 123 } |
| 124 |
| 125 bool operator!=(const SyscallSet::Iterator& lhs, |
| 126 const SyscallSet::Iterator& rhs) { |
| 127 return !(lhs == rhs); |
| 128 } |
| 129 |
94 } // namespace sandbox | 130 } // namespace sandbox |
OLD | NEW |