Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Side by Side Diff: chrome/browser/safe_browsing/signature_evaluator_mac_unittest.cc

Issue 1363613004: Implement anonymous, opt-in, collection of OS X binary integrity incidents. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Test checkperms changs Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/browser/safe_browsing/signature_evaluator_mac.h"
6
7 #include <CoreFoundation/CoreFoundation.h>
8 #include <sys/xattr.h>
9
10 #include <string>
11 #include <vector>
12
13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h"
15 #include "base/files/scoped_temp_dir.h"
16 #include "base/mac/mac_util.h"
17 #include "base/mac/scoped_cftyperef.h"
18 #include "base/path_service.h"
19 #include "base/test/scoped_path_override.h"
20 #include "chrome/browser/safe_browsing/incident_reporting/incident.h"
21 #include "chrome/browser/safe_browsing/incident_reporting/mock_incident_receiver .h"
22 #include "chrome/common/chrome_paths.h"
23 #include "chrome/common/safe_browsing/csd.pb.h"
24 #include "testing/gmock/include/gmock/gmock-matchers.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27
28 using ::testing::_;
29 using ::testing::StrictMock;
30
31 namespace safe_browsing {
32
33 namespace {
34
35 const char* const xattrs[] = {
36 "com.apple.cs.CodeDirectory",
37 "com.apple.cs.CodeSignature",
38 "com.apple.cs.CodeRequirements",
39 "com.apple.cs.CodeResources",
40 "com.apple.cs.CodeApplication",
41 "com.apple.cs.CodeEntitlements",
42 };
43
44 } // namespace
Robert Sesek 2015/10/08 19:20:06 nit: remove? there's a closing namespace online 32
Greg K 2015/10/09 17:12:01 The one at the bottom was incorrectly commented. I
45
46 class MacSignatureEvaluatorTest : public testing::Test {
47 protected:
48 void SetUp() override {
49 base::FilePath source_path;
50 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_path));
51 testdata_path_ =
52 source_path.AppendASCII("safe_browsing").AppendASCII("mach_o");
53
54 base::FilePath dir_exe;
55 ASSERT_TRUE(PathService::Get(base::DIR_EXE, &dir_exe));
56 base::FilePath file_exe;
57 ASSERT_TRUE(PathService::Get(base::FILE_EXE, &file_exe));
58
59 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
60 }
61
62 bool GetExecPath(const base::FilePath& bundle_url, base::FilePath* result) {
63 base::ScopedCFTypeRef<CFStringRef> path_str(CFStringCreateWithCString(
Robert Sesek 2015/10/08 19:20:06 SysUTF8ToCFStringRef
Greg K 2015/10/09 17:12:01 Done.
64 kCFAllocatorDefault, bundle_url.value().c_str(),
65 kCFStringEncodingUTF8));
66 if (!path_str.get())
67 return false;
68 base::ScopedCFTypeRef<CFURLRef> path_url(CFURLCreateWithFileSystemPath(
69 kCFAllocatorDefault, path_str, kCFURLPOSIXPathStyle, false));
70 if (!path_url.get())
71 return false;
72 base::ScopedCFTypeRef<CFBundleRef> bundle(
73 CFBundleCreate(kCFAllocatorDefault, path_url));
74 if (!bundle.get())
75 return false;
76
77 base::ScopedCFTypeRef<CFURLRef> exec_url(CFBundleCopyExecutableURL(bundle));
78 UInt8 path_buf[PATH_MAX];
79 if (!CFURLGetFileSystemRepresentation(exec_url, true, path_buf,
80 sizeof(path_buf)))
Robert Sesek 2015/10/08 19:20:06 nit: needs braces
Greg K 2015/10/09 17:12:01 Done.
81 return false;
82
83 *result = base::FilePath(reinterpret_cast<const char*>(path_buf));
84 return true;
85 }
86
87 bool SetupXattrs(const base::FilePath& path) {
88 char sentinel = 'A';
89 for (const auto& xattr : xattrs) {
90 std::vector<uint8_t> buf(10);
91 memset(&buf[0], sentinel++, buf.size());
92 if (setxattr(path.value().c_str(), xattr, &buf[0], buf.size(), 0, 0) != 0)
93 return false;
94 }
95 return true;
96 }
97
98 base::FilePath testdata_path_;
99 base::ScopedTempDir temp_dir_;
100 };
101
102 TEST_F(MacSignatureEvaluatorTest, SimpleTest) {
103 // This is a simple test that checks the validity of a signed executable.
104 // There is no designated requirement: we only check the embedded signature.
105 base::FilePath path = testdata_path_.AppendASCII("signedexecutablefat");
106 safe_browsing::MacSignatureEvaluator evaluator(path);
107 ASSERT_TRUE(evaluator.Initialize());
108
109 std::vector<ClientIncidentReport_IncidentData_BinaryIntegrityIncident>
110 results;
111 ASSERT_TRUE(evaluator.PerformEvaluation(results));
Robert Sesek 2015/10/08 19:20:06 EXPECT (and throughout where possible)
Greg K 2015/10/09 17:12:01 Done.
112 ASSERT_EQ(0u, results.size());
113 }
114
115 TEST_F(MacSignatureEvaluatorTest, SimpleTestWithDR) {
116 // This test checks the signer against a designated requirement description.
117 base::FilePath path = testdata_path_.AppendASCII("signedexecutablefat");
118 std::string requirement(
119 "certificate leaf[subject.CN]=\"untrusted@goat.local\"");
120 safe_browsing::MacSignatureEvaluator evaluator(path, requirement);
121 ASSERT_TRUE(evaluator.Initialize());
122
123 std::vector<ClientIncidentReport_IncidentData_BinaryIntegrityIncident>
124 results;
125 ASSERT_TRUE(evaluator.PerformEvaluation(results));
126 ASSERT_EQ(0u, results.size());
127 }
128
129 TEST_F(MacSignatureEvaluatorTest, SimpleTestWithBadDR) {
130 // Now test with a designated requirement that does not describe the signer.
131 base::FilePath path = testdata_path_.AppendASCII("signedexecutablefat");
132 safe_browsing::MacSignatureEvaluator evaluator(path, "anchor apple");
133 ASSERT_TRUE(evaluator.Initialize());
134
135 std::vector<ClientIncidentReport_IncidentData_BinaryIntegrityIncident>
136 results;
137 ASSERT_FALSE(evaluator.PerformEvaluation(results));
138 ASSERT_EQ(1u, results.size());
Robert Sesek 2015/10/08 19:20:06 This has to remain an ASSERT_ since you get result
Greg K 2015/10/09 17:12:01 Acknowledged.
139
140 const ClientIncidentReport_IncidentData_BinaryIntegrityIncident& result =
141 results[0];
142 ASSERT_EQ(-67050, result.sec_error());
143 ASSERT_TRUE(result.has_file_basename());
144 ASSERT_EQ("signedexecutablefat", result.file_basename());
145 ASSERT_TRUE(result.has_signature());
146 }
147
148 TEST_F(MacSignatureEvaluatorTest, SimpleBundleTest) {
149 // Now test a simple, validly signed bundle.
150 base::FilePath path = testdata_path_.AppendASCII("test-bundle.app");
151 base::FilePath exec_path;
152 ASSERT_TRUE(GetExecPath(path, &exec_path));
153
154 std::string requirement(
155 "certificate leaf[subject.CN]=\"untrusted@goat.local\"");
156 safe_browsing::MacSignatureEvaluator evaluator(exec_path, requirement);
157 ASSERT_TRUE(evaluator.Initialize());
158
159 std::vector<ClientIncidentReport_IncidentData_BinaryIntegrityIncident>
160 results;
161 ASSERT_TRUE(evaluator.PerformEvaluation(results));
162 ASSERT_EQ(0u, results.size());
163 }
164
165 TEST_F(MacSignatureEvaluatorTest, ModifiedMainExecTest32) {
166 // Now to a test modified, signed bundle.
167 base::FilePath path = testdata_path_.AppendASCII("modified-main-exec32.app");
168 base::FilePath exec_path;
169 ASSERT_TRUE(GetExecPath(path, &exec_path));
170
171 std::string requirement(
172 "certificate leaf[subject.CN]=\"untrusted@goat.local\"");
173 safe_browsing::MacSignatureEvaluator evaluator(exec_path, requirement);
174 ASSERT_TRUE(evaluator.Initialize());
175
176 std::vector<ClientIncidentReport_IncidentData_BinaryIntegrityIncident>
177 results;
178 ASSERT_FALSE(evaluator.PerformEvaluation(results));
179 ASSERT_EQ(1u, results.size());
180
181 const ClientIncidentReport_IncidentData_BinaryIntegrityIncident& incident =
182 results[0];
183 ASSERT_EQ(-67061, incident.sec_error());
184 ASSERT_EQ(exec_path.BaseName().value(), incident.file_basename());
185 }
186
187 TEST_F(MacSignatureEvaluatorTest, ModifiedMainExecTest64) {
188 // Snow Leopard does not know about the 64-bit slice so this test is
189 // irrelevant.
190 if (base::mac::IsOSLionOrLater()) {
Robert Sesek 2015/10/08 19:20:06 Generally prefer early returns for short-circuitin
Greg K 2015/10/09 17:12:01 Done.
191 // Now to a test modified, signed bundle.
192 base::FilePath path =
193 testdata_path_.AppendASCII("modified-main-exec64.app");
194 base::FilePath exec_path;
195 ASSERT_TRUE(GetExecPath(path, &exec_path));
196
197 std::string requirement(
198 "certificate leaf[subject.CN]=\"untrusted@goat.local\"");
199 safe_browsing::MacSignatureEvaluator evaluator(exec_path, requirement);
200 ASSERT_TRUE(evaluator.Initialize());
201
202 std::vector<ClientIncidentReport_IncidentData_BinaryIntegrityIncident>
203 results;
204 ASSERT_FALSE(evaluator.PerformEvaluation(results));
205 ASSERT_EQ(1u, results.size());
206
207 const ClientIncidentReport_IncidentData_BinaryIntegrityIncident& incident =
208 results[0];
209 ASSERT_EQ(-67061, incident.sec_error());
210 ASSERT_EQ(exec_path.BaseName().value(), incident.file_basename());
211 }
212 }
213
214 TEST_F(MacSignatureEvaluatorTest, ModifiedBundleAndExecTest) {
215 // Now test a modified, signed bundle with resources added and the main
216 // executable modified.
217 base::FilePath path =
218 testdata_path_.AppendASCII("modified-bundle-and-exec.app");
219 base::FilePath exec_path;
220 ASSERT_TRUE(GetExecPath(path, &exec_path));
221
222 std::string requirement(
223 "certificate leaf[subject.CN]=\"untrusted@goat.local\"");
224 safe_browsing::MacSignatureEvaluator evaluator(exec_path, requirement);
225 ASSERT_TRUE(evaluator.Initialize());
226
227 std::vector<ClientIncidentReport_IncidentData_BinaryIntegrityIncident>
228 results;
229 ASSERT_FALSE(evaluator.PerformEvaluation(results));
230 ASSERT_EQ(1u, results.size());
231
232 const ClientIncidentReport_IncidentData_BinaryIntegrityIncident& incident1 =
233 results[0];
234 ASSERT_TRUE(incident1.has_file_basename());
235 ASSERT_EQ(exec_path.BaseName().value(), incident1.file_basename());
236 ASSERT_EQ(-67061, incident1.sec_error());
237 ASSERT_TRUE(incident1.has_signature());
238 }
239
240 TEST_F(MacSignatureEvaluatorTest, ModifiedBundleTest) {
241 // Now test a modified, signed bundle. This bundle has
242 // the following problems:
243 // 1) A file was added (This should not be reported)
244 // 2) libsigned64.dylib was modified
245 // 3) executable32 was modified
246
247 base::FilePath orig_path = testdata_path_.AppendASCII("modified-bundle.app");
248 base::FilePath copied_path =
249 temp_dir_.path().AppendASCII("modified-bundle.app");
250 CHECK(base::CopyDirectory(orig_path, copied_path, true));
251
252 base::FilePath exec_path;
253 ASSERT_TRUE(GetExecPath(copied_path, &exec_path));
254
255 // Setup the extended attributes, which don't persist in the git repo.
256 ASSERT_TRUE(SetupXattrs(
257 copied_path.AppendASCII("Contents/Resources/Base.lproj/MainMenu.nib")));
258
259 std::string requirement(
260 "certificate leaf[subject.CN]=\"untrusted@goat.local\"");
261 safe_browsing::MacSignatureEvaluator evaluator(exec_path, requirement);
262 ASSERT_TRUE(evaluator.Initialize());
263
264 std::vector<ClientIncidentReport_IncidentData_BinaryIntegrityIncident>
265 results;
266 ASSERT_FALSE(evaluator.PerformEvaluation(results));
267 ASSERT_EQ(4u, results.size());
268
269 const ClientIncidentReport_IncidentData_BinaryIntegrityIncident* main_exec =
270 nullptr;
271 const ClientIncidentReport_IncidentData_BinaryIntegrityIncident* libsigned64 =
272 nullptr;
273 const ClientIncidentReport_IncidentData_BinaryIntegrityIncident*
274 executable32 = nullptr;
275 const ClientIncidentReport_IncidentData_BinaryIntegrityIncident* mainmenunib =
276 nullptr;
277 const ClientIncidentReport_IncidentData_BinaryIntegrityIncident*
278 codesign_cfg = nullptr;
279
280 for (const auto& incident : results) {
281 if (incident.file_basename() == exec_path.BaseName().value())
282 main_exec = &incident;
283 else if (incident.file_basename() == "libsigned64.dylib")
284 libsigned64 = &incident;
285 else if (incident.file_basename() == "executable32")
286 executable32 = &incident;
287 else if (incident.file_basename() == "MainMenu.nib")
288 mainmenunib = &incident;
289 else if (incident.file_basename() == "codesign.cfg")
290 codesign_cfg = &incident;
291 }
292 ASSERT_NE(main_exec, nullptr);
Robert Sesek 2015/10/08 19:20:06 Similarly, these should be ASSERT_ since they're d
Greg K 2015/10/09 17:12:01 Done.
293 ASSERT_NE(libsigned64, nullptr);
294 ASSERT_NE(executable32, nullptr);
295 // This is important. Do not collect information on extra files added.
296 ASSERT_EQ(codesign_cfg, nullptr);
297
298 ASSERT_TRUE(main_exec->has_file_basename());
299 ASSERT_EQ(exec_path.BaseName().value(), main_exec->file_basename());
300 ASSERT_TRUE(main_exec->has_signature());
301 ASSERT_EQ(-67054, main_exec->sec_error());
302
303 ASSERT_TRUE(libsigned64->has_file_basename());
304 ASSERT_EQ("libsigned64.dylib", libsigned64->file_basename());
305 ASSERT_TRUE(libsigned64->has_signature());
306
307 ASSERT_TRUE(executable32->has_file_basename());
308 ASSERT_EQ("executable32", executable32->file_basename());
309 ASSERT_TRUE(executable32->has_signature());
310
311 ASSERT_TRUE(mainmenunib->has_file_basename());
312 ASSERT_EQ("MainMenu.nib", mainmenunib->file_basename());
313 ASSERT_TRUE(mainmenunib->has_signature());
314 ASSERT_EQ(6, mainmenunib->signature().xattr_size());
315 // Manually convert the global xattrs array to a vector
316 std::vector<std::string> xattrs_known;
317 for (const auto& xattr : xattrs)
318 xattrs_known.push_back(xattr);
319
320 std::vector<std::string> xattrs_seen;
321 for (const auto& xattr : mainmenunib->signature().xattr()) {
322 ASSERT_TRUE(xattr.has_key());
323 ASSERT_TRUE(xattr.has_value());
324 xattrs_seen.push_back(xattr.key());
325 }
326 ASSERT_THAT(xattrs_known, ::testing::ContainerEq(xattrs_seen));
327 }
328
329 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698