OLD | NEW |
(Empty) | |
| 1 #include "sandbox/linux/services/broker_process.h" |
| 2 |
| 3 #include <errno.h> |
| 4 #include <fcntl.h> |
| 5 #include <sys/stat.h> |
| 6 #include <sys/types.h> |
| 7 #include <sys/wait.h> |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/logging.h" |
| 12 #include "sandbox/linux/tests/unit_tests.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace sandbox { |
| 16 |
| 17 TEST(BrokerProcess, CreateAndDestroy) { |
| 18 std::vector<std::string> file_whitelist; |
| 19 file_whitelist.push_back("/proc/cpuinfo"); |
| 20 |
| 21 BrokerProcess* open_broker = new BrokerProcess(file_whitelist); |
| 22 EXPECT_TRUE(open_broker->Init(NULL)); |
| 23 pid_t broker_pid = open_broker->broker_pid(); |
| 24 delete(open_broker); |
| 25 |
| 26 // Now we check that the broker has exited properly. |
| 27 int status = 0; |
| 28 EXPECT_EQ(waitpid(broker_pid, &status, 0), broker_pid); |
| 29 EXPECT_TRUE(WIFEXITED(status)); |
| 30 EXPECT_EQ(WEXITSTATUS(status), 0); |
| 31 } |
| 32 |
| 33 void TestOpenFile(bool fast_check_in_client) { |
| 34 std::vector<std::string> file_whitelist; |
| 35 const char kFileCpuInfo[] = "/proc/cpuinfo"; |
| 36 const char kDoesNotExistWhitelisted[] = "/proc/DOESNOTEXIST"; |
| 37 const char kDoesNotExist2[] = "/proc/DOESNOTEXIST2"; |
| 38 file_whitelist.push_back(kFileCpuInfo); |
| 39 file_whitelist.push_back(kDoesNotExistWhitelisted); |
| 40 |
| 41 BrokerProcess* open_broker = new BrokerProcess(file_whitelist, |
| 42 fast_check_in_client); |
| 43 EXPECT_TRUE(open_broker->Init(NULL)); |
| 44 pid_t broker_pid = open_broker->broker_pid(); |
| 45 |
| 46 int fd = -1; |
| 47 // This file is not whitelisted. |
| 48 fd = open_broker->Open(kDoesNotExist2, O_RDONLY); |
| 49 EXPECT_EQ(fd, -EPERM); |
| 50 // This file is whitelisted. |
| 51 fd = open_broker->Open(kDoesNotExistWhitelisted, O_RDONLY); |
| 52 EXPECT_EQ(fd, -ENOENT); |
| 53 fd = open_broker->Open(kFileCpuInfo, O_RDWR); |
| 54 EXPECT_EQ(fd, -EPERM); |
| 55 |
| 56 // Open cpuinfo via the broker. |
| 57 int cpuinfo_fd = open_broker->Open(kFileCpuInfo, O_RDONLY); |
| 58 ASSERT_GE(cpuinfo_fd, 0); |
| 59 char buf[3]; |
| 60 memset(buf, 0, sizeof(buf)); |
| 61 int read_len1 = read(cpuinfo_fd, buf, sizeof(buf)); |
| 62 EXPECT_GT(read_len1, 0); |
| 63 |
| 64 // Open cpuinfo directly. |
| 65 int cpuinfo_fd2 = open(kFileCpuInfo, O_RDONLY); |
| 66 ASSERT_GE(cpuinfo_fd2, 0); |
| 67 char buf2[3]; |
| 68 memset(buf2, 1, sizeof(buf2)); |
| 69 int read_len2 = read(cpuinfo_fd2, buf2, sizeof(buf2)); |
| 70 EXPECT_GT(read_len1, 0); |
| 71 |
| 72 // The following is not guaranteed true, but will be in practice. |
| 73 EXPECT_EQ(read_len1, read_len2); |
| 74 // Compare the cpuinfo as returned by the broker with the one we opened |
| 75 // ourselves. |
| 76 EXPECT_EQ(memcmp(buf, buf2, read_len1), 0); |
| 77 |
| 78 if (fd >= 0) |
| 79 close(fd); |
| 80 if (cpuinfo_fd >= 0) |
| 81 close(cpuinfo_fd); |
| 82 if (cpuinfo_fd2 >= 0) |
| 83 close(cpuinfo_fd); |
| 84 |
| 85 delete(open_broker); |
| 86 |
| 87 // Now we check that the broker has exited properly. |
| 88 int status = 0; |
| 89 EXPECT_EQ(waitpid(broker_pid, &status, 0), broker_pid); |
| 90 EXPECT_TRUE(WIFEXITED(status)); |
| 91 EXPECT_EQ(WEXITSTATUS(status), 0); |
| 92 } |
| 93 |
| 94 // Run the same thing twice. The second time, we make sure that no security |
| 95 // check is performed on the client. |
| 96 TEST(BrokerProcess, OpenFileWithClientCheck) { |
| 97 TestOpenFile(true /* fast_check_in_client */); |
| 98 } |
| 99 |
| 100 TEST(BrokerProcess, OpenFileNoClientCheck) { |
| 101 TestOpenFile(false /* fast_check_in_client */); |
| 102 } |
| 103 |
| 104 // Sandbox test because we could get a SIGPIPE. |
| 105 SANDBOX_TEST(BrokerProcess, BrokerDied) { |
| 106 std::vector<std::string> file_whitelist; |
| 107 file_whitelist.push_back("/proc/cpuinfo"); |
| 108 |
| 109 BrokerProcess open_broker(file_whitelist); |
| 110 SANDBOX_ASSERT(open_broker.Init(NULL)); |
| 111 pid_t broker_pid = open_broker.broker_pid(); |
| 112 SANDBOX_ASSERT(kill(broker_pid, SIGKILL) == 0); |
| 113 |
| 114 // Now we check that the broker has exited properly. |
| 115 int status = 0; |
| 116 SANDBOX_ASSERT(waitpid(broker_pid, &status, 0) == broker_pid); |
| 117 SANDBOX_ASSERT(WIFSIGNALED(status)); |
| 118 SANDBOX_ASSERT(WTERMSIG(status) == SIGKILL); |
| 119 // Hopefully doing Open with a dead broker won't SIGPIPE us. |
| 120 SANDBOX_ASSERT(open_broker.Open("/proc/cpuinfo", O_RDONLY) == -ENOMEM); |
| 121 } |
| 122 |
| 123 } // namespace sandbox |
OLD | NEW |