blob: 6d553c8fdd7b18d13f2d1d751e7c2f92303007c3 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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/sandbox_bpf.h"
6#include "sandbox/linux/seccomp-bpf/syscall_iterator.h"
7#include "sandbox/linux/tests/unit_tests.h"
8
Torne (Richard Coles)a3f6a492013-12-18 16:25:09 +00009namespace sandbox {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000010
11namespace {
12
13SANDBOX_TEST(SyscallIterator, Monotonous) {
14 for (int i = 0; i < 2; ++i) {
Torne (Richard Coles)f2477e02013-11-28 11:55:43 +000015 bool invalid_only = !i; // Testing both |invalid_only| cases.
Torne (Richard Coles)58218062012-11-14 11:43:16 +000016 SyscallIterator iter(invalid_only);
17 uint32_t next = iter.Next();
18
19 if (!invalid_only) {
20 // The iterator should start at 0.
21 SANDBOX_ASSERT(next == 0);
22 }
23 for (uint32_t last = next; !iter.Done(); last = next) {
24 next = iter.Next();
25 SANDBOX_ASSERT(last < next);
26 }
27 // The iterator should always return 0xFFFFFFFFu as the last value.
28 SANDBOX_ASSERT(next == 0xFFFFFFFFu);
29 }
30}
31
Torne (Richard Coles)5f1c9432014-08-12 13:47:38 +010032#if defined(__mips__)
33SANDBOX_TEST(SyscallIterator, PublicSyscallRangeMIPS) {
34 SyscallIterator iter(false);
35 uint32_t next = iter.Next();
36 SANDBOX_ASSERT(next == 0);
37
38 // Since on MIPS MIN_SYSCALL != 0 we need to move iterator to valid range.
39 next = iter.Next();
40 SANDBOX_ASSERT(next == MIN_SYSCALL - 1);
41
42 // The iterator should cover the public syscall range
43 // MIN_SYSCALL..MAX_PUBLIC_SYSCALL, without skipping syscalls.
44 for (uint32_t last = next; next < MAX_PUBLIC_SYSCALL + 1; last = next) {
45 SANDBOX_ASSERT((next = iter.Next()) == last + 1);
46 }
47 SANDBOX_ASSERT(next == MAX_PUBLIC_SYSCALL + 1);
48}
49#else
50SANDBOX_TEST(SyscallIterator, PublicSyscallRangeIntelArm) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000051 SyscallIterator iter(false);
52 uint32_t next = iter.Next();
53
54 // The iterator should cover the public syscall range
55 // MIN_SYSCALL..MAX_PUBLIC_SYSCALL, without skipping syscalls.
56 // We're assuming MIN_SYSCALL == 0 for all architectures,
57 // this is currently valid for Intel and ARM EABI.
58 SANDBOX_ASSERT(MIN_SYSCALL == 0);
59 SANDBOX_ASSERT(next == MIN_SYSCALL);
60 for (uint32_t last = next; next < MAX_PUBLIC_SYSCALL + 1; last = next) {
61 SANDBOX_ASSERT((next = iter.Next()) == last + 1);
62 }
63 SANDBOX_ASSERT(next == MAX_PUBLIC_SYSCALL + 1);
64}
Torne (Richard Coles)5f1c9432014-08-12 13:47:38 +010065#endif // defined(__mips__)
Torne (Richard Coles)58218062012-11-14 11:43:16 +000066
67#if defined(__arm__)
68SANDBOX_TEST(SyscallIterator, ARMPrivateSyscallRange) {
69 SyscallIterator iter(false);
70 uint32_t next = iter.Next();
71 while (next < MIN_PRIVATE_SYSCALL - 1) {
72 next = iter.Next();
73 }
74 // The iterator should cover the ARM private syscall range
75 // without skipping syscalls.
76 SANDBOX_ASSERT(next == MIN_PRIVATE_SYSCALL - 1);
77 for (uint32_t last = next; next < MAX_PRIVATE_SYSCALL + 1; last = next) {
78 SANDBOX_ASSERT((next = iter.Next()) == last + 1);
79 }
80 SANDBOX_ASSERT(next == MAX_PRIVATE_SYSCALL + 1);
81}
82
83SANDBOX_TEST(SyscallIterator, ARMHiddenSyscallRange) {
84 SyscallIterator iter(false);
85 uint32_t next = iter.Next();
86 while (next < MIN_GHOST_SYSCALL - 1) {
87 next = iter.Next();
88 }
89 // The iterator should cover the ARM hidden syscall range
90 // without skipping syscalls.
91 SANDBOX_ASSERT(next == MIN_GHOST_SYSCALL - 1);
92 for (uint32_t last = next; next < MAX_SYSCALL + 1; last = next) {
93 SANDBOX_ASSERT((next = iter.Next()) == last + 1);
94 }
95 SANDBOX_ASSERT(next == MAX_SYSCALL + 1);
96}
97#endif
98
99SANDBOX_TEST(SyscallIterator, Invalid) {
100 for (int i = 0; i < 2; ++i) {
Torne (Richard Coles)f2477e02013-11-28 11:55:43 +0000101 bool invalid_only = !i; // Testing both |invalid_only| cases.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000102 SyscallIterator iter(invalid_only);
103 uint32_t next = iter.Next();
104
105 while (next < MAX_SYSCALL + 1) {
106 next = iter.Next();
107 }
108
109 SANDBOX_ASSERT(next == MAX_SYSCALL + 1);
110 while (next < 0x7FFFFFFFu) {
111 next = iter.Next();
112 }
113
114 // The iterator should return the signed/unsigned corner cases.
115 SANDBOX_ASSERT(next == 0x7FFFFFFFu);
116 next = iter.Next();
117 SANDBOX_ASSERT(next == 0x80000000u);
118 SANDBOX_ASSERT(!iter.Done());
119 next = iter.Next();
120 SANDBOX_ASSERT(iter.Done());
121 SANDBOX_ASSERT(next == 0xFFFFFFFFu);
122 }
123}
124
Torne (Richard Coles)5f1c9432014-08-12 13:47:38 +0100125#if defined(__mips__)
126SANDBOX_TEST(SyscallIterator, InvalidOnlyMIPS) {
127 bool invalid_only = true;
128 SyscallIterator iter(invalid_only);
129 uint32_t next = iter.Next();
130 SANDBOX_ASSERT(next == 0);
131 // For Mips O32 ABI we're assuming MIN_SYSCALL == 4000.
132 SANDBOX_ASSERT(MIN_SYSCALL == 4000);
133
134 // Since on MIPS MIN_SYSCALL != 0, we need to move iterator to valid range
135 // The iterator should skip until the last invalid syscall in this range.
136 next = iter.Next();
137 SANDBOX_ASSERT(next == MIN_SYSCALL - 1);
138 next = iter.Next();
139 // First next invalid syscall should then be |MAX_PUBLIC_SYSCALL + 1|.
140 SANDBOX_ASSERT(next == MAX_PUBLIC_SYSCALL + 1);
141}
142
143#else
144
145SANDBOX_TEST(SyscallIterator, InvalidOnlyIntelArm) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000146 bool invalid_only = true;
147 SyscallIterator iter(invalid_only);
148 uint32_t next = iter.Next();
149 // We're assuming MIN_SYSCALL == 0 for all architectures,
150 // this is currently valid for Intel and ARM EABI.
151 // First invalid syscall should then be |MAX_PUBLIC_SYSCALL + 1|.
152 SANDBOX_ASSERT(MIN_SYSCALL == 0);
153 SANDBOX_ASSERT(next == MAX_PUBLIC_SYSCALL + 1);
154
155#if defined(__arm__)
156 next = iter.Next();
157 // The iterator should skip until the last invalid syscall in this range.
158 SANDBOX_ASSERT(next == MIN_PRIVATE_SYSCALL - 1);
159 while (next <= MAX_PRIVATE_SYSCALL) {
160 next = iter.Next();
161 }
162
163 next = iter.Next();
164 // The iterator should skip until the last invalid syscall in this range.
165 SANDBOX_ASSERT(next == MIN_GHOST_SYSCALL - 1);
166 while (next <= MAX_SYSCALL) {
167 next = iter.Next();
168 }
169 SANDBOX_ASSERT(next == MAX_SYSCALL + 1);
Torne (Richard Coles)5f1c9432014-08-12 13:47:38 +0100170#endif // defined(__arm__)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000171}
Torne (Richard Coles)5f1c9432014-08-12 13:47:38 +0100172#endif // defined(__mips__)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000173
174} // namespace
Torne (Richard Coles)a3f6a492013-12-18 16:25:09 +0000175
176} // namespace sandbox