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 #ifndef SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_ITERATOR_H__ | 5 #ifndef SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_ITERATOR_H__ |
6 #define SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_ITERATOR_H__ | 6 #define SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_ITERATOR_H__ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "sandbox/sandbox_export.h" | 11 #include "sandbox/sandbox_export.h" |
12 | 12 |
13 namespace sandbox { | 13 namespace sandbox { |
14 | 14 |
| 15 // TODO(mdempsky): Rename this header to syscall_set.h. |
| 16 |
15 // Iterates over the entire system call range from 0..0xFFFFFFFFu. This | 17 // Iterates over the entire system call range from 0..0xFFFFFFFFu. This |
16 // iterator is aware of how system calls look like and will skip quickly | 18 // iterator is aware of how system calls look like and will skip quickly |
17 // over ranges that can't contain system calls. It iterates more slowly | 19 // over ranges that can't contain system calls. It iterates more slowly |
18 // whenever it reaches a range that is potentially problematic, returning | 20 // whenever it reaches a range that is potentially problematic, returning |
19 // the last invalid value before a valid range of system calls, and the | 21 // the last invalid value before a valid range of system calls, and the |
20 // first invalid value after a valid range of syscalls. It iterates over | 22 // first invalid value after a valid range of syscalls. It iterates over |
21 // individual values whenever it is in the normal range for system calls | 23 // individual values whenever it is in the normal range for system calls |
22 // (typically MIN_SYSCALL..MAX_SYSCALL). | 24 // (typically MIN_SYSCALL..MAX_SYSCALL). |
23 // If |invalid_only| is true, this iterator will only return invalid | |
24 // syscall numbers, but will still skip quickly over invalid ranges, | |
25 // returning the first invalid value in the range and then skipping | |
26 // to the last invalid value in the range. | |
27 // | 25 // |
28 // Example usage: | 26 // Example usage: |
29 // for (SyscallIterator iter(false); !iter.Done(); ) { | 27 // for (uint32_t sysnum : SyscallSet::All()) { |
30 // uint32_t sysnum = iter.Next(); | |
31 // // Do something with sysnum. | 28 // // Do something with sysnum. |
32 // } | 29 // } |
33 // | 30 class SANDBOX_EXPORT SyscallSet { |
34 // TODO(markus): Make this a classic C++ iterator. | |
35 class SANDBOX_EXPORT SyscallIterator { | |
36 public: | 31 public: |
37 explicit SyscallIterator(bool invalid_only) | 32 class Iterator; |
38 : invalid_only_(invalid_only), done_(false), num_(0) {} | |
39 | 33 |
40 bool Done() const { return done_; } | 34 SyscallSet(const SyscallSet& ss) : set_(ss.set_) {} |
41 uint32_t Next(); | 35 ~SyscallSet() {} |
| 36 |
| 37 Iterator begin() const; |
| 38 Iterator end() const; |
| 39 |
| 40 // All returns a SyscallSet that contains both valid and invalid |
| 41 // system call numbers. |
| 42 static SyscallSet All() { return SyscallSet(Set::ALL); } |
| 43 |
| 44 // InvalidOnly returns a SyscallSet that contains only invalid |
| 45 // system call numbers, but still omits numbers in the middle of a |
| 46 // range of invalid system call numbers. |
| 47 static SyscallSet InvalidOnly() { return SyscallSet(Set::INVALID_ONLY); } |
| 48 |
| 49 // IsValid returns whether |num| specifies a valid system call |
| 50 // number. |
42 static bool IsValid(uint32_t num); | 51 static bool IsValid(uint32_t num); |
43 | 52 |
44 private: | 53 private: |
45 bool invalid_only_; | 54 enum class Set { ALL, INVALID_ONLY }; |
| 55 |
| 56 explicit SyscallSet(Set set) : set_(set) {} |
| 57 |
| 58 Set set_; |
| 59 |
| 60 friend bool operator==(const SyscallSet&, const SyscallSet&); |
| 61 DISALLOW_ASSIGN(SyscallSet); |
| 62 }; |
| 63 |
| 64 SANDBOX_EXPORT bool operator==(const SyscallSet& lhs, const SyscallSet& rhs); |
| 65 |
| 66 // Iterator provides C++ input iterator semantics for traversing a |
| 67 // SyscallSet. |
| 68 class SyscallSet::Iterator { |
| 69 public: |
| 70 Iterator(const Iterator& it) |
| 71 : set_(it.set_), done_(it.done_), num_(it.num_) {} |
| 72 ~Iterator() {} |
| 73 |
| 74 uint32_t operator*() const; |
| 75 Iterator& operator++(); |
| 76 |
| 77 private: |
| 78 Iterator(Set set, bool done); |
| 79 |
| 80 Set set_; |
46 bool done_; | 81 bool done_; |
47 uint32_t num_; | 82 uint32_t num_; |
48 | 83 |
49 DISALLOW_IMPLICIT_CONSTRUCTORS(SyscallIterator); | 84 friend SyscallSet; |
| 85 friend bool operator==(const Iterator&, const Iterator&); |
| 86 DISALLOW_ASSIGN(Iterator); |
50 }; | 87 }; |
51 | 88 |
| 89 SANDBOX_EXPORT bool operator==(const SyscallSet::Iterator& lhs, |
| 90 const SyscallSet::Iterator& rhs); |
| 91 SANDBOX_EXPORT bool operator!=(const SyscallSet::Iterator& lhs, |
| 92 const SyscallSet::Iterator& rhs); |
| 93 |
52 } // namespace sandbox | 94 } // namespace sandbox |
53 | 95 |
54 #endif // SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_ITERATOR_H__ | 96 #endif // SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_ITERATOR_H__ |
OLD | NEW |