OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "chrome/common/component_flash_hint_file.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <stdlib.h> |
| 9 #include <sys/mount.h> |
| 10 |
| 11 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/path_service.h" |
| 14 #include "base/test/scoped_path_override.h" |
| 15 #include "chrome/common/chrome_paths.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace chrome { |
| 19 |
| 20 class ComponentFlashHintFileTest : public testing::Test {}; |
| 21 |
| 22 TEST_F(ComponentFlashHintFileTest, ExistsTest) { |
| 23 base::ScopedPathOverride path_override(chrome::DIR_USER_DATA); |
| 24 EXPECT_FALSE(ComponentFlashHintFile::DoesHintFileExist()); |
| 25 } |
| 26 |
| 27 TEST_F(ComponentFlashHintFileTest, InstallTest) { |
| 28 base::ScopedPathOverride path_override(chrome::DIR_USER_DATA); |
| 29 EXPECT_FALSE(ComponentFlashHintFile::DoesHintFileExist()); |
| 30 |
| 31 base::FilePath flash_dir; |
| 32 ASSERT_TRUE(PathService::Get( |
| 33 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &flash_dir)); |
| 34 |
| 35 base::File::Error error; |
| 36 ASSERT_TRUE(base::CreateDirectoryAndGetError(flash_dir, &error)); |
| 37 |
| 38 // Write out a fixed byte array as the flash file. |
| 39 uint8_t file[] = { |
| 40 0x4c, 0x65, 0x74, 0x20, 0x75, 0x73, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x67, |
| 41 0x6f, 0x20, 0x67, 0x65, 0x6e, 0x74, 0x6c, 0x79, 0x0a, 0x54, 0x6f, 0x20, |
| 42 0x74, 0x68, 0x65, 0x20, 0x65, 0x6e, 0x64, 0x6c, 0x65, 0x73, 0x73, 0x20, |
| 43 0x77, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x20, 0x6e, 0x69, 0x67, 0x68, 0x74, |
| 44 0x0a, 0x41, 0x6e, 0x64, 0x20, 0x74, 0x68, 0x65, 0x20, 0x72, 0x65, 0x64, |
| 45 0x20, 0x74, 0x69, 0x64, 0x65, 0x20, 0x6b, 0x69, 0x73, 0x73, 0x65, 0x73, |
| 46 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x68, 0x6f, 0x72, 0x65}; |
| 47 flash_dir = flash_dir.Append("libflash.so"); |
| 48 std::string flash_version = "1.0.0.1"; |
| 49 ASSERT_TRUE(base::WriteFile(flash_dir, reinterpret_cast<const char*>(file), |
| 50 sizeof(file)) == sizeof(file)); |
| 51 ASSERT_TRUE(ComponentFlashHintFile::RecordFlashUpdate(flash_dir, flash_dir, |
| 52 flash_version)); |
| 53 ASSERT_TRUE(ComponentFlashHintFile::DoesHintFileExist()); |
| 54 |
| 55 // Confirm that the flash plugin can be verified and returned. |
| 56 base::FilePath returned_flash_path; |
| 57 std::string version; |
| 58 ASSERT_TRUE(ComponentFlashHintFile::VerifyAndReturnFlashLocation( |
| 59 &returned_flash_path, &version)); |
| 60 ASSERT_TRUE(returned_flash_path == flash_dir); |
| 61 ASSERT_TRUE(version == flash_version); |
| 62 |
| 63 // Now "corrupt" the flash file and make sure the checksum fails and nothing |
| 64 // is returned. |
| 65 srand48(time(NULL) ^ getpid()); |
| 66 int idx = lrand48() % ((sizeof(file) / sizeof(file[0])) - 1); |
| 67 if (file[idx] == std::numeric_limits<uint8_t>::max()) |
| 68 file[idx] = 0x00; |
| 69 else |
| 70 file[idx] = file[idx] + 1; |
| 71 ASSERT_TRUE(base::WriteFile(flash_dir, reinterpret_cast<const char*>(file), |
| 72 sizeof(file)) == sizeof(file)); |
| 73 base::FilePath empty_path; |
| 74 std::string empty_version; |
| 75 ASSERT_FALSE(ComponentFlashHintFile::VerifyAndReturnFlashLocation( |
| 76 &empty_path, &empty_version)); |
| 77 ASSERT_FALSE(empty_path == flash_dir); |
| 78 ASSERT_FALSE(empty_version == flash_version); |
| 79 } |
| 80 |
| 81 TEST_F(ComponentFlashHintFileTest, CorruptionTest) { |
| 82 base::ScopedPathOverride path_override(chrome::DIR_USER_DATA); |
| 83 EXPECT_FALSE(ComponentFlashHintFile::DoesHintFileExist()); |
| 84 |
| 85 base::FilePath flash_dir; |
| 86 ASSERT_TRUE(PathService::Get( |
| 87 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &flash_dir)); |
| 88 |
| 89 base::File::Error error; |
| 90 ASSERT_TRUE(base::CreateDirectoryAndGetError(flash_dir, &error)); |
| 91 flash_dir = flash_dir.Append("libflash.so"); |
| 92 |
| 93 const uint8_t file[] = { |
| 94 0x56, 0x61, 0x20, 0x67, 0x75, 0x76, 0x66, 0x20, 0x62, 0x61, 0x72, 0x20, |
| 95 0x62, 0x73, 0x20, 0x7a, 0x6e, 0x61, 0x6c, 0x20, 0x63, 0x62, 0x66, 0x66, |
| 96 0x76, 0x6f, 0x79, 0x72, 0x20, 0x6a, 0x62, 0x65, 0x79, 0x71, 0x66, 0x2c, |
| 97 0xa, 0x4e, 0x79, 0x79, 0x20, 0x73, 0x62, 0x65, 0x20, 0x67, 0x75, 0x72, |
| 98 0x20, 0x6f, 0x72, 0x66, 0x67, 0x20, 0x62, 0x65, 0x20, 0x66, 0x62, 0x7a, |
| 99 0x72, 0x20, 0x6f, 0x76, 0x6d, 0x6e, 0x65, 0x65, 0x72, 0x20, 0x67, 0x72, |
| 100 0x66, 0x67, 0x3f, 0xa, 0x56, 0x67, 0x20, 0x76, 0x66, 0x20, 0x6a, 0x75, |
| 101 0x6e, 0x67, 0x20, 0x76, 0x67, 0x20, 0x76, 0x66, 0x20, 0x6e, 0x61, 0x71, |
| 102 0x20, 0x6a, 0x75, 0x6e, 0x67, 0x72, 0x69, 0x72, 0x65, 0x2c, 0xa, 0x47, |
| 103 0x76, 0x7a, 0x72, 0x20, 0x76, 0x66, 0x20, 0x66, 0x67, 0x76, 0x79, 0x79, |
| 104 0x20, 0x67, 0x75, 0x72, 0x20, 0x76, 0x61, 0x73, 0x76, 0x61, 0x76, 0x67, |
| 105 0x72, 0x20, 0x77, 0x72, 0x66, 0x67, 0xa, 0x47, 0x75, 0x72, 0x20, 0x73, |
| 106 0x68, 0x67, 0x68, 0x65, 0x72, 0x20, 0x71, 0x76, 0x66, 0x6e, 0x63, 0x63, |
| 107 0x72, 0x6e, 0x65, 0x66, 0x20, 0x76, 0x61, 0x67, 0x62, 0x20, 0x7a, 0x72, |
| 108 0x7a, 0x62, 0x65, 0x6c, 0xa, 0x4a, 0x76, 0x67, 0x75, 0x20, 0x62, 0x61, |
| 109 0x79, 0x6c, 0x20, 0x6e, 0x20, 0x7a, 0x62, 0x7a, 0x72, 0x61, 0x67, 0x20, |
| 110 0x6f, 0x72, 0x67, 0x6a, 0x72, 0x72, 0x61, 0x2e, 0xa, 0x53, 0x62, 0x65, |
| 111 0x72, 0x69, 0x72, 0x65, 0x20, 0x71, 0x6a, 0x72, 0x79, 0x79, 0x66, 0x20, |
| 112 0x76, 0x61, 0x20, 0x67, 0x75, 0x6e, 0x67, 0x20, 0x7a, 0x62, 0x7a, 0x72, |
| 113 0x61, 0x67, 0x2c, 0xa, 0x55, 0x62, 0x63, 0x72, 0x20, 0x76, 0x66, 0x20, |
| 114 0x6a, 0x75, 0x6e, 0x67, 0x20, 0x65, 0x72, 0x7a, 0x6e, 0x76, 0x61, 0x66, |
| 115 0x20, 0x67, 0x62, 0x20, 0x6f, 0x72, 0x20, 0x66, 0x72, 0x72, 0x61, 0x2e, |
| 116 0x0}; |
| 117 ASSERT_TRUE(base::WriteFile(flash_dir, reinterpret_cast<const char*>(file), |
| 118 sizeof(file)) == sizeof(file)); |
| 119 std::string flash_version = "1.0.0.1"; |
| 120 ASSERT_TRUE(ComponentFlashHintFile::RecordFlashUpdate(flash_dir, flash_dir, |
| 121 flash_version)); |
| 122 ASSERT_TRUE(ComponentFlashHintFile::DoesHintFileExist()); |
| 123 |
| 124 // Now write out a new flash version that will not be moved into place. |
| 125 const uint8_t updated_file[] = { |
| 126 0x43, 0x72, 0x62, 0x63, 0x79, 0x72, 0x20, 0x66, 0x7a, 0x76, 0x79, 0x76, |
| 127 0x61, 0x74, 0x20, 0x67, 0x75, 0x65, 0x62, 0x68, 0x74, 0x75, 0x20, 0x67, |
| 128 0x75, 0x72, 0x76, 0x65, 0x20, 0x67, 0x72, 0x6e, 0x65, 0x66, 0xa, 0x4a, |
| 129 0x75, 0x62, 0x20, 0x70, 0x6e, 0x61, 0x20, 0x74, 0x76, 0x69, 0x72, 0x20, |
| 130 0x67, 0x75, 0x72, 0x7a, 0x20, 0x6f, 0x6e, 0x70, 0x78, 0x20, 0x67, 0x75, |
| 131 0x72, 0x76, 0x65, 0x20, 0x79, 0x76, 0x69, 0x72, 0x66, 0xa, 0x4e, 0x61, |
| 132 0x71, 0x20, 0x6e, 0x79, 0x79, 0x20, 0x67, 0x75, 0x62, 0x66, 0x72, 0x20, |
| 133 0x6a, 0x6e, 0x66, 0x67, 0x72, 0x71, 0x20, 0x6c, 0x72, 0x6e, 0x65, 0x66, |
| 134 0x3f, 0xa, 0x4a, 0x75, 0x62, 0x20, 0x6a, 0x76, 0x79, 0x79, 0x20, 0x63, |
| 135 0x6e, 0x6c, 0x3f, 0x0}; |
| 136 base::FilePath flash_dir_update; |
| 137 ASSERT_TRUE(PathService::Get( |
| 138 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &flash_dir_update)); |
| 139 flash_dir_update = flash_dir_update.Append("other_flash.so"); |
| 140 ASSERT_TRUE(base::WriteFile(flash_dir_update, |
| 141 reinterpret_cast<const char*>(updated_file), |
| 142 sizeof(updated_file)) == sizeof(updated_file)); |
| 143 ASSERT_TRUE(ComponentFlashHintFile::RecordFlashUpdate( |
| 144 flash_dir_update, flash_dir, flash_version)); |
| 145 // flash_dir_update needs to be moved to flash_dir, but if that fails (for the |
| 146 // test we don't even try), we need to revert the hint file. |
| 147 base::FilePath failed_flash_dir; |
| 148 std::string failed_version; |
| 149 ASSERT_FALSE(ComponentFlashHintFile::VerifyAndReturnFlashLocation( |
| 150 &failed_flash_dir, &failed_version)); |
| 151 } |
| 152 |
| 153 TEST_F(ComponentFlashHintFileTest, ExecTest1) { |
| 154 /* |
| 155 errno = 0; |
| 156 pid_t pid = base::ForkWithFlags(CLONE_NEWUSER | CLONE_NEWNS, nullptr, |
| 157 nullptr); |
| 158 PCHECK(errno == 0 || errno == EINVAL) << "Unexpected error condition " |
| 159 << errno; |
| 160 if (pid == -1) { |
| 161 LOG(ERROR) << "Fork failed, not running test."; |
| 162 return; |
| 163 } |
| 164 // Skip the test in the parent process. |
| 165 if (pid == 0) |
| 166 return; |
| 167 */ |
| 168 ASSERT_TRUE(unshare(CLONE_NEWUSER | CLONE_NEWNS) == 0); |
| 169 // Now mount a NOEXEC fs. |
| 170 const unsigned long tmpfs_flags = MS_NODEV | MS_NOSUID | MS_NOEXEC; |
| 171 base::ScopedTempDir temp_dir; |
| 172 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 173 ASSERT_TRUE(mount("tmpfs", temp_dir.path().value().c_str(), "tmpfs", |
| 174 tmpfs_flags, nullptr) == 0); |
| 175 base::FilePath file_path = temp_dir.path().Append("plugin.so"); |
| 176 const uint8_t file[] = { |
| 177 0x56, 0x61, 0x20, 0x67, 0x75, 0x72, 0x20, 0x70, 0x76, 0x67, 0x6c, 0x20, |
| 178 0x6a, 0x75, 0x72, 0x65, 0x72, 0x20, 0x61, 0x62, 0x6f, 0x62, 0x71, 0x6c, |
| 179 0x20, 0x66, 0x7a, 0x76, 0x79, 0x72, 0x66, 0x4e, 0x61, 0x71, 0x20, 0x61, |
| 180 0x62, 0x6f, 0x62, 0x71, 0x6c, 0x20, 0x71, 0x65, 0x72, 0x6e, 0x7a, 0x66, |
| 181 0x56, 0x61, 0x20, 0x67, 0x75, 0x72, 0x20, 0x70, 0x76, 0x67, 0x6c, 0x20, |
| 182 0x6a, 0x75, 0x72, 0x65, 0x72, 0x20, 0x71, 0x72, 0x66, 0x63, 0x72, 0x65, |
| 183 0x6e, 0x67, 0x76, 0x62, 0x61, 0x51, 0x65, 0x76, 0x69, 0x72, 0x66, 0x20, |
| 184 0x67, 0x75, 0x72, 0x20, 0x6f, 0x62, 0x65, 0x72, 0x71, 0x20, 0x67, 0x62, |
| 185 0x20, 0x72, 0x6b, 0x67, 0x65, 0x72, 0x7a, 0x72, 0x66, 0x57, 0x68, 0x66, |
| 186 0x67, 0x20, 0x62, 0x61, 0x72, 0x20, 0x66, 0x63, 0x6e, 0x65, 0x78, 0x20, |
| 187 0x62, 0x73, 0x20, 0x71, 0x72, 0x70, 0x72, 0x61, 0x70, 0x6c, 0x4e, 0x74, |
| 188 0x6e, 0x76, 0x61, 0x66, 0x67, 0x20, 0x6e, 0x20, 0x66, 0x67, 0x6e, 0x65, |
| 189 0x79, 0x72, 0x66, 0x66, 0x20, 0x61, 0x76, 0x74, 0x75, 0x67, 0x42, 0x61, |
| 190 0x72, 0x20, 0x74, 0x79, 0x62, 0x6a, 0x20, 0x62, 0x73, 0x20, 0x75, 0x62, |
| 191 0x63, 0x72, 0x20, 0x6e, 0x61, 0x71, 0x20, 0x71, 0x76, 0x74, 0x61, 0x76, |
| 192 0x67, 0x6c, 0x4e, 0x20, 0x70, 0x75, 0x76, 0x79, 0x71, 0x20, 0x70, 0x6e, |
| 193 0x61, 0x20, 0x73, 0x62, 0x79, 0x79, 0x62, 0x6a, 0x20, 0x67, 0x75, 0x72, |
| 194 0x20, 0x79, 0x76, 0x74, 0x75, 0x67, 0x0, |
| 195 }; |
| 196 ASSERT_TRUE(base::WriteFile(file_path, reinterpret_cast<const char*>(file), |
| 197 sizeof(file)) == sizeof(file)); |
| 198 ASSERT_FALSE(ComponentFlashHintFile::TestExecutableMapping(file_path)); |
| 199 // ASSERT_TRUE(umount(temp_dir.path().value().c_str()) == 0); |
| 200 } |
| 201 |
| 202 TEST_F(ComponentFlashHintFileTest, ExecTest2) { |
| 203 base::ScopedTempDir temp_dir; |
| 204 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 205 base::FilePath file_path = temp_dir.path().Append("plugin.so"); |
| 206 const uint8_t file[] = { |
| 207 0x55, 0x62, 0x79, 0x71, 0x20, 0x6c, 0x62, 0x68, 0x65, 0x20, |
| 208 0x73, 0x76, 0x65, 0x72, 0x58, 0x72, 0x72, 0x63, 0x20, 0x76, |
| 209 0x67, 0x20, 0x6f, 0x68, 0x65, 0x61, 0x76, 0x61, 0x74, 0x20, |
| 210 0x6f, 0x65, 0x76, 0x74, 0x75, 0x67, 0x0, |
| 211 }; |
| 212 |
| 213 ASSERT_TRUE(base::WriteFile(file_path, reinterpret_cast<const char*>(file), |
| 214 sizeof(file)) == sizeof(file)); |
| 215 ASSERT_TRUE(ComponentFlashHintFile::TestExecutableMapping(file_path)); |
| 216 } |
| 217 |
| 218 } // namespace chrome |
OLD | NEW |