Skip to content

Commit f3928e3

Browse files
authored
Fix several valgrind failures (#7789)
1 parent 19264de commit f3928e3

File tree

4 files changed

+8
-57
lines changed

4 files changed

+8
-57
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
### Fixed
88
* <How do the end-user experience this issue? what was the impact?> ([#????](https://github.com/realm/realm-core/issues/????), since v?.?.?)
9-
* None.
9+
* Valgrind could report a branch on an uninitialized read when opening something that is not an encrypted Realm file as an encrypted Realm file ([PR #7789](https://github.com/realm/realm-core/pull/7789), since v14.10.0).
1010

1111
### Breaking changes
1212
* None.

src/realm/util/encrypted_file_mapping.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ bool AESCryptor::constant_time_equals(const Hmac& a, const Hmac& b) const
377377
{
378378
// Constant-time memcmp to avoid timing attacks
379379
uint8_t result = 0;
380-
for (size_t i = 0; i < 224 / 8; ++i)
380+
for (size_t i = 0; i < a.size(); ++i)
381381
result |= a[i] ^ b[i];
382382
return result == 0;
383383
}
@@ -404,7 +404,7 @@ void AESCryptor::invalidate_ivs() noexcept
404404
AESCryptor::ReadResult AESCryptor::read(FileDesc fd, SizeType pos, char* dst, WriteObserver* observer)
405405
{
406406
uint32_t iv = 0;
407-
Hmac hmac{};
407+
Hmac hmac;
408408
// We're in a single-process scenario (or other processes are only reading),
409409
// so we can trust our in-memory caches and never need to retry
410410
if (!observer || observer->no_concurrent_writer_seen()) {
@@ -475,12 +475,12 @@ AESCryptor::ReadResult AESCryptor::attempt_read(FileDesc fd, SizeType pos, char*
475475
IVTable& iv = get_iv_table(fd, pos, iv_mode);
476476
iv_out = iv.iv1;
477477
if (iv.iv1 == 0) {
478-
std::fill(hmac.begin(), hmac.end(), 0);
478+
hmac.fill(0);
479479
return ReadResult::Uninitialized;
480480
}
481481

482482
size_t actual = check_read(fd, data_pos_to_file_pos(pos), m_rw_buffer.get());
483-
if (actual == 0) {
483+
if (actual < encryption_page_size) {
484484
return ReadResult::Eof;
485485
}
486486

test/test_group.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ TEST(Group_OpenUnencryptedFileWithKey_TwoPages)
119119
Group group;
120120
TableRef table = group.get_or_add_table("table");
121121
auto col = table->add_column(type_String, "str");
122-
table->create_object().set(col, std::string(page_size() - 100, '\1'));
122+
table->create_object().set(col, std::string(4096 - 100, '\1'));
123123
group.write(path);
124124
}
125125

@@ -135,7 +135,7 @@ TEST(Group_OpenUnencryptedFileWithKey_ThreePages)
135135
Group group;
136136
TableRef table = group.get_or_add_table("table");
137137
auto col = table->add_column(type_String, "str");
138-
std::string data(page_size() - 100, '\1');
138+
std::string data(4096 - 100, '\1');
139139
table->create_object().set<String>(col, data);
140140
table->create_object().set<String>(col, data);
141141
group.write(path);
@@ -285,7 +285,7 @@ TEST(Group_TableNameTooLong)
285285
{
286286
Group group;
287287
size_t buf_len = 64;
288-
std::unique_ptr<char[]> buf(new char[buf_len]);
288+
std::unique_ptr<char[]> buf(new char[buf_len]());
289289
CHECK_LOGIC_ERROR(group.add_table(StringData(buf.get(), buf_len)), ErrorCodes::InvalidName);
290290
group.add_table(StringData(buf.get(), buf_len - 1));
291291
}

test/valgrind.suppress

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,3 @@
1-
{
2-
<AllocSlab::all_files and AllocSlab::all_files_mutex intentionally not destructed to prevent races>
3-
Memcheck:Leak
4-
match-leak-kinds: reachable
5-
fun:_Znwm
6-
fun:_GLOBAL__sub_I_alloc_slab.cpp
7-
fun:call_init.part.0
8-
fun:call_init
9-
fun:_dl_init
10-
...
11-
}
12-
{
13-
<Entries added to the AllocSlab::all_files map are not freed namely `p = std::make_shared<MappedFile>();`>
14-
Memcheck:Leak
15-
match-leak-kinds: reachable
16-
fun:_Znwm
17-
fun:_ZNSt8_Rb_treeISsSt4pairIKSsSt8weak_ptrIN5realm9SlabAlloc10MappedFileEEESt10_Select1stIS7_ESt4lessISsESaIS7_EE22_M_emplace_hint_uniqueIIRKSt21piecewise_construct_tSt5tupleIIRS1_EESI_IIEEEEESt17_Rb_tree_iteratorIS7_ESt23_Rb_tree_const_iteratorIS7_EDpOT_
18-
fun:_ZN5realm9SlabAlloc11attach_fileERKSsRNS0_6ConfigE
19-
fun:_ZN5realm5Group4openERKSsPKcNS0_8OpenModeE
20-
...
21-
}
22-
{
23-
<std weak pointer allocated for entries in AllocSlab::all_files as above>
24-
Memcheck:Leak
25-
match-leak-kinds: reachable
26-
fun:_Znwm
27-
fun:_ZNSt8_Rb_treeISsSt4pairIKSsSt8weak_ptrIN5realm9SlabAlloc10MappedFileEEESt10_Select1stIS7_ESt4lessISsESaIS7_EE22_M_emplace_hint_uniqueIIRKSt21piecewise_construct_tSt5tupleIIRS1_EESI_IIEEEEESt17_Rb_tree_iteratorIS7_ESt23_Rb_tree_const_iteratorIS7_EDpOT_
28-
fun:_ZN5realm9SlabAlloc11attach_fileERKSsRNS0_6ConfigE
29-
fun:_ZN5realm11SharedGroup7do_openERKSsbbNS_18SharedGroupOptionsE
30-
fun:_ZN5realm11SharedGroupC2ERKSsbNS_18SharedGroupOptionsE.constprop.432
31-
...
32-
}
33-
{
34-
<We construct shared pointers with new. This covers two instances: `m_local_mappings.reset(new ...` and `new_mappings.reset(new ...`>
35-
Memcheck:Leak
36-
match-leak-kinds: reachable
37-
fun:_Znwm
38-
fun:_ZN5realm9SlabAlloc11attach_fileERKSsRNS0_6ConfigE
39-
...
40-
}
41-
{
42-
<SlabAlloc makes a deep copy of the realm file_path string which is still reachable as the keys of the all_files map>
43-
Memcheck:Leak
44-
fun:_Znwm
45-
fun:_ZNSs4_Rep9_S_createEmmRKSaIcE
46-
...
47-
fun:_ZN5realm9SlabAlloc11attach_fileERKSsRNS0_6ConfigE
48-
...
49-
}
501
{
512
<Supposed valgrind false positives>
523
Memcheck:Leak

0 commit comments

Comments
 (0)