1
0
mirror of https://github.com/Zygo/bees.git synced 2025-07-12 13:12:26 +02:00

lib: fs: stop using libbtrfs-dev helper functions to re-enable buffer length checks

The Linux kernel's btrfs headers are better than the libbtrfs-dev headers:

	- the libbtrfs-dev headers have C++ language compatibility issues

	- upstream version in Linux kernel is more accurate and up to date

	- macros in libbtrfs-dev's ctree.h hide information that would
	enable bees to perform runtime buffer length checking

	- enum types whose presence cannot be detected with #ifdef

When accessing members of metadata items from the filesystem, we want
to verify that the member we are accessing is within the boundaries of
the item that was retrieved; otherwise, a memory access violation may
occur or garbage may be returned to the caller.  A simple C++ template,
given a pointer to a structure member and a buffer, can determine that
the buffer contains enough bytes to safely access a struct member.
This was implemented back in 2016, but left unused due to ctree.h issues.

Some btrfs metadata structures have variable length despite using a
fixed-size in-memory structure.  The members that appear earliest in
the structure contain information about which following members of the
structure are used.  The item stored in the filesystem is truncated after
the last used member, and all following members must not be accessed.

'btrfs_stack_*' accessor macros obscure the memory boundaries of the
members they access, which makes it impossible for a C++ template to
verify the memory access.  If the template checks the length of the
entire structure, it will find an access violation for variable-length
metadata items because the item is rarely large enough for the entire
structure.

Get rid of all the libbtrfs-dev accessor macros and reimplement them
with the necessary buffer length checks.

Signed-off-by: Zygo Blaxell <bees@furryterror.org>
This commit is contained in:
Zygo Blaxell
2021-02-22 03:07:37 -05:00
parent 6eb7afa65c
commit 7f660f50b8
6 changed files with 96 additions and 61 deletions

View File

@ -649,8 +649,8 @@ BeesRoots::open_root_nocache(uint64_t rootid)
for (auto i : sk.m_result) {
sk.next_min(i);
if (i.type == BTRFS_ROOT_BACKREF_KEY && i.objectid == rootid) {
auto dirid = call_btrfs_get(btrfs_stack_root_ref_dirid, i.m_data);
auto name_len = call_btrfs_get(btrfs_stack_root_ref_name_len, i.m_data);
auto dirid = btrfs_get_member(&btrfs_root_ref::dirid, i.m_data);
auto name_len = btrfs_get_member(&btrfs_root_ref::name_len, i.m_data);
auto name_start = sizeof(struct btrfs_root_ref);
auto name_end = name_len + name_start;
THROW_CHECK2(runtime_error, i.m_data.size(), name_end, i.m_data.size() >= name_end);
@ -1100,7 +1100,7 @@ BeesCrawl::fetch_extents()
continue;
}
auto gen = call_btrfs_get(btrfs_stack_file_extent_generation, i.m_data);
auto gen = btrfs_get_member(&btrfs_file_extent_item::generation, i.m_data);
if (gen < get_state_end().m_min_transid) {
BEESCOUNT(crawl_gen_low);
++count_low;
@ -1126,7 +1126,7 @@ BeesCrawl::fetch_extents()
continue;
}
auto type = call_btrfs_get(btrfs_stack_file_extent_type, i.m_data);
auto type = btrfs_get_member(&btrfs_file_extent_item::type, i.m_data);
switch (type) {
default:
BEESLOGDEBUG("Unhandled file extent type " << type << " in root " << get_state_end().m_root << " ino " << i.objectid << " offset " << to_hex(i.offset));
@ -1144,10 +1144,10 @@ BeesCrawl::fetch_extents()
BEESCOUNT(crawl_prealloc);
// fallthrough
case BTRFS_FILE_EXTENT_REG: {
auto physical = call_btrfs_get(btrfs_stack_file_extent_disk_bytenr, i.m_data);
auto ram = call_btrfs_get(btrfs_stack_file_extent_ram_bytes, i.m_data);
auto len = call_btrfs_get(btrfs_stack_file_extent_num_bytes, i.m_data);
auto offset = call_btrfs_get(btrfs_stack_file_extent_offset, i.m_data);
auto physical = btrfs_get_member(&btrfs_file_extent_item::disk_bytenr, i.m_data);
auto ram = btrfs_get_member(&btrfs_file_extent_item::ram_bytes, i.m_data);
auto len = btrfs_get_member(&btrfs_file_extent_item::num_bytes, i.m_data);
auto offset = btrfs_get_member(&btrfs_file_extent_item::offset, i.m_data);
BEESTRACE("Root " << get_state_end().m_root << " ino " << i.objectid << " physical " << to_hex(physical)
<< " logical " << to_hex(i.offset) << ".." << to_hex(i.offset + len)
<< " gen " << gen);