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/seccomp-bpf/syscall_iterator.h"
7 #include "base/basictypes.h"
8 #include "sandbox/linux/seccomp-bpf/linux_seccomp.h"
12 uint32_t SyscallIterator::Next() {
19 // |num_| has been initialized to 0, which we assume is also MIN_SYSCALL.
20 // This true for supported architectures (Intel and ARM EABI).
21 COMPILE_ASSERT(MIN_SYSCALL
== 0u, min_syscall_should_always_be_zero
);
24 // First we iterate up to MAX_PUBLIC_SYSCALL, which is equal to MAX_SYSCALL
25 // on Intel architectures, but leaves room for private syscalls on ARM.
26 if (num_
<= MAX_PUBLIC_SYSCALL
) {
27 if (invalid_only_
&& num_
< MAX_PUBLIC_SYSCALL
) {
28 num_
= MAX_PUBLIC_SYSCALL
;
33 // ARM EABI includes "ARM private" system calls starting at
34 // MIN_PRIVATE_SYSCALL, and a "ghost syscall private to the kernel" at
36 } else if (num_
< MIN_PRIVATE_SYSCALL
- 1) {
37 num_
= MIN_PRIVATE_SYSCALL
- 1;
38 } else if (num_
<= MAX_PRIVATE_SYSCALL
) {
39 if (invalid_only_
&& num_
< MAX_PRIVATE_SYSCALL
) {
40 num_
= MAX_PRIVATE_SYSCALL
;
44 } else if (num_
< MIN_GHOST_SYSCALL
- 1) {
45 num_
= MIN_GHOST_SYSCALL
- 1;
46 } else if (num_
<= MAX_SYSCALL
) {
47 if (invalid_only_
&& num_
< MAX_SYSCALL
) {
53 // BPF programs only ever operate on unsigned quantities. So, that's how
54 // we iterate; we return values from 0..0xFFFFFFFFu. But there are places,
55 // where the kernel might interpret system call numbers as signed
56 // quantities, so the boundaries between signed and unsigned values are
57 // potential problem cases. We want to explicitly return these values from
59 } else if (num_
< 0x7FFFFFFFu
) {
61 } else if (num_
< 0x80000000u
) {
63 } else if (num_
< 0xFFFFFFFFu
) {
66 } while (invalid_only_
&& IsValid(val
));
68 done_
|= val
== 0xFFFFFFFFu
;
72 bool SyscallIterator::IsValid(uint32_t num
) {
73 uint32_t min_syscall
= MIN_SYSCALL
;
74 if (num
>= min_syscall
&& num
<= MAX_PUBLIC_SYSCALL
) {
77 if (IsArmPrivate(num
)) {
83 #if defined(__arm__) && (defined(__thumb__) || defined(__ARM_EABI__))
84 bool SyscallIterator::IsArmPrivate(uint32_t num
) {
85 return (num
>= MIN_PRIVATE_SYSCALL
&& num
<= MAX_PRIVATE_SYSCALL
) ||
86 (num
>= MIN_GHOST_SYSCALL
&& num
<= MAX_SYSCALL
);
89 bool SyscallIterator::IsArmPrivate(uint32_t) { return false; }
92 } // namespace sandbox