From ca3b7743aea74bbb7185f1aac53494ac3e86c26a Mon Sep 17 00:00:00 2001 From: "caitkp@chromium.org" Date: Thu, 16 Jan 2014 15:46:33 +0000 Subject: 1. Make sure chrome_elf.dll imports nothing besides kernel32, advapi32, and some msvc libs (DEBUG builds) 2. Add gyp action and test to ensure chrome_elf.dll is always the first entry in chrome.exe's import table. Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=242834 Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=243048 Review URL: https://codereview.chromium.org/109483003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@245197 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome_elf/chrome_elf.gyp | 1 + chrome_elf/elf_imports_unittest.cc | 94 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 chrome_elf/elf_imports_unittest.cc (limited to 'chrome_elf') diff --git a/chrome_elf/chrome_elf.gyp b/chrome_elf/chrome_elf.gyp index 8a4e87e..92c8294 100644 --- a/chrome_elf/chrome_elf.gyp +++ b/chrome_elf/chrome_elf.gyp @@ -45,6 +45,7 @@ 'type': 'executable', 'sources': [ 'blacklist/test/blacklist_test.cc', + 'elf_imports_unittest.cc', 'ntdll_cache_unittest.cc', ], 'include_dirs': [ diff --git a/chrome_elf/elf_imports_unittest.cc b/chrome_elf/elf_imports_unittest.cc new file mode 100644 index 0000000..8fcad97 --- /dev/null +++ b/chrome_elf/elf_imports_unittest.cc @@ -0,0 +1,94 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include +#include + +#include +#include + +#include "base/base_paths.h" +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "base/files/file_path.h" +#include "base/files/memory_mapped_file.h" +#include "base/path_service.h" +#include "base/strings/string_util.h" +#include "base/win/pe_image.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +class ELFImportsTest : public testing::Test { + protected: + static bool ImportsCallback(const base::win::PEImage &image, + LPCSTR module, + PIMAGE_THUNK_DATA name_table, + PIMAGE_THUNK_DATA iat, + PVOID cookie) { + std::vector* import_list = + reinterpret_cast*>(cookie); + import_list->push_back(module); + return true; + } + + void GetImports(const base::FilePath& module_path, + std::vector* imports) { + ASSERT_TRUE(imports != NULL); + + base::MemoryMappedFile module_mmap; + + ASSERT_TRUE(module_mmap.Initialize(module_path)); + base::win::PEImageAsData pe_image_data( + reinterpret_cast(const_cast(module_mmap.data()))); + pe_image_data.EnumImportChunks(ELFImportsTest::ImportsCallback, imports); + } +}; + +TEST_F(ELFImportsTest, ChromeElfSanityCheck) { + std::vector elf_imports; + + base::FilePath dll; + ASSERT_TRUE(PathService::Get(base::DIR_EXE, &dll)); + dll = dll.Append(L"chrome_elf.dll"); + GetImports(dll, &elf_imports); + + // Check that ELF has imports. + ASSERT_LT(0u, elf_imports.size()); + + std::vector::iterator it(elf_imports.begin()); + + static const char* const kValidFilePatterns[] = { + "KERNEL32.dll", + "MSVC*", + "ADVAPI32.dll"}; + + // Make sure all of ELF's imports are in the valid imports list. + for (; it != elf_imports.end(); it++) { + bool match = false; + for (int i = 0; i < arraysize(kValidFilePatterns); ++i) { + if (MatchPattern(*it, kValidFilePatterns[i])) + match = true; + } + ASSERT_TRUE(match) << "Illegal import in chrome_elf.dll."; + } +} + +TEST_F(ELFImportsTest, ChromeExeSanityCheck) { + std::vector exe_imports; + + base::FilePath exe; + ASSERT_TRUE(PathService::Get(base::DIR_EXE, &exe)); + exe = exe.Append(L"chrome.exe"); + GetImports(exe, &exe_imports); + + // Check that chrome.exe has imports. + ASSERT_LT(0u, exe_imports.size()); + + // Chrome.exe's first import must be ELF. + EXPECT_EQ("chrome_elf.dll", exe_imports[0]) << + "Illegal import order in chrome.exe"; +} + +} // namespace -- cgit v1.1