-
Notifications
You must be signed in to change notification settings - Fork 1
/
imageloader_impl.cc
380 lines (315 loc) · 11.7 KB
/
imageloader_impl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Copyright 2016 The Chromium OS 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 "imageloader/imageloader_impl.h"
#include <linux/magic.h>
#include <sys/statvfs.h>
#include <sys/vfs.h>
#include <memory>
#include <string>
#include <base/containers/adapters.h>
#include <base/files/file_path.h>
#include <base/files/file_util.h>
#include <base/files/important_file_writer.h>
#include <base/json/json_string_value_serializer.h>
#include <base/logging.h>
#include <base/values.h>
#include <base/version.h>
#include <chromeos/dbus/service_constants.h>
#include "imageloader/component.h"
#include "imageloader/dlc.h"
namespace imageloader {
namespace {
using imageloader::kBadResult;
// The name of the file containing the latest component version.
constexpr char kLatestVersionFile[] = "latest-version";
// The maximum size of the latest-version file.
constexpr int kMaximumLatestVersionSize = 4096;
// Maximum ID length.
constexpr size_t kMaxIdLength = 40;
// |mount_base_path| is the subfolder where all components are mounted.
// For example "/mnt/imageloader."
base::FilePath GetMountPoint(const base::FilePath& mount_base_path,
const std::string& component_name,
const std::string& component_version) {
return mount_base_path.Append(component_name).Append(component_version);
}
bool AssertComponentDirPerms(const base::FilePath& path) {
int mode;
if (!GetPosixFilePermissions(path, &mode))
return false;
return mode == kComponentDirPerms;
}
} // namespace
// static
bool ImageLoaderImpl::IsIdValid(const std::string& id) {
// |id| can not be empty or start with a non-alphanumerical character.
if (id.empty() || id.length() > kMaxIdLength ||
(!isalpha(id[0]) && !isdigit(id[0]))) {
LOG(ERROR) << "Invalid ID: " << id;
return false;
}
// id can only contain alphanumerical character plus '_' and '-'.
for (const char& c : id) {
if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-') {
LOG(ERROR) << "Invalid ID: " << id;
return false;
}
}
return true;
}
bool ImageLoaderImpl::LoadComponent(const std::string& name,
const std::string& mount_point_str,
HelperProcessProxy* proxy) {
if (!IsIdValid(name)) {
return false;
}
base::FilePath component_path;
if (!GetPathToCurrentComponentVersion(name, &component_path)) {
return false;
}
std::unique_ptr<Component> component =
Component::Create(component_path, config_.keys);
if (!component) {
LOG(ERROR) << "Failed to initialize component: " << name;
return false;
}
base::FilePath mount_point(mount_point_str);
return component->Mount(proxy, mount_point);
}
std::string ImageLoaderImpl::LoadComponent(const std::string& name,
HelperProcessProxy* proxy) {
if (!IsIdValid(name)) {
return kBadResult;
}
base::FilePath component_path;
if (!GetPathToCurrentComponentVersion(name, &component_path)) {
return kBadResult;
}
return LoadComponentAtPath(name, component_path, proxy);
}
std::string ImageLoaderImpl::LoadDlcImage(const std::string& id,
const std::string& package,
const std::string& a_or_b,
HelperProcessProxy* proxy) {
if (!IsIdValid(id) || !IsIdValid(package)) {
return kBadResult;
}
Dlc dlc(id, package, config_.mount_path);
return dlc.Mount(proxy, a_or_b) ? dlc.GetMountPoint().value() : kBadResult;
}
std::string ImageLoaderImpl::LoadComponentAtPath(
const std::string& name,
const base::FilePath& component_path,
HelperProcessProxy* proxy) {
if (!IsIdValid(name)) {
return kBadResult;
}
std::unique_ptr<Component> component =
Component::Create(component_path, config_.keys);
if (!component) {
LOG(ERROR) << "Failed to initialize component: " << name;
return kBadResult;
}
base::FilePath mount_point(
GetMountPoint(config_.mount_path, name, component->manifest().version()));
return component->Mount(proxy, mount_point) ? mount_point.value()
: kBadResult;
}
bool ImageLoaderImpl::RemoveComponent(const std::string& name) {
if (!IsIdValid(name)) {
return false;
}
base::FilePath component_root(GetComponentRoot(name));
base::FilePath component_path;
if (!GetPathToCurrentComponentVersion(name, &component_path)) {
LOG(ERROR) << "Failed to get current component version: " << name;
return false;
}
return RemoveComponentAtPath(name, component_root, component_path);
}
bool ImageLoaderImpl::CleanupAll(bool dry_run,
const base::FilePath& parent_dir,
std::vector<std::string>* paths,
HelperProcessProxy* proxy) {
return proxy->SendUnmountAllCommand(dry_run, parent_dir.value(), paths);
}
bool ImageLoaderImpl::Cleanup(const base::FilePath& path,
HelperProcessProxy* proxy) {
return proxy->SendUnmountCommand(path.value());
}
bool ImageLoaderImpl::UnloadDlcImage(const std::string& id,
const std::string& package,
HelperProcessProxy* proxy) {
if (!IsIdValid(id)) {
return false;
}
return proxy->SendUnmountCommand(
Dlc::GetMountPoint(config_.mount_path, id, package).value());
}
bool ImageLoaderImpl::RemoveComponentAtPath(
const std::string& name,
const base::FilePath& component_root,
const base::FilePath& component_path) {
if (!IsIdValid(name)) {
return false;
}
// Check if component is removable.
std::unique_ptr<Component> component =
Component::Create(component_path, config_.keys);
if (!component) {
LOG(ERROR) << "Failed to initialize component: " << name;
return false;
}
if (!component->manifest().is_removable()) {
LOG(ERROR) << "Component is not removable";
return false;
}
// Remove the component (all versions) and latest-version file.
if (!base::DeletePathRecursively(component_root)) {
LOG(ERROR) << "Failed to delete component.";
return false;
}
return true;
}
bool ImageLoaderImpl::RegisterComponent(
const std::string& name,
const std::string& version,
const std::string& component_folder_abs_path) {
if (!IsIdValid(name)) {
return false;
}
base::FilePath components_dir(config_.storage_dir);
// If the directory is writable by others, do not trust the components.
if (!AssertComponentDirPerms(components_dir))
return false;
std::string old_version_hint;
base::FilePath version_hint_path(GetLatestVersionFilePath(name));
bool have_old_version = base::PathExists(version_hint_path);
if (have_old_version) {
if (!base::ReadFileToStringWithMaxSize(version_hint_path, &old_version_hint,
kMaximumLatestVersionSize)) {
return false;
}
// Check for version rollback.
base::Version current_version(old_version_hint);
base::Version new_version(version);
if (!new_version.IsValid()) {
return false;
}
if (current_version.IsValid() && new_version <= current_version) {
LOG(ERROR) << "Version [" << new_version << "] is not newer than ["
<< current_version << "] for component [" << name
<< "] and cannot be registered.";
return false;
}
}
// Check if this specific component already exists in the filesystem.
base::FilePath component_root(GetComponentRoot(name));
if (!base::PathExists(component_root)) {
if (mkdir(component_root.value().c_str(), kComponentDirPerms) != 0) {
PLOG(ERROR) << "Could not create component specific directory.";
return false;
}
}
std::unique_ptr<Component> component = Component::Create(
base::FilePath(component_folder_abs_path), config_.keys);
if (!component)
return false;
// Check that the reported version matches the component manifest version.
if (component->manifest().version() != version) {
LOG(ERROR) << "Version in signed manifest does not match the reported "
"component version.";
return false;
}
// Take ownership of the component and verify it.
base::FilePath version_path(GetVersionPath(name, version));
// If |version_path| exists but was not the active version, ImageLoader
// probably crashed previously and could not cleanup.
if (base::PathExists(version_path)) {
base::DeletePathRecursively(version_path);
}
if (mkdir(version_path.value().c_str(), kComponentDirPerms) != 0) {
PLOG(ERROR) << "Could not create directory for new component version.";
return false;
}
if (!component->CopyTo(version_path)) {
base::DeletePathRecursively(version_path);
return false;
}
if (!base::ImportantFileWriter::WriteFileAtomically(version_hint_path,
version)) {
base::DeletePathRecursively(version_path);
LOG(ERROR) << "Failed to update current version hint file.";
return false;
}
// Now delete the old component version, if there was one.
if (have_old_version) {
base::DeletePathRecursively(GetVersionPath(name, old_version_hint));
}
return true;
}
std::string ImageLoaderImpl::GetComponentVersion(const std::string& name) {
if (!IsIdValid(name)) {
return kBadResult;
}
base::FilePath component_path;
if (!GetPathToCurrentComponentVersion(name, &component_path)) {
return kBadResult;
}
std::unique_ptr<Component> component =
Component::Create(component_path, config_.keys);
if (!component)
return kBadResult;
return component->manifest().version();
}
bool ImageLoaderImpl::GetComponentMetadata(
const std::string& name, std::map<std::string, std::string>* out_metadata) {
if (!IsIdValid(name)) {
return false;
}
base::FilePath component_path;
if (!GetPathToCurrentComponentVersion(name, &component_path)) {
return false;
}
std::unique_ptr<Component> component =
Component::Create(component_path, config_.keys);
if (!component)
return false;
*out_metadata = component->manifest().metadata();
return true;
}
base::FilePath ImageLoaderImpl::GetLatestVersionFilePath(
const std::string& component_name) {
return config_.storage_dir.Append(component_name).Append(kLatestVersionFile);
}
base::FilePath ImageLoaderImpl::GetVersionPath(
const std::string& component_name, const std::string& version) {
return config_.storage_dir.Append(component_name).Append(version);
}
base::FilePath ImageLoaderImpl::GetComponentRoot(
const std::string& component_name) {
return config_.storage_dir.Append(component_name);
}
bool ImageLoaderImpl::GetPathToCurrentComponentVersion(
const std::string& component_name, base::FilePath* result) {
base::FilePath component_root(GetComponentRoot(component_name));
base::FilePath latest_version_path = GetLatestVersionFilePath(component_name);
// Check that the version file exists, otherwise the logging when
// ReadFileToString fails confuses the crash reporting. If the file doesn't
// exist, the component most likely isn't installed.
if (!base::PathExists(latest_version_path)) {
LOG(INFO) << "The latest-version file does not exist. Component "
<< component_name << " is probably not installed.";
return false;
}
std::string latest_version;
if (!base::ReadFileToStringWithMaxSize(latest_version_path, &latest_version,
kMaximumLatestVersionSize)) {
LOG(ERROR) << "Failed to read latest-version file.";
return false;
}
*result = component_root.Append(latest_version);
return true;
}
} // namespace imageloader