-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdm-bht_test.cc
381 lines (303 loc) · 12.3 KB
/
dm-bht_test.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
// Copyright 2011 The ChromiumOS Authors
// Use of this source code is governed by the GPL v2 license that can
// be found in the LICENSE file.
//
// Basic unittesting of dm-bht using google-gtest.
#include <stdlib.h>
#include <string>
#include <vector>
#include <base/logging.h>
#include <gtest/gtest.h>
#include "verity/dm-bht.h"
namespace verity {
void* my_memalign(size_t boundary, size_t size) {
void* memptr;
if (posix_memalign(&memptr, boundary, size))
return NULL;
return memptr;
}
TEST(DmBht, CreateFailOnOverflow) {
struct dm_bht bht;
EXPECT_EQ(-EINVAL, dm_bht_create(&bht, UINT_MAX, "sha256"));
}
TEST(DmBht, BadAlgorithmName) {
struct dm_bht bht;
EXPECT_EQ(-EINVAL, dm_bht_create(&bht, 10, "foo"));
}
// Simple test to help valgrind/tcmalloc catch bad mem management
TEST(DmBht, CreateZeroPopulateDestroy) {
struct dm_bht bht;
sector_t sectors;
// This should fail.
unsigned int blocks, total_blocks = 16384;
uint8_t* data = static_cast<uint8_t*>(my_memalign(PAGE_SIZE, PAGE_SIZE));
blocks = total_blocks;
// Store all the block hashes of blocks of 0.
memset(reinterpret_cast<void*>(data), 0, sizeof(data));
EXPECT_EQ(0, dm_bht_create(&bht, blocks, "sha256"));
dm_bht_set_read_cb(&bht, dm_bht_zeroread_callback);
sectors = dm_bht_sectors(&bht);
std::vector<uint8_t> hash_data(verity_to_bytes(sectors));
dm_bht_set_buffer(&bht, hash_data.data());
do {
EXPECT_EQ(dm_bht_store_block(&bht, blocks - 1, data), 0);
} while (--blocks > 0);
// Load the tree from the pre-populated hash data
for (blocks = 0; blocks < total_blocks; blocks += bht.node_count)
EXPECT_GE(dm_bht_populate(&bht, reinterpret_cast<void*>(this), blocks), 0);
EXPECT_EQ(0, dm_bht_compute(&bht));
EXPECT_EQ(0, dm_bht_destroy(&bht));
free(data);
}
class MemoryBhtTest : public ::testing::Test {
public:
void SetUp() { bht_ = NULL; }
void TearDown() {
hash_data_.clear();
if (bht_)
delete bht_;
bht_ = NULL;
}
int Read(sector_t start, uint8_t* dst, sector_t count) {
EXPECT_LT(start, sectors_);
EXPECT_EQ(verity_to_bytes(count), PAGE_SIZE);
uint8_t* src = &hash_data_[verity_to_bytes(start)];
memcpy(dst, src, verity_to_bytes(count));
return 0;
}
static int ReadCallback(void* mbht_instance,
sector_t start,
uint8_t* dst,
sector_t count,
struct dm_bht_entry* entry) {
MemoryBhtTest* mbht = reinterpret_cast<MemoryBhtTest*>(mbht_instance);
mbht->Read(start, dst, count);
dm_bht_read_completed(entry, 0);
return 0;
}
protected:
// Creates a new dm_bht and sets it in the existing MemoryBht.
void SetupHash(const unsigned int total_blocks,
const char* digest_algorithm,
const char* salt,
void* hash_data) {
struct dm_bht bht;
uint8_t* data = static_cast<uint8_t*>(my_memalign(PAGE_SIZE, PAGE_SIZE));
memset(data, 0, PAGE_SIZE);
EXPECT_EQ(0, dm_bht_create(&bht, total_blocks, digest_algorithm));
if (salt)
dm_bht_set_salt(&bht, salt);
dm_bht_set_buffer(&bht, hash_data);
unsigned int blocks = total_blocks;
do {
EXPECT_EQ(dm_bht_store_block(&bht, blocks - 1, data), 0);
} while (--blocks > 0);
EXPECT_EQ(0, dm_bht_compute(&bht));
uint8_t digest[1024];
dm_bht_root_hexdigest(&bht, digest, sizeof(digest));
LOG(INFO) << "MemoryBhtTest root is " << digest;
EXPECT_EQ(0, dm_bht_destroy(&bht));
free(data);
}
void SetupBht(const unsigned int total_blocks,
const char* digest_algorithm,
const char* salt) {
if (bht_)
delete bht_;
bht_ = new dm_bht;
EXPECT_EQ(0, dm_bht_create(bht_, total_blocks, digest_algorithm));
sectors_ = dm_bht_sectors(bht_);
hash_data_.resize(verity_to_bytes(sectors_));
if (salt)
dm_bht_set_salt(bht_, salt);
SetupHash(total_blocks, digest_algorithm, salt, &hash_data_[0]);
dm_bht_set_read_cb(bht_, MemoryBhtTest::ReadCallback);
// Load the tree from the pre-populated hash data
unsigned int blocks;
for (blocks = 0; blocks < total_blocks; blocks += bht_->node_count)
EXPECT_GE(dm_bht_populate(bht_, reinterpret_cast<void*>(this), blocks),
0);
}
struct dm_bht* bht_;
std::vector<uint8_t> hash_data_;
sector_t sectors_;
};
TEST_F(MemoryBhtTest, CreateThenVerifyOk) {
static const unsigned int total_blocks = 16384;
// Set the root hash for a 0-filled image
static const char kRootDigest[] =
"45d65d6f9e5a962f4d80b5f1bd7a918152251c27bdad8c5f52b590c129833372";
// A page of all zeros
uint8_t* zero_page = static_cast<uint8_t*>(my_memalign(PAGE_SIZE, PAGE_SIZE));
memset(zero_page, 0, PAGE_SIZE);
SetupBht(total_blocks, "sha256", NULL);
dm_bht_set_root_hexdigest(bht_,
reinterpret_cast<const uint8_t*>(kRootDigest));
for (unsigned int blocks = 0; blocks < total_blocks; ++blocks) {
EXPECT_EQ(0, dm_bht_verify_block(bht_, blocks, zero_page, 0));
}
EXPECT_EQ(0, dm_bht_destroy(bht_));
free(zero_page);
}
TEST_F(MemoryBhtTest, CreateThenVerifySingleLevel) {
static const unsigned int total_blocks = 32;
// Set the root hash for a 0-filled image
static const char kRootDigest[] =
"2d3a43008286f56536fa24dcdbf14d342f0548827e374210415c7be0b610d2ba";
// A page of all zeros
uint8_t* zero_page = static_cast<uint8_t*>(my_memalign(PAGE_SIZE, PAGE_SIZE));
memset(zero_page, 0, PAGE_SIZE);
SetupBht(total_blocks, "sha256", NULL);
dm_bht_set_root_hexdigest(bht_,
reinterpret_cast<const uint8_t*>(kRootDigest));
for (unsigned int blocks = 0; blocks < total_blocks; ++blocks) {
EXPECT_EQ(0, dm_bht_verify_block(bht_, blocks, zero_page, 0));
}
EXPECT_EQ(0, dm_bht_destroy(bht_));
free(zero_page);
}
TEST_F(MemoryBhtTest, CreateThenVerifyRealParameters) {
static const unsigned int total_blocks = 217600;
// Set the root hash for a 0-filled image
static const char kRootDigest[] =
"15d5a180b5080a1d43e3fbd1f2cd021d0fc3ea91a8e330bad468b980c2fd4d8b";
// A page of all zeros
uint8_t* zero_page = static_cast<uint8_t*>(my_memalign(PAGE_SIZE, PAGE_SIZE));
memset(zero_page, 0, PAGE_SIZE);
SetupBht(total_blocks, "sha256", NULL);
dm_bht_set_root_hexdigest(bht_,
reinterpret_cast<const uint8_t*>(kRootDigest));
for (unsigned int blocks = 0; blocks < total_blocks; ++blocks) {
EXPECT_EQ(0, dm_bht_verify_block(bht_, blocks, zero_page, 0));
}
EXPECT_EQ(0, dm_bht_destroy(bht_));
free(zero_page);
}
TEST_F(MemoryBhtTest, CreateThenVerifyOddLeafCount) {
static const unsigned int total_blocks = 16383;
// Set the root hash for a 0-filled image
static const char kRootDigest[] =
"dc8cec4220d388b05ba75c853f858bb8cc25edfb1d5d2f3be6bdf9edfa66dc6a";
// A page of all zeros
uint8_t* zero_page = static_cast<uint8_t*>(my_memalign(PAGE_SIZE, PAGE_SIZE));
memset(zero_page, 0, PAGE_SIZE);
SetupBht(total_blocks, "sha256", NULL);
dm_bht_set_root_hexdigest(bht_,
reinterpret_cast<const uint8_t*>(kRootDigest));
for (unsigned int blocks = 0; blocks < total_blocks; ++blocks) {
EXPECT_EQ(0, dm_bht_verify_block(bht_, blocks, zero_page, 0));
}
EXPECT_EQ(0, dm_bht_destroy(bht_));
free(zero_page);
}
TEST_F(MemoryBhtTest, CreateThenVerifyOddNodeCount) {
static const unsigned int total_blocks = 16000;
// Set the root hash for a 0-filled image
static const char kRootDigest[] =
"10832dd62c427bcf68c56c8de0d1f9c32b61d9e5ddf43c77c56a97b372ad4b07";
// A page of all zeros
uint8_t* zero_page = static_cast<uint8_t*>(my_memalign(PAGE_SIZE, PAGE_SIZE));
memset(zero_page, 0, PAGE_SIZE);
SetupBht(total_blocks, "sha256", NULL);
dm_bht_set_root_hexdigest(bht_,
reinterpret_cast<const uint8_t*>(kRootDigest));
for (unsigned int blocks = 0; blocks < total_blocks; ++blocks) {
EXPECT_EQ(0, dm_bht_verify_block(bht_, blocks, zero_page, 0));
}
EXPECT_EQ(0, dm_bht_destroy(bht_));
free(zero_page);
}
TEST_F(MemoryBhtTest, CreateThenVerifyBadHashBlock) {
static const unsigned int total_blocks = 16384;
// Set the root hash for a 0-filled image
static const char kRootDigest[] =
"45d65d6f9e5a962f4d80b5f1bd7a918152251c27bdad8c5f52b590c129833372";
// A page of all zeros
uint8_t* zero_page = static_cast<uint8_t*>(my_memalign(PAGE_SIZE, PAGE_SIZE));
memset(zero_page, 0, PAGE_SIZE);
SetupBht(total_blocks, "sha256", NULL);
dm_bht_set_root_hexdigest(bht_,
reinterpret_cast<const uint8_t*>(kRootDigest));
// TODO(wad) add tests for partial tree validity/verification
// Corrupt one has hblock
static const unsigned int kBadBlock = 256;
uint8_t* bad_hash_block =
static_cast<uint8_t*>(my_memalign(PAGE_SIZE, PAGE_SIZE));
memset(bad_hash_block, 'A', PAGE_SIZE);
EXPECT_EQ(dm_bht_store_block(bht_, kBadBlock, bad_hash_block), 0);
// Attempt to verify both the bad block and all the neighbors.
EXPECT_LT(dm_bht_verify_block(bht_, kBadBlock + 1, zero_page, 0), 0);
EXPECT_LT(dm_bht_verify_block(bht_, kBadBlock + 2, zero_page, 0), 0);
EXPECT_LT(dm_bht_verify_block(bht_, kBadBlock + (bht_->node_count / 2),
zero_page, 0),
0);
EXPECT_LT(dm_bht_verify_block(bht_, kBadBlock, zero_page, 0), 0);
// Verify that the prior entry is untouched and still safe
EXPECT_EQ(dm_bht_verify_block(bht_, kBadBlock - 1, zero_page, 0), 0);
// Same for the next entry
EXPECT_EQ(
dm_bht_verify_block(bht_, kBadBlock + bht_->node_count, zero_page, 0), 0);
EXPECT_EQ(0, dm_bht_destroy(bht_));
free(bad_hash_block);
free(zero_page);
}
TEST_F(MemoryBhtTest, CreateThenVerifyBadDataBlock) {
static const unsigned int total_blocks = 384;
SetupBht(total_blocks, "sha256", NULL);
// Set the root hash for a 0-filled image
static const char kRootDigest[] =
"45d65d6f9e5a962f4d80b5f1bd7a918152251c27bdad8c5f52b590c129833372";
dm_bht_set_root_hexdigest(bht_,
reinterpret_cast<const uint8_t*>(kRootDigest));
// A corrupt page
uint8_t* bad_page = static_cast<uint8_t*>(my_memalign(PAGE_SIZE, PAGE_SIZE));
memset(bad_page, 'A', PAGE_SIZE);
EXPECT_LT(dm_bht_verify_block(bht_, 0, bad_page, 0), 0);
EXPECT_LT(dm_bht_verify_block(bht_, 127, bad_page, 0), 0);
EXPECT_LT(dm_bht_verify_block(bht_, 128, bad_page, 0), 0);
EXPECT_LT(dm_bht_verify_block(bht_, 255, bad_page, 0), 0);
EXPECT_LT(dm_bht_verify_block(bht_, 256, bad_page, 0), 0);
EXPECT_LT(dm_bht_verify_block(bht_, 383, bad_page, 0), 0);
EXPECT_EQ(0, dm_bht_destroy(bht_));
free(bad_page);
}
TEST_F(MemoryBhtTest, CreateThenVerifyOkSalt) {
static const unsigned int total_blocks = 16384;
// Set the root hash for a 0-filled image
static const char kRootDigest[] =
"8015fea349568f5135ecc833bbc79c9179377207382b53c68d93190b286b1256";
static const char salt[] =
"01ad1f06255d452d91337bf037953053cc3e452541db4b8ca05811bf3e2b6027";
// A page of all zeros
uint8_t* zero_page = static_cast<uint8_t*>(my_memalign(PAGE_SIZE, PAGE_SIZE));
memset(zero_page, 0, PAGE_SIZE);
SetupBht(total_blocks, "sha256", salt);
dm_bht_set_root_hexdigest(bht_,
reinterpret_cast<const uint8_t*>(kRootDigest));
for (unsigned int blocks = 0; blocks < total_blocks; ++blocks) {
EXPECT_EQ(0, dm_bht_verify_block(bht_, blocks, zero_page, 0));
}
EXPECT_EQ(0, dm_bht_destroy(bht_));
free(zero_page);
}
TEST_F(MemoryBhtTest, CreateThenVerifyOkLongSalt) {
static const unsigned int total_blocks = 16384;
// Set the root hash for a 0-filled image
static const char kRootDigest[] =
"8015fea349568f5135ecc833bbc79c9179377207382b53c68d93190b286b1256";
static const char salt[] =
"01ad1f06255d452d91337bf037953053cc3e452541db4b8ca05811bf3e2b6027b2188a1"
"d";
// A page of all zeros
uint8_t* zero_page = static_cast<uint8_t*>(my_memalign(PAGE_SIZE, PAGE_SIZE));
memset(zero_page, 0, PAGE_SIZE);
SetupBht(total_blocks, "sha256", salt);
dm_bht_set_root_hexdigest(bht_,
reinterpret_cast<const uint8_t*>(kRootDigest));
for (unsigned int blocks = 0; blocks < total_blocks; ++blocks) {
EXPECT_EQ(0, dm_bht_verify_block(bht_, blocks, zero_page, 0));
}
EXPECT_EQ(0, dm_bht_destroy(bht_));
free(zero_page);
}
} // namespace verity