Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(274)

Side by Side Diff: sandbox/linux/seccomp-bpf/syscall_iterator.cc

Issue 670183003: Update from chromium 62675d9fb31fb8cedc40f68e78e8445a74f362e7 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "sandbox/linux/seccomp-bpf/syscall_iterator.h"
6
7 #include "base/logging.h"
8 #include "base/macros.h"
9 #include "sandbox/linux/seccomp-bpf/linux_seccomp.h"
10
11 namespace sandbox {
12
13 namespace {
14
15 #if defined(__mips__) && (_MIPS_SIM == _MIPS_SIM_ABI32)
16 // This is true for Mips O32 ABI.
17 COMPILE_ASSERT(MIN_SYSCALL == __NR_Linux, min_syscall_should_be_4000);
18 #else
19 // This true for supported architectures (Intel and ARM EABI).
20 COMPILE_ASSERT(MIN_SYSCALL == 0u, min_syscall_should_always_be_zero);
21 #endif
22
23 // SyscallRange represents an inclusive range of system call numbers.
24 struct SyscallRange {
25 uint32_t first;
26 uint32_t last;
27 };
28
29 const SyscallRange kValidSyscallRanges[] = {
30 // First we iterate up to MAX_PUBLIC_SYSCALL, which is equal to MAX_SYSCALL
31 // on Intel architectures, but leaves room for private syscalls on ARM.
32 {MIN_SYSCALL, MAX_PUBLIC_SYSCALL},
33 #if defined(__arm__)
34 // ARM EABI includes "ARM private" system calls starting at
35 // MIN_PRIVATE_SYSCALL, and a "ghost syscall private to the kernel" at
36 // MIN_GHOST_SYSCALL.
37 {MIN_PRIVATE_SYSCALL, MAX_PRIVATE_SYSCALL},
38 {MIN_GHOST_SYSCALL, MAX_SYSCALL},
39 #endif
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.
44 uint32_t NextSyscall(uint32_t cur, bool invalid_only) {
45 for (const SyscallRange& range : kValidSyscallRanges) {
46 if (range.first > 0 && cur < range.first - 1) {
47 return range.first - 1;
48 }
49 if (cur <= range.last) {
50 if (invalid_only) {
51 return range.last + 1;
52 }
53 return cur + 1;
54 }
55 }
56
57 // BPF programs only ever operate on unsigned quantities. So, that's how
58 // we iterate; we return values from 0..0xFFFFFFFFu. But there are places,
59 // where the kernel might interpret system call numbers as signed
60 // quantities, so the boundaries between signed and unsigned values are
61 // potential problem cases. We want to explicitly return these values from
62 // our iterator.
63 if (cur < 0x7FFFFFFFu)
64 return 0x7FFFFFFFu;
65 if (cur < 0x80000000u)
66 return 0x80000000u;
67
68 if (cur < 0xFFFFFFFFu)
69 return 0xFFFFFFFFu;
70 return 0;
71 }
72
73 } // namespace
74
75 SyscallSet::Iterator SyscallSet::begin() const {
76 return Iterator(set_, false);
77 }
78
79 SyscallSet::Iterator SyscallSet::end() const {
80 return Iterator(set_, true);
81 }
82
83 bool SyscallSet::IsValid(uint32_t num) {
84 for (const SyscallRange& range : kValidSyscallRanges) {
85 if (num >= range.first && num <= range.last) {
86 return true;
87 }
88 }
89 return false;
90 }
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
130 } // namespace sandbox
OLDNEW
« no previous file with comments | « sandbox/linux/seccomp-bpf/syscall_iterator.h ('k') | sandbox/linux/seccomp-bpf/syscall_iterator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698