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

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

Issue 299743002: Add domain-specific language for BPF policies (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Respond to jln feedback Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 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-helpers/bpf_dsl.h"
6
7 #include <errno.h>
8 #include <netinet/in.h>
9 #include <sys/socket.h>
10 #include <sys/utsname.h>
11
12 #include "base/macros.h"
13 #include "sandbox/linux/seccomp-bpf/bpf_tests.h"
14 #include "sandbox/linux/seccomp-bpf/errorcode.h"
15 #include "sandbox/linux/seccomp-bpf/sandbox_bpf_policy.h"
16
17 using namespace sandbox::bpf_dsl;
18
19 namespace sandbox {
20
21 namespace {
22
23 class BasicPolicy : public SandboxBPFDSLPolicy {
24 public:
25 BasicPolicy() {}
26 virtual ResultExpr EvaluateSyscall(int sysno) const OVERRIDE {
27 if (sysno == __NR_getpgid) {
28 const Arg<pid_t> pid(0);
29 return If(pid == 0, Error(EPERM)).Else(Error(EINVAL));
30 }
31 return Allow();
32 }
33
34 private:
35 DISALLOW_COPY_AND_ASSIGN(BasicPolicy);
36 };
37
38 BPF_TEST_C(BPFDSL, Basic, BasicPolicy) {
jln (very slow on Chromium) 2014/06/25 00:14:53 Let's set errno to 0 first.
mdempsky 2014/06/25 23:50:22 Done. I added a helper macro that clears errno an
39 BPF_ASSERT_EQ(-1, getpgid(0));
40 BPF_ASSERT_EQ(EPERM, errno);
41
42 BPF_ASSERT_EQ(-1, getpgid(1));
43 BPF_ASSERT_EQ(EINVAL, errno);
44 }
45
46 class BooleanLogicPolicy : public SandboxBPFDSLPolicy {
47 public:
48 BooleanLogicPolicy() {}
49 virtual ResultExpr EvaluateSyscall(int sysno) const OVERRIDE {
50 if (sysno == __NR_socketpair) {
51 const Arg<int> domain(0), type(1), protocol(2);
52 return If(domain == AF_UNIX &&
53 (type == SOCK_STREAM || type == SOCK_DGRAM) &&
54 protocol == 0,
55 Error(EPERM)).Else(Error(EINVAL));
56 }
57 return Allow();
58 }
59
60 private:
61 DISALLOW_COPY_AND_ASSIGN(BooleanLogicPolicy);
62 };
63
64 void AssertSocketPairError(int expected_errno,
65 int domain,
66 int type,
67 int protocol) {
68 int sv[2];
69 BPF_ASSERT_EQ(-1, socketpair(domain, type, protocol, sv));
70 BPF_ASSERT_EQ(expected_errno, errno);
71 }
72
73 BPF_TEST_C(BPFDSL, BooleanLogic, BooleanLogicPolicy) {
jln (very slow on Chromium) 2014/06/25 00:14:54 errno = 0;
mdempsky 2014/06/25 23:50:22 Done.
74 // Acceptable combinations that should return EPERM.
75 AssertSocketPairError(EPERM, AF_UNIX, SOCK_STREAM, 0);
76 AssertSocketPairError(EPERM, AF_UNIX, SOCK_DGRAM, 0);
77
78 // Combinations that are invalid for only one reason; should return EINVAL.
79 AssertSocketPairError(EINVAL, AF_INET, SOCK_STREAM, 0);
80 AssertSocketPairError(EINVAL, AF_UNIX, SOCK_SEQPACKET, 0);
81 AssertSocketPairError(EINVAL, AF_UNIX, SOCK_STREAM, IPPROTO_TCP);
82
83 // Completely unacceptable combination; should also return EINVAL.
84 AssertSocketPairError(EINVAL, AF_INET, SOCK_SEQPACKET, IPPROTO_UDP);
85 }
86
87 static const uintptr_t kDeadBeefAddr =
88 static_cast<uintptr_t>(0xdeadbeefdeadbeefULL);
89
90 class ArgSizePolicy : public SandboxBPFDSLPolicy {
91 public:
92 ArgSizePolicy() {}
93 virtual ResultExpr EvaluateSyscall(int sysno) const OVERRIDE {
94 if (sysno == __NR_uname) {
95 const Arg<uintptr_t> addr(0);
96 return If(addr == kDeadBeefAddr, Error(EPERM)).Else(Allow());
97 }
98 return Allow();
99 }
100
101 private:
102 DISALLOW_COPY_AND_ASSIGN(ArgSizePolicy);
103 };
104
105 BPF_TEST_C(BPFDSL, ArgSizeTest, ArgSizePolicy) {
106 struct utsname buf;
107 BPF_ASSERT_EQ(0, uname(&buf));
108
109 BPF_ASSERT_EQ(-1, uname(reinterpret_cast<struct utsname*>(kDeadBeefAddr)));
110 BPF_ASSERT_EQ(EPERM, errno);
111 }
112
jln (very slow on Chromium) 2014/06/25 00:14:53 Let's add a Size test for a 32 bits argument like
113 class TrappingPolicy : public SandboxBPFDSLPolicy {
114 public:
115 TrappingPolicy() {}
116 virtual ResultExpr EvaluateSyscall(int sysno) const OVERRIDE {
117 if (sysno == __NR_uname) {
118 return Trap(UnameTrap, &count_);
119 }
120 return Allow();
121 }
122
123 private:
124 static intptr_t count_;
125
126 static intptr_t UnameTrap(const struct arch_seccomp_data& data, void* aux) {
127 BPF_ASSERT_EQ(&count_, aux);
128 return ++count_;
129 }
130
131 DISALLOW_COPY_AND_ASSIGN(TrappingPolicy);
132 };
133
134 intptr_t TrappingPolicy::count_;
135
136 BPF_TEST_C(BPFDSL, TrapTest, TrappingPolicy) {
137 BPF_ASSERT_EQ(1, uname(NULL));
138 BPF_ASSERT_EQ(2, uname(NULL));
139 BPF_ASSERT_EQ(3, uname(NULL));
140 }
141
142 class MaskingPolicy : public SandboxBPFDSLPolicy {
143 public:
144 MaskingPolicy() {}
145 virtual ResultExpr EvaluateSyscall(int sysno) const OVERRIDE {
146 if (sysno == __NR_setuid) {
147 const Arg<uid_t> uid(0);
148 return If((uid & 0xf) == 0, Error(EINVAL)).Else(Error(EACCES));
149 }
150 if (sysno == __NR_setgid) {
151 const Arg<gid_t> gid(0);
152 return If((gid & 0xf0) == 0xf0, Error(EINVAL)).Else(Error(EACCES));
153 }
154 return Allow();
155 }
156
157 private:
158 DISALLOW_COPY_AND_ASSIGN(MaskingPolicy);
159 };
160
161 BPF_TEST_C(BPFDSL, MaskTest, MaskingPolicy) {
162 for (uid_t uid = 0; uid < 0x100; ++uid) {
163 BPF_ASSERT_EQ(-1, setuid(uid));
164 BPF_ASSERT_EQ((uid & 0xf) == 0 ? EINVAL : EACCES, errno);
165 }
166
167 for (gid_t gid = 0; gid < 0x100; ++gid) {
168 BPF_ASSERT_EQ(-1, setgid(gid));
169 BPF_ASSERT_EQ((gid & 0xf0) == 0xf0 ? EINVAL : EACCES, errno);
170 }
171 }
172
173 class ElseIfPolicy : public SandboxBPFDSLPolicy {
174 public:
175 ElseIfPolicy() {}
176 virtual ResultExpr EvaluateSyscall(int sysno) const OVERRIDE {
177 if (sysno == __NR_setuid) {
178 const Arg<uid_t> uid(0);
179 return If(uid == 0, Error(0))
180 .ElseIf((uid & 0xf0f0) == 0, Error(EINVAL))
181 .ElseIf((uid & 0x00ff) == 0, Error(EEXIST))
182 .Else(Error(EACCES));
183 }
184 return Allow();
185 }
186
187 private:
188 DISALLOW_COPY_AND_ASSIGN(ElseIfPolicy);
189 };
190
191 BPF_TEST_C(BPFDSL, ElseIfTest, ElseIfPolicy) {
192 BPF_ASSERT_EQ(0, setuid(0));
193
194 BPF_ASSERT_EQ(-1, setuid(0x0001));
195 BPF_ASSERT_EQ(EINVAL, errno);
196 BPF_ASSERT_EQ(-1, setuid(0x0100));
197 BPF_ASSERT_EQ(EINVAL, errno);
198
jln (very slow on Chromium) 2014/06/25 00:14:53 Currently this doesn't test the order if the two "
mdempsky 2014/06/25 23:50:22 I believe the 0x100 test case covered this, but th
199 BPF_ASSERT_EQ(-1, setuid(0x1000));
200 BPF_ASSERT_EQ(EEXIST, errno);
201 BPF_ASSERT_EQ(-1, setuid(0x2000));
202 BPF_ASSERT_EQ(EEXIST, errno);
203
204 BPF_ASSERT_EQ(-1, setuid(0x1234));
205 BPF_ASSERT_EQ(EACCES, errno);
206 }
207
208 } // namespace
209
210 } // namespace sandbox
OLDNEW
« sandbox/linux/seccomp-bpf-helpers/bpf_dsl.cc ('K') | « sandbox/linux/seccomp-bpf-helpers/bpf_dsl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698