-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_attrs_cleaner_lib.cc
242 lines (202 loc) · 7.95 KB
/
file_attrs_cleaner_lib.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
// Copyright 2018 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 "init/file_attrs_cleaner.h"
#include <dirent.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/xattr.h>
#include <unistd.h>
#include <linux/fs.h>
#include <string>
#include <vector>
#include <base/files/file_path.h>
#include <base/files/scoped_file.h>
#include <base/logging.h>
namespace file_attrs_cleaner {
const char xdg_origin_url[] = "user.xdg.origin.url";
const char xdg_referrer_url[] = "user.xdg.referrer.url";
namespace {
struct ScopedDirDeleter {
inline void operator()(DIR* dirp) const {
if (dirp)
closedir(dirp);
}
};
using ScopedDir = std::unique_ptr<DIR, ScopedDirDeleter>;
bool CheckSucceeded(AttributeCheckStatus status) {
return status == AttributeCheckStatus::NO_ATTR ||
status == AttributeCheckStatus::CLEARED;
}
} // namespace
AttributeCheckStatus CheckFileAttributes(const base::FilePath& path,
bool isdir,
int fd) {
long flags; // NOLINT(runtime/int)
if (ioctl(fd, FS_IOC_GETFLAGS, &flags) != 0) {
PLOG(WARNING) << "Getting flags on " << path.value() << " failed";
return AttributeCheckStatus::ERROR;
}
if (flags & FS_IMMUTABLE_FL) {
LOG(WARNING) << "Immutable bit found on " << path.value()
<< "; clearing it";
flags &= ~FS_IMMUTABLE_FL;
if (ioctl(fd, FS_IOC_SETFLAGS, &flags) != 0) {
PLOG(ERROR) << "Unable to clear immutable bit on " << path.value();
return AttributeCheckStatus::CLEAR_FAILED;
}
return AttributeCheckStatus::CLEARED;
}
// The other file attribute flags look benign at this point.
return AttributeCheckStatus::NO_ATTR;
}
AttributeCheckStatus RemoveURLExtendedAttributes(const base::FilePath& path) {
bool found_xattr = false;
bool xattr_success = true;
for (const auto& attr_name : {xdg_origin_url, xdg_referrer_url}) {
if (getxattr(path.value().c_str(), attr_name, nullptr, 0) >= 0) {
// Attribute exists, clear it.
found_xattr = true;
bool res = removexattr(path.value().c_str(), attr_name) == 0;
if (!res) {
PLOG(ERROR) << "Unable to remove extended attribute '" << attr_name
<< "' from " << path.value();
}
xattr_success &= res;
}
}
if (found_xattr) {
return xattr_success ? AttributeCheckStatus::CLEARED
: AttributeCheckStatus::CLEAR_FAILED;
} else {
return AttributeCheckStatus::NO_ATTR;
}
}
bool ScanDir(const base::FilePath& dir,
const std::vector<std::string>& skip_recurse,
int* url_xattrs_count) {
// Internally glibc will use O_CLOEXEC when opening the directory.
// Unfortunately, there is no opendirat() helper we could use (so that ScanDir
// could accept a fd argument).
//
// We could use openat() ourselves and pass that to fdopendir(), but that has
// two downsides: (1) We can't use ScopedFD because opendir() will take over
// the fd -- when closedir() is called, close() will also be called. We can't
// skip the closedir() because we need to let the C library release resources
// associated with the DIR* handle. (2) When using fdopendir(), glibc will
// use fcntl() to make sure O_CLOEXEC is set even if we set it ourselves when
// we called openat(). It works, but adds a bit of syscall overhead here.
// Along those lines, we could dup() the fd passed in, but that would also add
// syscall overhead with no real benefit.
//
// So unless we change the signature of ScanDir to take a fd of the open dir
// to scan, we stick with opendir() here. Since this program only runs during
// early OS init, there shouldn't be other programs in the system racing with
// us to cause problems.
*url_xattrs_count = 0;
ScopedDir dirp(opendir(dir.value().c_str()));
if (dirp.get() == nullptr) {
PLOG(WARNING) << "Unable to open directory " << dir.value();
// This is a best effort routine so don't fail if the directory cannot be
// opened.
return true;
}
int dfd = dirfd(dirp.get());
if (!CheckSucceeded(CheckFileAttributes(dir, true /*isdir*/, dfd))) {
// This should never really fail...
return false;
}
// We might need this if we descend into a subdir. But if it's a leaf
// directory (no subdirs), we can skip the stat overhead entirely.
bool have_dirst = false;
struct stat dirst;
// Scan all the entries in this directory.
bool ret = true;
struct dirent* de;
std::vector<base::FilePath> subdirs;
while ((de = readdir(dirp.get())) != nullptr) {
CHECK(de->d_type != DT_UNKNOWN);
// Skip symlinks.
if (de->d_type == DT_LNK)
continue;
// Skip the . and .. fake directories.
const std::string_view name(de->d_name);
if (name == "." || name == "..")
continue;
// If the path component is listed in |skip_recurse|, skip it.
if (std::find(skip_recurse.begin(), skip_recurse.end(), name) !=
skip_recurse.end())
continue;
const base::FilePath path = dir.Append(de->d_name);
if (de->d_type == DT_DIR) {
// Don't cross mountpoints.
if (!have_dirst) {
// Load this on demand so leaf dirs don't waste time.
have_dirst = true;
if (fstat(dfd, &dirst) != 0) {
PLOG(ERROR) << "Unable to stat " << dir.value();
ret = false;
continue;
}
}
struct stat subdirst;
if (fstatat(dfd, de->d_name, &subdirst, 0) != 0) {
PLOG(ERROR) << "Unable to stat " << path.value();
ret = false;
continue;
}
if (dirst.st_dev != subdirst.st_dev) {
DVLOG(1) << "Skipping mounted directory " << path.value();
continue;
}
// Enqueue this directory for recursing.
// Recursing here is problematic because it means that |dirp| remains
// open for the lifetime of the process. Having a handle to the directory
// open for that long causes problems if the tool is still running when
// a user logs in. This can happen if the user has a lot of files in
// their home directory.
subdirs.push_back(path);
} else if (de->d_type == DT_REG) {
// Check the settings on this file.
// Extended attributes can be read even on encrypted files, so remove
// them by path and not by file descriptor. Since the removal is
// best-effort anyway, TOCTOU issues should not be a problem.
AttributeCheckStatus status = RemoveURLExtendedAttributes(path);
ret &= CheckSucceeded(status);
if (status == AttributeCheckStatus::CLEARED)
++(*url_xattrs_count);
base::ScopedFD fd(openat(dfd, de->d_name,
O_RDONLY | O_NONBLOCK | O_NOFOLLOW | O_CLOEXEC));
if (!fd.is_valid()) {
// This routine can be executed over encrypted filesystems.
// ENOKEY is normal for encrypted files, so don't log in that case.
// We might be running in parallel with other programs which might
// delete paths on the fly, so ignore ENOENT too.
if (errno != ENOKEY || errno != ENOENT)
PLOG(WARNING) << "Skipping path: " << path.value();
// This is a best effort routine so don't fail if the path cannot be
// opened.
continue;
}
ret &=
CheckSucceeded(CheckFileAttributes(path, false /*is_dir*/, fd.get()));
} else {
LOG(WARNING) << "Skipping path: " << path.value() << ": unknown type "
<< de->d_type;
}
}
if (closedir(dirp.release()) != 0)
PLOG(ERROR) << "Unable to close directory " << dir.value();
int sub_xattrs_count = 0;
for (const auto& subdir : subdirs) {
// Descend into this directory.
if (ScanDir(subdir, skip_recurse, &sub_xattrs_count))
*url_xattrs_count += sub_xattrs_count;
else
ret = false;
}
return ret;
}
} // namespace file_attrs_cleaner