Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/syscall_broker/broker_file_permission.h" | |
| 6 | |
| 7 #include <fcntl.h> | |
| 8 #include <string.h> | |
| 9 #include <sys/stat.h> | |
| 10 #include <sys/types.h> | |
| 11 | |
| 12 #include "base/logging.h" | |
| 13 #include "sandbox/linux/tests/test_utils.h" | |
| 14 #include "sandbox/linux/tests/unit_tests.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace sandbox { | |
| 18 | |
| 19 namespace syscall_broker { | |
| 20 | |
|
jln (very slow on Chromium)
2014/11/26 01:02:31
add also an anonymous namespace (so that the test
leecam
2014/11/26 18:35:33
Done.
| |
| 21 // Creation tests are DEATH tests as a bad permission causes termination. | |
|
jln (very slow on Chromium)
2014/11/26 01:02:32
Please, use SANDBOX_TEST() instad.
leecam
2014/11/26 18:35:33
Done.
| |
| 22 SANDBOX_DEATH_TEST(BrokerFilePermission, CreateGood, DEATH_SUCCESS()) { | |
| 23 const char k_Path[] = "/tmp/good"; | |
|
jln (very slow on Chromium)
2014/11/26 01:02:31
No "_", just kPath (valid a bunch of times in this
leecam
2014/11/26 18:35:33
Done.
| |
| 24 BrokerFilePermission perm = BrokerFilePermission::ReadOnly(k_Path); | |
| 25 } | |
| 26 | |
| 27 SANDBOX_DEATH_TEST(BrokerFilePermission, CreateGoodRecursive, DEATH_SUCCESS()) { | |
| 28 const char k_Path[] = "/tmp/good/"; | |
| 29 BrokerFilePermission perm = BrokerFilePermission::ReadOnlyRecursive(k_Path); | |
| 30 } | |
| 31 | |
| 32 SANDBOX_DEATH_TEST(BrokerFilePermission, CreateBad, DEATH_MESSAGE("")) { | |
|
jln (very slow on Chromium)
2014/11/26 01:02:31
Is there a message to specify ?
Death test are no
leecam
2014/11/26 18:35:33
Done.
| |
| 33 const char k_Path[] = "/tmp/bad/"; | |
| 34 BrokerFilePermission perm = BrokerFilePermission::ReadOnly(k_Path); | |
| 35 } | |
| 36 | |
| 37 SANDBOX_DEATH_TEST(BrokerFilePermission, | |
| 38 CreateBadRecursive, | |
| 39 DEATH_MESSAGE("")) { | |
| 40 const char k_Path[] = "/tmp/bad"; | |
| 41 BrokerFilePermission perm = BrokerFilePermission::ReadOnlyRecursive(k_Path); | |
| 42 } | |
| 43 | |
| 44 SANDBOX_DEATH_TEST(BrokerFilePermission, CreateBadNotAbs, DEATH_MESSAGE("")) { | |
| 45 const char k_Path[] = "tmp/bad"; | |
| 46 BrokerFilePermission perm = BrokerFilePermission::ReadOnly(k_Path); | |
| 47 } | |
| 48 | |
| 49 SANDBOX_DEATH_TEST(BrokerFilePermission, CreateBadEmpty, DEATH_MESSAGE("")) { | |
| 50 const char k_Path[] = ""; | |
| 51 BrokerFilePermission perm = BrokerFilePermission::ReadOnly(k_Path); | |
| 52 } | |
| 53 | |
| 54 void CheckPerm(BrokerFilePermission& perm, | |
|
jln (very slow on Chromium)
2014/11/26 01:02:31
Please, add a comment to explain what this functio
jln (very slow on Chromium)
2014/11/26 01:02:31
Non const reference are evil (and forbidden)! Look
leecam
2014/11/26 18:35:33
Done.
leecam
2014/11/26 18:35:33
Done.
| |
| 55 const char* path, | |
| 56 int access_flags, | |
| 57 bool create) { | |
| 58 const char* file_to_open; | |
|
jln (very slow on Chromium)
2014/11/26 01:02:31
= NULL;
leecam
2014/11/26 18:35:33
Done.
| |
| 59 | |
| 60 // check bad perms | |
| 61 switch (access_flags) { | |
| 62 case O_RDONLY: | |
| 63 ASSERT_TRUE(perm.CheckOpen(path, O_RDONLY, &file_to_open, NULL)); | |
| 64 ASSERT_FALSE(perm.CheckOpen(path, O_WRONLY, &file_to_open, NULL)); | |
| 65 ASSERT_FALSE(perm.CheckOpen(path, O_RDWR, &file_to_open, NULL)); | |
| 66 break; | |
| 67 case O_WRONLY: | |
| 68 ASSERT_FALSE(perm.CheckOpen(path, O_RDONLY, &file_to_open, NULL)); | |
| 69 ASSERT_TRUE(perm.CheckOpen(path, O_WRONLY, &file_to_open, NULL)); | |
| 70 ASSERT_FALSE(perm.CheckOpen(path, O_RDWR, &file_to_open, NULL)); | |
| 71 break; | |
| 72 case O_RDWR: | |
| 73 ASSERT_TRUE(perm.CheckOpen(path, O_RDONLY, &file_to_open, NULL)); | |
| 74 ASSERT_TRUE(perm.CheckOpen(path, O_WRONLY, &file_to_open, NULL)); | |
| 75 ASSERT_TRUE(perm.CheckOpen(path, O_RDWR, &file_to_open, NULL)); | |
| 76 break; | |
| 77 default: | |
| 78 // Bad test case | |
| 79 NOTREACHED(); | |
| 80 } | |
| 81 | |
| 82 // O_SYNC can be defined as (__O_SYNC|O_DSYNC) | |
| 83 #ifdef O_DSYNC | |
| 84 const int sync_flag = O_SYNC & ~O_DSYNC; | |
|
jln (very slow on Chromium)
2014/11/26 01:02:32
style: kSyncFlag
leecam
2014/11/26 18:35:33
Done.
| |
| 85 #else | |
| 86 const int sync_flag = O_SYNC; | |
| 87 #endif | |
| 88 | |
| 89 // check every possible flag and act accordingly. | |
| 90 for (int i = 2; i < 32; i++) { | |
|
jln (very slow on Chromium)
2014/11/26 01:02:31
Why not start at 0?
leecam
2014/11/26 18:35:33
This checks the additional open(2) flags. The O_RD
jln (very slow on Chromium)
2014/11/26 19:49:06
This needs to be documented.
At the very least, m
leecam
2014/11/26 20:55:47
Done.
| |
| 91 int flag = 1 << i; | |
| 92 switch (flag) { | |
| 93 case O_APPEND: | |
| 94 case O_ASYNC: | |
| 95 case O_DIRECT: | |
| 96 case O_DIRECTORY: | |
| 97 #ifdef O_DSYNC | |
| 98 case O_DSYNC: | |
| 99 #endif | |
| 100 case O_EXCL: | |
| 101 case O_LARGEFILE: | |
| 102 case O_NOATIME: | |
| 103 case O_NOCTTY: | |
| 104 case O_NOFOLLOW: | |
| 105 case O_NONBLOCK: | |
| 106 #if (O_NONBLOCK != O_NDELAY) | |
| 107 case O_NDELAY: | |
| 108 #endif | |
| 109 case sync_flag: | |
| 110 case O_TRUNC: | |
| 111 ASSERT_TRUE( | |
| 112 perm.CheckOpen(path, access_flags | flag, &file_to_open, NULL)); | |
| 113 break; | |
| 114 case O_CLOEXEC: | |
| 115 case O_CREAT: | |
| 116 default: | |
| 117 ASSERT_FALSE( | |
| 118 perm.CheckOpen(path, access_flags | flag, &file_to_open, NULL)); | |
| 119 } | |
| 120 } | |
| 121 if (create) { | |
| 122 bool unlink; | |
| 123 ASSERT_TRUE(perm.CheckOpen(path, O_CREAT | O_EXCL | access_flags, | |
| 124 &file_to_open, &unlink)); | |
| 125 ASSERT_FALSE(unlink); | |
| 126 } else { | |
| 127 ASSERT_FALSE(perm.CheckOpen(path, O_CREAT | O_EXCL | access_flags, | |
| 128 &file_to_open, NULL)); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 TEST(BrokerFilePermission, ReadOnly) { | |
| 133 const char k_Path[] = "/tmp/good"; | |
| 134 BrokerFilePermission perm = BrokerFilePermission::ReadOnly(k_Path); | |
| 135 CheckPerm(perm, k_Path, O_RDONLY, false); | |
|
jln (very slow on Chromium)
2014/11/26 01:02:32
Add comments that CheckPerm() has to be the very l
leecam
2014/11/26 18:35:33
Done.
| |
| 136 } | |
| 137 | |
| 138 TEST(BrokerFilePermission, ReadOnlyRecursive) { | |
| 139 const char k_Path[] = "/tmp/good/"; | |
| 140 const char k_PathFile[] = "/tmp/good/file"; | |
| 141 BrokerFilePermission perm = BrokerFilePermission::ReadOnlyRecursive(k_Path); | |
| 142 CheckPerm(perm, k_PathFile, O_RDONLY, false); | |
| 143 } | |
| 144 | |
| 145 TEST(BrokerFilePermission, WriteOnly) { | |
| 146 const char k_Path[] = "/tmp/good"; | |
| 147 BrokerFilePermission perm = BrokerFilePermission::WriteOnly(k_Path); | |
| 148 CheckPerm(perm, k_Path, O_WRONLY, false); | |
| 149 } | |
| 150 | |
| 151 TEST(BrokerFilePermission, ReadWrite) { | |
| 152 const char k_Path[] = "/tmp/good"; | |
| 153 BrokerFilePermission perm = BrokerFilePermission::ReadWrite(k_Path); | |
| 154 CheckPerm(perm, k_Path, O_RDWR, false); | |
| 155 } | |
| 156 | |
| 157 TEST(BrokerFilePermission, ReadWriteCreate) { | |
| 158 const char k_Path[] = "/tmp/good"; | |
| 159 BrokerFilePermission perm = BrokerFilePermission::ReadWriteCreate(k_Path); | |
| 160 CheckPerm(perm, k_Path, O_RDWR, true); | |
| 161 } | |
| 162 | |
| 163 void CheckUnlink(BrokerFilePermission& perm, | |
| 164 const char* path, | |
| 165 int access_flags) { | |
| 166 bool unlink; | |
| 167 ASSERT_FALSE(perm.CheckOpen(path, access_flags, NULL, &unlink)); | |
| 168 ASSERT_FALSE(perm.CheckOpen(path, access_flags | O_CREAT, NULL, &unlink)); | |
| 169 ASSERT_TRUE( | |
| 170 perm.CheckOpen(path, access_flags | O_CREAT | O_EXCL, NULL, &unlink)); | |
| 171 ASSERT_TRUE(unlink); | |
| 172 } | |
| 173 | |
| 174 TEST(BrokerFilePermission, ReadWriteCreateUnlink) { | |
| 175 const char k_Path[] = "/tmp/good"; | |
| 176 BrokerFilePermission perm = | |
| 177 BrokerFilePermission::ReadWriteCreateUnlink(k_Path); | |
| 178 CheckUnlink(perm, k_Path, O_RDWR); | |
| 179 } | |
| 180 | |
| 181 TEST(BrokerFilePermission, ReadWriteCreateUnlinkRecursive) { | |
| 182 const char k_Path[] = "/tmp/good/"; | |
| 183 const char k_PathFile[] = "/tmp/good/file"; | |
| 184 BrokerFilePermission perm = | |
| 185 BrokerFilePermission::ReadWriteCreateUnlinkRecursive(k_Path); | |
| 186 CheckUnlink(perm, k_PathFile, O_RDWR); | |
| 187 } | |
| 188 | |
| 189 class BrokerFilePermissionTester { | |
| 190 public: | |
| 191 static bool ValidatePath(const char* path) { | |
| 192 return BrokerFilePermission::ValidatePath(path); | |
| 193 } | |
| 194 | |
| 195 private: | |
| 196 DISALLOW_COPY_AND_ASSIGN(BrokerFilePermissionTester); | |
| 197 }; | |
| 198 | |
| 199 TEST(BrokerFilePermission, ValidatePath) { | |
| 200 ASSERT_TRUE(BrokerFilePermissionTester::ValidatePath("/path")); | |
|
jln (very slow on Chromium)
2014/11/26 01:02:31
s/ASSERT/EXPECT in this function.
leecam
2014/11/26 18:35:33
Done.
| |
| 201 ASSERT_TRUE(BrokerFilePermissionTester::ValidatePath("/")); | |
| 202 ASSERT_TRUE(BrokerFilePermissionTester::ValidatePath("/..path")); | |
| 203 | |
| 204 ASSERT_FALSE(BrokerFilePermissionTester::ValidatePath("")); | |
| 205 ASSERT_FALSE(BrokerFilePermissionTester::ValidatePath("bad")); | |
| 206 ASSERT_FALSE(BrokerFilePermissionTester::ValidatePath("/bad/")); | |
| 207 ASSERT_FALSE(BrokerFilePermissionTester::ValidatePath("bad/")); | |
| 208 ASSERT_FALSE(BrokerFilePermissionTester::ValidatePath("/bad/..")); | |
| 209 ASSERT_FALSE(BrokerFilePermissionTester::ValidatePath("/bad/../bad")); | |
| 210 ASSERT_FALSE(BrokerFilePermissionTester::ValidatePath("/../bad")); | |
| 211 } | |
| 212 | |
| 213 } // namespace syscall_broker | |
| 214 | |
| 215 } // namespace sandbox | |
| OLD | NEW |