OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "sandbox/mac/bootstrap_sandbox.h" | 5 #include "sandbox/mac/bootstrap_sandbox.h" |
6 | 6 |
7 #include <CoreFoundation/CoreFoundation.h> | 7 #include <CoreFoundation/CoreFoundation.h> |
8 #import <Foundation/Foundation.h> | 8 #import <Foundation/Foundation.h> |
9 #include <mach/mach.h> | 9 #include <mach/mach.h> |
10 #include <servers/bootstrap.h> | 10 #include <servers/bootstrap.h> |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 virtual void SetUp() OVERRIDE { | 81 virtual void SetUp() OVERRIDE { |
82 base::MultiProcessTest::SetUp(); | 82 base::MultiProcessTest::SetUp(); |
83 | 83 |
84 sandbox_ = BootstrapSandbox::Create(); | 84 sandbox_ = BootstrapSandbox::Create(); |
85 ASSERT_TRUE(sandbox_.get()); | 85 ASSERT_TRUE(sandbox_.get()); |
86 } | 86 } |
87 | 87 |
88 BootstrapSandboxPolicy BaselinePolicy() { | 88 BootstrapSandboxPolicy BaselinePolicy() { |
89 BootstrapSandboxPolicy policy; | 89 BootstrapSandboxPolicy policy; |
90 if (base::mac::IsOSSnowLeopard()) | 90 if (base::mac::IsOSSnowLeopard()) |
91 policy["com.apple.SecurityServer"] = Rule(POLICY_ALLOW); | 91 policy.rules["com.apple.SecurityServer"] = Rule(POLICY_ALLOW); |
92 return policy; | 92 return policy; |
93 } | 93 } |
94 | 94 |
95 void RunChildWithPolicy(int policy_id, | 95 void RunChildWithPolicy(int policy_id, |
96 const char* child_name, | 96 const char* child_name, |
97 base::ProcessHandle* out_pid) { | 97 base::ProcessHandle* out_pid) { |
98 sandbox_->PrepareToForkWithPolicy(policy_id); | 98 sandbox_->PrepareToForkWithPolicy(policy_id); |
99 base::ProcessHandle pid = SpawnChild(child_name); | 99 base::ProcessHandle pid = SpawnChild(child_name); |
100 ASSERT_GT(pid, 0); | 100 ASSERT_GT(pid, 0); |
101 sandbox_->FinishedFork(pid); | 101 sandbox_->FinishedFork(pid); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 EXPECT_EQ(nil, [observer object]); | 142 EXPECT_EQ(nil, [observer object]); |
143 } | 143 } |
144 | 144 |
145 // Run the test with notifications permitted. | 145 // Run the test with notifications permitted. |
146 TEST_F(BootstrapSandboxTest, DistributedNotifications_SandboxAllow) { | 146 TEST_F(BootstrapSandboxTest, DistributedNotifications_SandboxAllow) { |
147 base::scoped_nsobject<DistributedNotificationObserver> observer( | 147 base::scoped_nsobject<DistributedNotificationObserver> observer( |
148 [[DistributedNotificationObserver alloc] init]); | 148 [[DistributedNotificationObserver alloc] init]); |
149 | 149 |
150 BootstrapSandboxPolicy policy(BaselinePolicy()); | 150 BootstrapSandboxPolicy policy(BaselinePolicy()); |
151 // 10.9: | 151 // 10.9: |
152 policy["com.apple.distributed_notifications@Uv3"] = Rule(POLICY_ALLOW); | 152 policy.rules["com.apple.distributed_notifications@Uv3"] = Rule(POLICY_ALLOW); |
153 policy["com.apple.distributed_notifications@1v3"] = Rule(POLICY_ALLOW); | 153 policy.rules["com.apple.distributed_notifications@1v3"] = Rule(POLICY_ALLOW); |
154 // 10.6: | 154 // 10.6: |
155 policy["com.apple.system.notification_center"] = Rule(POLICY_ALLOW); | 155 policy.rules["com.apple.system.notification_center"] = Rule(POLICY_ALLOW); |
156 policy["com.apple.distributed_notifications.2"] = Rule(POLICY_ALLOW); | 156 policy.rules["com.apple.distributed_notifications.2"] = Rule(POLICY_ALLOW); |
157 sandbox_->RegisterSandboxPolicy(2, policy); | 157 sandbox_->RegisterSandboxPolicy(2, policy); |
158 | 158 |
159 base::ProcessHandle pid; | 159 base::ProcessHandle pid; |
160 RunChildWithPolicy(2, kNotificationTestMain, &pid); | 160 RunChildWithPolicy(2, kNotificationTestMain, &pid); |
161 | 161 |
162 [observer waitForNotification]; | 162 [observer waitForNotification]; |
163 EXPECT_EQ(1, [observer receivedCount]); | 163 EXPECT_EQ(1, [observer receivedCount]); |
164 EXPECT_EQ(pid, [[observer object] intValue]); | 164 EXPECT_EQ(pid, [[observer object] intValue]); |
165 } | 165 } |
166 | 166 |
167 MULTIPROCESS_TEST_MAIN(PostNotification) { | 167 MULTIPROCESS_TEST_MAIN(PostNotification) { |
168 [[NSDistributedNotificationCenter defaultCenter] | 168 [[NSDistributedNotificationCenter defaultCenter] |
169 postNotificationName:kTestNotification | 169 postNotificationName:kTestNotification |
170 object:[NSString stringWithFormat:@"%d", getpid()]]; | 170 object:[NSString stringWithFormat:@"%d", getpid()]]; |
171 return 0; | 171 return 0; |
172 } | 172 } |
173 | 173 |
174 const char kTestServer[] = "org.chromium.test_bootstrap_server"; | 174 const char kTestServer[] = "org.chromium.test_bootstrap_server"; |
175 | 175 |
176 TEST_F(BootstrapSandboxTest, PolicyDenyError) { | 176 TEST_F(BootstrapSandboxTest, PolicyDenyError) { |
177 BootstrapSandboxPolicy policy(BaselinePolicy()); | 177 BootstrapSandboxPolicy policy(BaselinePolicy()); |
178 policy[kTestServer] = Rule(POLICY_DENY_ERROR); | 178 policy.rules[kTestServer] = Rule(POLICY_DENY_ERROR); |
179 sandbox_->RegisterSandboxPolicy(1, policy); | 179 sandbox_->RegisterSandboxPolicy(1, policy); |
180 | 180 |
181 RunChildWithPolicy(1, "PolicyDenyError", NULL); | 181 RunChildWithPolicy(1, "PolicyDenyError", NULL); |
182 } | 182 } |
183 | 183 |
184 MULTIPROCESS_TEST_MAIN(PolicyDenyError) { | 184 MULTIPROCESS_TEST_MAIN(PolicyDenyError) { |
185 mach_port_t port = MACH_PORT_NULL; | 185 mach_port_t port = MACH_PORT_NULL; |
186 kern_return_t kr = bootstrap_look_up(bootstrap_port, kTestServer, | 186 kern_return_t kr = bootstrap_look_up(bootstrap_port, kTestServer, |
187 &port); | 187 &port); |
188 CHECK_EQ(BOOTSTRAP_UNKNOWN_SERVICE, kr); | 188 CHECK_EQ(BOOTSTRAP_UNKNOWN_SERVICE, kr); |
189 CHECK(port == MACH_PORT_NULL); | 189 CHECK(port == MACH_PORT_NULL); |
190 | 190 |
191 kr = bootstrap_look_up(bootstrap_port, "org.chromium.some_other_server", | 191 kr = bootstrap_look_up(bootstrap_port, "org.chromium.some_other_server", |
192 &port); | 192 &port); |
193 CHECK_EQ(BOOTSTRAP_UNKNOWN_SERVICE, kr); | 193 CHECK_EQ(BOOTSTRAP_UNKNOWN_SERVICE, kr); |
194 CHECK(port == MACH_PORT_NULL); | 194 CHECK(port == MACH_PORT_NULL); |
195 | 195 |
196 return 0; | 196 return 0; |
197 } | 197 } |
198 | 198 |
199 TEST_F(BootstrapSandboxTest, PolicyDenyDummyPort) { | 199 TEST_F(BootstrapSandboxTest, PolicyDenyDummyPort) { |
200 BootstrapSandboxPolicy policy(BaselinePolicy()); | 200 BootstrapSandboxPolicy policy(BaselinePolicy()); |
201 policy[kTestServer] = Rule(POLICY_DENY_DUMMY_PORT); | 201 policy.rules[kTestServer] = Rule(POLICY_DENY_DUMMY_PORT); |
202 sandbox_->RegisterSandboxPolicy(1, policy); | 202 sandbox_->RegisterSandboxPolicy(1, policy); |
203 | 203 |
204 RunChildWithPolicy(1, "PolicyDenyDummyPort", NULL); | 204 RunChildWithPolicy(1, "PolicyDenyDummyPort", NULL); |
205 } | 205 } |
206 | 206 |
207 MULTIPROCESS_TEST_MAIN(PolicyDenyDummyPort) { | 207 MULTIPROCESS_TEST_MAIN(PolicyDenyDummyPort) { |
208 mach_port_t port = MACH_PORT_NULL; | 208 mach_port_t port = MACH_PORT_NULL; |
209 kern_return_t kr = bootstrap_look_up(bootstrap_port, kTestServer, | 209 kern_return_t kr = bootstrap_look_up(bootstrap_port, kTestServer, |
210 &port); | 210 &port); |
211 CHECK_EQ(KERN_SUCCESS, kr); | 211 CHECK_EQ(KERN_SUCCESS, kr); |
(...skipping 28 matching lines...) Expand all Loading... |
240 ASSERT_EQ(KERN_SUCCESS, mach_port_insert_right(task, port, port, | 240 ASSERT_EQ(KERN_SUCCESS, mach_port_insert_right(task, port, port, |
241 MACH_MSG_TYPE_MAKE_SEND)); | 241 MACH_MSG_TYPE_MAKE_SEND)); |
242 base::mac::ScopedMachSendRight scoped_port_send(port); | 242 base::mac::ScopedMachSendRight scoped_port_send(port); |
243 | 243 |
244 send_rights = 0; | 244 send_rights = 0; |
245 ASSERT_EQ(KERN_SUCCESS, mach_port_get_refs(task, port, MACH_PORT_RIGHT_SEND, | 245 ASSERT_EQ(KERN_SUCCESS, mach_port_get_refs(task, port, MACH_PORT_RIGHT_SEND, |
246 &send_rights)); | 246 &send_rights)); |
247 EXPECT_EQ(1u, send_rights); | 247 EXPECT_EQ(1u, send_rights); |
248 | 248 |
249 BootstrapSandboxPolicy policy(BaselinePolicy()); | 249 BootstrapSandboxPolicy policy(BaselinePolicy()); |
250 policy[kTestServer] = Rule(port); | 250 policy.rules[kTestServer] = Rule(port); |
251 sandbox_->RegisterSandboxPolicy(1, policy); | 251 sandbox_->RegisterSandboxPolicy(1, policy); |
252 | 252 |
253 RunChildWithPolicy(1, "PolicySubstitutePort", NULL); | 253 RunChildWithPolicy(1, "PolicySubstitutePort", NULL); |
254 | 254 |
255 struct SubstitutePortAckRecv msg; | 255 struct SubstitutePortAckRecv msg; |
256 bzero(&msg, sizeof(msg)); | 256 bzero(&msg, sizeof(msg)); |
257 msg.header.msgh_size = sizeof(msg); | 257 msg.header.msgh_size = sizeof(msg); |
258 msg.header.msgh_local_port = port; | 258 msg.header.msgh_local_port = port; |
259 kern_return_t kr = mach_msg(&msg.header, MACH_RCV_MSG, 0, | 259 kern_return_t kr = mach_msg(&msg.header, MACH_RCV_MSG, 0, |
260 msg.header.msgh_size, port, | 260 msg.header.msgh_size, port, |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 ASSERT_EQ(KERN_SUCCESS, mach_port_get_refs(task, port, MACH_PORT_RIGHT_SEND, | 333 ASSERT_EQ(KERN_SUCCESS, mach_port_get_refs(task, port, MACH_PORT_RIGHT_SEND, |
334 &send_rights)); | 334 &send_rights)); |
335 // On 10.6, bootstrap_lookup2 may add an extra right to place it in a per- | 335 // On 10.6, bootstrap_lookup2 may add an extra right to place it in a per- |
336 // process cache. | 336 // process cache. |
337 if (base::mac::IsOSSnowLeopard()) | 337 if (base::mac::IsOSSnowLeopard()) |
338 EXPECT_TRUE(send_rights == 3u || send_rights == 2u) << send_rights; | 338 EXPECT_TRUE(send_rights == 3u || send_rights == 2u) << send_rights; |
339 else | 339 else |
340 EXPECT_EQ(2u, send_rights); | 340 EXPECT_EQ(2u, send_rights); |
341 } | 341 } |
342 | 342 |
| 343 const char kDefaultRuleTestAllow[] = |
| 344 "org.chromium.sandbox.test.DefaultRuleAllow"; |
| 345 const char kDefaultRuleTestDeny[] = |
| 346 "org.chromium.sandbox.test.DefaultRuleAllow.Deny"; |
| 347 |
| 348 TEST_F(BootstrapSandboxTest, DefaultRuleAllow) { |
| 349 mach_port_t task = mach_task_self(); |
| 350 |
| 351 mach_port_t port; |
| 352 ASSERT_EQ(KERN_SUCCESS, mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, |
| 353 &port)); |
| 354 base::mac::ScopedMachReceiveRight scoped_port_recv(port); |
| 355 |
| 356 ASSERT_EQ(KERN_SUCCESS, mach_port_insert_right(task, port, port, |
| 357 MACH_MSG_TYPE_MAKE_SEND)); |
| 358 base::mac::ScopedMachSendRight scoped_port_send(port); |
| 359 |
| 360 BootstrapSandboxPolicy policy; |
| 361 policy.default_rule = Rule(POLICY_ALLOW); |
| 362 policy.rules[kDefaultRuleTestAllow] = Rule(port); |
| 363 policy.rules[kDefaultRuleTestDeny] = Rule(POLICY_DENY_ERROR); |
| 364 sandbox_->RegisterSandboxPolicy(3, policy); |
| 365 |
| 366 base::scoped_nsobject<DistributedNotificationObserver> observer( |
| 367 [[DistributedNotificationObserver alloc] init]); |
| 368 |
| 369 int pid = 0; |
| 370 RunChildWithPolicy(3, "DefaultRuleAllow", &pid); |
| 371 EXPECT_GT(pid, 0); |
| 372 |
| 373 [observer waitForNotification]; |
| 374 EXPECT_EQ(1, [observer receivedCount]); |
| 375 EXPECT_EQ(pid, [[observer object] intValue]); |
| 376 |
| 377 struct SubstitutePortAckRecv msg; |
| 378 bzero(&msg, sizeof(msg)); |
| 379 msg.header.msgh_size = sizeof(msg); |
| 380 msg.header.msgh_local_port = port; |
| 381 kern_return_t kr = mach_msg(&msg.header, MACH_RCV_MSG, 0, |
| 382 msg.header.msgh_size, port, |
| 383 TestTimeouts::tiny_timeout().InMilliseconds(), MACH_PORT_NULL); |
| 384 EXPECT_EQ(KERN_SUCCESS, kr); |
| 385 |
| 386 EXPECT_EQ(0, strncmp(kSubstituteAck, msg.buf, sizeof(msg.buf))); |
| 387 } |
| 388 |
| 389 MULTIPROCESS_TEST_MAIN(DefaultRuleAllow) { |
| 390 [[NSDistributedNotificationCenter defaultCenter] |
| 391 postNotificationName:kTestNotification |
| 392 object:[NSString stringWithFormat:@"%d", getpid()]]; |
| 393 |
| 394 mach_port_t port = MACH_PORT_NULL; |
| 395 CHECK_EQ(BOOTSTRAP_UNKNOWN_SERVICE, bootstrap_look_up(bootstrap_port, |
| 396 const_cast<char*>(kDefaultRuleTestDeny), &port)); |
| 397 CHECK(port == MACH_PORT_NULL); |
| 398 |
| 399 CHECK_EQ(KERN_SUCCESS, bootstrap_look_up(bootstrap_port, |
| 400 const_cast<char*>(kDefaultRuleTestAllow), &port)); |
| 401 CHECK(port != MACH_PORT_NULL); |
| 402 |
| 403 struct SubstitutePortAckSend msg; |
| 404 bzero(&msg, sizeof(msg)); |
| 405 msg.header.msgh_size = sizeof(msg); |
| 406 msg.header.msgh_remote_port = port; |
| 407 msg.header.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_MOVE_SEND); |
| 408 strncpy(msg.buf, kSubstituteAck, sizeof(msg.buf)); |
| 409 |
| 410 CHECK_EQ(KERN_SUCCESS, mach_msg_send(&msg.header)); |
| 411 |
| 412 return 0; |
| 413 } |
| 414 |
343 } // namespace sandbox | 415 } // namespace sandbox |
OLD | NEW |