| 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 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 EXPECT_EQ(0, code); | 131 EXPECT_EQ(0, code); |
| 132 | 132 |
| 133 [observer waitForNotification]; | 133 [observer waitForNotification]; |
| 134 EXPECT_EQ(1, [observer receivedCount]); | 134 EXPECT_EQ(1, [observer receivedCount]); |
| 135 EXPECT_EQ(process.Pid(), [[observer object] intValue]); | 135 EXPECT_EQ(process.Pid(), [[observer object] intValue]); |
| 136 } | 136 } |
| 137 | 137 |
| 138 // Run the test with the sandbox enabled without notifications on the policy | 138 // Run the test with the sandbox enabled without notifications on the policy |
| 139 // whitelist. | 139 // whitelist. |
| 140 TEST_F(BootstrapSandboxTest, DistributedNotifications_SandboxDeny) { | 140 TEST_F(BootstrapSandboxTest, DistributedNotifications_SandboxDeny) { |
| 141 if (base::mac::IsOSSierraOrLater()) { |
| 142 LOG(ERROR) << "BootstrapSandbox does not work on macOS Sierra or later."; |
| 143 return; |
| 144 } |
| 145 |
| 141 base::scoped_nsobject<DistributedNotificationObserver> observer( | 146 base::scoped_nsobject<DistributedNotificationObserver> observer( |
| 142 [[DistributedNotificationObserver alloc] init]); | 147 [[DistributedNotificationObserver alloc] init]); |
| 143 | 148 |
| 144 sandbox_->RegisterSandboxPolicy(1, BaselinePolicy()); | 149 sandbox_->RegisterSandboxPolicy(1, BaselinePolicy()); |
| 145 RunChildWithPolicy(1, kNotificationTestMain, NULL); | 150 RunChildWithPolicy(1, kNotificationTestMain, NULL); |
| 146 | 151 |
| 147 [observer waitForNotification]; | 152 [observer waitForNotification]; |
| 148 EXPECT_EQ(0, [observer receivedCount]); | 153 EXPECT_EQ(0, [observer receivedCount]); |
| 149 EXPECT_EQ(nil, [observer object]); | 154 EXPECT_EQ(nil, [observer object]); |
| 150 } | 155 } |
| 151 | 156 |
| 152 // Run the test with notifications permitted. | 157 // Run the test with notifications permitted. |
| 153 TEST_F(BootstrapSandboxTest, DistributedNotifications_SandboxAllow) { | 158 TEST_F(BootstrapSandboxTest, DistributedNotifications_SandboxAllow) { |
| 159 if (base::mac::IsOSSierraOrLater()) { |
| 160 LOG(ERROR) << "BootstrapSandbox does not work on macOS Sierra or later."; |
| 161 return; |
| 162 } |
| 163 |
| 154 base::scoped_nsobject<DistributedNotificationObserver> observer( | 164 base::scoped_nsobject<DistributedNotificationObserver> observer( |
| 155 [[DistributedNotificationObserver alloc] init]); | 165 [[DistributedNotificationObserver alloc] init]); |
| 156 | 166 |
| 157 BootstrapSandboxPolicy policy(BaselinePolicy()); | 167 BootstrapSandboxPolicy policy(BaselinePolicy()); |
| 158 // 10.9: | 168 // 10.9: |
| 159 policy.rules["com.apple.distributed_notifications@Uv3"] = Rule(POLICY_ALLOW); | 169 policy.rules["com.apple.distributed_notifications@Uv3"] = Rule(POLICY_ALLOW); |
| 160 policy.rules["com.apple.distributed_notifications@1v3"] = Rule(POLICY_ALLOW); | 170 policy.rules["com.apple.distributed_notifications@1v3"] = Rule(POLICY_ALLOW); |
| 161 // 10.6: | 171 // 10.6: |
| 162 policy.rules["com.apple.system.notification_center"] = Rule(POLICY_ALLOW); | 172 policy.rules["com.apple.system.notification_center"] = Rule(POLICY_ALLOW); |
| 163 policy.rules["com.apple.distributed_notifications.2"] = Rule(POLICY_ALLOW); | 173 policy.rules["com.apple.distributed_notifications.2"] = Rule(POLICY_ALLOW); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 174 MULTIPROCESS_TEST_MAIN(PostNotification) { | 184 MULTIPROCESS_TEST_MAIN(PostNotification) { |
| 175 [[NSDistributedNotificationCenter defaultCenter] | 185 [[NSDistributedNotificationCenter defaultCenter] |
| 176 postNotificationName:kTestNotification | 186 postNotificationName:kTestNotification |
| 177 object:[NSString stringWithFormat:@"%d", getpid()]]; | 187 object:[NSString stringWithFormat:@"%d", getpid()]]; |
| 178 return 0; | 188 return 0; |
| 179 } | 189 } |
| 180 | 190 |
| 181 const char kTestServer[] = "org.chromium.test_bootstrap_server"; | 191 const char kTestServer[] = "org.chromium.test_bootstrap_server"; |
| 182 | 192 |
| 183 TEST_F(BootstrapSandboxTest, PolicyDenyError) { | 193 TEST_F(BootstrapSandboxTest, PolicyDenyError) { |
| 194 if (base::mac::IsOSSierraOrLater()) { |
| 195 LOG(ERROR) << "BootstrapSandbox does not work on macOS Sierra or later."; |
| 196 return; |
| 197 } |
| 184 BootstrapSandboxPolicy policy(BaselinePolicy()); | 198 BootstrapSandboxPolicy policy(BaselinePolicy()); |
| 185 policy.rules[kTestServer] = Rule(POLICY_DENY_ERROR); | 199 policy.rules[kTestServer] = Rule(POLICY_DENY_ERROR); |
| 186 sandbox_->RegisterSandboxPolicy(1, policy); | 200 sandbox_->RegisterSandboxPolicy(1, policy); |
| 187 | 201 |
| 188 RunChildWithPolicy(1, "PolicyDenyError", NULL); | 202 RunChildWithPolicy(1, "PolicyDenyError", NULL); |
| 189 } | 203 } |
| 190 | 204 |
| 191 MULTIPROCESS_TEST_MAIN(PolicyDenyError) { | 205 MULTIPROCESS_TEST_MAIN(PolicyDenyError) { |
| 192 mach_port_t port = MACH_PORT_NULL; | 206 mach_port_t port = MACH_PORT_NULL; |
| 193 kern_return_t kr = bootstrap_look_up(bootstrap_port, kTestServer, | 207 kern_return_t kr = bootstrap_look_up(bootstrap_port, kTestServer, |
| 194 &port); | 208 &port); |
| 195 CHECK_EQ(BOOTSTRAP_UNKNOWN_SERVICE, kr); | 209 CHECK_EQ(BOOTSTRAP_UNKNOWN_SERVICE, kr); |
| 196 CHECK(port == MACH_PORT_NULL); | 210 CHECK(port == MACH_PORT_NULL); |
| 197 | 211 |
| 198 kr = bootstrap_look_up(bootstrap_port, "org.chromium.some_other_server", | 212 kr = bootstrap_look_up(bootstrap_port, "org.chromium.some_other_server", |
| 199 &port); | 213 &port); |
| 200 CHECK_EQ(BOOTSTRAP_UNKNOWN_SERVICE, kr); | 214 CHECK_EQ(BOOTSTRAP_UNKNOWN_SERVICE, kr); |
| 201 CHECK(port == MACH_PORT_NULL); | 215 CHECK(port == MACH_PORT_NULL); |
| 202 | 216 |
| 203 return 0; | 217 return 0; |
| 204 } | 218 } |
| 205 | 219 |
| 206 TEST_F(BootstrapSandboxTest, PolicyDenyDummyPort) { | 220 TEST_F(BootstrapSandboxTest, PolicyDenyDummyPort) { |
| 221 if (base::mac::IsOSSierraOrLater()) { |
| 222 LOG(ERROR) << "BootstrapSandbox does not work on macOS Sierra or later."; |
| 223 return; |
| 224 } |
| 207 BootstrapSandboxPolicy policy(BaselinePolicy()); | 225 BootstrapSandboxPolicy policy(BaselinePolicy()); |
| 208 policy.rules[kTestServer] = Rule(POLICY_DENY_DUMMY_PORT); | 226 policy.rules[kTestServer] = Rule(POLICY_DENY_DUMMY_PORT); |
| 209 sandbox_->RegisterSandboxPolicy(1, policy); | 227 sandbox_->RegisterSandboxPolicy(1, policy); |
| 210 | 228 |
| 211 RunChildWithPolicy(1, "PolicyDenyDummyPort", NULL); | 229 RunChildWithPolicy(1, "PolicyDenyDummyPort", NULL); |
| 212 } | 230 } |
| 213 | 231 |
| 214 MULTIPROCESS_TEST_MAIN(PolicyDenyDummyPort) { | 232 MULTIPROCESS_TEST_MAIN(PolicyDenyDummyPort) { |
| 215 mach_port_t port = MACH_PORT_NULL; | 233 mach_port_t port = MACH_PORT_NULL; |
| 216 kern_return_t kr = bootstrap_look_up(bootstrap_port, kTestServer, | 234 kern_return_t kr = bootstrap_look_up(bootstrap_port, kTestServer, |
| 217 &port); | 235 &port); |
| 218 CHECK_EQ(KERN_SUCCESS, kr); | 236 CHECK_EQ(KERN_SUCCESS, kr); |
| 219 CHECK(port != MACH_PORT_NULL); | 237 CHECK(port != MACH_PORT_NULL); |
| 220 return 0; | 238 return 0; |
| 221 } | 239 } |
| 222 | 240 |
| 223 struct SubstitutePortAckSend { | 241 struct SubstitutePortAckSend { |
| 224 mach_msg_header_t header; | 242 mach_msg_header_t header; |
| 225 char buf[32]; | 243 char buf[32]; |
| 226 }; | 244 }; |
| 227 | 245 |
| 228 struct SubstitutePortAckRecv : public SubstitutePortAckSend { | 246 struct SubstitutePortAckRecv : public SubstitutePortAckSend { |
| 229 mach_msg_trailer_t trailer; | 247 mach_msg_trailer_t trailer; |
| 230 }; | 248 }; |
| 231 | 249 |
| 232 const char kSubstituteAck[] = "Hello, this is doge!"; | 250 const char kSubstituteAck[] = "Hello, this is doge!"; |
| 233 | 251 |
| 234 TEST_F(BootstrapSandboxTest, PolicySubstitutePort) { | 252 TEST_F(BootstrapSandboxTest, PolicySubstitutePort) { |
| 253 if (base::mac::IsOSSierraOrLater()) { |
| 254 LOG(ERROR) << "BootstrapSandbox does not work on macOS Sierra or later."; |
| 255 return; |
| 256 } |
| 257 |
| 235 mach_port_t task = mach_task_self(); | 258 mach_port_t task = mach_task_self(); |
| 236 | 259 |
| 237 mach_port_t port; | 260 mach_port_t port; |
| 238 ASSERT_EQ(KERN_SUCCESS, mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, | 261 ASSERT_EQ(KERN_SUCCESS, mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, |
| 239 &port)); | 262 &port)); |
| 240 base::mac::ScopedMachReceiveRight scoped_port(port); | 263 base::mac::ScopedMachReceiveRight scoped_port(port); |
| 241 | 264 |
| 242 mach_port_urefs_t send_rights = 0; | 265 mach_port_urefs_t send_rights = 0; |
| 243 ASSERT_EQ(KERN_SUCCESS, mach_port_get_refs(task, port, MACH_PORT_RIGHT_SEND, | 266 ASSERT_EQ(KERN_SUCCESS, mach_port_get_refs(task, port, MACH_PORT_RIGHT_SEND, |
| 244 &send_rights)); | 267 &send_rights)); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 &send_rights)); | 364 &send_rights)); |
| 342 EXPECT_EQ(2u, send_rights); | 365 EXPECT_EQ(2u, send_rights); |
| 343 } | 366 } |
| 344 | 367 |
| 345 const char kDefaultRuleTestAllow[] = | 368 const char kDefaultRuleTestAllow[] = |
| 346 "org.chromium.sandbox.test.DefaultRuleAllow"; | 369 "org.chromium.sandbox.test.DefaultRuleAllow"; |
| 347 const char kDefaultRuleTestDeny[] = | 370 const char kDefaultRuleTestDeny[] = |
| 348 "org.chromium.sandbox.test.DefaultRuleAllow.Deny"; | 371 "org.chromium.sandbox.test.DefaultRuleAllow.Deny"; |
| 349 | 372 |
| 350 TEST_F(BootstrapSandboxTest, DefaultRuleAllow) { | 373 TEST_F(BootstrapSandboxTest, DefaultRuleAllow) { |
| 374 if (base::mac::IsOSSierraOrLater()) { |
| 375 LOG(ERROR) << "BootstrapSandbox does not work on macOS Sierra or later."; |
| 376 return; |
| 377 } |
| 378 |
| 351 mach_port_t task = mach_task_self(); | 379 mach_port_t task = mach_task_self(); |
| 352 | 380 |
| 353 mach_port_t port; | 381 mach_port_t port; |
| 354 ASSERT_EQ(KERN_SUCCESS, mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, | 382 ASSERT_EQ(KERN_SUCCESS, mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, |
| 355 &port)); | 383 &port)); |
| 356 base::mac::ScopedMachReceiveRight scoped_port_recv(port); | 384 base::mac::ScopedMachReceiveRight scoped_port_recv(port); |
| 357 | 385 |
| 358 ASSERT_EQ(KERN_SUCCESS, mach_port_insert_right(task, port, port, | 386 ASSERT_EQ(KERN_SUCCESS, mach_port_insert_right(task, port, port, |
| 359 MACH_MSG_TYPE_MAKE_SEND)); | 387 MACH_MSG_TYPE_MAKE_SEND)); |
| 360 base::mac::ScopedMachSendRight scoped_port_send(port); | 388 base::mac::ScopedMachSendRight scoped_port_send(port); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 msg.header.msgh_remote_port = port; | 436 msg.header.msgh_remote_port = port; |
| 409 msg.header.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_MOVE_SEND); | 437 msg.header.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_MOVE_SEND); |
| 410 strncpy(msg.buf, kSubstituteAck, sizeof(msg.buf)); | 438 strncpy(msg.buf, kSubstituteAck, sizeof(msg.buf)); |
| 411 | 439 |
| 412 CHECK_EQ(KERN_SUCCESS, mach_msg_send(&msg.header)); | 440 CHECK_EQ(KERN_SUCCESS, mach_msg_send(&msg.header)); |
| 413 | 441 |
| 414 return 0; | 442 return 0; |
| 415 } | 443 } |
| 416 | 444 |
| 417 TEST_F(BootstrapSandboxTest, ChildOutliveSandbox) { | 445 TEST_F(BootstrapSandboxTest, ChildOutliveSandbox) { |
| 446 if (base::mac::IsOSSierraOrLater()) { |
| 447 LOG(ERROR) << "BootstrapSandbox does not work on macOS Sierra or later."; |
| 448 return; |
| 449 } |
| 450 |
| 418 const int kTestPolicyId = 1; | 451 const int kTestPolicyId = 1; |
| 419 mach_port_t task = mach_task_self(); | 452 mach_port_t task = mach_task_self(); |
| 420 | 453 |
| 421 // Create a server port. | 454 // Create a server port. |
| 422 mach_port_t port; | 455 mach_port_t port; |
| 423 ASSERT_EQ(KERN_SUCCESS, mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, | 456 ASSERT_EQ(KERN_SUCCESS, mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, |
| 424 &port)); | 457 &port)); |
| 425 base::mac::ScopedMachReceiveRight scoped_port_recv(port); | 458 base::mac::ScopedMachReceiveRight scoped_port_recv(port); |
| 426 | 459 |
| 427 ASSERT_EQ(KERN_SUCCESS, mach_port_insert_right(task, port, port, | 460 ASSERT_EQ(KERN_SUCCESS, mach_port_insert_right(task, port, port, |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 kr = mach_msg_receive(&rcv_msg.header); | 540 kr = mach_msg_receive(&rcv_msg.header); |
| 508 MACH_CHECK(kr == KERN_SUCCESS, kr) << "mach_msg_receive"; | 541 MACH_CHECK(kr == KERN_SUCCESS, kr) << "mach_msg_receive"; |
| 509 | 542 |
| 510 // Try to message the sandbox. | 543 // Try to message the sandbox. |
| 511 bootstrap_look_up(bootstrap_port, "test", &port); | 544 bootstrap_look_up(bootstrap_port, "test", &port); |
| 512 | 545 |
| 513 return 0; | 546 return 0; |
| 514 } | 547 } |
| 515 | 548 |
| 516 } // namespace sandbox | 549 } // namespace sandbox |
| OLD | NEW |