blob: cb33404dcfd2e05742b484810bcd9893a7da7a4c [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Allie Woodeb9e6d82015-04-17 13:55:30 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/filesystem_verifier_action.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070018
Amin Hassanid3f4bea2018-04-30 14:52:40 -070019#include <memory>
Allie Woodeb9e6d82015-04-17 13:55:30 -070020#include <string>
Amin Hassanid3f4bea2018-04-30 14:52:40 -070021#include <utility>
Allie Woodeb9e6d82015-04-17 13:55:30 -070022
Alex Deymo20c99202015-07-09 16:14:16 -070023#include <base/bind.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070024#include <base/posix/eintr_wrapper.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070025#include <brillo/message_loops/fake_message_loop.h>
26#include <brillo/message_loops/message_loop_utils.h>
Sen Jiang57f91802017-11-14 17:42:13 -080027#include <brillo/secure_blob.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070028#include <gtest/gtest.h>
29
Alex Deymo39910dc2015-11-09 17:04:30 -080030#include "update_engine/common/hash_calculator.h"
31#include "update_engine/common/test_utils.h"
32#include "update_engine/common/utils.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070033
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070034using brillo::MessageLoop;
Allie Woodeb9e6d82015-04-17 13:55:30 -070035using std::string;
Allie Woodeb9e6d82015-04-17 13:55:30 -070036
37namespace chromeos_update_engine {
38
39class FilesystemVerifierActionTest : public ::testing::Test {
40 protected:
Amin Hassani008c4582019-01-13 16:22:47 -080041 void SetUp() override { loop_.SetAsCurrent(); }
Alex Deymo20c99202015-07-09 16:14:16 -070042
43 void TearDown() override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070044 EXPECT_EQ(0, brillo::MessageLoopRunMaxIterations(&loop_, 1));
Alex Deymo20c99202015-07-09 16:14:16 -070045 }
46
Allie Woodeb9e6d82015-04-17 13:55:30 -070047 // Returns true iff test has completed successfully.
Sen Jiangfef85fd2016-03-25 15:32:49 -070048 bool DoTest(bool terminate_early, bool hash_fail);
Allie Woodeb9e6d82015-04-17 13:55:30 -070049
Sen Jiang3eeaf7d2018-10-11 13:55:32 -070050 void BuildActions(const InstallPlan& install_plan);
51
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070052 brillo::FakeMessageLoop loop_{nullptr};
Sen Jiang57f91802017-11-14 17:42:13 -080053 ActionProcessor processor_;
Allie Woodeb9e6d82015-04-17 13:55:30 -070054};
55
56class FilesystemVerifierActionTestDelegate : public ActionProcessorDelegate {
57 public:
Amin Hassaniabe4a772018-07-26 11:19:10 -070058 FilesystemVerifierActionTestDelegate()
59 : ran_(false), code_(ErrorCode::kError) {}
60
Allie Woodeb9e6d82015-04-17 13:55:30 -070061 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
Amin Hassaniabe4a772018-07-26 11:19:10 -070062 MessageLoop::current()->BreakLoop();
Allie Woodeb9e6d82015-04-17 13:55:30 -070063 }
64 void ProcessingStopped(const ActionProcessor* processor) {
Amin Hassaniabe4a772018-07-26 11:19:10 -070065 MessageLoop::current()->BreakLoop();
Allie Woodeb9e6d82015-04-17 13:55:30 -070066 }
67 void ActionCompleted(ActionProcessor* processor,
68 AbstractAction* action,
69 ErrorCode code) {
70 if (action->Type() == FilesystemVerifierAction::StaticType()) {
71 ran_ = true;
72 code_ = code;
Amin Hassaniabe4a772018-07-26 11:19:10 -070073 EXPECT_FALSE(static_cast<FilesystemVerifierAction*>(action)->src_stream_);
Amin Hassanid3f4bea2018-04-30 14:52:40 -070074 } else if (action->Type() ==
75 ObjectCollectorAction<InstallPlan>::StaticType()) {
76 auto collector_action =
77 static_cast<ObjectCollectorAction<InstallPlan>*>(action);
78 install_plan_.reset(new InstallPlan(collector_action->object()));
Allie Woodeb9e6d82015-04-17 13:55:30 -070079 }
80 }
81 bool ran() const { return ran_; }
82 ErrorCode code() const { return code_; }
83
Amin Hassanid3f4bea2018-04-30 14:52:40 -070084 std::unique_ptr<InstallPlan> install_plan_;
85
Allie Woodeb9e6d82015-04-17 13:55:30 -070086 private:
Allie Woodeb9e6d82015-04-17 13:55:30 -070087 bool ran_;
88 ErrorCode code_;
89};
90
Allie Woodeb9e6d82015-04-17 13:55:30 -070091bool FilesystemVerifierActionTest::DoTest(bool terminate_early,
Sen Jiangfef85fd2016-03-25 15:32:49 -070092 bool hash_fail) {
Sen Jiang0779a152018-07-02 17:34:56 -070093 test_utils::ScopedTempFile a_loop_file("a_loop_file.XXXXXX");
Allie Woodeb9e6d82015-04-17 13:55:30 -070094
Alex Deymo20c99202015-07-09 16:14:16 -070095 // Make random data for a.
Allie Woodeb9e6d82015-04-17 13:55:30 -070096 const size_t kLoopFileSize = 10 * 1024 * 1024 + 512;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070097 brillo::Blob a_loop_data(kLoopFileSize);
Allie Woodeb9e6d82015-04-17 13:55:30 -070098 test_utils::FillWithData(&a_loop_data);
99
Allie Woodeb9e6d82015-04-17 13:55:30 -0700100 // Write data to disk
Sen Jiang0779a152018-07-02 17:34:56 -0700101 if (!(test_utils::WriteFileVector(a_loop_file.path(), a_loop_data))) {
Allie Woodeb9e6d82015-04-17 13:55:30 -0700102 ADD_FAILURE();
103 return false;
104 }
105
106 // Attach loop devices to the files
107 string a_dev;
Alex Deymocbc22742016-03-04 17:53:02 -0800108 test_utils::ScopedLoopbackDeviceBinder a_dev_releaser(
Sen Jiang0779a152018-07-02 17:34:56 -0700109 a_loop_file.path(), false, &a_dev);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700110 if (!(a_dev_releaser.is_bound())) {
111 ADD_FAILURE();
112 return false;
113 }
114
Sen Jiang0779a152018-07-02 17:34:56 -0700115 LOG(INFO) << "verifying: " << a_loop_file.path() << " (" << a_dev << ")";
Allie Woodeb9e6d82015-04-17 13:55:30 -0700116
117 bool success = true;
118
119 // Set up the action objects
120 InstallPlan install_plan;
Alex Deymo763e7db2015-08-27 21:08:08 -0700121 install_plan.source_slot = 0;
122 install_plan.target_slot = 1;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700123 InstallPlan::Partition part;
124 part.name = "part";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700125 part.target_size = kLoopFileSize - (hash_fail ? 1 : 0);
126 part.target_path = a_dev;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700127 if (!HashCalculator::RawHashOfData(a_loop_data, &part.target_hash)) {
128 ADD_FAILURE();
129 success = false;
Sen Jiangf9874812015-12-08 16:11:48 -0800130 }
131 part.source_size = kLoopFileSize;
132 part.source_path = a_dev;
Sen Jiangf9874812015-12-08 16:11:48 -0800133 if (!HashCalculator::RawHashOfData(a_loop_data, &part.source_hash)) {
134 ADD_FAILURE();
135 success = false;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700136 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700137 install_plan.partitions = {part};
Allie Woodeb9e6d82015-04-17 13:55:30 -0700138
Sen Jiang3eeaf7d2018-10-11 13:55:32 -0700139 BuildActions(install_plan);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700140
Amin Hassaniabe4a772018-07-26 11:19:10 -0700141 FilesystemVerifierActionTestDelegate delegate;
Sen Jiang57f91802017-11-14 17:42:13 -0800142 processor_.set_delegate(&delegate);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700143
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700144 loop_.PostTask(FROM_HERE,
145 base::Bind(
146 [](ActionProcessor* processor, bool terminate_early) {
147 processor->StartProcessing();
148 if (terminate_early) {
149 processor->StopProcessing();
150 }
151 },
Sen Jiang57f91802017-11-14 17:42:13 -0800152 base::Unretained(&processor_),
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700153 terminate_early));
Alex Deymo20c99202015-07-09 16:14:16 -0700154 loop_.Run();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700155
156 if (!terminate_early) {
157 bool is_delegate_ran = delegate.ran();
158 EXPECT_TRUE(is_delegate_ran);
159 success = success && is_delegate_ran;
160 } else {
161 EXPECT_EQ(ErrorCode::kError, delegate.code());
162 return (ErrorCode::kError == delegate.code());
163 }
164 if (hash_fail) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700165 ErrorCode expected_exit_code = ErrorCode::kNewRootfsVerificationError;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700166 EXPECT_EQ(expected_exit_code, delegate.code());
167 return (expected_exit_code == delegate.code());
168 }
169 EXPECT_EQ(ErrorCode::kSuccess, delegate.code());
170
171 // Make sure everything in the out_image is there
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700172 brillo::Blob a_out;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700173 if (!utils::ReadFile(a_dev, &a_out)) {
174 ADD_FAILURE();
175 return false;
176 }
177 const bool is_a_file_reading_eq =
178 test_utils::ExpectVectorsEq(a_loop_data, a_out);
179 EXPECT_TRUE(is_a_file_reading_eq);
180 success = success && is_a_file_reading_eq;
181
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700182 bool is_install_plan_eq = (*delegate.install_plan_ == install_plan);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700183 EXPECT_TRUE(is_install_plan_eq);
184 success = success && is_install_plan_eq;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700185 return success;
186}
187
Sen Jiang3eeaf7d2018-10-11 13:55:32 -0700188void FilesystemVerifierActionTest::BuildActions(
189 const InstallPlan& install_plan) {
190 auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
191 auto verifier_action = std::make_unique<FilesystemVerifierAction>();
192 auto collector_action =
193 std::make_unique<ObjectCollectorAction<InstallPlan>>();
194
195 feeder_action->set_obj(install_plan);
196
197 BondActions(feeder_action.get(), verifier_action.get());
198 BondActions(verifier_action.get(), collector_action.get());
199
200 processor_.EnqueueAction(std::move(feeder_action));
201 processor_.EnqueueAction(std::move(verifier_action));
202 processor_.EnqueueAction(std::move(collector_action));
203}
204
Allie Woodeb9e6d82015-04-17 13:55:30 -0700205class FilesystemVerifierActionTest2Delegate : public ActionProcessorDelegate {
206 public:
207 void ActionCompleted(ActionProcessor* processor,
208 AbstractAction* action,
209 ErrorCode code) {
210 if (action->Type() == FilesystemVerifierAction::StaticType()) {
211 ran_ = true;
212 code_ = code;
213 }
214 }
Allie Woodeb9e6d82015-04-17 13:55:30 -0700215 bool ran_;
216 ErrorCode code_;
217};
218
219TEST_F(FilesystemVerifierActionTest, MissingInputObjectTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700220 auto copier_action = std::make_unique<FilesystemVerifierAction>();
221 auto collector_action =
222 std::make_unique<ObjectCollectorAction<InstallPlan>>();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700223
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700224 BondActions(copier_action.get(), collector_action.get());
Allie Woodeb9e6d82015-04-17 13:55:30 -0700225
Sen Jiang57f91802017-11-14 17:42:13 -0800226 processor_.EnqueueAction(std::move(copier_action));
227 processor_.EnqueueAction(std::move(collector_action));
Sen Jiang3eeaf7d2018-10-11 13:55:32 -0700228
229 FilesystemVerifierActionTest2Delegate delegate;
230 processor_.set_delegate(&delegate);
231
Sen Jiang57f91802017-11-14 17:42:13 -0800232 processor_.StartProcessing();
233 EXPECT_FALSE(processor_.IsRunning());
Allie Woodeb9e6d82015-04-17 13:55:30 -0700234 EXPECT_TRUE(delegate.ran_);
235 EXPECT_EQ(ErrorCode::kError, delegate.code_);
236}
237
238TEST_F(FilesystemVerifierActionTest, NonExistentDriveTest) {
Alex Deymo64d98782016-02-05 18:03:48 -0800239 InstallPlan install_plan;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700240 InstallPlan::Partition part;
241 part.name = "nope";
242 part.source_path = "/no/such/file";
243 part.target_path = "/no/such/file";
244 install_plan.partitions = {part};
245
Sen Jiang3eeaf7d2018-10-11 13:55:32 -0700246 BuildActions(install_plan);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700247
Sen Jiang3eeaf7d2018-10-11 13:55:32 -0700248 FilesystemVerifierActionTest2Delegate delegate;
249 processor_.set_delegate(&delegate);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700250
Sen Jiang57f91802017-11-14 17:42:13 -0800251 processor_.StartProcessing();
252 EXPECT_FALSE(processor_.IsRunning());
Allie Woodeb9e6d82015-04-17 13:55:30 -0700253 EXPECT_TRUE(delegate.ran_);
Sen Jiang57f91802017-11-14 17:42:13 -0800254 EXPECT_EQ(ErrorCode::kFilesystemVerifierError, delegate.code_);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700255}
256
257TEST_F(FilesystemVerifierActionTest, RunAsRootVerifyHashTest) {
Alex Deymo80f70ff2016-02-10 16:08:11 -0800258 ASSERT_EQ(0U, getuid());
Sen Jiangfef85fd2016-03-25 15:32:49 -0700259 EXPECT_TRUE(DoTest(false, false));
Allie Woodeb9e6d82015-04-17 13:55:30 -0700260}
261
262TEST_F(FilesystemVerifierActionTest, RunAsRootVerifyHashFailTest) {
Alex Deymo80f70ff2016-02-10 16:08:11 -0800263 ASSERT_EQ(0U, getuid());
Sen Jiangfef85fd2016-03-25 15:32:49 -0700264 EXPECT_TRUE(DoTest(false, true));
Allie Woodeb9e6d82015-04-17 13:55:30 -0700265}
266
267TEST_F(FilesystemVerifierActionTest, RunAsRootTerminateEarlyTest) {
Alex Deymo80f70ff2016-02-10 16:08:11 -0800268 ASSERT_EQ(0U, getuid());
Sen Jiangfef85fd2016-03-25 15:32:49 -0700269 EXPECT_TRUE(DoTest(true, false));
Alex Deymob9e8e262015-08-03 20:23:03 -0700270 // TerminateEarlyTest may leak some null callbacks from the Stream class.
Amin Hassani008c4582019-01-13 16:22:47 -0800271 while (loop_.RunOnce(false)) {
272 }
Allie Woodeb9e6d82015-04-17 13:55:30 -0700273}
274
Sen Jiang57f91802017-11-14 17:42:13 -0800275#ifdef __ANDROID__
Amin Hassani2e4eda52019-01-07 14:01:17 -0800276TEST_F(FilesystemVerifierActionTest, RunAsRootWriteVerityTest) {
Sen Jiang57f91802017-11-14 17:42:13 -0800277 test_utils::ScopedTempFile part_file("part_file.XXXXXX");
278 constexpr size_t filesystem_size = 200 * 4096;
279 constexpr size_t part_size = 256 * 4096;
280 brillo::Blob part_data(filesystem_size, 0x1);
281 part_data.resize(part_size);
282 ASSERT_TRUE(test_utils::WriteFileVector(part_file.path(), part_data));
283 string target_path;
284 test_utils::ScopedLoopbackDeviceBinder target_device(
285 part_file.path(), true, &target_path);
286
287 InstallPlan install_plan;
288 InstallPlan::Partition part;
289 part.name = "part";
290 part.target_path = target_path;
291 part.target_size = part_size;
292 part.block_size = 4096;
293 part.hash_tree_algorithm = "sha1";
294 part.hash_tree_data_offset = 0;
295 part.hash_tree_data_size = filesystem_size;
296 part.hash_tree_offset = filesystem_size;
297 part.hash_tree_size = 3 * 4096;
Sen Jianga778e5b2018-09-13 11:42:56 -0700298 part.fec_data_offset = 0;
299 part.fec_data_size = filesystem_size + part.hash_tree_size;
300 part.fec_offset = part.fec_data_size;
301 part.fec_size = 2 * 4096;
302 part.fec_roots = 2;
Sen Jiang57f91802017-11-14 17:42:13 -0800303 // for i in {1..$((200 * 4096))}; do echo -n -e '\x1' >> part; done
304 // avbtool add_hashtree_footer --image part --partition_size $((256 * 4096))
Sen Jianga778e5b2018-09-13 11:42:56 -0700305 // --partition_name part --do_not_append_vbmeta_image
306 // --output_vbmeta_image vbmeta
Sen Jiang57f91802017-11-14 17:42:13 -0800307 // truncate -s $((256 * 4096)) part
308 // sha256sum part | xxd -r -p | hexdump -v -e '/1 "0x%02x, "'
Sen Jianga778e5b2018-09-13 11:42:56 -0700309 part.target_hash = {0x28, 0xd4, 0x96, 0x75, 0x4c, 0xf5, 0x8a, 0x3e,
310 0x31, 0x85, 0x08, 0x92, 0x85, 0x62, 0xf0, 0x37,
311 0xbc, 0x8d, 0x7e, 0xa4, 0xcb, 0x24, 0x18, 0x7b,
312 0xf3, 0xeb, 0xb5, 0x8d, 0x6f, 0xc8, 0xd8, 0x1a};
Sen Jiang57f91802017-11-14 17:42:13 -0800313 // avbtool info_image --image vbmeta | grep Salt | cut -d':' -f 2 |
314 // xxd -r -p | hexdump -v -e '/1 "0x%02x, "'
315 part.hash_tree_salt = {0x9e, 0xcb, 0xf8, 0xd5, 0x0b, 0xb4, 0x43,
316 0x0a, 0x7a, 0x10, 0xad, 0x96, 0xd7, 0x15,
317 0x70, 0xba, 0xed, 0x27, 0xe2, 0xae};
318 install_plan.partitions = {part};
319
Sen Jiang3eeaf7d2018-10-11 13:55:32 -0700320 BuildActions(install_plan);
Sen Jiang57f91802017-11-14 17:42:13 -0800321
Sen Jiang3eeaf7d2018-10-11 13:55:32 -0700322 FilesystemVerifierActionTestDelegate delegate;
323 processor_.set_delegate(&delegate);
Sen Jiang57f91802017-11-14 17:42:13 -0800324
325 loop_.PostTask(
326 FROM_HERE,
327 base::Bind(
328 [](ActionProcessor* processor) { processor->StartProcessing(); },
329 base::Unretained(&processor_)));
330 loop_.Run();
331
332 EXPECT_FALSE(processor_.IsRunning());
333 EXPECT_TRUE(delegate.ran());
334 EXPECT_EQ(ErrorCode::kSuccess, delegate.code());
335}
336#endif // __ANDROID__
337
Amin Hassani2e4eda52019-01-07 14:01:17 -0800338TEST_F(FilesystemVerifierActionTest, RunAsRootSkipWriteVerityTest) {
Sen Jiang3eeaf7d2018-10-11 13:55:32 -0700339 test_utils::ScopedTempFile part_file("part_file.XXXXXX");
340 constexpr size_t filesystem_size = 200 * 4096;
341 constexpr size_t part_size = 256 * 4096;
342 brillo::Blob part_data(part_size);
343 test_utils::FillWithData(&part_data);
344 ASSERT_TRUE(test_utils::WriteFileVector(part_file.path(), part_data));
345 string target_path;
346 test_utils::ScopedLoopbackDeviceBinder target_device(
347 part_file.path(), true, &target_path);
348
349 InstallPlan install_plan;
350 install_plan.write_verity = false;
351 InstallPlan::Partition part;
352 part.name = "part";
353 part.target_path = target_path;
354 part.target_size = part_size;
355 part.block_size = 4096;
356 part.hash_tree_data_offset = 0;
357 part.hash_tree_data_size = filesystem_size;
358 part.hash_tree_offset = filesystem_size;
359 part.hash_tree_size = 3 * 4096;
360 part.fec_data_offset = 0;
361 part.fec_data_size = filesystem_size + part.hash_tree_size;
362 part.fec_offset = part.fec_data_size;
363 part.fec_size = 2 * 4096;
364 EXPECT_TRUE(HashCalculator::RawHashOfData(part_data, &part.target_hash));
365 install_plan.partitions = {part};
366
367 BuildActions(install_plan);
368
369 FilesystemVerifierActionTestDelegate delegate;
370 processor_.set_delegate(&delegate);
371
372 loop_.PostTask(
373 FROM_HERE,
374 base::Bind(
375 [](ActionProcessor* processor) { processor->StartProcessing(); },
376 base::Unretained(&processor_)));
377 loop_.Run();
378
379 EXPECT_FALSE(processor_.IsRunning());
380 EXPECT_TRUE(delegate.ran());
381 EXPECT_EQ(ErrorCode::kSuccess, delegate.code());
382}
Allie Woodeb9e6d82015-04-17 13:55:30 -0700383} // namespace chromeos_update_engine