-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathenv_encryption.cc
621 lines (560 loc) · 23 KB
/
env_encryption.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
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// Copyright 2018 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
//
//
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
// This source code is licensed under both the GPLv2 (found at
// https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html)
// and Apache 2.0 License (found in licenses/APL.txt in the root
// of this repository).
#include <algorithm>
#include "../switching_provider.h"
#include "aligned_buffer.h"
#include "env_encryption.h"
namespace rocksdb_utils {
class EncryptedSequentialFile : public rocksdb::SequentialFile {
private:
std::unique_ptr<rocksdb::SequentialFile> file_;
std::unique_ptr<BlockAccessCipherStream> stream_;
uint64_t offset_;
public:
// Default constructor.
EncryptedSequentialFile(rocksdb::SequentialFile* f, BlockAccessCipherStream* s)
: file_(f), stream_(s), offset_(0) {}
// Read up to "n" bytes from the file. "scratch[0..n-1]" may be
// written by this routine. Sets "*result" to the data that was
// read (including if fewer than "n" bytes were successfully read).
// May set "*result" to point at data in "scratch[0..n-1]", so
// "scratch[0..n-1]" must be live when "*result" is used.
// If an error was encountered, returns a non-OK status.
//
// REQUIRES: External synchronization
virtual rocksdb::Status Read(size_t n, rocksdb::Slice* result, char* scratch) override {
assert(scratch);
rocksdb::Status status = file_->Read(n, result, scratch);
if (!status.ok()) {
return status;
}
status = stream_->Decrypt(offset_, (char*)result->data(), result->size());
offset_ += result->size(); // We've already ready data from disk, so update offset_ even if
// decryption fails.
return status;
}
// Skip "n" bytes from the file. This is guaranteed to be no
// slower that reading the same data, but may be faster.
//
// If end of file is reached, skipping will stop at the end of the
// file, and Skip will return OK.
//
// REQUIRES: External synchronization
virtual rocksdb::Status Skip(uint64_t n) override {
auto status = file_->Skip(n);
if (!status.ok()) {
return status;
}
offset_ += n;
return status;
}
// Indicates the upper layers if the current SequentialFile implementation
// uses direct IO.
virtual bool use_direct_io() const override { return file_->use_direct_io(); }
// Use the returned alignment value to allocate
// aligned buffer for Direct I/O
virtual size_t GetRequiredBufferAlignment() const override {
return file_->GetRequiredBufferAlignment();
}
// Remove any kind of caching of data from the offset to offset+length
// of this file. If the length is 0, then it refers to the end of file.
// If the system is not caching the file contents, then this is a noop.
virtual rocksdb::Status InvalidateCache(size_t offset, size_t length) override {
return file_->InvalidateCache(offset, length);
}
// Positioned Read for direct I/O
// If Direct I/O enabled, offset, n, and scratch should be properly aligned
virtual rocksdb::Status PositionedRead(uint64_t offset, size_t n, rocksdb::Slice* result,
char* scratch) override {
assert(scratch);
auto status = file_->PositionedRead(offset, n, result, scratch);
if (!status.ok()) {
return status;
}
offset_ = offset + result->size();
status = stream_->Decrypt(offset, (char*)result->data(), result->size());
return status;
}
};
// A file abstraction for randomly reading the contents of a file.
class EncryptedRandomAccessFile : public rocksdb::RandomAccessFile {
private:
std::unique_ptr<rocksdb::RandomAccessFile> file_;
std::unique_ptr<BlockAccessCipherStream> stream_;
public:
EncryptedRandomAccessFile(rocksdb::RandomAccessFile* f, BlockAccessCipherStream* s)
: file_(f), stream_(s) {}
// Read up to "n" bytes from the file starting at "offset".
// "scratch[0..n-1]" may be written by this routine. Sets "*result"
// to the data that was read (including if fewer than "n" bytes were
// successfully read). May set "*result" to point at data in
// "scratch[0..n-1]", so "scratch[0..n-1]" must be live when
// "*result" is used. If an error was encountered, returns a non-OK
// status.
//
// Safe for concurrent use by multiple threads.
// If Direct I/O enabled, offset, n, and scratch should be aligned properly.
virtual rocksdb::Status Read(uint64_t offset, size_t n, rocksdb::Slice* result,
char* scratch) const override {
assert(scratch);
auto status = file_->Read(offset, n, result, scratch);
if (!status.ok()) {
return status;
}
status = stream_->Decrypt(offset, (char*)result->data(), result->size());
return status;
}
// Readahead the file starting from offset by n bytes for caching.
virtual rocksdb::Status Prefetch(uint64_t offset, size_t n) override {
// return rocksdb::Status::OK();
return file_->Prefetch(offset, n);
}
// Tries to get an unique ID for this file that will be the same each time
// the file is opened (and will stay the same while the file is open).
// Furthermore, it tries to make this ID at most "max_size" bytes. If such an
// ID can be created this function returns the length of the ID and places it
// in "id"; otherwise, this function returns 0, in which case "id"
// may not have been modified.
//
// This function guarantees, for IDs from a given environment, two unique ids
// cannot be made equal to eachother by adding arbitrary bytes to one of
// them. That is, no unique ID is the prefix of another.
//
// This function guarantees that the returned ID will not be interpretable as
// a single varint.
//
// Note: these IDs are only valid for the duration of the process.
virtual size_t GetUniqueId(char* id, size_t max_size) const override {
return file_->GetUniqueId(id, max_size);
};
virtual void Hint(AccessPattern pattern) override { file_->Hint(pattern); }
// Indicates the upper layers if the current RandomAccessFile implementation
// uses direct IO.
virtual bool use_direct_io() const override { return file_->use_direct_io(); }
// Use the returned alignment value to allocate
// aligned buffer for Direct I/O
virtual size_t GetRequiredBufferAlignment() const override {
return file_->GetRequiredBufferAlignment();
}
// Remove any kind of caching of data from the offset to offset+length
// of this file. If the length is 0, then it refers to the end of file.
// If the system is not caching the file contents, then this is a noop.
virtual rocksdb::Status InvalidateCache(size_t offset, size_t length) override {
return file_->InvalidateCache(offset, length);
}
};
// A file abstraction for sequential writing. The implementation
// must provide buffering since callers may append small fragments
// at a time to the file.
class EncryptedWritableFile : public rocksdb::WritableFileWrapper {
private:
std::unique_ptr<rocksdb::WritableFile> file_;
std::unique_ptr<BlockAccessCipherStream> stream_;
public:
// Default constructor.
EncryptedWritableFile(rocksdb::WritableFile* f, BlockAccessCipherStream* s)
: rocksdb::WritableFileWrapper(f), file_(f), stream_(s) {}
rocksdb::Status Append(const rocksdb::Slice& data) override {
AlignedBuffer buf;
rocksdb::Status status;
rocksdb::Slice dataToAppend(data);
if (data.size() > 0) {
auto offset = file_->GetFileSize(); // size including prefix
// Encrypt in cloned buffer
buf.Alignment(GetRequiredBufferAlignment());
buf.AllocateNewBuffer(data.size());
memmove(buf.BufferStart(), data.data(), data.size());
status = stream_->Encrypt(offset, buf.BufferStart(), data.size());
if (!status.ok()) {
return status;
}
dataToAppend = rocksdb::Slice(buf.BufferStart(), data.size());
}
status = file_->Append(dataToAppend);
if (!status.ok()) {
return status;
}
return status;
}
rocksdb::Status PositionedAppend(const rocksdb::Slice& data, uint64_t offset) override {
AlignedBuffer buf;
rocksdb::Status status;
rocksdb::Slice dataToAppend(data);
if (data.size() > 0) {
// Encrypt in cloned buffer
buf.Alignment(GetRequiredBufferAlignment());
buf.AllocateNewBuffer(data.size());
memmove(buf.BufferStart(), data.data(), data.size());
status = stream_->Encrypt(offset, buf.BufferStart(), data.size());
if (!status.ok()) {
return status;
}
dataToAppend = rocksdb::Slice(buf.BufferStart(), data.size());
}
status = file_->PositionedAppend(dataToAppend, offset);
if (!status.ok()) {
return status;
}
return status;
}
// Indicates the upper layers if the current WritableFile implementation
// uses direct IO.
virtual bool use_direct_io() const override { return file_->use_direct_io(); }
// Use the returned alignment value to allocate
// aligned buffer for Direct I/O
virtual size_t GetRequiredBufferAlignment() const override {
return file_->GetRequiredBufferAlignment();
}
};
// A file abstraction for random reading and writing.
class EncryptedRandomRWFile : public rocksdb::RandomRWFile {
private:
std::unique_ptr<rocksdb::RandomRWFile> file_;
std::unique_ptr<BlockAccessCipherStream> stream_;
public:
EncryptedRandomRWFile(rocksdb::RandomRWFile* f, BlockAccessCipherStream* s)
: file_(f), stream_(s) {}
// Indicates if the class makes use of direct I/O
// If false you must pass aligned buffer to Write()
virtual bool use_direct_io() const override { return file_->use_direct_io(); }
// Use the returned alignment value to allocate
// aligned buffer for Direct I/O
virtual size_t GetRequiredBufferAlignment() const override {
return file_->GetRequiredBufferAlignment();
}
// Write bytes in `data` at offset `offset`, Returns rocksdb::Status::OK() on success.
// Pass aligned buffer when use_direct_io() returns true.
virtual rocksdb::Status Write(uint64_t offset, const rocksdb::Slice& data) override {
AlignedBuffer buf;
rocksdb::Status status;
rocksdb::Slice dataToWrite(data);
if (data.size() > 0) {
// Encrypt in cloned buffer
buf.Alignment(GetRequiredBufferAlignment());
buf.AllocateNewBuffer(data.size());
memmove(buf.BufferStart(), data.data(), data.size());
status = stream_->Encrypt(offset, buf.BufferStart(), data.size());
if (!status.ok()) {
return status;
}
dataToWrite = rocksdb::Slice(buf.BufferStart(), data.size());
}
status = file_->Write(offset, dataToWrite);
return status;
}
// Read up to `n` bytes starting from offset `offset` and store them in
// result, provided `scratch` size should be at least `n`.
// Returns rocksdb::Status::OK() on success.
virtual rocksdb::Status Read(uint64_t offset, size_t n, rocksdb::Slice* result,
char* scratch) const override {
assert(scratch);
auto status = file_->Read(offset, n, result, scratch);
if (!status.ok()) {
return status;
}
status = stream_->Decrypt(offset, (char*)result->data(), result->size());
return status;
}
virtual rocksdb::Status Flush() override { return file_->Flush(); }
virtual rocksdb::Status Sync() override { return file_->Sync(); }
virtual rocksdb::Status Fsync() override { return file_->Fsync(); }
virtual rocksdb::Status Close() override { return file_->Close(); }
};
// EncryptedEnv implements an Env wrapper that adds encryption to files stored on disk.
class EncryptedEnv : public rocksdb::EnvWrapper {
public:
EncryptedEnv(rocksdb::Env* base_env, EncryptionProvider* provider, CipherStreamCreator* creator)
: rocksdb::EnvWrapper(base_env), provider_(provider), stream_creator_(creator) {}
// NewSequentialFile opens a file for sequential reading.
virtual rocksdb::Status NewSequentialFile(const std::string& fname,
std::unique_ptr<rocksdb::SequentialFile>* result,
const rocksdb::EnvOptions& options) override {
result->reset();
if (options.use_mmap_reads) {
return rocksdb::Status::InvalidArgument();
}
// Open file using underlying Env implementation
std::unique_ptr<rocksdb::SequentialFile> underlying;
auto status = rocksdb::EnvWrapper::NewSequentialFile(fname, &underlying, options);
if (!status.ok()) {
return status;
}
// Create cipher stream
std::unique_ptr<BlockAccessCipherStream> stream;
status = provider_->CreateCipherStream(stream_creator_, fname, false /* new_file */, &stream);
if (!status.ok()) {
return status;
}
(*result) = std::unique_ptr<rocksdb::SequentialFile>(
new EncryptedSequentialFile(underlying.release(), stream.release()));
return rocksdb::Status::OK();
}
// NewRandomAccessFile opens a file for random read access.
virtual rocksdb::Status NewRandomAccessFile(const std::string& fname,
std::unique_ptr<rocksdb::RandomAccessFile>* result,
const rocksdb::EnvOptions& options) override {
result->reset();
if (options.use_mmap_reads) {
return rocksdb::Status::InvalidArgument();
}
// Open file using underlying Env implementation
std::unique_ptr<rocksdb::RandomAccessFile> underlying;
auto status = rocksdb::EnvWrapper::NewRandomAccessFile(fname, &underlying, options);
if (!status.ok()) {
return status;
}
// Create cipher stream
std::unique_ptr<BlockAccessCipherStream> stream;
status = provider_->CreateCipherStream(stream_creator_, fname, false /* new_file */, &stream);
if (!status.ok()) {
return status;
}
(*result) = std::unique_ptr<rocksdb::RandomAccessFile>(
new EncryptedRandomAccessFile(underlying.release(), stream.release()));
return rocksdb::Status::OK();
}
// NewWritableFile opens a file for sequential writing.
virtual rocksdb::Status NewWritableFile(const std::string& fname,
std::unique_ptr<rocksdb::WritableFile>* result,
const rocksdb::EnvOptions& options) override {
result->reset();
if (options.use_mmap_writes) {
return rocksdb::Status::InvalidArgument();
}
// Open file using underlying Env implementation
std::unique_ptr<rocksdb::WritableFile> underlying;
rocksdb::Status status = rocksdb::EnvWrapper::NewWritableFile(fname, &underlying, options);
if (!status.ok()) {
return status;
}
// Create cipher stream
std::unique_ptr<BlockAccessCipherStream> stream;
status = provider_->CreateCipherStream(stream_creator_, fname, true /* new_file */, &stream);
if (!status.ok()) {
return status;
}
(*result) = std::unique_ptr<rocksdb::WritableFile>(
new EncryptedWritableFile(underlying.release(), stream.release()));
return rocksdb::Status::OK();
}
// Create an object that writes to a new file with the specified
// name. Deletes any existing file with the same name and creates a
// new file. On success, stores a pointer to the new file in
// *result and returns OK. On failure stores nullptr in *result and
// returns non-OK.
//
// The returned file will only be accessed by one thread at a time.
virtual rocksdb::Status ReopenWritableFile(const std::string& fname,
std::unique_ptr<rocksdb::WritableFile>* result,
const rocksdb::EnvOptions& options) override {
result->reset();
if (options.use_mmap_writes) {
return rocksdb::Status::InvalidArgument();
}
// Open file using underlying Env implementation
std::unique_ptr<rocksdb::WritableFile> underlying;
rocksdb::Status status = rocksdb::EnvWrapper::ReopenWritableFile(fname, &underlying, options);
if (!status.ok()) {
return status;
}
// Create cipher stream
std::unique_ptr<BlockAccessCipherStream> stream;
status = provider_->CreateCipherStream(stream_creator_, fname, true /* new_file */, &stream);
if (!status.ok()) {
return status;
}
(*result) = std::unique_ptr<rocksdb::WritableFile>(
new EncryptedWritableFile(underlying.release(), stream.release()));
return rocksdb::Status::OK();
}
// Reuse an existing file by renaming it and opening it as writable.
virtual rocksdb::Status ReuseWritableFile(const std::string& fname, const std::string& old_fname,
std::unique_ptr<rocksdb::WritableFile>* result,
const rocksdb::EnvOptions& options) override {
result->reset();
if (options.use_mmap_writes) {
return rocksdb::Status::InvalidArgument();
}
// Open file using underlying Env implementation
std::unique_ptr<rocksdb::WritableFile> underlying;
rocksdb::Status status =
rocksdb::EnvWrapper::ReuseWritableFile(fname, old_fname, &underlying, options);
if (!status.ok()) {
return status;
}
// Create cipher stream
std::unique_ptr<BlockAccessCipherStream> stream;
status = provider_->CreateCipherStream(stream_creator_, fname, true /* new_file */, &stream);
if (!status.ok()) {
return status;
}
(*result) = std::unique_ptr<rocksdb::WritableFile>(
new EncryptedWritableFile(underlying.release(), stream.release()));
return rocksdb::Status::OK();
}
// Open `fname` for random read and write, if file dont exist the file
// will be created. On success, stores a pointer to the new file in
// *result and returns OK. On failure returns non-OK.
//
// The returned file will only be accessed by one thread at a time.
virtual rocksdb::Status NewRandomRWFile(const std::string& fname,
std::unique_ptr<rocksdb::RandomRWFile>* result,
const rocksdb::EnvOptions& options) override {
result->reset();
if (options.use_mmap_reads || options.use_mmap_writes) {
return rocksdb::Status::InvalidArgument();
}
// Check file exists
bool isNewFile = !FileExists(fname).ok();
// Open file using underlying Env implementation
std::unique_ptr<rocksdb::RandomRWFile> underlying;
rocksdb::Status status = rocksdb::EnvWrapper::NewRandomRWFile(fname, &underlying, options);
if (!status.ok()) {
return status;
}
// Create cipher stream, indicating whether this is a new file.
std::unique_ptr<BlockAccessCipherStream> stream;
status =
provider_->CreateCipherStream(stream_creator_, fname, isNewFile /* new_file */, &stream);
if (!status.ok()) {
return status;
}
(*result) = std::unique_ptr<rocksdb::RandomRWFile>(
new EncryptedRandomRWFile(underlying.release(), stream.release()));
return rocksdb::Status::OK();
}
virtual rocksdb::Status DeleteFile(const std::string& fname) override {
auto status = provider_->DeleteFile(fname);
if (!status.ok()) {
return status;
}
return EnvWrapper::DeleteFile(fname);
}
virtual rocksdb::Status RenameFile(const std::string& src, const std::string& target) override {
auto status = provider_->RenameFile(src, target);
if (!status.ok()) {
return status;
}
return EnvWrapper::RenameFile(src, target);
}
virtual rocksdb::Status LinkFile(const std::string& src, const std::string& target) override {
auto status = provider_->LinkFile(src, target);
if (!status.ok()) {
return status;
}
return EnvWrapper::LinkFile(src, target);
}
private:
EncryptionProvider* provider_;
CipherStreamCreator* stream_creator_;
};
// Returns an Env that encrypts data when stored on disk and decrypts data when
// read from disk.
rocksdb::Env* NewEncryptedEnv(rocksdb::Env* base_env, EncryptionProvider* provider,
CipherStreamCreator* creator) {
return new EncryptedEnv(base_env, provider, creator);
}
// Encrypt one or more (partial) blocks of data at the file offset.
// Length of data is given in dataSize.
rocksdb::Status BlockAccessCipherStream::Encrypt(uint64_t fileOffset, char* data, size_t dataSize) {
// Calculate block index
auto blockSize = BlockSize();
uint64_t blockIndex = fileOffset / blockSize;
size_t blockOffset = fileOffset % blockSize;
std::unique_ptr<char[]> blockBuffer;
std::string scratch;
AllocateScratch(scratch);
// Encrypt individual blocks.
while (1) {
char* block = data;
size_t n = std::min(dataSize, blockSize - blockOffset);
if (n != blockSize) {
// We're not encrypting a full block.
// Copy data to blockBuffer
if (!blockBuffer.get()) {
// Allocate buffer
blockBuffer = std::unique_ptr<char[]>(new char[blockSize]);
}
block = blockBuffer.get();
// Copy plain data to block buffer
memmove(block + blockOffset, data, n);
}
auto status = EncryptBlock(blockIndex, block, (char*)scratch.data());
if (!status.ok()) {
return status;
}
if (block != data) {
// Copy encrypted data back to `data`.
memmove(data, block + blockOffset, n);
}
dataSize -= n;
if (dataSize == 0) {
return rocksdb::Status::OK();
}
data += n;
blockOffset = 0;
blockIndex++;
}
}
// Decrypt one or more (partial) blocks of data at the file offset.
// Length of data is given in dataSize.
rocksdb::Status BlockAccessCipherStream::Decrypt(uint64_t fileOffset, char* data, size_t dataSize) {
// Calculate block index
auto blockSize = BlockSize();
uint64_t blockIndex = fileOffset / blockSize;
size_t blockOffset = fileOffset % blockSize;
std::unique_ptr<char[]> blockBuffer;
std::string scratch;
AllocateScratch(scratch);
// Decrypt individual blocks.
while (1) {
char* block = data;
size_t n = std::min(dataSize, blockSize - blockOffset);
if (n != blockSize) {
// We're not decrypting a full block.
// Copy data to blockBuffer
if (!blockBuffer.get()) {
// Allocate buffer
blockBuffer = std::unique_ptr<char[]>(new char[blockSize]);
}
block = blockBuffer.get();
// Copy encrypted data to block buffer
memmove(block + blockOffset, data, n);
}
auto status = DecryptBlock(blockIndex, block, (char*)scratch.data());
if (!status.ok()) {
return status;
}
if (block != data) {
// Copy decrypted data back to `data`.
memmove(data, block + blockOffset, n);
}
dataSize -= n;
if (dataSize == 0) {
return rocksdb::Status::OK();
}
data += n;
blockOffset = 0;
blockIndex++;
}
}
} // namespace rocksdb_utils