-
Notifications
You must be signed in to change notification settings - Fork 59
/
lite_fs.cpp
475 lines (433 loc) · 17.3 KB
/
lite_fs.cpp
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#include "lite_fs.h"
#include "case_fold.h"
#include "constants.h"
#include "logger.h"
#include <cryptopp/base32.h>
#include <cerrno>
#include <mutex>
namespace securefs
{
namespace lite
{
File::File(std::shared_ptr<securefs::FileStream> file_stream,
const key_type& master_key,
unsigned block_size,
unsigned iv_size,
bool check)
: m_file_stream(file_stream)
{
m_file_stream->lock(true);
DEFER(m_file_stream->unlock());
m_crypt_stream.emplace(file_stream, master_key, block_size, iv_size, check);
}
File::~File() {}
void File::fstat(struct fuse_stat* stat)
{
m_file_stream->fstat(stat);
stat->st_size = AESGCMCryptStream::calculate_real_size(
stat->st_size, m_crypt_stream->get_block_size(), m_crypt_stream->get_iv_size());
}
FileSystem::FileSystem(std::shared_ptr<const securefs::OSService> root,
const key_type& name_key,
const key_type& content_key,
const key_type& xattr_key,
unsigned block_size,
unsigned iv_size,
unsigned flags)
: m_name_encryptor(name_key.data(), name_key.size())
, m_content_key(content_key)
, m_root(std::move(root))
, m_block_size(block_size)
, m_iv_size(iv_size)
, m_flags(flags)
{
byte null_iv[12] = {0};
m_xattr_enc.SetKeyWithIV(xattr_key.data(), xattr_key.size(), null_iv, sizeof(null_iv));
m_xattr_dec.SetKeyWithIV(xattr_key.data(), xattr_key.size(), null_iv, sizeof(null_iv));
}
FileSystem::~FileSystem() {}
InvalidFilenameException::~InvalidFilenameException() {}
std::string InvalidFilenameException::message() const
{
return strprintf("Invalid filename \"%s\"", m_filename.c_str());
}
std::string encrypt_path(CryptoPP::Base32Encoder& encoder, AES_SIV& encryptor, StringRef path)
{
byte buffer[2032];
std::string result;
result.reserve((path.size() * 8 + 4) / 5);
size_t last_nonseparator_index = 0;
for (size_t i = 0; i <= path.size(); ++i)
{
if (i >= path.size() || path[i] == '/')
{
if (i > last_nonseparator_index)
{
const char* slice = path.data() + last_nonseparator_index;
size_t slice_size = i - last_nonseparator_index;
if (slice_size > 2000)
throwVFSException(ENAMETOOLONG);
encryptor.encrypt_and_authenticate(
slice, slice_size, nullptr, 0, buffer + AES_SIV::IV_SIZE, buffer);
encoder.Initialize();
encoder.Put(buffer, slice_size + AES_SIV::IV_SIZE);
encoder.MessageEnd();
auto encoded_size = encoder.MaxRetrievable();
if (encoded_size > 0)
{
auto current_size = result.size();
result.resize(current_size + encoded_size);
encoder.Get(reinterpret_cast<byte*>(&result[current_size]), encoded_size);
}
}
if (i < path.size())
result.push_back('/');
last_nonseparator_index = i + 1;
}
}
return result;
}
std::string encrypt_path(AES_SIV& encryptor, StringRef path)
{
CryptoPP::Base32Encoder enc;
return encrypt_path(enc, encryptor, path);
}
std::string decrypt_path(AES_SIV& decryptor, StringRef path)
{
byte buffer[2032];
byte string_buffer[2032];
std::string result;
result.reserve(path.size() * 5 / 8);
size_t last_nonseparator_index = 0;
CryptoPP::Base32Decoder decoder;
for (size_t i = 0; i <= path.size(); ++i)
{
if (i >= path.size() || path[i] == '/')
{
if (i > last_nonseparator_index)
{
const char* slice = path.data() + last_nonseparator_index;
size_t slice_size = i - last_nonseparator_index;
decoder.Initialize();
decoder.Put(reinterpret_cast<const byte*>(slice), slice_size);
decoder.MessageEnd();
auto decoded_size = decoder.MaxRetrievable();
if (decoded_size > sizeof(buffer))
throwVFSException(ENAMETOOLONG);
if (decoded_size <= AES_SIV::IV_SIZE)
throwVFSException(EINVAL);
decoder.Get(buffer, decoded_size);
bool success = decryptor.decrypt_and_verify(buffer + AES_SIV::IV_SIZE,
decoded_size - AES_SIV::IV_SIZE,
nullptr,
0,
string_buffer,
buffer);
if (!success)
throw InvalidFilenameException(path.to_string());
result.append(reinterpret_cast<const char*>(string_buffer),
decoded_size - AES_SIV::IV_SIZE);
}
if (i < path.size())
result.push_back('/');
last_nonseparator_index = i + 1;
}
}
return result;
}
std::string FileSystem::translate_path(StringRef path, bool preserve_leading_slash)
{
if (path.empty())
{
return {};
}
else if (path.size() == 1 && path[0] == '/')
{
if (preserve_leading_slash)
{
return "/";
}
else
{
return ".";
}
}
else
{
std::string str
= lite::encrypt_path(m_encoder,
m_name_encryptor,
(m_flags & kOptionCaseFoldFileName) ? case_fold(path) : path);
if (!preserve_leading_slash && !str.empty() && str[0] == '/')
{
str.erase(str.begin());
}
TRACE_LOG("Translate path %s into %s", path.c_str(), str.c_str());
return str;
}
}
AutoClosedFile FileSystem::open(StringRef path, int flags, fuse_mode_t mode)
{
if (flags & O_APPEND)
throwVFSException(ENOTSUP);
// Files cannot be opened write-only because the header must be read in order to derive the
// session key
if ((flags & O_ACCMODE) == O_WRONLY)
{
flags = (flags & ~O_ACCMODE) | O_RDWR;
}
if ((flags & O_CREAT))
{
mode |= S_IRUSR;
}
auto file_stream = m_root->open_file_stream(translate_path(path, false), flags, mode);
AutoClosedFile fp(new File(file_stream,
m_content_key,
m_block_size,
m_iv_size,
(m_flags & kOptionNoAuthentication) == 0));
if (flags & O_TRUNC)
fp->resize(0);
return fp;
}
bool FileSystem::stat(StringRef path, struct fuse_stat* buf)
{
auto enc_path = translate_path(path, false);
if (!m_root->stat(enc_path, buf))
return false;
if (buf->st_size <= 0)
return true;
switch (buf->st_mode & S_IFMT)
{
case S_IFLNK:
{
std::string buffer(buf->st_size, '\0');
if (m_root->readlink(enc_path, &buffer[0], buffer.size()) != buf->st_size)
throwVFSException(EIO);
auto resolved = decrypt_path(m_name_encryptor, buffer);
buf->st_size = resolved.size();
break;
}
case S_IFDIR:
break;
case S_IFREG:
buf->st_size
= AESGCMCryptStream::calculate_real_size(buf->st_size, m_block_size, m_iv_size);
break;
default:
throwVFSException(ENOTSUP);
}
return true;
}
void FileSystem::mkdir(StringRef path, fuse_mode_t mode)
{
m_root->mkdir(translate_path(path, false), mode);
}
void FileSystem::rmdir(StringRef path)
{
m_root->remove_directory(translate_path(path, false));
}
void FileSystem::rename(StringRef from, StringRef to)
{
m_root->rename(translate_path(from, false), translate_path(to, false));
}
void FileSystem::chmod(StringRef path, fuse_mode_t mode)
{
if (!(mode & S_IRUSR))
{
WARN_LOG("Change the mode of file %s to 0%o which denies user read access. "
"Mysterious bugs will occur.",
path.c_str(),
static_cast<unsigned>(mode));
}
m_root->chmod(translate_path(path, false), mode);
}
size_t FileSystem::readlink(StringRef path, char* buf, size_t size)
{
if (size <= 0)
return size;
auto max_size = size / 5 * 8 + 32;
auto underbuf = securefs::make_unique_array<char>(max_size);
memset(underbuf.get(), 0, max_size);
m_root->readlink(translate_path(path, false), underbuf.get(), max_size - 1);
std::string resolved = decrypt_path(m_name_encryptor, underbuf.get());
size_t copy_size = std::min(resolved.size(), size - 1);
memcpy(buf, resolved.data(), copy_size);
buf[copy_size] = '\0';
return copy_size;
}
void FileSystem::symlink(StringRef to, StringRef from)
{
auto eto = translate_path(to, true), efrom = translate_path(from, false);
m_root->symlink(eto, efrom);
}
void FileSystem::utimens(StringRef path, const fuse_timespec* ts)
{
m_root->utimens(translate_path(path, false), ts);
}
void FileSystem::unlink(StringRef path) { m_root->remove_file(translate_path(path, false)); }
void FileSystem::link(StringRef src, StringRef dest)
{
m_root->link(translate_path(src, false), translate_path(dest, false));
}
void FileSystem::statvfs(struct fuse_statvfs* buf) { m_root->statfs(buf); }
class LiteDirectoryTraverser : public DirectoryTraverser
{
private:
CryptoPP::Base32Decoder decoder;
std::unique_ptr<DirectoryTraverser> m_underlying_traverser;
AES_SIV m_name_encryptor;
public:
explicit LiteDirectoryTraverser(std::unique_ptr<DirectoryTraverser> underlying_traverser,
const AES_SIV& name_encryptor)
: m_underlying_traverser(std::move(underlying_traverser))
, m_name_encryptor(name_encryptor)
{
}
~LiteDirectoryTraverser() {}
bool next(std::string* name, struct fuse_stat* stbuf) override
{
std::string under_name;
byte buffer[2000];
while (1)
{
if (!m_underlying_traverser->next(&under_name, stbuf))
return false;
if (!name)
return true;
if (under_name.empty() || under_name[0] == '.')
continue;
try
{
decoder.Initialize();
decoder.Put(reinterpret_cast<const byte*>(under_name.data()),
under_name.size());
decoder.MessageEnd();
auto size = decoder.MaxRetrievable();
if (size > sizeof(buffer) || size <= AES_SIV::IV_SIZE)
{
WARN_LOG("Skipping too large/small encrypted filename %s",
under_name.c_str());
continue;
}
decoder.Get(buffer, sizeof(buffer));
name->assign(size - AES_SIV::IV_SIZE, '\0');
bool success = m_name_encryptor.decrypt_and_verify(buffer + AES_SIV::IV_SIZE,
size - AES_SIV::IV_SIZE,
nullptr,
0,
&(*name)[0],
buffer);
if (!success)
{
WARN_LOG("Skipping filename %s (decrypted to %s) since it fails "
"authentication check",
under_name.c_str(),
name->c_str());
continue;
}
}
catch (const std::exception& e)
{
WARN_LOG("Skipping filename %s due to exception in decoding: %s",
under_name.c_str(),
e.what());
continue;
}
return true;
}
}
};
std::unique_ptr<DirectoryTraverser> FileSystem::create_traverser(StringRef path)
{
if (path.empty())
throwVFSException(EINVAL);
return securefs::make_unique<LiteDirectoryTraverser>(
m_root->create_traverser(translate_path(path, false)), this->m_name_encryptor);
}
#ifdef __APPLE__
ssize_t
FileSystem::getxattr(const char* path, const char* name, void* buf, size_t size) noexcept
{
if (!buf)
{
return m_root->getxattr(translate_path(path, false).c_str(), name, nullptr, 0);
}
try
{
auto iv_size = m_iv_size;
auto mac_size = AESGCMCryptStream::get_mac_size();
auto underbuf = securefs::make_unique_array<byte>(size + iv_size + mac_size);
ssize_t readlen = m_root->getxattr(translate_path(path, false).c_str(),
name,
underbuf.get(),
size + iv_size + mac_size);
if (readlen <= 0)
return readlen;
if (readlen <= iv_size + mac_size)
return -EIO;
bool success
= m_xattr_dec.DecryptAndVerify(static_cast<byte*>(buf),
underbuf.get() + readlen - mac_size,
mac_size,
underbuf.get(),
static_cast<int>(iv_size),
nullptr,
0,
underbuf.get() + iv_size,
static_cast<size_t>(readlen) - iv_size - mac_size);
if (!success)
{
ERROR_LOG("Encrypted extended attribute for file %s and name %s fails "
"ciphertext integrity check",
path,
name);
return -EIO;
}
return readlen - iv_size - mac_size;
}
catch (const std::exception& e)
{
ERROR_LOG("Error decrypting extended attribute for file %s and name %s (%s)",
path,
name,
e.what());
return -EIO;
}
}
int FileSystem::setxattr(
const char* path, const char* name, void* buf, size_t size, int flags) noexcept
{
try
{
auto iv_size = m_iv_size;
auto mac_size = AESGCMCryptStream::get_mac_size();
auto underbuf = securefs::make_unique_array<byte>(size + iv_size + mac_size);
m_csrng.GenerateBlock(underbuf.get(), iv_size);
m_xattr_enc.EncryptAndAuthenticate(underbuf.get() + iv_size,
underbuf.get() + iv_size + size,
mac_size,
underbuf.get(),
static_cast<int>(iv_size),
nullptr,
0,
static_cast<const byte*>(buf),
size);
return m_root->setxattr(translate_path(path, false).c_str(),
name,
underbuf.get(),
size + iv_size + mac_size,
flags);
}
catch (const std::exception& e)
{
ERROR_LOG("Error encrypting extended attribute for file %s and name %s (%s)",
path,
name,
e.what());
return -EIO;
}
}
#endif
}
}